utils.js 引入
//调用md5.js
var md5 = require("md5.js");//网上找一个小程序的md5.js
//懒得朋友可以下载:https://download.csdn.net/download/qvyyid/10673368;
var app_secret = "7a0d4ab95d293d00107be2a698efe1e1";
var UNITS = {
'年': 31557600000,
'月': 2629800000,
'天': 86400000,
'小时': 3600000,
'分钟': 60000,
'秒': 1000
};
//获取当前时间戳
function getTimestamp() {
//return(Date.parse(new Date()) / 1000).toString();
var date = new Date()
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
var time = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
return time;
};
//获取sign签名
function getSign(keyOptions) {
var data = keyOptions;
delete data.sign;
var str = "";
var arr = [];
var v = 0;
for (var key in data) {
arr[v] = key;
v++;
};
arr.sort();
for (var i = 0; i < arr.length; i++) {
var key = arr[i];
str += key + data[key];
};
return md5.md5(str + app_secret);
};
//获取发送de数据
function getdata(options) {
if (options == '') {
options = {};
}
options.timestamp = getTimestamp();
var timestamp = getTimestamp();
var sign = getSign(options).toLowerCase();
var data = {
timestamp: timestamp,
sign: sign,
};
for (var key in data) {
options[key] = data[key];
};
return options;
};
module.exports = {
getdata: getdata
}
其他页面调用:
var utils = require('/utils/util.js');
var params = utils.getdata(param);//加密签名