postparameter.js
2.4 KB
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
const MD5 = require('./md5.js');
// appKey需要提供给商城使用,保持export
const app_key = require('../config').app_key; //dev
const secret = require('../config').secret; //dev
const getSession = () => {
try {
var value = wx.getStorageSync('session')
if (value) {
return value;
} else {
return 0;
}
} catch (e) {
return 0;
}
}
const getPostParameter = (request) => {
var post_data = '';
var param_array = new Object();
let session = getSession();
if (session && session > 0) {
param_array.session = session;
}
param_array.format = 'json';
param_array.sign_method = 'md5';
param_array.timestamp = Date.parse(new Date());
param_array.app_key = app_key;
param_array.v = '1.0';
for (var p in request) { // 方法
if (request[p] != null && request[p] != undefined && typeof (request[p]) != "function") {
param_array[p] = request[p];
}
}
{
var param_sign;
var arrayKey = [];
var strTemp;
var arrayKeyTemp = [];
for (var p in param_array) { // 方法
if (typeof (param_array[p]) != "function") {
arrayKeyTemp.push(p);
}
}
// 最后显示所有的属性
arrayKeyTemp.sort();
var strTemp = secret;
for (var i = 0; i < arrayKeyTemp.length; i++) {
if (Array.isArray(param_array[arrayKeyTemp[i]])) {
strTemp = strTemp + arrayKeyTemp[i] + JSON.stringify(param_array[arrayKeyTemp[i]]);
} else {
strTemp = strTemp + arrayKeyTemp[i] + param_array[arrayKeyTemp[i]];
}
}
strTemp = strTemp + secret;
param_sign = MD5.hexMD5(strTemp).toString().toUpperCase();
post_data = 'sign=' + param_sign.toUpperCase();
for (var i = 0; i < arrayKeyTemp.length; i++) {
if (Array.isArray(param_array[arrayKeyTemp[i]])) {
post_data = post_data + '&' + arrayKeyTemp[i] + '=' + encodeURI(JSON.stringify(param_array[arrayKeyTemp[i]]));
} else {
post_data = post_data + '&' + arrayKeyTemp[i] + '=' + encodeURI(param_array[arrayKeyTemp[i]]);
}
}
}
// // 本地环境
// const LOCAL_URL = "http://192.168.80.8:9010/router?";
// // 开发环境
// const DEV_URL = "http://192.168.1.30:9010/router?";
// // 测试环境
// const TEST_URL = "http://www-test.herentech.com:9010/router?";
// // 生产环境
// const ONLINE_URL = "http://www.herentech.com:9010/router?";
return post_data;
}
module.exports=getPostParameter