jQuery 扩展新函数,增强ajax、post等方法

实现$.ajax$.post的拦截器方法来统一操作一些加解密逻辑

  • 重写$.ajax$.post
  • 扩展新函数$.ajaxEx$.postEx

明显这里扩展新函数更加合理。$.get$.load等扩展也是一样

扩展新函数$.ajaxEx$.postEx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
!(function ($, json, Ts) {
// 构建 AES 密钥
var aesKey = Ts.AES.randomKey();
// 使用 RSA 公钥加密 AES 密钥
var ak = Ts.RSA.encrypt(publicExponent, modulus, aesKey);

// 为 jQuery 添加新函数
$.extend({
ajaxEx: function (url, options) {
// If url is an object, simulate pre-1.5 signature
if ( typeof url === "object" ) {
options = url;
url = undefined;
}

// Force options to be an object
options = options || {};

var data = options['data'];
if(typeof data === 'string') {
throw new Error('opt.data parameter can not be a string!');
}
// Force options to be an object
data = data || {};

// 使用 AES 加密数据
var enc = Ts.AES.encrypt(aesKey, json.stringify(data));

var fn = {
success: null,
complete: null
};
if (options.success) {
fn.success = options.success;
}
if (options.complete) {
fn.complete = options.complete;
}

var opts = {};
opts['data'] = {};
opts['data']['ak'] = ak;
opts['data']['encrypt'] = enc;

if (fn.success !== null) {
opts['success'] = function (data, ts, xhr) {
if (data['errcode'] === '0' && data && data['data'] && data['data']['encrypt']) {
var _encrypt = data['data']['encrypt'];
// 使用 RSA 解密 AES 密钥,再使用解密出来的密钥解密数据
var _decrypt = Ts.AES.decrypt(Ts.AES.getKey(), _encrypt);
var _data = $.extend({}, data);
_data['data'] = eval('(' + _decrypt + ')');
var _xhr = $.extend({}, xhr);
_xhr['responseJSON'] = _data;
_xhr['responseText'] = json.stringify(_data);
fn.success(_data, ts, _xhr);
} else {
fn.success(data, ts, xhr);
}
};
}

if (fn.complete !== null) {
opts['complete'] = function (xhr, ts) {
var data = json.parse(xhr.responseText);
if (data['errcode'] === '0' && data && data['data'] && data['data']['encrypt']) {
var _encrypt = data['data']['encrypt'];
// 使用 RSA 解密 AES 密钥,再使用解密出来的密钥解密数据
data['data'] = json.parse(Ts.AES.decrypt(Ts.AES.getKey(), _encrypt));
var _xhr = $.extend({}, xhr);
_xhr['responseJSON'] = data;
_xhr['responseText'] = json.stringify(data);
fn.complete(_xhr, ts);
} else {
fn.complete(xhr, ts);
}
};
}

var _opt = $.extend(options, opts);
return $.ajax(url, _opt);
},
postEx: function (url, data, callback, type) {
// Shift arguments if data argument was omitted
if (typeof data === 'function') {
type = type || callback;
callback = data;
data = undefined;
}

if(typeof data === 'string') {
throw new Error('opt.data parameter can not be a string!');
}
// Force options to be an object
data = data || {};

// 使用 AES 加密数据
var enc = Ts.AES.encrypt(aesKey, json.stringify(data));

var fn = {
callback: null
};
fn.callback = callback;

var _data = {};
_data['ak'] = ak;
_data['encrypt'] = enc;

if (fn.callback !== null) {
callback = function (data, ts, xhr) {
if (data['errcode'] === '0' && data && data['data'] && data['data']['encrypt']) {
var _encrypt = data['data']['encrypt'];
// 使用 RSA 解密 AES 密钥,再使用解密出来的密钥解密数据
var _decrypt = Ts.AES.decrypt(Ts.AES.getKey(), _encrypt);
var _data = $.extend({}, data);
_data['data'] = eval('(' + _decrypt + ')');
var _xhr = $.extend({}, xhr);
_xhr['responseJSON'] = _data;
_xhr['responseText'] = json.stringify(_data);
fn.callback(_data, ts, _xhr);
} else {
fn.callback(data, ts, xhr);
}
};
}
return $.post(url, _data, callback, type);
}
});
})(jQuery, JSON, Ts);

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
$.ajaxEx({
url: 'http://localhost:8080/test',
type: 'post',
dataType: 'json',
data: {
'id': '123',
'name': 'test'
},
success: function(data, ts, xhr) {
console.log('data: %o', data);
console.log('ts: %o', ts);
console.log('xhr: %o', xhr);
}
});

$.ajaxEx({
url: 'http://localhost:8080/test',
type: 'post',
dataType: 'json',
data: {
'id': '123',
'name': 'test'
},
complete: function(xhr, ts) {
console.log('xhr: %o', xhr);
console.log('ts: %o', ts);
}
});


$.postEx('http://localhost:8080/test', {
'id': '123',
'name': 'test'
}, function (data, ts, xhr) {
console.log('data: %o', data);
console.log('ts: %o', ts);
console.log('xhr: %o', xhr);
}, 'json');
  • 本文作者: forever杨
  • 本文链接: https://blog.yl-online.top/posts/66aefafa.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。如果文章内容对你有用,请记录到你的笔记中。本博客站点随时会停止服务,请不要收藏、转载!