Skip to content

Commit a4288f0

Browse files
committed
将生成 X-LC-Sign 签名的方法移到 utils 模块中。
1 parent cdd4459 commit a4288f0

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

src/av.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@ AV.localStorage = require('./localstorage');
2323
// 挂载所有内部配置项
2424
AV._config = AV._config || {};
2525

26-
// 挂载内部使用的工具方法
27-
AV._utils = AV._utils || {};
28-
2926
// 以下模块为了兼容原有代码,使用这种加载方式。
30-
require('./utils')(AV);
27+
require('./utils').setAV(AV);
3128
require('./error')(AV);
3229
require('./event')(AV);
3330
require('./geopoint')(AV);

src/browserify-wrapper/ajax-browser.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,8 @@
55

66
'use strict';
77

8-
const md5 = require('md5');
98
const AVPromise = require('../promise');
10-
11-
// 计算 X-LC-Sign 的签名方法
12-
const sign = (key, isMasterKey) => {
13-
const now = new Date().getTime();
14-
const signature = md5(now + key);
15-
if (isMasterKey) {
16-
return signature + ',' + now + ',master';
17-
} else {
18-
return signature + ',' + now;
19-
}
20-
};
9+
const AVUtils = require('../utils');
2110

2211
const ajax = (method, url, data, success, error) => {
2312
const AV = global.AV;
@@ -61,9 +50,9 @@ const ajax = (method, url, data, success, error) => {
6150
xhr.open(method, url, true);
6251
xhr.setRequestHeader('X-LC-Id', appId);
6352
// 浏览器端不支持传入 masterKey 做 sign
64-
const signature = sign(appKey);
53+
const signature = AVUtils.sign(appKey);
6554
xhr.setRequestHeader('X-LC-Sign', signature);
66-
xhr.setRequestHeader('Content-Type', 'application/json');
55+
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
6756
xhr.send(data);
6857
return promise._thenRunCallbacks(options);
6958
};

src/utils.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
'use strict';
77

88
const _ = require('underscore');
9+
const md5 = require('md5');
910

10-
module.exports = function(AV) {
11+
const init = (AV) => {
1112

1213
// 挂载一些配置
1314
let AVConfig = AV._config;
@@ -664,4 +665,23 @@ module.exports = function(AV) {
664665
AV._isNullOrUndefined = function(x) {
665666
return _.isNull(x) || _.isUndefined(x);
666667
};
668+
669+
};
670+
671+
module.exports = {
672+
673+
setAV: (AV) => {
674+
init(AV);
675+
},
676+
677+
// 计算 X-LC-Sign 的签名方法
678+
sign: (key, isMasterKey) => {
679+
const now = new Date().getTime();
680+
const signature = md5(now + key);
681+
if (isMasterKey) {
682+
return signature + ',' + now + ',master';
683+
} else {
684+
return signature + ',' + now;
685+
}
686+
}
667687
};

0 commit comments

Comments
 (0)