公共方法
parent
c47c0299c7
commit
4f2e4b37f2
@ -0,0 +1,108 @@
|
|||||||
|
const app = getApp();
|
||||||
|
const utils = require("utils");
|
||||||
|
var addressInfo = {
|
||||||
|
/*--------查询收货地址列表-------*/
|
||||||
|
queryAddressList: function(succcess, fail) {
|
||||||
|
var item = {};
|
||||||
|
item.openId = wx.getStorageSync("openId")
|
||||||
|
var pointAddress = JSON.stringify(item)
|
||||||
|
var data = {
|
||||||
|
"pointAddress": pointAddress,
|
||||||
|
"method": "point.address.get",
|
||||||
|
};
|
||||||
|
var ignores = ["pointAddress"];
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores,
|
||||||
|
function(json) {
|
||||||
|
var result = json.data;
|
||||||
|
succcess(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
/*--------添加收货地址-------*/
|
||||||
|
addAddress: function(pointAddress, succcess, fail) {
|
||||||
|
var pointAddress = JSON.stringify(pointAddress)
|
||||||
|
var data = {
|
||||||
|
"pointAddress": pointAddress,
|
||||||
|
"method": "point.address.save",
|
||||||
|
};
|
||||||
|
|
||||||
|
var ignores = ["pointAddress"];
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores,
|
||||||
|
function(json) {
|
||||||
|
var result = json.data;
|
||||||
|
succcess(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
/*--------更新收货地址-------*/
|
||||||
|
updateAddress: function(pointAddress, succcess, fail) {
|
||||||
|
var pointAddress = JSON.stringify(pointAddress)
|
||||||
|
var data = {
|
||||||
|
"pointAddress": pointAddress,
|
||||||
|
"method": "point.address.update",
|
||||||
|
};
|
||||||
|
var ignores = ["pointAddress"];
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores,
|
||||||
|
function(json) {
|
||||||
|
var result = json.data;
|
||||||
|
succcess(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
/*--------删除收货地址-------*/
|
||||||
|
deleteAddress: function(pointAddress, succcess, fail) {
|
||||||
|
var pointAddress = JSON.stringify(pointAddress)
|
||||||
|
var data = {
|
||||||
|
"pointAddress": pointAddress,
|
||||||
|
"method": "point.address.delete",
|
||||||
|
};
|
||||||
|
var ignores = ["pointAddress"];
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores,
|
||||||
|
function(json) {
|
||||||
|
var result = json.data;
|
||||||
|
succcess(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
/*--------查询订单收货地址-------*/
|
||||||
|
queryConfirmOrderAddress: function(succcess, fail) {
|
||||||
|
var item = {};
|
||||||
|
item.openId = wx.getStorageSync("openId")
|
||||||
|
var pointAddress = JSON.stringify(item)
|
||||||
|
var data = {
|
||||||
|
"pointAddress": pointAddress,
|
||||||
|
"method": "get.point.confirm.order.address",
|
||||||
|
};
|
||||||
|
var ignores = ["pointAddress"];
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores,
|
||||||
|
function(json) {
|
||||||
|
var result = json.data;
|
||||||
|
succcess(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
addressInfo: addressInfo
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
const duration = 300;
|
||||||
|
const timingFun = "ease";
|
||||||
|
const width = 0;
|
||||||
|
|
||||||
|
var ani_width = function(duration,timingFunc,width){
|
||||||
|
if (undefined != duration){
|
||||||
|
this.duration = duration;
|
||||||
|
}
|
||||||
|
if (undefined != timingFun) {
|
||||||
|
this.timingFun = timingFun;
|
||||||
|
}
|
||||||
|
if (undefined != width) {
|
||||||
|
this.width = width;
|
||||||
|
}
|
||||||
|
|
||||||
|
var animation = wx.createAnimation({
|
||||||
|
duration: duration,
|
||||||
|
timingFunction: timingFunc
|
||||||
|
});
|
||||||
|
animation.width(width).step();
|
||||||
|
return animation.export();
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
ani_width: ani_width
|
||||||
|
}
|
@ -0,0 +1,307 @@
|
|||||||
|
var wxRequest = require('../utils/wxRequest');
|
||||||
|
var app=getApp();
|
||||||
|
var $ = {
|
||||||
|
extend: function (flag, p, c) {
|
||||||
|
var c = c || {};
|
||||||
|
for (var i in p) {
|
||||||
|
if (!p.hasOwnProperty(i)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (typeof p[i] === 'object') {
|
||||||
|
c[i] = (p[i].constructor === Array) ? [] : {};
|
||||||
|
deepCopy(p[i], c[i]);
|
||||||
|
} else {
|
||||||
|
c[i] = p[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return c;
|
||||||
|
},
|
||||||
|
each: function (object, callback, args) {
|
||||||
|
var name, i = 0,
|
||||||
|
length = object.length,
|
||||||
|
isObj = length === undefined || typeof object == 'function';
|
||||||
|
|
||||||
|
if (args) {
|
||||||
|
if (isObj) {
|
||||||
|
for (name in object) {
|
||||||
|
if (callback.apply(object[name], args) === false) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (; i < length;) {
|
||||||
|
if (callback.apply(object[i++], args) === false) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A special, fast, case for the most common use of each
|
||||||
|
} else {
|
||||||
|
if (isObj) {
|
||||||
|
for (name in object) {
|
||||||
|
if (callback.call(object[name], name, object[name]) === false) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (; i < length;) {
|
||||||
|
if (callback.call(object[i], i, object[i++]) === false) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function sha1(x, blen) {
|
||||||
|
function add32(a, b) {
|
||||||
|
var lsw = (a & 0xFFFF) + (b & 0xFFFF);
|
||||||
|
var msw = (a >> 16) + (b >> 16) + (lsw >> 16);
|
||||||
|
return (msw << 16) | (lsw & 0xFFFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
function AA(a, b, c, d, e) {
|
||||||
|
b = (b >>> 27) | (b << 5);
|
||||||
|
var lsw = (a & 0xFFFF) + (b & 0xFFFF) + (c & 0xFFFF) + (d & 0xFFFF) + (e & 0xFFFF);
|
||||||
|
var msw = (a >> 16) + (b >> 16) + (c >> 16) + (d >> 16) + (e >> 16) + (lsw >> 16);
|
||||||
|
return (msw << 16) | (lsw & 0xFFFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
function RR(w, j) {
|
||||||
|
var n = w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16];
|
||||||
|
return (n >>> 31) | (n << 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
var len = blen * 8;
|
||||||
|
x[len >> 5] |= 0x80 << (24 - len % 32);
|
||||||
|
x[((len + 64 >> 9) << 4) + 15] = len;
|
||||||
|
var w = new Array(80);
|
||||||
|
|
||||||
|
var k1 = 0x5A827999;
|
||||||
|
var k2 = 0x6ED9EBA1;
|
||||||
|
var k3 = 0x8F1BBCDC;
|
||||||
|
var k4 = 0xCA62C1D6;
|
||||||
|
|
||||||
|
var h0 = 0x67452301;
|
||||||
|
var h1 = 0xEFCDAB89;
|
||||||
|
var h2 = 0x98BADCFE;
|
||||||
|
var h3 = 0x10325476;
|
||||||
|
var h4 = 0xC3D2E1F0;
|
||||||
|
|
||||||
|
for (var i = 0; i < x.length; i += 16) {
|
||||||
|
var j = 0;
|
||||||
|
var t;
|
||||||
|
var a = h0;
|
||||||
|
var b = h1;
|
||||||
|
var c = h2;
|
||||||
|
var d = h3;
|
||||||
|
var e = h4;
|
||||||
|
while (j < 16) {
|
||||||
|
w[j] = x[i + j];
|
||||||
|
t = AA(e, a, d ^ (b & (c ^ d)), w[j], k1);
|
||||||
|
e = d;
|
||||||
|
d = c;
|
||||||
|
c = (b >>> 2) | (b << 30);
|
||||||
|
b = a;
|
||||||
|
a = t;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
while (j < 20) {
|
||||||
|
w[j] = RR(w, j);
|
||||||
|
t = AA(e, a, d ^ (b & (c ^ d)), w[j], k1);
|
||||||
|
e = d;
|
||||||
|
d = c;
|
||||||
|
c = (b >>> 2) | (b << 30);
|
||||||
|
b = a;
|
||||||
|
a = t;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
while (j < 40) {
|
||||||
|
w[j] = RR(w, j);
|
||||||
|
t = AA(e, a, b ^ c ^ d, w[j], k2);
|
||||||
|
e = d;
|
||||||
|
d = c;
|
||||||
|
c = (b >>> 2) | (b << 30);
|
||||||
|
b = a;
|
||||||
|
a = t;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
while (j < 60) {
|
||||||
|
w[j] = RR(w, j);
|
||||||
|
t = AA(e, a, (b & c) | (d & (b | c)), w[j], k3);
|
||||||
|
e = d;
|
||||||
|
d = c;
|
||||||
|
c = (b >>> 2) | (b << 30);
|
||||||
|
b = a;
|
||||||
|
a = t;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
while (j < 80) {
|
||||||
|
w[j] = RR(w, j);
|
||||||
|
t = AA(e, a, b ^ c ^ d, w[j], k4);
|
||||||
|
e = d;
|
||||||
|
d = c;
|
||||||
|
c = (b >>> 2) | (b << 30);
|
||||||
|
b = a;
|
||||||
|
a = t;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
h0 = add32(h0, a);
|
||||||
|
h1 = add32(h1, b);
|
||||||
|
h2 = add32(h2, c);
|
||||||
|
h3 = add32(h3, d);
|
||||||
|
h4 = add32(h4, e);
|
||||||
|
}
|
||||||
|
return [h0, h1, h2, h3, h4];
|
||||||
|
}
|
||||||
|
|
||||||
|
var encoding = {
|
||||||
|
strToBe32s: function (str) {
|
||||||
|
var be = [];
|
||||||
|
var len = Math.floor(str.length / 4);
|
||||||
|
var i, j;
|
||||||
|
for (i = 0, j = 0; i < len; i++ , j += 4) {
|
||||||
|
be[i] = ((str.charCodeAt(j) & 0xff) << 24) | ((str.charCodeAt(j + 1) & 0xff) << 16) | ((str.charCodeAt(j + 2) & 0xff) << 8) | (str.charCodeAt(j + 3) & 0xff);
|
||||||
|
}
|
||||||
|
while (j < str.length) {
|
||||||
|
be[j >> 2] |= (str.charCodeAt(j) & 0xff) << (24 - (j * 8) % 32);
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
return be;
|
||||||
|
},
|
||||||
|
|
||||||
|
be32sToHex: function (be) {
|
||||||
|
var hex = '0123456789ABCDEF';
|
||||||
|
var str = '';
|
||||||
|
for (var i = 0; i < be.length * 4; i++) {
|
||||||
|
str += hex.charAt((be[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xF) + hex.charAt((be[i >> 2] >> ((3 - i % 4) * 8)) & 0xF);
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
$.encoding = encoding;
|
||||||
|
var digests = {
|
||||||
|
hexSha1Str: function (str) {
|
||||||
|
return $.encoding.be32sToHex($.digests.sha1Str(str));
|
||||||
|
},
|
||||||
|
sha1Str: function (str) {
|
||||||
|
return sha1($.encoding.strToBe32s(str), str.length);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
$.digests = digests;
|
||||||
|
|
||||||
|
var utils = {
|
||||||
|
inArray: function (needle, array, bool) {
|
||||||
|
if (typeof needle == "string" || typeof needle == "number") {
|
||||||
|
var len = array.length;
|
||||||
|
for (var i = 0; i < len; i++) {
|
||||||
|
if (needle === array[i]) {
|
||||||
|
if (bool) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
$.utils = utils;
|
||||||
|
|
||||||
|
var cardApi = {
|
||||||
|
sign: function (params, ignores) {
|
||||||
|
var timestamp = (new Date()).valueOf();
|
||||||
|
var openAppKey = app.globalData.memberAppKey;
|
||||||
|
var openAppSecret = app.globalData.memberAppSecret;
|
||||||
|
var appSecret = openAppSecret;
|
||||||
|
var defaults = {
|
||||||
|
appKey: openAppKey,
|
||||||
|
v: '1.0',
|
||||||
|
format: 'json',
|
||||||
|
locale: 'zh-CN',
|
||||||
|
client: 'weixin',
|
||||||
|
timestamp: timestamp
|
||||||
|
};
|
||||||
|
var options = $.extend(true, defaults, params);
|
||||||
|
var paramNames = [];
|
||||||
|
$.each(options, function (key, value) {
|
||||||
|
if (!$.utils.inArray(key, ignores, false)) {
|
||||||
|
paramNames.push(key);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
paramNames = paramNames.sort();
|
||||||
|
var stringBuilder = [];
|
||||||
|
stringBuilder.push(appSecret);
|
||||||
|
$.each(paramNames, function (inx, value) {
|
||||||
|
stringBuilder.push(value);
|
||||||
|
stringBuilder.push(options[value]);
|
||||||
|
});
|
||||||
|
stringBuilder.push(appSecret);
|
||||||
|
options.sign = $.digests.hexSha1Str(stringBuilder.join(""));
|
||||||
|
|
||||||
|
return options;
|
||||||
|
},
|
||||||
|
ajax: function (params, ignores) {
|
||||||
|
var data = this.sign(params, ignores);
|
||||||
|
var app = getApp();
|
||||||
|
var url = app.globalData.memberUrl;
|
||||||
|
return wxRequest.postRequest(url, data);
|
||||||
|
},
|
||||||
|
ajax2: function (params, ignores) {
|
||||||
|
var data = this.sign(params, ignores);
|
||||||
|
var app = getApp();
|
||||||
|
var url = "http://127.0.0.1:8008/openApi/api";
|
||||||
|
return wxRequest.postRequest(url, data);
|
||||||
|
},
|
||||||
|
ajaxApi: function (params, ignores, success, error,urls) {
|
||||||
|
var data = this.sign(params, ignores);
|
||||||
|
var app = getApp();
|
||||||
|
var url="";
|
||||||
|
if(urls){
|
||||||
|
url =urls;
|
||||||
|
}else{
|
||||||
|
url = app.openApiParam.serverUrl
|
||||||
|
}
|
||||||
|
wx.request({
|
||||||
|
url: url,
|
||||||
|
method: 'POST',
|
||||||
|
data: data,
|
||||||
|
dataType: 'json',
|
||||||
|
header: {
|
||||||
|
'content-type': 'application/x-www-form-urlencoded'
|
||||||
|
},
|
||||||
|
success: function (json) {
|
||||||
|
if (json.errorToken) {
|
||||||
|
if (typeof error == 'function') {
|
||||||
|
error.call(error, json);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (typeof success == 'function') {
|
||||||
|
success.call(success, json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fail: function () {
|
||||||
|
if (typeof error == 'function') {
|
||||||
|
var json = {
|
||||||
|
"errorToken": "@@$-ERROR_TOKEN$-@@",
|
||||||
|
"code": "0",
|
||||||
|
"message": "网络好像有问题,无法访问云端服务!",
|
||||||
|
"solution": "请检查本机网络是否能正常访问互联网",
|
||||||
|
"subErrors": ""
|
||||||
|
};
|
||||||
|
error.call(error, json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
cardApi: cardApi
|
||||||
|
};
|
@ -0,0 +1,404 @@
|
|||||||
|
var CHAR_TILDE = 126;
|
||||||
|
var CODE_FNC1 = 102;
|
||||||
|
|
||||||
|
var SET_STARTA = 103;
|
||||||
|
var SET_STARTB = 104;
|
||||||
|
var SET_STARTC = 105;
|
||||||
|
var SET_SHIFT = 98;
|
||||||
|
var SET_CODEA = 101;
|
||||||
|
var SET_CODEB = 100;
|
||||||
|
var SET_STOP = 106;
|
||||||
|
|
||||||
|
|
||||||
|
var REPLACE_CODES = {
|
||||||
|
CHAR_TILDE: CODE_FNC1 //~ corresponds to FNC1 in GS1-128 standard
|
||||||
|
}
|
||||||
|
|
||||||
|
var CODESET = {
|
||||||
|
ANY: 1,
|
||||||
|
AB: 2,
|
||||||
|
A: 3,
|
||||||
|
B: 4,
|
||||||
|
C: 5
|
||||||
|
};
|
||||||
|
|
||||||
|
function getBytes(str) {
|
||||||
|
var bytes = [];
|
||||||
|
for (var i = 0; i < str.length; i++) {
|
||||||
|
bytes.push(str.charCodeAt(i));
|
||||||
|
}
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.code128 = function (ctx, text, width, height) {
|
||||||
|
|
||||||
|
width = parseInt(width);
|
||||||
|
|
||||||
|
height = parseInt(height);
|
||||||
|
|
||||||
|
var codes = stringToCode128(text);
|
||||||
|
console.debug('codes', codes)
|
||||||
|
|
||||||
|
var g = new Graphics(ctx, width, height);
|
||||||
|
|
||||||
|
var barWeight = g.area.width / ((codes.length - 3) * 11 + 35);
|
||||||
|
|
||||||
|
var x = g.area.left;
|
||||||
|
var y = g.area.top;
|
||||||
|
for (var i = 0; i < codes.length; i++) {
|
||||||
|
var c = codes[i];
|
||||||
|
//two bars at a time: 1 black and 1 white
|
||||||
|
for (var bar = 0; bar < 8; bar += 2) {
|
||||||
|
var barW = PATTERNS[c][bar] * barWeight;
|
||||||
|
// var barH = height - y - this.border;
|
||||||
|
var barH = height - y;
|
||||||
|
var spcW = PATTERNS[c][bar + 1] * barWeight;
|
||||||
|
|
||||||
|
//no need to draw if 0 width
|
||||||
|
if (barW > 0) {
|
||||||
|
g.fillFgRect(x, y, barW, barH);
|
||||||
|
}
|
||||||
|
|
||||||
|
x += barW + spcW;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function stringToCode128(text) {
|
||||||
|
var barc = {
|
||||||
|
currcs: CODESET.C
|
||||||
|
};
|
||||||
|
|
||||||
|
var bytes = getBytes(text);
|
||||||
|
//decide starting codeset
|
||||||
|
var index = bytes[0] == CHAR_TILDE ? 1 : 0;
|
||||||
|
|
||||||
|
var csa1 = bytes.length > 0 ? codeSetAllowedFor(bytes[index++]) : CODESET.AB;
|
||||||
|
var csa2 = bytes.length > 0 ? codeSetAllowedFor(bytes[index++]) : CODESET.AB;
|
||||||
|
barc.currcs = getBestStartSet(csa1, csa2);
|
||||||
|
barc.currcs = perhapsCodeC(bytes, barc.currcs);
|
||||||
|
|
||||||
|
//if no codeset changes this will end up with bytes.length+3
|
||||||
|
//start, checksum and stop
|
||||||
|
var codes = new Array();
|
||||||
|
|
||||||
|
switch (barc.currcs) {
|
||||||
|
case CODESET.A:
|
||||||
|
codes.push(SET_STARTA);
|
||||||
|
break;
|
||||||
|
case CODESET.B:
|
||||||
|
codes.push(SET_STARTB);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
codes.push(SET_STARTC);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for (var i = 0; i < bytes.length; i++) {
|
||||||
|
var b1 = bytes[i]; //get the first of a pair
|
||||||
|
//should we translate/replace
|
||||||
|
if (b1 in REPLACE_CODES) {
|
||||||
|
codes.push(REPLACE_CODES[b1]);
|
||||||
|
i++ //jump to next
|
||||||
|
b1 = bytes[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
//get the next in the pair if possible
|
||||||
|
var b2 = bytes.length > (i + 1) ? bytes[i + 1] : -1;
|
||||||
|
|
||||||
|
codes = codes.concat(codesForChar(b1, b2, barc.currcs));
|
||||||
|
//code C takes 2 chars each time
|
||||||
|
if (barc.currcs == CODESET.C) i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
//calculate checksum according to Code 128 standards
|
||||||
|
var checksum = codes[0];
|
||||||
|
for (var weight = 1; weight < codes.length; weight++) {
|
||||||
|
checksum += (weight * codes[weight]);
|
||||||
|
}
|
||||||
|
codes.push(checksum % 103);
|
||||||
|
|
||||||
|
codes.push(SET_STOP);
|
||||||
|
|
||||||
|
//encoding should now be complete
|
||||||
|
return codes;
|
||||||
|
|
||||||
|
function getBestStartSet(csa1, csa2) {
|
||||||
|
//tries to figure out the best codeset
|
||||||
|
//to start with to get the most compact code
|
||||||
|
var vote = 0;
|
||||||
|
vote += csa1 == CODESET.A ? 1 : 0;
|
||||||
|
vote += csa1 == CODESET.B ? -1 : 0;
|
||||||
|
vote += csa2 == CODESET.A ? 1 : 0;
|
||||||
|
vote += csa2 == CODESET.B ? -1 : 0;
|
||||||
|
//tie goes to B due to my own predudices
|
||||||
|
return vote > 0 ? CODESET.A : CODESET.B;
|
||||||
|
}
|
||||||
|
|
||||||
|
function perhapsCodeC(bytes, codeset) {
|
||||||
|
for (var i = 0; i < bytes.length; i++) {
|
||||||
|
var b = bytes[i]
|
||||||
|
if ((b < 48 || b > 57) && b != CHAR_TILDE)
|
||||||
|
return codeset;
|
||||||
|
}
|
||||||
|
if (bytes.length % 2 == 0)
|
||||||
|
return CODESET.C;
|
||||||
|
else
|
||||||
|
return CODESET.A;
|
||||||
|
}
|
||||||
|
|
||||||
|
//chr1 is current byte
|
||||||
|
//chr2 is the next byte to process. looks ahead.
|
||||||
|
function codesForChar(chr1, chr2, currcs) {
|
||||||
|
var result = [];
|
||||||
|
var shifter = -1;
|
||||||
|
|
||||||
|
if (charCompatible(chr1, currcs)) {
|
||||||
|
if (currcs == CODESET.C) {
|
||||||
|
if (chr2 == -1) {
|
||||||
|
shifter = SET_CODEB;
|
||||||
|
currcs = CODESET.B;
|
||||||
|
}
|
||||||
|
else if ((chr2 != -1) && !charCompatible(chr2, currcs)) {
|
||||||
|
//need to check ahead as well
|
||||||
|
if (charCompatible(chr2, CODESET.A)) {
|
||||||
|
shifter = SET_CODEA;
|
||||||
|
currcs = CODESET.A;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
shifter = SET_CODEB;
|
||||||
|
currcs = CODESET.B;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
//if there is a next char AND that next char is also not compatible
|
||||||
|
if ((chr2 != -1) && !charCompatible(chr2, currcs)) {
|
||||||
|
//need to switch code sets
|
||||||
|
switch (currcs) {
|
||||||
|
case CODESET.A:
|
||||||
|
shifter = SET_CODEB;
|
||||||
|
currcs = CODESET.B;
|
||||||
|
break;
|
||||||
|
case CODESET.B:
|
||||||
|
shifter = SET_CODEA;
|
||||||
|
currcs = CODESET.A;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
//no need to shift code sets, a temporary SHIFT will suffice
|
||||||
|
shifter = SET_SHIFT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//ok some type of shift is nessecary
|
||||||
|
if (shifter != -1) {
|
||||||
|
result.push(shifter);
|
||||||
|
result.push(codeValue(chr2));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (currcs == CODESET.C) {
|
||||||
|
//include next as well
|
||||||
|
result.push(codeValue(chr1, chr2));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result.push(codeValue(chr1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
barc.currcs = currcs;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//reduce the ascii code to fit into the Code128 char table
|
||||||
|
function codeValue(chr1, chr2) {
|
||||||
|
if (typeof chr2 == "undefined") {
|
||||||
|
return chr1 >= 32 ? chr1 - 32 : chr1 + 64;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return parseInt(String.fromCharCode(chr1) + String.fromCharCode(chr2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function charCompatible(chr, codeset) {
|
||||||
|
var csa = codeSetAllowedFor(chr);
|
||||||
|
if (csa == CODESET.ANY) return true;
|
||||||
|
//if we need to change from current
|
||||||
|
if (csa == CODESET.AB) return true;
|
||||||
|
if (csa == CODESET.A && codeset == CODESET.A) return true;
|
||||||
|
if (csa == CODESET.B && codeset == CODESET.B) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function codeSetAllowedFor(chr) {
|
||||||
|
if (chr >= 48 && chr <= 57) {
|
||||||
|
//0-9
|
||||||
|
return CODESET.ANY;
|
||||||
|
}
|
||||||
|
else if (chr >= 32 && chr <= 95) {
|
||||||
|
//0-9 A-Z
|
||||||
|
return CODESET.AB;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
//if non printable
|
||||||
|
return chr < 32 ? CODESET.A : CODESET.B;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var Graphics = function (ctx, width, height) {
|
||||||
|
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
this.quiet = Math.round(this.width / 40);
|
||||||
|
|
||||||
|
this.border_size = 0;
|
||||||
|
this.padding_width = 0;
|
||||||
|
|
||||||
|
this.area = {
|
||||||
|
width: width - this.padding_width * 2 - this.quiet * 2,
|
||||||
|
height: height - this.border_size * 2,
|
||||||
|
top: this.border_size - 4,
|
||||||
|
left: this.padding_width + this.quiet
|
||||||
|
};
|
||||||
|
|
||||||
|
this.ctx = ctx;
|
||||||
|
this.fg = "#000000";
|
||||||
|
this.bg = "#ffffff";
|
||||||
|
|
||||||
|
// fill background
|
||||||
|
this.fillBgRect(0, 0, width, height);
|
||||||
|
|
||||||
|
// fill center to create border
|
||||||
|
this.fillBgRect(0, this.border_size, width, height - this.border_size * 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
//use native color
|
||||||
|
Graphics.prototype._fillRect = function (x, y, width, height, color) {
|
||||||
|
this.ctx.setFillStyle(color)
|
||||||
|
this.ctx.fillRect(x, y, width, height)
|
||||||
|
}
|
||||||
|
|
||||||
|
Graphics.prototype.fillFgRect = function (x, y, width, height) {
|
||||||
|
this._fillRect(x, y, width, height, this.fg);
|
||||||
|
}
|
||||||
|
|
||||||
|
Graphics.prototype.fillBgRect = function (x, y, width, height) {
|
||||||
|
this._fillRect(x, y, width, height, this.bg);
|
||||||
|
}
|
||||||
|
|
||||||
|
var PATTERNS = [
|
||||||
|
[2, 1, 2, 2, 2, 2, 0, 0], // 0
|
||||||
|
[2, 2, 2, 1, 2, 2, 0, 0], // 1
|
||||||
|
[2, 2, 2, 2, 2, 1, 0, 0], // 2
|
||||||
|
[1, 2, 1, 2, 2, 3, 0, 0], // 3
|
||||||
|
[1, 2, 1, 3, 2, 2, 0, 0], // 4
|
||||||
|
[1, 3, 1, 2, 2, 2, 0, 0], // 5
|
||||||
|
[1, 2, 2, 2, 1, 3, 0, 0], // 6
|
||||||
|
[1, 2, 2, 3, 1, 2, 0, 0], // 7
|
||||||
|
[1, 3, 2, 2, 1, 2, 0, 0], // 8
|
||||||
|
[2, 2, 1, 2, 1, 3, 0, 0], // 9
|
||||||
|
[2, 2, 1, 3, 1, 2, 0, 0], // 10
|
||||||
|
[2, 3, 1, 2, 1, 2, 0, 0], // 11
|
||||||
|
[1, 1, 2, 2, 3, 2, 0, 0], // 12
|
||||||
|
[1, 2, 2, 1, 3, 2, 0, 0], // 13
|
||||||
|
[1, 2, 2, 2, 3, 1, 0, 0], // 14
|
||||||
|
[1, 1, 3, 2, 2, 2, 0, 0], // 15
|
||||||
|
[1, 2, 3, 1, 2, 2, 0, 0], // 16
|
||||||
|
[1, 2, 3, 2, 2, 1, 0, 0], // 17
|
||||||
|
[2, 2, 3, 2, 1, 1, 0, 0], // 18
|
||||||
|
[2, 2, 1, 1, 3, 2, 0, 0], // 19
|
||||||
|
[2, 2, 1, 2, 3, 1, 0, 0], // 20
|
||||||
|
[2, 1, 3, 2, 1, 2, 0, 0], // 21
|
||||||
|
[2, 2, 3, 1, 1, 2, 0, 0], // 22
|
||||||
|
[3, 1, 2, 1, 3, 1, 0, 0], // 23
|
||||||
|
[3, 1, 1, 2, 2, 2, 0, 0], // 24
|
||||||
|
[3, 2, 1, 1, 2, 2, 0, 0], // 25
|
||||||
|
[3, 2, 1, 2, 2, 1, 0, 0], // 26
|
||||||
|
[3, 1, 2, 2, 1, 2, 0, 0], // 27
|
||||||
|
[3, 2, 2, 1, 1, 2, 0, 0], // 28
|
||||||
|
[3, 2, 2, 2, 1, 1, 0, 0], // 29
|
||||||
|
[2, 1, 2, 1, 2, 3, 0, 0], // 30
|
||||||
|
[2, 1, 2, 3, 2, 1, 0, 0], // 31
|
||||||
|
[2, 3, 2, 1, 2, 1, 0, 0], // 32
|
||||||
|
[1, 1, 1, 3, 2, 3, 0, 0], // 33
|
||||||
|
[1, 3, 1, 1, 2, 3, 0, 0], // 34
|
||||||
|
[1, 3, 1, 3, 2, 1, 0, 0], // 35
|
||||||
|
[1, 1, 2, 3, 1, 3, 0, 0], // 36
|
||||||
|
[1, 3, 2, 1, 1, 3, 0, 0], // 37
|
||||||
|
[1, 3, 2, 3, 1, 1, 0, 0], // 38
|
||||||
|
[2, 1, 1, 3, 1, 3, 0, 0], // 39
|
||||||
|
[2, 3, 1, 1, 1, 3, 0, 0], // 40
|
||||||
|
[2, 3, 1, 3, 1, 1, 0, 0], // 41
|
||||||
|
[1, 1, 2, 1, 3, 3, 0, 0], // 42
|
||||||
|
[1, 1, 2, 3, 3, 1, 0, 0], // 43
|
||||||
|
[1, 3, 2, 1, 3, 1, 0, 0], // 44
|
||||||
|
[1, 1, 3, 1, 2, 3, 0, 0], // 45
|
||||||
|
[1, 1, 3, 3, 2, 1, 0, 0], // 46
|
||||||
|
[1, 3, 3, 1, 2, 1, 0, 0], // 47
|
||||||
|
[3, 1, 3, 1, 2, 1, 0, 0], // 48
|
||||||
|
[2, 1, 1, 3, 3, 1, 0, 0], // 49
|
||||||
|
[2, 3, 1, 1, 3, 1, 0, 0], // 50
|
||||||
|
[2, 1, 3, 1, 1, 3, 0, 0], // 51
|
||||||
|
[2, 1, 3, 3, 1, 1, 0, 0], // 52
|
||||||
|
[2, 1, 3, 1, 3, 1, 0, 0], // 53
|
||||||
|
[3, 1, 1, 1, 2, 3, 0, 0], // 54
|
||||||
|
[3, 1, 1, 3, 2, 1, 0, 0], // 55
|
||||||
|
[3, 3, 1, 1, 2, 1, 0, 0], // 56
|
||||||
|
[3, 1, 2, 1, 1, 3, 0, 0], // 57
|
||||||
|
[3, 1, 2, 3, 1, 1, 0, 0], // 58
|
||||||
|
[3, 3, 2, 1, 1, 1, 0, 0], // 59
|
||||||
|
[3, 1, 4, 1, 1, 1, 0, 0], // 60
|
||||||
|
[2, 2, 1, 4, 1, 1, 0, 0], // 61
|
||||||
|
[4, 3, 1, 1, 1, 1, 0, 0], // 62
|
||||||
|
[1, 1, 1, 2, 2, 4, 0, 0], // 63
|
||||||
|
[1, 1, 1, 4, 2, 2, 0, 0], // 64
|
||||||
|
[1, 2, 1, 1, 2, 4, 0, 0], // 65
|
||||||
|
[1, 2, 1, 4, 2, 1, 0, 0], // 66
|
||||||
|
[1, 4, 1, 1, 2, 2, 0, 0], // 67
|
||||||
|
[1, 4, 1, 2, 2, 1, 0, 0], // 68
|
||||||
|
[1, 1, 2, 2, 1, 4, 0, 0], // 69
|
||||||
|
[1, 1, 2, 4, 1, 2, 0, 0], // 70
|
||||||
|
[1, 2, 2, 1, 1, 4, 0, 0], // 71
|
||||||
|
[1, 2, 2, 4, 1, 1, 0, 0], // 72
|
||||||
|
[1, 4, 2, 1, 1, 2, 0, 0], // 73
|
||||||
|
[1, 4, 2, 2, 1, 1, 0, 0], // 74
|
||||||
|
[2, 4, 1, 2, 1, 1, 0, 0], // 75
|
||||||
|
[2, 2, 1, 1, 1, 4, 0, 0], // 76
|
||||||
|
[4, 1, 3, 1, 1, 1, 0, 0], // 77
|
||||||
|
[2, 4, 1, 1, 1, 2, 0, 0], // 78
|
||||||
|
[1, 3, 4, 1, 1, 1, 0, 0], // 79
|
||||||
|
[1, 1, 1, 2, 4, 2, 0, 0], // 80
|
||||||
|
[1, 2, 1, 1, 4, 2, 0, 0], // 81
|
||||||
|
[1, 2, 1, 2, 4, 1, 0, 0], // 82
|
||||||
|
[1, 1, 4, 2, 1, 2, 0, 0], // 83
|
||||||
|
[1, 2, 4, 1, 1, 2, 0, 0], // 84
|
||||||
|
[1, 2, 4, 2, 1, 1, 0, 0], // 85
|
||||||
|
[4, 1, 1, 2, 1, 2, 0, 0], // 86
|
||||||
|
[4, 2, 1, 1, 1, 2, 0, 0], // 87
|
||||||
|
[4, 2, 1, 2, 1, 1, 0, 0], // 88
|
||||||
|
[2, 1, 2, 1, 4, 1, 0, 0], // 89
|
||||||
|
[2, 1, 4, 1, 2, 1, 0, 0], // 90
|
||||||
|
[4, 1, 2, 1, 2, 1, 0, 0], // 91
|
||||||
|
[1, 1, 1, 1, 4, 3, 0, 0], // 92
|
||||||
|
[1, 1, 1, 3, 4, 1, 0, 0], // 93
|
||||||
|
[1, 3, 1, 1, 4, 1, 0, 0], // 94
|
||||||
|
[1, 1, 4, 1, 1, 3, 0, 0], // 95
|
||||||
|
[1, 1, 4, 3, 1, 1, 0, 0], // 96
|
||||||
|
[4, 1, 1, 1, 1, 3, 0, 0], // 97
|
||||||
|
[4, 1, 1, 3, 1, 1, 0, 0], // 98
|
||||||
|
[1, 1, 3, 1, 4, 1, 0, 0], // 99
|
||||||
|
[1, 1, 4, 1, 3, 1, 0, 0], // 100
|
||||||
|
[3, 1, 1, 1, 4, 1, 0, 0], // 101
|
||||||
|
[4, 1, 1, 1, 3, 1, 0, 0], // 102
|
||||||
|
[2, 1, 1, 4, 1, 2, 0, 0], // 103
|
||||||
|
[2, 1, 1, 2, 1, 4, 0, 0], // 104
|
||||||
|
[2, 1, 1, 2, 3, 2, 0, 0], // 105
|
||||||
|
[2, 3, 3, 1, 1, 1, 2, 0] // 106
|
||||||
|
]
|
@ -0,0 +1,685 @@
|
|||||||
|
const app = getApp();
|
||||||
|
const utils = require("utils");
|
||||||
|
/**
|
||||||
|
* 检测会员信息
|
||||||
|
*/
|
||||||
|
var checkMemberInfo = function(params, ignores, succcess, fail) {
|
||||||
|
app.jsapi.memberApi(app.globalData.appMemberKey, app.globalData.appMemberSecret, app.globalData.serverMemberUrl).ajax(params, ignores,
|
||||||
|
function(json) {
|
||||||
|
var result = json.data;
|
||||||
|
succcess(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
var cardGoodsDetails = function(goods) {
|
||||||
|
var arr = [];
|
||||||
|
goods = JSON.parse(goods);
|
||||||
|
for (var i = 0; i < goods.length; i++) {
|
||||||
|
var item = goods[i];
|
||||||
|
var map = {};
|
||||||
|
map.goodsId = item.productNo; // 商品编码
|
||||||
|
map.name = item.goodsName; // 商品名称
|
||||||
|
map.spec = item.specName; // 商品规格
|
||||||
|
map.quantity = item.count; // 数量
|
||||||
|
map.price = item.price; // 单价
|
||||||
|
map.discountMoney = item.discountTotal; // 优惠金额
|
||||||
|
map.money = item.amountTotal; // 金额
|
||||||
|
arr.push(map);
|
||||||
|
}
|
||||||
|
return JSON.stringify(arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
var cardMoney = function(money) {
|
||||||
|
return parseInt(parseFloat(money) * 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cardInfo 会员卡信息
|
||||||
|
* prepayment 预支付编码
|
||||||
|
* amount 刷卡金额(精确到分)
|
||||||
|
*/
|
||||||
|
var getCardPayInfo = function(cardInfo, prepayment, amount, passwd) {
|
||||||
|
var arr = [];
|
||||||
|
var info = {};
|
||||||
|
info.prepayment = prepayment;
|
||||||
|
info.cardNo = cardInfo.cardNo;
|
||||||
|
info.amount = amount;
|
||||||
|
info.isNoPwd = 1;
|
||||||
|
info.passwd = passwd;
|
||||||
|
arr.push(info);
|
||||||
|
console.log(info);
|
||||||
|
return JSON.stringify(arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
var getPays = function(money) {
|
||||||
|
var arr = [];
|
||||||
|
var pays = {};
|
||||||
|
pays.payTypeNo = "02";
|
||||||
|
pays.payType = "会员卡";
|
||||||
|
pays.money = money;
|
||||||
|
arr.push(pays);
|
||||||
|
return JSON.stringify(arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 充值方案
|
||||||
|
*/
|
||||||
|
var WxRechargeScheme = function(success, fail) {
|
||||||
|
var params = {
|
||||||
|
"wid": app.globalData.memberWid,
|
||||||
|
'method': 'weixin.recharge.scheme'
|
||||||
|
}
|
||||||
|
console.log(params);
|
||||||
|
var ignores = [];
|
||||||
|
//
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(params, ignores,
|
||||||
|
function(json) {
|
||||||
|
var result = json.data;
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取手机号
|
||||||
|
*/
|
||||||
|
var wx_getPhoneNumber = function(iv, data, success, fail) {
|
||||||
|
wx.request({
|
||||||
|
url: app.globalData.serverUrlExtend,
|
||||||
|
method: "post",
|
||||||
|
header: {
|
||||||
|
'content-type': 'application/x-www-form-urlencoded'
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
"type": "wxdcgetphone",
|
||||||
|
"iv": iv,
|
||||||
|
"loginKey": wx.getStorageSync("loginKey"),
|
||||||
|
"encryptedData": data
|
||||||
|
},
|
||||||
|
success: function(res) {
|
||||||
|
success(res);
|
||||||
|
},
|
||||||
|
fail: function(res) {
|
||||||
|
fail(res);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询优惠券信息
|
||||||
|
*/
|
||||||
|
var queryStoreCouponsList = function(params, success, fail) {
|
||||||
|
params.method = "elec.coupon.query";
|
||||||
|
params.workerNo = app.workerNo;
|
||||||
|
params.sourceSign = app.sourceSign;
|
||||||
|
var ignores = [];
|
||||||
|
app.jsapi.memberApi(app.globalData.appMemberKey, app.globalData.appMemberSecret, app.globalData.serverMemberUrl).ajax(params, ignores,
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询优惠券信息
|
||||||
|
*/
|
||||||
|
var queryGroupCouponsList = function(params, success, fail) {
|
||||||
|
params.method = "elec.coupon.query";
|
||||||
|
params.workerNo = app.globalData.workerNo;
|
||||||
|
params.sourceSign = app.sourceSign;
|
||||||
|
var ignores = [];
|
||||||
|
// app.globalData.memberUrl
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret,app.globalData.memberUrl).ajax(params, ignores,
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询会员信息
|
||||||
|
*/
|
||||||
|
var wx_cardInfo = function(cardNo, success, fail) {
|
||||||
|
var params = {
|
||||||
|
"method": "card.info",
|
||||||
|
"cardNo": cardNo,
|
||||||
|
"shopNo": app.globalData.shopNo,
|
||||||
|
"posNo": app.globalData.posNo,
|
||||||
|
}
|
||||||
|
var ignores = [];
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(params, ignores,
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 优惠券名称
|
||||||
|
*/
|
||||||
|
var getCouponsName = function(cardType) {
|
||||||
|
return cardType == "CASH" ? "代金券" : cardType == "DISCOUNT" ? "折扣券" : cardType == "GIFT" ? "兑换券" : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 价值
|
||||||
|
*/
|
||||||
|
var getCardWorth = function(cardType, reduceCost, discount) {
|
||||||
|
return cardType == "CASH" ? (reduceCost / 100 + "元") : cardType == "DISCOUNT" ? discount : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 价值
|
||||||
|
*/
|
||||||
|
var getCardWorthInfo = function(cardType, reduceCost, discount) {
|
||||||
|
return cardType == "CASH" ? ("¥" + reduceCost) : cardType == "DISCOUNT" ? (discount + "折") : "兑换券";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*--------随机字符串-------*/
|
||||||
|
function randomString() {
|
||||||
|
var randomString = "";
|
||||||
|
for (var i = 1; i <= 5; i++) {
|
||||||
|
randomString += parseInt(Math.random() * 10);
|
||||||
|
}
|
||||||
|
return randomString;
|
||||||
|
}
|
||||||
|
/*--------自动生成会员订单号-------*/
|
||||||
|
function getTradeNo() {
|
||||||
|
console.log("-----------------");
|
||||||
|
return app.globalData.shopNo + utils.getFormatTime(new Date(), 2) + this.randomString();
|
||||||
|
}
|
||||||
|
/*--------领券中心-------*/
|
||||||
|
var couponCenter = function couponCenter(couponType, type, success, fail) {
|
||||||
|
var data = {
|
||||||
|
'groupNo': app.globalData.groupNo,
|
||||||
|
'wid': app.globalData.memberWid,
|
||||||
|
'memberId': wx.getStorageSync("memberId"),
|
||||||
|
'type': type,
|
||||||
|
'useFlag': 0,
|
||||||
|
'method': 'query.coupon.exchange.schemes'
|
||||||
|
}
|
||||||
|
if (couponType) {
|
||||||
|
data.couponType = couponType
|
||||||
|
}
|
||||||
|
var ignores = [];
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores,
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/*--------免费领取优惠券接口-------*/
|
||||||
|
var exchangeCouponScheme = function(schemeId, success, fail) {
|
||||||
|
var data = {
|
||||||
|
'schemeId': schemeId,
|
||||||
|
'memberId': wx.getStorageSync("memberId"),
|
||||||
|
'number': 1,
|
||||||
|
'openId': wx.getStorageSync("openId"),
|
||||||
|
'shopNo': app.globalData.shopNo,
|
||||||
|
'posNo': app.globalData.posNo,
|
||||||
|
'workerNo': app.globalData.workerNo,
|
||||||
|
'sourceSign': app.globalData.sourceSign,
|
||||||
|
'method': 'elec.coupon.exchange'
|
||||||
|
}
|
||||||
|
var ignores = [];
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores,
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/*--------发送fromid后台给用户推送消息-------*/
|
||||||
|
var sendTemPlate = function(type, fromId, couponId, success, fail) {
|
||||||
|
var data = {};
|
||||||
|
if (couponId) {
|
||||||
|
data = {
|
||||||
|
'openId': wx.getStorageSync('openId'),
|
||||||
|
'formId': fromId,
|
||||||
|
'wid': app.globalData.memberWid,
|
||||||
|
'sendType': type,
|
||||||
|
'method': 'get.smweixin.formId',
|
||||||
|
'couponId': couponId,
|
||||||
|
'memberId': wx.getStorageSync('memberId')
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
data = {
|
||||||
|
'openId': wx.getStorageSync('openId'),
|
||||||
|
'formId': fromId,
|
||||||
|
'wid': app.globalData.memberWid,
|
||||||
|
'sendType': type,
|
||||||
|
'method': 'get.smweixin.formId',
|
||||||
|
'memberId': wx.getStorageSync('memberId')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var ignores = [];
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores,
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------查询积分商品列表-------*/
|
||||||
|
var pointProduct = function(type, success, fail) {
|
||||||
|
var data = {
|
||||||
|
"wid": app.globalData.memberWid,
|
||||||
|
"method": "query.point.product.list",
|
||||||
|
"pageType": type
|
||||||
|
};
|
||||||
|
var ignores = [];
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores,
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------查询积分商品详情-------*/
|
||||||
|
var queryPointProductDetail = function(id, schemeId, success, fail) {
|
||||||
|
var data = {
|
||||||
|
"method": "query.point.product.detail",
|
||||||
|
"productId": id,
|
||||||
|
"schemeId": schemeId
|
||||||
|
};
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [],
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*--------查询积分所有商品-------*/
|
||||||
|
var pointAllTypeProduct = function(success, fail) {
|
||||||
|
var data = {
|
||||||
|
"wid": app.globalData.memberWid,
|
||||||
|
"method": "query.point.product.noPageType",
|
||||||
|
};
|
||||||
|
var ignores = [];
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores,
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 小程序优惠活动弹窗
|
||||||
|
*/
|
||||||
|
var judgeUpWindown = function(success, fail,type) {
|
||||||
|
var data = {
|
||||||
|
'openId': wx.getStorageSync("openId"),
|
||||||
|
'wid': app.globalData.memberWid,
|
||||||
|
'memberId': wx.getStorageSync("memberId"),
|
||||||
|
'method': 'query.upWindow.coupon.list',
|
||||||
|
'type': app.temData.type
|
||||||
|
}
|
||||||
|
if (type) {
|
||||||
|
data.type = type
|
||||||
|
}
|
||||||
|
// app.globalData.memberUrl
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [],
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 小程序弹窗一键领取
|
||||||
|
*/
|
||||||
|
var receive = function(schemeId, success, fail) {
|
||||||
|
var data = {
|
||||||
|
'cardNo': wx.getStorageSync("cardInfo").cardNo,
|
||||||
|
'wid': app.globalData.memberWid,
|
||||||
|
'memberId': wx.getStorageSync("memberId"),
|
||||||
|
'method': 'elec.promotion.packet.receive',
|
||||||
|
'schemeId': schemeId,
|
||||||
|
'sourceSign': "wechat"
|
||||||
|
}
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [],
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭弹窗
|
||||||
|
*/
|
||||||
|
var closeUpWindow = function(schemeId, success, fail) {
|
||||||
|
var data = {
|
||||||
|
'cardNo': wx.getStorageSync("cardInfo").cardNo,
|
||||||
|
'wid': app.globalData.memberWid,
|
||||||
|
'memberId': wx.getStorageSync("memberId"),
|
||||||
|
'method': 'elec.promotion.packet.unreceive',
|
||||||
|
'schemeId': schemeId,
|
||||||
|
'sourceSign': "wechat"
|
||||||
|
}
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [],
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 会员分享规则详情
|
||||||
|
*/
|
||||||
|
var memberShareRule = function(success, fail) {
|
||||||
|
var data = {
|
||||||
|
'shopNo': app.globalData.shopNo,
|
||||||
|
'posNo': app.globalData.posNo,
|
||||||
|
'method': 'pro.share.member.info',
|
||||||
|
}
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [],
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
会员活动 大转盘
|
||||||
|
*/
|
||||||
|
var memberActivity = function(success, fail) {
|
||||||
|
var data = {
|
||||||
|
'groupId': app.globalData.groupId,
|
||||||
|
'method': 'pro.activity.list',
|
||||||
|
}
|
||||||
|
//app.globalData.memberUrl
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl ).ajax(data, [],
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
会员活动 大转盘 抽奖
|
||||||
|
*/
|
||||||
|
var memberActivityLuckyDraw = function(activityId, success, fail) {
|
||||||
|
var data = {
|
||||||
|
'wid': app.globalData.memberWid,
|
||||||
|
"memberId": wx.getStorageSync("memberId"),
|
||||||
|
"activityId": activityId,
|
||||||
|
"shopNo": app.globalData.shopNo,
|
||||||
|
"postNo": app.globalData.posNo,
|
||||||
|
"workerNo": app.globalData.workerNo,
|
||||||
|
"sourceSign": app.sourceSign,
|
||||||
|
"mobile": wx.getStorageSync("cardInfo").mobile,
|
||||||
|
"cardNo": wx.getStorageSync("cardInfo").cardNo,
|
||||||
|
'method': 'member.activity.lucky.draw',
|
||||||
|
}
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [],
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 会员营销活动记录
|
||||||
|
*/
|
||||||
|
var memberActivityRecorde = function(success, fail) {
|
||||||
|
var data = {
|
||||||
|
'memberId': wx.getStorageSync("memberId"),
|
||||||
|
'method': 'member.activity.record',
|
||||||
|
}
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [],
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 会员营销活动记录
|
||||||
|
*/
|
||||||
|
var memberActivityJoinNumber = function(activityId, success, fail) {
|
||||||
|
var data = {
|
||||||
|
'memberId': wx.getStorageSync("memberId"),
|
||||||
|
'method': 'pro.activity.join.number',
|
||||||
|
'activityId': activityId
|
||||||
|
}
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [],
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询邀请好友活动
|
||||||
|
*/
|
||||||
|
var getInvitationFriend = function(success, fail) {
|
||||||
|
var data = {
|
||||||
|
'method': 'pro.share.member.info',
|
||||||
|
"shopNo": app.globalData.shopNo,
|
||||||
|
"posNo": app.globalData.posNo,
|
||||||
|
}
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [],
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邀请好友 好友注册发送邀请人奖品
|
||||||
|
*/
|
||||||
|
var sendGift = function (success, fail) {
|
||||||
|
var data = {
|
||||||
|
'method': 'pro.activity.share.new',
|
||||||
|
'activityId': app.temData.activityId,
|
||||||
|
'wid': app.globalData.memberWid,
|
||||||
|
'memberId': app.temData.fromMemberId,
|
||||||
|
'sourceSign': app.sourceSign,
|
||||||
|
'toMemberId': wx.getStorageSync("memberId"),
|
||||||
|
"shopNo": app.globalData.shopNo,
|
||||||
|
"posNo": app.globalData.posNo,
|
||||||
|
'workerNo': app.globalData.workerNo,
|
||||||
|
'openId': app.temData.openId,
|
||||||
|
}
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [],
|
||||||
|
function (json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function (error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查看我邀请的好友人数
|
||||||
|
*/
|
||||||
|
var getSendGiftNumber = function (activityId,success, fail) {
|
||||||
|
var data = {
|
||||||
|
'method': 'pro.activity.share.number',
|
||||||
|
'activityId': activityId,
|
||||||
|
'wid': app.globalData.memberWid,
|
||||||
|
'memberId': wx.getStorageSync("memberId"),
|
||||||
|
}
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [],
|
||||||
|
function (json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function (error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据openId查询用户信息
|
||||||
|
*/
|
||||||
|
var getMemberInfoByOpenId = function(success, fail) {
|
||||||
|
var params = {
|
||||||
|
"method": "small.weixin.member.info.wid1",
|
||||||
|
"wid": app.globalData.memberWid,
|
||||||
|
"openId": app.openId,
|
||||||
|
};
|
||||||
|
var ignores = ["openId", "nickName", "gender", "avatarUrl", "country", "province", "city"];
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(params, ignores,
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询优惠券适用门店数量
|
||||||
|
*/
|
||||||
|
var getCouponApplyShopNum = function(couponId,longitude,latitude,success,fail) {
|
||||||
|
var params = {
|
||||||
|
'couponId': couponId,
|
||||||
|
'pageNum': 1,
|
||||||
|
'pageSize': 1,
|
||||||
|
'property': "",
|
||||||
|
'keyword': "",
|
||||||
|
'longitude': longitude,
|
||||||
|
'latitude': latitude,
|
||||||
|
'method': 'elec.coupon.shops.distance'
|
||||||
|
};
|
||||||
|
var ignores = ['property', 'keyword'];
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(params, ignores,
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 查询优惠券适用门店数量
|
||||||
|
*/
|
||||||
|
var getCouponApplyShop = function(couponId,pageNum,pageSize,longitude,latitude,name,value,success,fail) {
|
||||||
|
var params = {
|
||||||
|
'couponId': couponId,
|
||||||
|
'pageNum': pageNum,
|
||||||
|
'pageSize': pageSize,
|
||||||
|
'property':name,
|
||||||
|
'keyword': value,
|
||||||
|
'longitude': longitude,
|
||||||
|
'latitude': latitude,
|
||||||
|
'method': 'elec.coupon.shops.distance'
|
||||||
|
};
|
||||||
|
var ignores = ['property', 'keyword'];
|
||||||
|
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(params, ignores,
|
||||||
|
function(json) {
|
||||||
|
success(json);
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
fail(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
module.exports = {
|
||||||
|
checkMemberInfo: checkMemberInfo,
|
||||||
|
cardGoodsDetails: cardGoodsDetails,
|
||||||
|
cardMoney: cardMoney,
|
||||||
|
getCardPayInfo: getCardPayInfo,
|
||||||
|
getPays: getPays,
|
||||||
|
WxRechargeScheme: WxRechargeScheme,
|
||||||
|
getTradeNo: getTradeNo,
|
||||||
|
randomString: randomString,
|
||||||
|
queryGroupCouponsList: queryGroupCouponsList,
|
||||||
|
getCouponsName: getCouponsName,
|
||||||
|
getCardWorth: getCardWorth,
|
||||||
|
getCardWorthInfo: getCardWorthInfo,
|
||||||
|
wx_getPhoneNumber: wx_getPhoneNumber,
|
||||||
|
wx_cardInfo: wx_cardInfo,
|
||||||
|
couponCenter: couponCenter,
|
||||||
|
exchangeCouponScheme: exchangeCouponScheme,
|
||||||
|
sendTemPlate: sendTemPlate,
|
||||||
|
pointProduct: pointProduct,
|
||||||
|
queryPointProductDetail: queryPointProductDetail,
|
||||||
|
judgeUpWindown: judgeUpWindown,
|
||||||
|
receive: receive,
|
||||||
|
closeUpWindow: closeUpWindow,
|
||||||
|
pointAllTypeProduct: pointAllTypeProduct,
|
||||||
|
memberShareRule: memberShareRule,
|
||||||
|
memberActivity: memberActivity,
|
||||||
|
memberActivityLuckyDraw: memberActivityLuckyDraw,
|
||||||
|
memberActivityRecorde: memberActivityRecorde,
|
||||||
|
memberActivityJoinNumber: memberActivityJoinNumber,
|
||||||
|
getInvitationFriend: getInvitationFriend,
|
||||||
|
sendGift: sendGift,
|
||||||
|
getSendGiftNumber: getSendGiftNumber,
|
||||||
|
getMemberInfoByOpenId:getMemberInfoByOpenId,
|
||||||
|
getCouponApplyShop:getCouponApplyShop,
|
||||||
|
getCouponApplyShopNum:getCouponApplyShopNum
|
||||||
|
// getMemberExtend: getMemberExtend
|
||||||
|
}
|
@ -0,0 +1,116 @@
|
|||||||
|
var cartUtils = {};
|
||||||
|
var app = {};
|
||||||
|
|
||||||
|
var init = function (app,cartUtils){
|
||||||
|
this.app = app;
|
||||||
|
this.cartUtils = cartUtils;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加入购物车
|
||||||
|
*/
|
||||||
|
var addToCart = function (goods, num) {
|
||||||
|
var suiltData = {};
|
||||||
|
if (goods.suitFlag == 1){
|
||||||
|
suiltData = processSuitData(this.app,goods, num);
|
||||||
|
}
|
||||||
|
var options = {
|
||||||
|
boxPrice: goods.boxPrice,
|
||||||
|
uniqueId: goods.uniqueId,
|
||||||
|
productId: goods.productId,
|
||||||
|
productNo: goods.productNo,
|
||||||
|
unitId: goods.unitId,
|
||||||
|
categoryId: goods.categoryId,
|
||||||
|
goodsId: goods.goodsId,
|
||||||
|
goodsName: goods.name,
|
||||||
|
specId: goods.specId,
|
||||||
|
specName: goods.specName,
|
||||||
|
makeCount: undefined == goods.makeCount ? 0 : goods.makeCount,
|
||||||
|
price: goods.price,
|
||||||
|
memberPrice: goods.memberPrice,
|
||||||
|
makeId: undefined == goods.makeId ? "" : goods.makeId,
|
||||||
|
makeName: undefined == goods.makeName ? "" : goods.makeName,
|
||||||
|
makePrice: undefined == goods.makePrice ? "" : goods.makePrice,
|
||||||
|
makeAmount: undefined == goods.makeAmount ? 0 : goods.makeAmount,
|
||||||
|
num: num,
|
||||||
|
isSuit: goods.suitFlag == 1 ?true:false,
|
||||||
|
addAmount: undefined == suiltData.addAmount ? 0 : suiltData.addAmount,
|
||||||
|
suitDetails: undefined == suiltData.suitDetails ? [] : suiltData.suitDetails
|
||||||
|
}
|
||||||
|
// 商品压入购物车
|
||||||
|
this.cartUtils.putCartMap(options);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理套餐商品加入购物车
|
||||||
|
*/
|
||||||
|
function processSuitData(app,goods,num){
|
||||||
|
var suiltData = {};
|
||||||
|
|
||||||
|
var suitDetails = [];
|
||||||
|
goods.suitList.forEach(function (group, ginx) {
|
||||||
|
// 获取已选中套餐明细
|
||||||
|
group.detail.forEach(function (item, inx) {
|
||||||
|
if (item.select) {
|
||||||
|
var suitItem = {
|
||||||
|
goodsId: item.id,
|
||||||
|
name: item.productName,
|
||||||
|
productId: item.productId,
|
||||||
|
productNo: item.productNo,
|
||||||
|
categoryId: item.categoryId,
|
||||||
|
specId: item.specId,
|
||||||
|
specName: item.specName,
|
||||||
|
unitId: item.unitId,
|
||||||
|
num: item.num,
|
||||||
|
quantity:item.quantity,
|
||||||
|
price: item.price,
|
||||||
|
addPrice: item.addPrice,
|
||||||
|
memberPrice: item.memberPrice,
|
||||||
|
makeId: item.makeId,
|
||||||
|
makeName: item.makeName,
|
||||||
|
makePrice: item.makePrice,
|
||||||
|
makeAmount: item.makeAmount,
|
||||||
|
}
|
||||||
|
suitDetails.push(suitItem);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
suiltData.addAmount = goods.addAmount;
|
||||||
|
suiltData.suitDetails = suitDetails;
|
||||||
|
return suiltData;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从购物车中减去商品数量
|
||||||
|
*/
|
||||||
|
var addNumberByCart = function (uniqueId) {
|
||||||
|
var specMap = this.cartUtils.getSpecMapByCart();
|
||||||
|
if (undefined != specMap[uniqueId]) {
|
||||||
|
var specGoods = specMap[uniqueId];
|
||||||
|
var num = this.app.utils.numAdd(specGoods.num, 1);
|
||||||
|
this.addToCart(specGoods, num);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从购物车中减去商品数量
|
||||||
|
*/
|
||||||
|
var decNumberByCart = function (uniqueId) {
|
||||||
|
var specMap = this.cartUtils.getSpecMapByCart();
|
||||||
|
if (undefined != specMap[uniqueId]) {
|
||||||
|
var specGoods = specMap[uniqueId];
|
||||||
|
var num = 0;
|
||||||
|
if (specGoods.num > 0) {
|
||||||
|
num = this.app.utils.numSub(specGoods.num, 1);
|
||||||
|
}
|
||||||
|
this.addToCart(specGoods, num);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
init: init,
|
||||||
|
addToCart: addToCart,
|
||||||
|
addNumberByCart: addNumberByCart,
|
||||||
|
decNumberByCart: decNumberByCart
|
||||||
|
}
|
@ -0,0 +1,883 @@
|
|||||||
|
|
||||||
|
/**
|
||||||
|
* DES加密/解密
|
||||||
|
* @Copyright Copyright (c) 2006
|
||||||
|
* @author Guapo
|
||||||
|
* @see DESCore
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* encrypt the string to string made up of hex
|
||||||
|
* return the encrypted string
|
||||||
|
*/
|
||||||
|
function strEnc(data, firstKey, secondKey, thirdKey) {
|
||||||
|
|
||||||
|
var leng = data.length;
|
||||||
|
var encData = "";
|
||||||
|
var firstKeyBt, secondKeyBt, thirdKeyBt, firstLength, secondLength, thirdLength;
|
||||||
|
if (firstKey != null && firstKey != "") {
|
||||||
|
firstKeyBt = getKeyBytes(firstKey);
|
||||||
|
firstLength = firstKeyBt.length;
|
||||||
|
}
|
||||||
|
if (secondKey != null && secondKey != "") {
|
||||||
|
secondKeyBt = getKeyBytes(secondKey);
|
||||||
|
secondLength = secondKeyBt.length;
|
||||||
|
}
|
||||||
|
if (thirdKey != null && thirdKey != "") {
|
||||||
|
thirdKeyBt = getKeyBytes(thirdKey);
|
||||||
|
thirdLength = thirdKeyBt.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (leng > 0) {
|
||||||
|
if (leng < 4) {
|
||||||
|
var bt = strToBt(data);
|
||||||
|
var encByte;
|
||||||
|
if (firstKey != null && firstKey != "" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != "") {
|
||||||
|
var tempBt;
|
||||||
|
var x, y, z;
|
||||||
|
tempBt = bt;
|
||||||
|
for (x = 0; x < firstLength; x++) {
|
||||||
|
tempBt = enc(tempBt, firstKeyBt[x]);
|
||||||
|
}
|
||||||
|
for (y = 0; y < secondLength; y++) {
|
||||||
|
tempBt = enc(tempBt, secondKeyBt[y]);
|
||||||
|
}
|
||||||
|
for (z = 0; z < thirdLength; z++) {
|
||||||
|
tempBt = enc(tempBt, thirdKeyBt[z]);
|
||||||
|
}
|
||||||
|
encByte = tempBt;
|
||||||
|
} else {
|
||||||
|
if (firstKey != null && firstKey != "" && secondKey != null && secondKey != "") {
|
||||||
|
var tempBt;
|
||||||
|
var x, y;
|
||||||
|
tempBt = bt;
|
||||||
|
for (x = 0; x < firstLength; x++) {
|
||||||
|
tempBt = enc(tempBt, firstKeyBt[x]);
|
||||||
|
}
|
||||||
|
for (y = 0; y < secondLength; y++) {
|
||||||
|
tempBt = enc(tempBt, secondKeyBt[y]);
|
||||||
|
}
|
||||||
|
encByte = tempBt;
|
||||||
|
} else {
|
||||||
|
if (firstKey != null && firstKey != "") {
|
||||||
|
var tempBt;
|
||||||
|
var x = 0;
|
||||||
|
tempBt = bt;
|
||||||
|
for (x = 0; x < firstLength; x++) {
|
||||||
|
tempBt = enc(tempBt, firstKeyBt[x]);
|
||||||
|
}
|
||||||
|
encByte = tempBt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
encData = bt64ToHex(encByte);
|
||||||
|
} else {
|
||||||
|
var iterator = parseInt(leng / 4);
|
||||||
|
var remainder = leng % 4;
|
||||||
|
var i = 0;
|
||||||
|
for (i = 0; i < iterator; i++) {
|
||||||
|
var tempData = data.substring(i * 4 + 0, i * 4 + 4);
|
||||||
|
var tempByte = strToBt(tempData);
|
||||||
|
var encByte;
|
||||||
|
if (firstKey != null && firstKey != "" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != "") {
|
||||||
|
var tempBt;
|
||||||
|
var x, y, z;
|
||||||
|
tempBt = tempByte;
|
||||||
|
for (x = 0; x < firstLength; x++) {
|
||||||
|
tempBt = enc(tempBt, firstKeyBt[x]);
|
||||||
|
}
|
||||||
|
for (y = 0; y < secondLength; y++) {
|
||||||
|
tempBt = enc(tempBt, secondKeyBt[y]);
|
||||||
|
}
|
||||||
|
for (z = 0; z < thirdLength; z++) {
|
||||||
|
tempBt = enc(tempBt, thirdKeyBt[z]);
|
||||||
|
}
|
||||||
|
encByte = tempBt;
|
||||||
|
} else {
|
||||||
|
if (firstKey != null && firstKey != "" && secondKey != null && secondKey != "") {
|
||||||
|
var tempBt;
|
||||||
|
var x, y;
|
||||||
|
tempBt = tempByte;
|
||||||
|
for (x = 0; x < firstLength; x++) {
|
||||||
|
tempBt = enc(tempBt, firstKeyBt[x]);
|
||||||
|
}
|
||||||
|
for (y = 0; y < secondLength; y++) {
|
||||||
|
tempBt = enc(tempBt, secondKeyBt[y]);
|
||||||
|
}
|
||||||
|
encByte = tempBt;
|
||||||
|
} else {
|
||||||
|
if (firstKey != null && firstKey != "") {
|
||||||
|
var tempBt;
|
||||||
|
var x;
|
||||||
|
tempBt = tempByte;
|
||||||
|
for (x = 0; x < firstLength; x++) {
|
||||||
|
tempBt = enc(tempBt, firstKeyBt[x]);
|
||||||
|
}
|
||||||
|
encByte = tempBt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
encData += bt64ToHex(encByte);
|
||||||
|
}
|
||||||
|
if (remainder > 0) {
|
||||||
|
var remainderData = data.substring(iterator * 4 + 0, leng);
|
||||||
|
var tempByte = strToBt(remainderData);
|
||||||
|
var encByte;
|
||||||
|
if (firstKey != null && firstKey != "" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != "") {
|
||||||
|
var tempBt;
|
||||||
|
var x, y, z;
|
||||||
|
tempBt = tempByte;
|
||||||
|
for (x = 0; x < firstLength; x++) {
|
||||||
|
tempBt = enc(tempBt, firstKeyBt[x]);
|
||||||
|
}
|
||||||
|
for (y = 0; y < secondLength; y++) {
|
||||||
|
tempBt = enc(tempBt, secondKeyBt[y]);
|
||||||
|
}
|
||||||
|
for (z = 0; z < thirdLength; z++) {
|
||||||
|
tempBt = enc(tempBt, thirdKeyBt[z]);
|
||||||
|
}
|
||||||
|
encByte = tempBt;
|
||||||
|
} else {
|
||||||
|
if (firstKey != null && firstKey != "" && secondKey != null && secondKey != "") {
|
||||||
|
var tempBt;
|
||||||
|
var x, y;
|
||||||
|
tempBt = tempByte;
|
||||||
|
for (x = 0; x < firstLength; x++) {
|
||||||
|
tempBt = enc(tempBt, firstKeyBt[x]);
|
||||||
|
}
|
||||||
|
for (y = 0; y < secondLength; y++) {
|
||||||
|
tempBt = enc(tempBt, secondKeyBt[y]);
|
||||||
|
}
|
||||||
|
encByte = tempBt;
|
||||||
|
} else {
|
||||||
|
if (firstKey != null && firstKey != "") {
|
||||||
|
var tempBt;
|
||||||
|
var x;
|
||||||
|
tempBt = tempByte;
|
||||||
|
for (x = 0; x < firstLength; x++) {
|
||||||
|
tempBt = enc(tempBt, firstKeyBt[x]);
|
||||||
|
}
|
||||||
|
encByte = tempBt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
encData += bt64ToHex(encByte);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return encData;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* decrypt the encrypted string to the original string
|
||||||
|
*
|
||||||
|
* return the original string
|
||||||
|
*/
|
||||||
|
function strDec(data, firstKey, secondKey, thirdKey) {
|
||||||
|
var leng = data.length;
|
||||||
|
var decStr = "";
|
||||||
|
var firstKeyBt, secondKeyBt, thirdKeyBt, firstLength, secondLength, thirdLength;
|
||||||
|
if (firstKey != null && firstKey != "") {
|
||||||
|
firstKeyBt = getKeyBytes(firstKey);
|
||||||
|
firstLength = firstKeyBt.length;
|
||||||
|
}
|
||||||
|
if (secondKey != null && secondKey != "") {
|
||||||
|
secondKeyBt = getKeyBytes(secondKey);
|
||||||
|
secondLength = secondKeyBt.length;
|
||||||
|
}
|
||||||
|
if (thirdKey != null && thirdKey != "") {
|
||||||
|
thirdKeyBt = getKeyBytes(thirdKey);
|
||||||
|
thirdLength = thirdKeyBt.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
var iterator = parseInt(leng / 16);
|
||||||
|
var i = 0;
|
||||||
|
for (i = 0; i < iterator; i++) {
|
||||||
|
var tempData = data.substring(i * 16 + 0, i * 16 + 16);
|
||||||
|
var strByte = hexToBt64(tempData);
|
||||||
|
var intByte = new Array(64);
|
||||||
|
var j = 0;
|
||||||
|
for (j = 0; j < 64; j++) {
|
||||||
|
intByte[j] = parseInt(strByte.substring(j, j + 1));
|
||||||
|
}
|
||||||
|
var decByte;
|
||||||
|
if (firstKey != null && firstKey != "" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != "") {
|
||||||
|
var tempBt;
|
||||||
|
var x, y, z;
|
||||||
|
tempBt = intByte;
|
||||||
|
for (x = thirdLength - 1; x >= 0; x--) {
|
||||||
|
tempBt = dec(tempBt, thirdKeyBt[x]);
|
||||||
|
}
|
||||||
|
for (y = secondLength - 1; y >= 0; y--) {
|
||||||
|
tempBt = dec(tempBt, secondKeyBt[y]);
|
||||||
|
}
|
||||||
|
for (z = firstLength - 1; z >= 0; z--) {
|
||||||
|
tempBt = dec(tempBt, firstKeyBt[z]);
|
||||||
|
}
|
||||||
|
decByte = tempBt;
|
||||||
|
} else {
|
||||||
|
if (firstKey != null && firstKey != "" && secondKey != null && secondKey != "") {
|
||||||
|
var tempBt;
|
||||||
|
var x, y, z;
|
||||||
|
tempBt = intByte;
|
||||||
|
for (x = secondLength - 1; x >= 0; x--) {
|
||||||
|
tempBt = dec(tempBt, secondKeyBt[x]);
|
||||||
|
}
|
||||||
|
for (y = firstLength - 1; y >= 0; y--) {
|
||||||
|
tempBt = dec(tempBt, firstKeyBt[y]);
|
||||||
|
}
|
||||||
|
decByte = tempBt;
|
||||||
|
} else {
|
||||||
|
if (firstKey != null && firstKey != "") {
|
||||||
|
var tempBt;
|
||||||
|
var x, y, z;
|
||||||
|
tempBt = intByte;
|
||||||
|
for (x = firstLength - 1; x >= 0; x--) {
|
||||||
|
tempBt = dec(tempBt, firstKeyBt[x]);
|
||||||
|
}
|
||||||
|
decByte = tempBt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
decStr += byteToString(decByte);
|
||||||
|
}
|
||||||
|
return decStr;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* chang the string into the bit array
|
||||||
|
*
|
||||||
|
* return bit array(it's length % 64 = 0)
|
||||||
|
*/
|
||||||
|
function getKeyBytes(key) {
|
||||||
|
var keyBytes = new Array();
|
||||||
|
var leng = key.length;
|
||||||
|
var iterator = parseInt(leng / 4);
|
||||||
|
var remainder = leng % 4;
|
||||||
|
var i = 0;
|
||||||
|
for (i = 0; i < iterator; i++) {
|
||||||
|
keyBytes[i] = strToBt(key.substring(i * 4 + 0, i * 4 + 4));
|
||||||
|
}
|
||||||
|
if (remainder > 0) {
|
||||||
|
keyBytes[i] = strToBt(key.substring(i * 4 + 0, leng));
|
||||||
|
}
|
||||||
|
return keyBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* chang the string(it's length <= 4) into the bit array
|
||||||
|
*
|
||||||
|
* return bit array(it's length = 64)
|
||||||
|
*/
|
||||||
|
function strToBt(str) {
|
||||||
|
var leng = str.length;
|
||||||
|
var bt = new Array(64);
|
||||||
|
if (leng < 4) {
|
||||||
|
var i = 0, j = 0, p = 0, q = 0;
|
||||||
|
for (i = 0; i < leng; i++) {
|
||||||
|
var k = str.charCodeAt(i);
|
||||||
|
for (j = 0; j < 16; j++) {
|
||||||
|
var pow = 1, m = 0;
|
||||||
|
for (m = 15; m > j; m--) {
|
||||||
|
pow *= 2;
|
||||||
|
}
|
||||||
|
bt[16 * i + j] = parseInt(k / pow) % 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (p = leng; p < 4; p++) {
|
||||||
|
var k = 0;
|
||||||
|
for (q = 0; q < 16; q++) {
|
||||||
|
var pow = 1, m = 0;
|
||||||
|
for (m = 15; m > q; m--) {
|
||||||
|
pow *= 2;
|
||||||
|
}
|
||||||
|
bt[16 * p + q] = parseInt(k / pow) % 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (i = 0; i < 4; i++) {
|
||||||
|
var k = str.charCodeAt(i);
|
||||||
|
for (j = 0; j < 16; j++) {
|
||||||
|
var pow = 1;
|
||||||
|
for (m = 15; m > j; m--) {
|
||||||
|
pow *= 2;
|
||||||
|
}
|
||||||
|
bt[16 * i + j] = parseInt(k / pow) % 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bt;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* chang the bit(it's length = 4) into the hex
|
||||||
|
*
|
||||||
|
* return hex
|
||||||
|
*/
|
||||||
|
function bt4ToHex(binary) {
|
||||||
|
var hex;
|
||||||
|
switch (binary) {
|
||||||
|
case "0000": hex = "0"; break;
|
||||||
|
case "0001": hex = "1"; break;
|
||||||
|
case "0010": hex = "2"; break;
|
||||||
|
case "0011": hex = "3"; break;
|
||||||
|
case "0100": hex = "4"; break;
|
||||||
|
case "0101": hex = "5"; break;
|
||||||
|
case "0110": hex = "6"; break;
|
||||||
|
case "0111": hex = "7"; break;
|
||||||
|
case "1000": hex = "8"; break;
|
||||||
|
case "1001": hex = "9"; break;
|
||||||
|
case "1010": hex = "A"; break;
|
||||||
|
case "1011": hex = "B"; break;
|
||||||
|
case "1100": hex = "C"; break;
|
||||||
|
case "1101": hex = "D"; break;
|
||||||
|
case "1110": hex = "E"; break;
|
||||||
|
case "1111": hex = "F"; break;
|
||||||
|
}
|
||||||
|
return hex;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* chang the hex into the bit(it's length = 4)
|
||||||
|
*
|
||||||
|
* return the bit(it's length = 4)
|
||||||
|
*/
|
||||||
|
function hexToBt4(hex) {
|
||||||
|
var binary;
|
||||||
|
switch (hex) {
|
||||||
|
case "0": binary = "0000"; break;
|
||||||
|
case "1": binary = "0001"; break;
|
||||||
|
case "2": binary = "0010"; break;
|
||||||
|
case "3": binary = "0011"; break;
|
||||||
|
case "4": binary = "0100"; break;
|
||||||
|
case "5": binary = "0101"; break;
|
||||||
|
case "6": binary = "0110"; break;
|
||||||
|
case "7": binary = "0111"; break;
|
||||||
|
case "8": binary = "1000"; break;
|
||||||
|
case "9": binary = "1001"; break;
|
||||||
|
case "A": binary = "1010"; break;
|
||||||
|
case "B": binary = "1011"; break;
|
||||||
|
case "C": binary = "1100"; break;
|
||||||
|
case "D": binary = "1101"; break;
|
||||||
|
case "E": binary = "1110"; break;
|
||||||
|
case "F": binary = "1111"; break;
|
||||||
|
}
|
||||||
|
return binary;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* chang the bit(it's length = 64) into the string
|
||||||
|
*
|
||||||
|
* return string
|
||||||
|
*/
|
||||||
|
function byteToString(byteData) {
|
||||||
|
var str = "";
|
||||||
|
for (var i = 0; i < 4; i++) {
|
||||||
|
var count = 0;
|
||||||
|
for (var j = 0; j < 16; j++) {
|
||||||
|
var pow = 1;
|
||||||
|
for (var m = 15; m > j; m--) {
|
||||||
|
pow *= 2;
|
||||||
|
}
|
||||||
|
count += byteData[16 * i + j] * pow;
|
||||||
|
}
|
||||||
|
if (count != 0) {
|
||||||
|
str += String.fromCharCode(count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
function bt64ToHex(byteData) {
|
||||||
|
var hex = "";
|
||||||
|
for (var i = 0; i < 16; i++) {
|
||||||
|
var bt = "";
|
||||||
|
for (var j = 0; j < 4; j++) {
|
||||||
|
bt += byteData[i * 4 + j];
|
||||||
|
}
|
||||||
|
hex += bt4ToHex(bt);
|
||||||
|
}
|
||||||
|
return hex;
|
||||||
|
}
|
||||||
|
|
||||||
|
function hexToBt64(hex) {
|
||||||
|
var binary = "";
|
||||||
|
for (var i = 0; i < 16; i++) {
|
||||||
|
binary += hexToBt4(hex.substring(i, i + 1));
|
||||||
|
}
|
||||||
|
return binary;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* the 64 bit des core arithmetic
|
||||||
|
*/
|
||||||
|
|
||||||
|
function enc(dataByte, keyByte) {
|
||||||
|
var keys = generateKeys(keyByte);
|
||||||
|
var ipByte = initPermute(dataByte);
|
||||||
|
var ipLeft = new Array(32);
|
||||||
|
var ipRight = new Array(32);
|
||||||
|
var tempLeft = new Array(32);
|
||||||
|
var i = 0, j = 0, k = 0, m = 0, n = 0;
|
||||||
|
for (k = 0; k < 32; k++) {
|
||||||
|
ipLeft[k] = ipByte[k];
|
||||||
|
ipRight[k] = ipByte[32 + k];
|
||||||
|
}
|
||||||
|
for (i = 0; i < 16; i++) {
|
||||||
|
for (j = 0; j < 32; j++) {
|
||||||
|
tempLeft[j] = ipLeft[j];
|
||||||
|
ipLeft[j] = ipRight[j];
|
||||||
|
}
|
||||||
|
var key = new Array(48);
|
||||||
|
for (m = 0; m < 48; m++) {
|
||||||
|
key[m] = keys[i][m];
|
||||||
|
}
|
||||||
|
var tempRight = xor(pPermute(sBoxPermute(xor(expandPermute(ipRight), key))), tempLeft);
|
||||||
|
for (n = 0; n < 32; n++) {
|
||||||
|
ipRight[n] = tempRight[n];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
var finalData = new Array(64);
|
||||||
|
for (i = 0; i < 32; i++) {
|
||||||
|
finalData[i] = ipRight[i];
|
||||||
|
finalData[32 + i] = ipLeft[i];
|
||||||
|
}
|
||||||
|
return finallyPermute(finalData);
|
||||||
|
}
|
||||||
|
|
||||||
|
function dec(dataByte, keyByte) {
|
||||||
|
var keys = generateKeys(keyByte);
|
||||||
|
var ipByte = initPermute(dataByte);
|
||||||
|
var ipLeft = new Array(32);
|
||||||
|
var ipRight = new Array(32);
|
||||||
|
var tempLeft = new Array(32);
|
||||||
|
var i = 0, j = 0, k = 0, m = 0, n = 0;
|
||||||
|
for (k = 0; k < 32; k++) {
|
||||||
|
ipLeft[k] = ipByte[k];
|
||||||
|
ipRight[k] = ipByte[32 + k];
|
||||||
|
}
|
||||||
|
for (i = 15; i >= 0; i--) {
|
||||||
|
for (j = 0; j < 32; j++) {
|
||||||
|
tempLeft[j] = ipLeft[j];
|
||||||
|
ipLeft[j] = ipRight[j];
|
||||||
|
}
|
||||||
|
var key = new Array(48);
|
||||||
|
for (m = 0; m < 48; m++) {
|
||||||
|
key[m] = keys[i][m];
|
||||||
|
}
|
||||||
|
|
||||||
|
var tempRight = xor(pPermute(sBoxPermute(xor(expandPermute(ipRight), key))), tempLeft);
|
||||||
|
for (n = 0; n < 32; n++) {
|
||||||
|
ipRight[n] = tempRight[n];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
var finalData = new Array(64);
|
||||||
|
for (i = 0; i < 32; i++) {
|
||||||
|
finalData[i] = ipRight[i];
|
||||||
|
finalData[32 + i] = ipLeft[i];
|
||||||
|
}
|
||||||
|
return finallyPermute(finalData);
|
||||||
|
}
|
||||||
|
|
||||||
|
function initPermute(originalData) {
|
||||||
|
var ipByte = new Array(64);
|
||||||
|
for (var i = 0, m = 1, n = 0; i < 4; i++ , m += 2, n += 2) {
|
||||||
|
for (var j = 7, k = 0; j >= 0; j-- , k++) {
|
||||||
|
ipByte[i * 8 + k] = originalData[j * 8 + m];
|
||||||
|
ipByte[i * 8 + k + 32] = originalData[j * 8 + n];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ipByte;
|
||||||
|
}
|
||||||
|
|
||||||
|
function expandPermute(rightData) {
|
||||||
|
var epByte = new Array(48);
|
||||||
|
for (var i = 0; i < 8; i++) {
|
||||||
|
if (i == 0) {
|
||||||
|
epByte[i * 6 + 0] = rightData[31];
|
||||||
|
} else {
|
||||||
|
epByte[i * 6 + 0] = rightData[i * 4 - 1];
|
||||||
|
}
|
||||||
|
epByte[i * 6 + 1] = rightData[i * 4 + 0];
|
||||||
|
epByte[i * 6 + 2] = rightData[i * 4 + 1];
|
||||||
|
epByte[i * 6 + 3] = rightData[i * 4 + 2];
|
||||||
|
epByte[i * 6 + 4] = rightData[i * 4 + 3];
|
||||||
|
if (i == 7) {
|
||||||
|
epByte[i * 6 + 5] = rightData[0];
|
||||||
|
} else {
|
||||||
|
epByte[i * 6 + 5] = rightData[i * 4 + 4];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return epByte;
|
||||||
|
}
|
||||||
|
|
||||||
|
function xor(byteOne, byteTwo) {
|
||||||
|
var xorByte = new Array(byteOne.length);
|
||||||
|
for (var i = 0; i < byteOne.length; i++) {
|
||||||
|
xorByte[i] = byteOne[i] ^ byteTwo[i];
|
||||||
|
}
|
||||||
|
return xorByte;
|
||||||
|
}
|
||||||
|
|
||||||
|
function sBoxPermute(expandByte) {
|
||||||
|
|
||||||
|
var sBoxByte = new Array(32);
|
||||||
|
var binary = "";
|
||||||
|
var s1 = [
|
||||||
|
[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7],
|
||||||
|
[0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8],
|
||||||
|
[4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0],
|
||||||
|
[15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13]];
|
||||||
|
|
||||||
|
/* Table - s2 */
|
||||||
|
var s2 = [
|
||||||
|
[15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10],
|
||||||
|
[3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5],
|
||||||
|
[0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15],
|
||||||
|
[13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9]];
|
||||||
|
|
||||||
|
/* Table - s3 */
|
||||||
|
var s3 = [
|
||||||
|
[10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8],
|
||||||
|
[13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1],
|
||||||
|
[13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7],
|
||||||
|
[1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12]];
|
||||||
|
/* Table - s4 */
|
||||||
|
var s4 = [
|
||||||
|
[7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15],
|
||||||
|
[13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9],
|
||||||
|
[10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4],
|
||||||
|
[3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14]];
|
||||||
|
|
||||||
|
/* Table - s5 */
|
||||||
|
var s5 = [
|
||||||
|
[2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9],
|
||||||
|
[14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6],
|
||||||
|
[4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14],
|
||||||
|
[11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3]];
|
||||||
|
|
||||||
|
/* Table - s6 */
|
||||||
|
var s6 = [
|
||||||
|
[12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11],
|
||||||
|
[10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8],
|
||||||
|
[9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6],
|
||||||
|
[4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13]];
|
||||||
|
|
||||||
|
/* Table - s7 */
|
||||||
|
var s7 = [
|
||||||
|
[4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1],
|
||||||
|
[13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6],
|
||||||
|
[1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2],
|
||||||
|
[6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12]];
|
||||||
|
|
||||||
|
/* Table - s8 */
|
||||||
|
var s8 = [
|
||||||
|
[13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7],
|
||||||
|
[1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2],
|
||||||
|
[7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8],
|
||||||
|
[2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11]];
|
||||||
|
|
||||||
|
for (var m = 0; m < 8; m++) {
|
||||||
|
var i = 0, j = 0;
|
||||||
|
i = expandByte[m * 6 + 0] * 2 + expandByte[m * 6 + 5];
|
||||||
|
j = expandByte[m * 6 + 1] * 2 * 2 * 2
|
||||||
|
+ expandByte[m * 6 + 2] * 2 * 2
|
||||||
|
+ expandByte[m * 6 + 3] * 2
|
||||||
|
+ expandByte[m * 6 + 4];
|
||||||
|
switch (m) {
|
||||||
|
case 0:
|
||||||
|
binary = getBoxBinary(s1[i][j]);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
binary = getBoxBinary(s2[i][j]);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
binary = getBoxBinary(s3[i][j]);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
binary = getBoxBinary(s4[i][j]);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
binary = getBoxBinary(s5[i][j]);
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
binary = getBoxBinary(s6[i][j]);
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
binary = getBoxBinary(s7[i][j]);
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
binary = getBoxBinary(s8[i][j]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
sBoxByte[m * 4 + 0] = parseInt(binary.substring(0, 1));
|
||||||
|
sBoxByte[m * 4 + 1] = parseInt(binary.substring(1, 2));
|
||||||
|
sBoxByte[m * 4 + 2] = parseInt(binary.substring(2, 3));
|
||||||
|
sBoxByte[m * 4 + 3] = parseInt(binary.substring(3, 4));
|
||||||
|
}
|
||||||
|
return sBoxByte;
|
||||||
|
}
|
||||||
|
|
||||||
|
function pPermute(sBoxByte) {
|
||||||
|
var pBoxPermute = new Array(32);
|
||||||
|
pBoxPermute[0] = sBoxByte[15];
|
||||||
|
pBoxPermute[1] = sBoxByte[6];
|
||||||
|
pBoxPermute[2] = sBoxByte[19];
|
||||||
|
pBoxPermute[3] = sBoxByte[20];
|
||||||
|
pBoxPermute[4] = sBoxByte[28];
|
||||||
|
pBoxPermute[5] = sBoxByte[11];
|
||||||
|
pBoxPermute[6] = sBoxByte[27];
|
||||||
|
pBoxPermute[7] = sBoxByte[16];
|
||||||
|
pBoxPermute[8] = sBoxByte[0];
|
||||||
|
pBoxPermute[9] = sBoxByte[14];
|
||||||
|
pBoxPermute[10] = sBoxByte[22];
|
||||||
|
pBoxPermute[11] = sBoxByte[25];
|
||||||
|
pBoxPermute[12] = sBoxByte[4];
|
||||||
|
pBoxPermute[13] = sBoxByte[17];
|
||||||
|
pBoxPermute[14] = sBoxByte[30];
|
||||||
|
pBoxPermute[15] = sBoxByte[9];
|
||||||
|
pBoxPermute[16] = sBoxByte[1];
|
||||||
|
pBoxPermute[17] = sBoxByte[7];
|
||||||
|
pBoxPermute[18] = sBoxByte[23];
|
||||||
|
pBoxPermute[19] = sBoxByte[13];
|
||||||
|
pBoxPermute[20] = sBoxByte[31];
|
||||||
|
pBoxPermute[21] = sBoxByte[26];
|
||||||
|
pBoxPermute[22] = sBoxByte[2];
|
||||||
|
pBoxPermute[23] = sBoxByte[8];
|
||||||
|
pBoxPermute[24] = sBoxByte[18];
|
||||||
|
pBoxPermute[25] = sBoxByte[12];
|
||||||
|
pBoxPermute[26] = sBoxByte[29];
|
||||||
|
pBoxPermute[27] = sBoxByte[5];
|
||||||
|
pBoxPermute[28] = sBoxByte[21];
|
||||||
|
pBoxPermute[29] = sBoxByte[10];
|
||||||
|
pBoxPermute[30] = sBoxByte[3];
|
||||||
|
pBoxPermute[31] = sBoxByte[24];
|
||||||
|
return pBoxPermute;
|
||||||
|
}
|
||||||
|
|
||||||
|
function finallyPermute(endByte) {
|
||||||
|
var fpByte = new Array(64);
|
||||||
|
fpByte[0] = endByte[39];
|
||||||
|
fpByte[1] = endByte[7];
|
||||||
|
fpByte[2] = endByte[47];
|
||||||
|
fpByte[3] = endByte[15];
|
||||||
|
fpByte[4] = endByte[55];
|
||||||
|
fpByte[5] = endByte[23];
|
||||||
|
fpByte[6] = endByte[63];
|
||||||
|
fpByte[7] = endByte[31];
|
||||||
|
fpByte[8] = endByte[38];
|
||||||
|
fpByte[9] = endByte[6];
|
||||||
|
fpByte[10] = endByte[46];
|
||||||
|
fpByte[11] = endByte[14];
|
||||||
|
fpByte[12] = endByte[54];
|
||||||
|
fpByte[13] = endByte[22];
|
||||||
|
fpByte[14] = endByte[62];
|
||||||
|
fpByte[15] = endByte[30];
|
||||||
|
fpByte[16] = endByte[37];
|
||||||
|
fpByte[17] = endByte[5];
|
||||||
|
fpByte[18] = endByte[45];
|
||||||
|
fpByte[19] = endByte[13];
|
||||||
|
fpByte[20] = endByte[53];
|
||||||
|
fpByte[21] = endByte[21];
|
||||||
|
fpByte[22] = endByte[61];
|
||||||
|
fpByte[23] = endByte[29];
|
||||||
|
fpByte[24] = endByte[36];
|
||||||
|
fpByte[25] = endByte[4];
|
||||||
|
fpByte[26] = endByte[44];
|
||||||
|
fpByte[27] = endByte[12];
|
||||||
|
fpByte[28] = endByte[52];
|
||||||
|
fpByte[29] = endByte[20];
|
||||||
|
fpByte[30] = endByte[60];
|
||||||
|
fpByte[31] = endByte[28];
|
||||||
|
fpByte[32] = endByte[35];
|
||||||
|
fpByte[33] = endByte[3];
|
||||||
|
fpByte[34] = endByte[43];
|
||||||
|
fpByte[35] = endByte[11];
|
||||||
|
fpByte[36] = endByte[51];
|
||||||
|
fpByte[37] = endByte[19];
|
||||||
|
fpByte[38] = endByte[59];
|
||||||
|
fpByte[39] = endByte[27];
|
||||||
|
fpByte[40] = endByte[34];
|
||||||
|
fpByte[41] = endByte[2];
|
||||||
|
fpByte[42] = endByte[42];
|
||||||
|
fpByte[43] = endByte[10];
|
||||||
|
fpByte[44] = endByte[50];
|
||||||
|
fpByte[45] = endByte[18];
|
||||||
|
fpByte[46] = endByte[58];
|
||||||
|
fpByte[47] = endByte[26];
|
||||||
|
fpByte[48] = endByte[33];
|
||||||
|
fpByte[49] = endByte[1];
|
||||||
|
fpByte[50] = endByte[41];
|
||||||
|
fpByte[51] = endByte[9];
|
||||||
|
fpByte[52] = endByte[49];
|
||||||
|
fpByte[53] = endByte[17];
|
||||||
|
fpByte[54] = endByte[57];
|
||||||
|
fpByte[55] = endByte[25];
|
||||||
|
fpByte[56] = endByte[32];
|
||||||
|
fpByte[57] = endByte[0];
|
||||||
|
fpByte[58] = endByte[40];
|
||||||
|
fpByte[59] = endByte[8];
|
||||||
|
fpByte[60] = endByte[48];
|
||||||
|
fpByte[61] = endByte[16];
|
||||||
|
fpByte[62] = endByte[56];
|
||||||
|
fpByte[63] = endByte[24];
|
||||||
|
return fpByte;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getBoxBinary(i) {
|
||||||
|
var binary = "";
|
||||||
|
switch (i) {
|
||||||
|
case 0: binary = "0000"; break;
|
||||||
|
case 1: binary = "0001"; break;
|
||||||
|
case 2: binary = "0010"; break;
|
||||||
|
case 3: binary = "0011"; break;
|
||||||
|
case 4: binary = "0100"; break;
|
||||||
|
case 5: binary = "0101"; break;
|
||||||
|
case 6: binary = "0110"; break;
|
||||||
|
case 7: binary = "0111"; break;
|
||||||
|
case 8: binary = "1000"; break;
|
||||||
|
case 9: binary = "1001"; break;
|
||||||
|
case 10: binary = "1010"; break;
|
||||||
|
case 11: binary = "1011"; break;
|
||||||
|
case 12: binary = "1100"; break;
|
||||||
|
case 13: binary = "1101"; break;
|
||||||
|
case 14: binary = "1110"; break;
|
||||||
|
case 15: binary = "1111"; break;
|
||||||
|
}
|
||||||
|
return binary;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* generate 16 keys for xor
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
function generateKeys(keyByte) {
|
||||||
|
var key = new Array(56);
|
||||||
|
var keys = new Array();
|
||||||
|
|
||||||
|
keys[0] = new Array();
|
||||||
|
keys[1] = new Array();
|
||||||
|
keys[2] = new Array();
|
||||||
|
keys[3] = new Array();
|
||||||
|
keys[4] = new Array();
|
||||||
|
keys[5] = new Array();
|
||||||
|
keys[6] = new Array();
|
||||||
|
keys[7] = new Array();
|
||||||
|
keys[8] = new Array();
|
||||||
|
keys[9] = new Array();
|
||||||
|
keys[10] = new Array();
|
||||||
|
keys[11] = new Array();
|
||||||
|
keys[12] = new Array();
|
||||||
|
keys[13] = new Array();
|
||||||
|
keys[14] = new Array();
|
||||||
|
keys[15] = new Array();
|
||||||
|
var loop = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1];
|
||||||
|
|
||||||
|
for (var i = 0; i < 7; i++) {
|
||||||
|
for (var j = 0, k = 7; j < 8; j++ , k--) {
|
||||||
|
key[i * 8 + j] = keyByte[8 * k + i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var i = 0;
|
||||||
|
for (i = 0; i < 16; i++) {
|
||||||
|
var tempLeft = 0;
|
||||||
|
var tempRight = 0;
|
||||||
|
for (j = 0; j < loop[i]; j++) {
|
||||||
|
tempLeft = key[0];
|
||||||
|
tempRight = key[28];
|
||||||
|
for (k = 0; k < 27; k++) {
|
||||||
|
key[k] = key[k + 1];
|
||||||
|
key[28 + k] = key[29 + k];
|
||||||
|
}
|
||||||
|
key[27] = tempLeft;
|
||||||
|
key[55] = tempRight;
|
||||||
|
}
|
||||||
|
var tempKey = new Array(48);
|
||||||
|
tempKey[0] = key[13];
|
||||||
|
tempKey[1] = key[16];
|
||||||
|
tempKey[2] = key[10];
|
||||||
|
tempKey[3] = key[23];
|
||||||
|
tempKey[4] = key[0];
|
||||||
|
tempKey[5] = key[4];
|
||||||
|
tempKey[6] = key[2];
|
||||||
|
tempKey[7] = key[27];
|
||||||
|
tempKey[8] = key[14];
|
||||||
|
tempKey[9] = key[5];
|
||||||
|
tempKey[10] = key[20];
|
||||||
|
tempKey[11] = key[9];
|
||||||
|
tempKey[12] = key[22];
|
||||||
|
tempKey[13] = key[18];
|
||||||
|
tempKey[14] = key[11];
|
||||||
|
tempKey[15] = key[3];
|
||||||
|
tempKey[16] = key[25];
|
||||||
|
tempKey[17] = key[7];
|
||||||
|
tempKey[18] = key[15];
|
||||||
|
tempKey[19] = key[6];
|
||||||
|
tempKey[20] = key[26];
|
||||||
|
tempKey[21] = key[19];
|
||||||
|
tempKey[22] = key[12];
|
||||||
|
tempKey[23] = key[1];
|
||||||
|
tempKey[24] = key[40];
|
||||||
|
tempKey[25] = key[51];
|
||||||
|
tempKey[26] = key[30];
|
||||||
|
tempKey[27] = key[36];
|
||||||
|
tempKey[28] = key[46];
|
||||||
|
tempKey[29] = key[54];
|
||||||
|
tempKey[30] = key[29];
|
||||||
|
tempKey[31] = key[39];
|
||||||
|
tempKey[32] = key[50];
|
||||||
|
tempKey[33] = key[44];
|
||||||
|
tempKey[34] = key[32];
|
||||||
|
tempKey[35] = key[47];
|
||||||
|
tempKey[36] = key[43];
|
||||||
|
tempKey[37] = key[48];
|
||||||
|
tempKey[38] = key[38];
|
||||||
|
tempKey[39] = key[55];
|
||||||
|
tempKey[40] = key[33];
|
||||||
|
tempKey[41] = key[52];
|
||||||
|
tempKey[42] = key[45];
|
||||||
|
tempKey[43] = key[41];
|
||||||
|
tempKey[44] = key[49];
|
||||||
|
tempKey[45] = key[35];
|
||||||
|
tempKey[46] = key[28];
|
||||||
|
tempKey[47] = key[31];
|
||||||
|
var m = 0;
|
||||||
|
switch (i) {
|
||||||
|
case 0: for (m = 0; m < 48; m++) { keys[0][m] = tempKey[m]; } break;
|
||||||
|
case 1: for (m = 0; m < 48; m++) { keys[1][m] = tempKey[m]; } break;
|
||||||
|
case 2: for (m = 0; m < 48; m++) { keys[2][m] = tempKey[m]; } break;
|
||||||
|
case 3: for (m = 0; m < 48; m++) { keys[3][m] = tempKey[m]; } break;
|
||||||
|
case 4: for (m = 0; m < 48; m++) { keys[4][m] = tempKey[m]; } break;
|
||||||
|
case 5: for (m = 0; m < 48; m++) { keys[5][m] = tempKey[m]; } break;
|
||||||
|
case 6: for (m = 0; m < 48; m++) { keys[6][m] = tempKey[m]; } break;
|
||||||
|
case 7: for (m = 0; m < 48; m++) { keys[7][m] = tempKey[m]; } break;
|
||||||
|
case 8: for (m = 0; m < 48; m++) { keys[8][m] = tempKey[m]; } break;
|
||||||
|
case 9: for (m = 0; m < 48; m++) { keys[9][m] = tempKey[m]; } break;
|
||||||
|
case 10: for (m = 0; m < 48; m++) { keys[10][m] = tempKey[m]; } break;
|
||||||
|
case 11: for (m = 0; m < 48; m++) { keys[11][m] = tempKey[m]; } break;
|
||||||
|
case 12: for (m = 0; m < 48; m++) { keys[12][m] = tempKey[m]; } break;
|
||||||
|
case 13: for (m = 0; m < 48; m++) { keys[13][m] = tempKey[m]; } break;
|
||||||
|
case 14: for (m = 0; m < 48; m++) { keys[14][m] = tempKey[m]; } break;
|
||||||
|
case 15: for (m = 0; m < 48; m++) { keys[15][m] = tempKey[m]; } break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return keys;
|
||||||
|
}
|
||||||
|
|
||||||
|
function encryptString(encStr) {
|
||||||
|
return strEnc(encStr, '$QBOSSY$', '$QBOSSY$', '$QBOSSY$')
|
||||||
|
}
|
||||||
|
|
||||||
|
function decryptString(decStr) {
|
||||||
|
return strDec(decStr, '$QBOSSY$', '$QBOSSY$', '$QBOSSY$');
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
encryptString: encryptString,
|
||||||
|
decryptString: decryptString
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,221 @@
|
|||||||
|
//Paul Tero, July 2001
|
||||||
|
//http://www.tero.co.uk/des/
|
||||||
|
//
|
||||||
|
//Optimised for performance with large blocks by Michael Hayworth, November 2001
|
||||||
|
//http://www.netdealing.com
|
||||||
|
//
|
||||||
|
//THIS SOFTWARE IS PROVIDED "AS IS" AND
|
||||||
|
//ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
//IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
//ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||||
|
//FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
//DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
//OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
//HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
//OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
//SUCH DAMAGE.
|
||||||
|
|
||||||
|
//des
|
||||||
|
//this takes the key, the message, and whether to encrypt or decrypt
|
||||||
|
var des = function des(key, message, encrypt, mode, iv, padding) {
|
||||||
|
//declaring this locally speeds things up a bit
|
||||||
|
var spfunction1 = new Array(0x1010400, 0, 0x10000, 0x1010404, 0x1010004, 0x10404, 0x4, 0x10000, 0x400, 0x1010400, 0x1010404, 0x400, 0x1000404, 0x1010004, 0x1000000, 0x4, 0x404, 0x1000400, 0x1000400, 0x10400, 0x10400, 0x1010000, 0x1010000, 0x1000404, 0x10004, 0x1000004, 0x1000004, 0x10004, 0, 0x404, 0x10404, 0x1000000, 0x10000, 0x1010404, 0x4, 0x1010000, 0x1010400, 0x1000000, 0x1000000, 0x400, 0x1010004, 0x10000, 0x10400, 0x1000004, 0x400, 0x4, 0x1000404, 0x10404, 0x1010404, 0x10004, 0x1010000, 0x1000404, 0x1000004, 0x404, 0x10404, 0x1010400, 0x404, 0x1000400, 0x1000400, 0, 0x10004, 0x10400, 0, 0x1010004);
|
||||||
|
var spfunction2 = new Array(-0x7fef7fe0, -0x7fff8000, 0x8000, 0x108020, 0x100000, 0x20, -0x7fefffe0, -0x7fff7fe0, -0x7fffffe0, -0x7fef7fe0, -0x7fef8000, -0x80000000, -0x7fff8000, 0x100000, 0x20, -0x7fefffe0, 0x108000, 0x100020, -0x7fff7fe0, 0, -0x80000000, 0x8000, 0x108020, -0x7ff00000, 0x100020, -0x7fffffe0, 0, 0x108000, 0x8020, -0x7fef8000, -0x7ff00000, 0x8020, 0, 0x108020, -0x7fefffe0, 0x100000, -0x7fff7fe0, -0x7ff00000, -0x7fef8000, 0x8000, -0x7ff00000, -0x7fff8000, 0x20, -0x7fef7fe0, 0x108020, 0x20, 0x8000, -0x80000000, 0x8020, -0x7fef8000, 0x100000, -0x7fffffe0, 0x100020, -0x7fff7fe0, -0x7fffffe0, 0x100020, 0x108000, 0, -0x7fff8000, 0x8020, -0x80000000, -0x7fefffe0, -0x7fef7fe0, 0x108000);
|
||||||
|
var spfunction3 = new Array(0x208, 0x8020200, 0, 0x8020008, 0x8000200, 0, 0x20208, 0x8000200, 0x20008, 0x8000008, 0x8000008, 0x20000, 0x8020208, 0x20008, 0x8020000, 0x208, 0x8000000, 0x8, 0x8020200, 0x200, 0x20200, 0x8020000, 0x8020008, 0x20208, 0x8000208, 0x20200, 0x20000, 0x8000208, 0x8, 0x8020208, 0x200, 0x8000000, 0x8020200, 0x8000000, 0x20008, 0x208, 0x20000, 0x8020200, 0x8000200, 0, 0x200, 0x20008, 0x8020208, 0x8000200, 0x8000008, 0x200, 0, 0x8020008, 0x8000208, 0x20000, 0x8000000, 0x8020208, 0x8, 0x20208, 0x20200, 0x8000008, 0x8020000, 0x8000208, 0x208, 0x8020000, 0x20208, 0x8, 0x8020008, 0x20200);
|
||||||
|
var spfunction4 = new Array(0x802001, 0x2081, 0x2081, 0x80, 0x802080, 0x800081, 0x800001, 0x2001, 0, 0x802000, 0x802000, 0x802081, 0x81, 0, 0x800080, 0x800001, 0x1, 0x2000, 0x800000, 0x802001, 0x80, 0x800000, 0x2001, 0x2080, 0x800081, 0x1, 0x2080, 0x800080, 0x2000, 0x802080, 0x802081, 0x81, 0x800080, 0x800001, 0x802000, 0x802081, 0x81, 0, 0, 0x802000, 0x2080, 0x800080, 0x800081, 0x1, 0x802001, 0x2081, 0x2081, 0x80, 0x802081, 0x81, 0x1, 0x2000, 0x800001, 0x2001, 0x802080, 0x800081, 0x2001, 0x2080, 0x800000, 0x802001, 0x80, 0x800000, 0x2000, 0x802080);
|
||||||
|
var spfunction5 = new Array(0x100, 0x2080100, 0x2080000, 0x42000100, 0x80000, 0x100, 0x40000000, 0x2080000, 0x40080100, 0x80000, 0x2000100, 0x40080100, 0x42000100, 0x42080000, 0x80100, 0x40000000, 0x2000000, 0x40080000, 0x40080000, 0, 0x40000100, 0x42080100, 0x42080100, 0x2000100, 0x42080000, 0x40000100, 0, 0x42000000, 0x2080100, 0x2000000, 0x42000000, 0x80100, 0x80000, 0x42000100, 0x100, 0x2000000, 0x40000000, 0x2080000, 0x42000100, 0x40080100, 0x2000100, 0x40000000, 0x42080000, 0x2080100, 0x40080100, 0x100, 0x2000000, 0x42080000, 0x42080100, 0x80100, 0x42000000, 0x42080100, 0x2080000, 0, 0x40080000, 0x42000000, 0x80100, 0x2000100, 0x40000100, 0x80000, 0, 0x40080000, 0x2080100, 0x40000100);
|
||||||
|
var spfunction6 = new Array(0x20000010, 0x20400000, 0x4000, 0x20404010, 0x20400000, 0x10, 0x20404010, 0x400000, 0x20004000, 0x404010, 0x400000, 0x20000010, 0x400010, 0x20004000, 0x20000000, 0x4010, 0, 0x400010, 0x20004010, 0x4000, 0x404000, 0x20004010, 0x10, 0x20400010, 0x20400010, 0, 0x404010, 0x20404000, 0x4010, 0x404000, 0x20404000, 0x20000000, 0x20004000, 0x10, 0x20400010, 0x404000, 0x20404010, 0x400000, 0x4010, 0x20000010, 0x400000, 0x20004000, 0x20000000, 0x4010, 0x20000010, 0x20404010, 0x404000, 0x20400000, 0x404010, 0x20404000, 0, 0x20400010, 0x10, 0x4000, 0x20400000, 0x404010, 0x4000, 0x400010, 0x20004010, 0, 0x20404000, 0x20000000, 0x400010, 0x20004010);
|
||||||
|
var spfunction7 = new Array(0x200000, 0x4200002, 0x4000802, 0, 0x800, 0x4000802, 0x200802, 0x4200800, 0x4200802, 0x200000, 0, 0x4000002, 0x2, 0x4000000, 0x4200002, 0x802, 0x4000800, 0x200802, 0x200002, 0x4000800, 0x4000002, 0x4200000, 0x4200800, 0x200002, 0x4200000, 0x800, 0x802, 0x4200802, 0x200800, 0x2, 0x4000000, 0x200800, 0x4000000, 0x200800, 0x200000, 0x4000802, 0x4000802, 0x4200002, 0x4200002, 0x2, 0x200002, 0x4000000, 0x4000800, 0x200000, 0x4200800, 0x802, 0x200802, 0x4200800, 0x802, 0x4000002, 0x4200802, 0x4200000, 0x200800, 0, 0x2, 0x4200802, 0, 0x200802, 0x4200000, 0x800, 0x4000002, 0x4000800, 0x800, 0x200002);
|
||||||
|
var spfunction8 = new Array(0x10001040, 0x1000, 0x40000, 0x10041040, 0x10000000, 0x10001040, 0x40, 0x10000000, 0x40040, 0x10040000, 0x10041040, 0x41000, 0x10041000, 0x41040, 0x1000, 0x40, 0x10040000, 0x10000040, 0x10001000, 0x1040, 0x41000, 0x40040, 0x10040040, 0x10041000, 0x1040, 0, 0, 0x10040040, 0x10000040, 0x10001000, 0x41040, 0x40000, 0x41040, 0x40000, 0x10041000, 0x1000, 0x40, 0x10040040, 0x1000, 0x41040, 0x10001000, 0x40, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x40000, 0x10001040, 0, 0x10041040, 0x40040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0, 0x10041040, 0x41000, 0x41000, 0x1040, 0x1040, 0x40040, 0x10000000, 0x10041000);
|
||||||
|
|
||||||
|
//create the 16 or 48 subkeys we will need
|
||||||
|
var keys = des_createKeys(key);
|
||||||
|
var m = 0, i, j, temp, temp2, right1, right2, left, right, looping;
|
||||||
|
var cbcleft, cbcleft2, cbcright, cbcright2
|
||||||
|
var endloop, loopinc;
|
||||||
|
var len = message.length;
|
||||||
|
var chunk = 0;
|
||||||
|
//set up the loops for single and triple des
|
||||||
|
var iterations = keys.length == 32 ? 3 : 9; //single or triple des
|
||||||
|
if (iterations == 3) { looping = encrypt ? new Array(0, 32, 2) : new Array(30, -2, -2); }
|
||||||
|
else { looping = encrypt ? new Array(0, 32, 2, 62, 30, -2, 64, 96, 2) : new Array(94, 62, -2, 32, 64, 2, 30, -2, -2); }
|
||||||
|
|
||||||
|
//pad the message depending on the padding parameter
|
||||||
|
if (padding == 2) message += " "; //pad the message with spaces
|
||||||
|
else if (padding == 1) { temp = 8 - (len % 8); message += String.fromCharCode(temp, temp, temp, temp, temp, temp, temp, temp); if (temp == 8) len += 8; } //PKCS7 padding
|
||||||
|
else if (!padding) message += "\0\0\0\0\0\0\0\0"; //pad the message out with null bytes
|
||||||
|
|
||||||
|
//store the result here
|
||||||
|
var result = "";
|
||||||
|
var tempresult = "";
|
||||||
|
|
||||||
|
if (mode == 1) { //CBC mode
|
||||||
|
cbcleft = (iv.charCodeAt(m++) << 24) | (iv.charCodeAt(m++) << 16) | (iv.charCodeAt(m++) << 8) | iv.charCodeAt(m++);
|
||||||
|
cbcright = (iv.charCodeAt(m++) << 24) | (iv.charCodeAt(m++) << 16) | (iv.charCodeAt(m++) << 8) | iv.charCodeAt(m++);
|
||||||
|
m = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//loop through each 64 bit chunk of the message
|
||||||
|
while (m < len) {
|
||||||
|
left = (message.charCodeAt(m++) << 24) | (message.charCodeAt(m++) << 16) | (message.charCodeAt(m++) << 8) | message.charCodeAt(m++);
|
||||||
|
right = (message.charCodeAt(m++) << 24) | (message.charCodeAt(m++) << 16) | (message.charCodeAt(m++) << 8) | message.charCodeAt(m++);
|
||||||
|
|
||||||
|
//for Cipher Block Chaining mode, xor the message with the previous result
|
||||||
|
if (mode == 1) { if (encrypt) { left ^= cbcleft; right ^= cbcright; } else { cbcleft2 = cbcleft; cbcright2 = cbcright; cbcleft = left; cbcright = right; } }
|
||||||
|
|
||||||
|
//first each 64 but chunk of the message must be permuted according to IP
|
||||||
|
temp = ((left >>> 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4);
|
||||||
|
temp = ((left >>> 16) ^ right) & 0x0000ffff; right ^= temp; left ^= (temp << 16);
|
||||||
|
temp = ((right >>> 2) ^ left) & 0x33333333; left ^= temp; right ^= (temp << 2);
|
||||||
|
temp = ((right >>> 8) ^ left) & 0x00ff00ff; left ^= temp; right ^= (temp << 8);
|
||||||
|
temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1);
|
||||||
|
|
||||||
|
left = ((left << 1) | (left >>> 31));
|
||||||
|
right = ((right << 1) | (right >>> 31));
|
||||||
|
|
||||||
|
//do this either 1 or 3 times for each chunk of the message
|
||||||
|
for (j = 0; j < iterations; j += 3) {
|
||||||
|
endloop = looping[j + 1];
|
||||||
|
loopinc = looping[j + 2];
|
||||||
|
//now go through and perform the encryption or decryption
|
||||||
|
for (i = looping[j]; i != endloop; i += loopinc) { //for efficiency
|
||||||
|
right1 = right ^ keys[i];
|
||||||
|
right2 = ((right >>> 4) | (right << 28)) ^ keys[i + 1];
|
||||||
|
//the result is attained by passing these bytes through the S selection functions
|
||||||
|
temp = left;
|
||||||
|
left = right;
|
||||||
|
right = temp ^ (spfunction2[(right1 >>> 24) & 0x3f] | spfunction4[(right1 >>> 16) & 0x3f]
|
||||||
|
| spfunction6[(right1 >>> 8) & 0x3f] | spfunction8[right1 & 0x3f]
|
||||||
|
| spfunction1[(right2 >>> 24) & 0x3f] | spfunction3[(right2 >>> 16) & 0x3f]
|
||||||
|
| spfunction5[(right2 >>> 8) & 0x3f] | spfunction7[right2 & 0x3f]);
|
||||||
|
}
|
||||||
|
temp = left; left = right; right = temp; //unreverse left and right
|
||||||
|
} //for either 1 or 3 iterations
|
||||||
|
|
||||||
|
//move then each one bit to the right
|
||||||
|
left = ((left >>> 1) | (left << 31));
|
||||||
|
right = ((right >>> 1) | (right << 31));
|
||||||
|
|
||||||
|
//now perform IP-1, which is IP in the opposite direction
|
||||||
|
temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1);
|
||||||
|
temp = ((right >>> 8) ^ left) & 0x00ff00ff; left ^= temp; right ^= (temp << 8);
|
||||||
|
temp = ((right >>> 2) ^ left) & 0x33333333; left ^= temp; right ^= (temp << 2);
|
||||||
|
temp = ((left >>> 16) ^ right) & 0x0000ffff; right ^= temp; left ^= (temp << 16);
|
||||||
|
temp = ((left >>> 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4);
|
||||||
|
|
||||||
|
//for Cipher Block Chaining mode, xor the message with the previous result
|
||||||
|
if (mode == 1) { if (encrypt) { cbcleft = left; cbcright = right; } else { left ^= cbcleft2; right ^= cbcright2; } }
|
||||||
|
tempresult += String.fromCharCode((left >>> 24), ((left >>> 16) & 0xff), ((left >>> 8) & 0xff), (left & 0xff), (right >>> 24), ((right >>> 16) & 0xff), ((right >>> 8) & 0xff), (right & 0xff));
|
||||||
|
|
||||||
|
chunk += 8;
|
||||||
|
if (chunk == 512) { result += tempresult; tempresult = ""; chunk = 0; }
|
||||||
|
} //for every 8 characters, or 64 bits in the message
|
||||||
|
|
||||||
|
//return the result as an array
|
||||||
|
return result + tempresult;
|
||||||
|
} //end of des
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//des_createKeys
|
||||||
|
//this takes as input a 64 bit key (even though only 56 bits are used)
|
||||||
|
//as an array of 2 integers, and returns 16 48 bit keys
|
||||||
|
function des_createKeys(key) {
|
||||||
|
//declaring this locally speeds things up a bit
|
||||||
|
var pc2bytes0 = new Array(0, 0x4, 0x20000000, 0x20000004, 0x10000, 0x10004, 0x20010000, 0x20010004, 0x200, 0x204, 0x20000200, 0x20000204, 0x10200, 0x10204, 0x20010200, 0x20010204);
|
||||||
|
var pc2bytes1 = new Array(0, 0x1, 0x100000, 0x100001, 0x4000000, 0x4000001, 0x4100000, 0x4100001, 0x100, 0x101, 0x100100, 0x100101, 0x4000100, 0x4000101, 0x4100100, 0x4100101);
|
||||||
|
var pc2bytes2 = new Array(0, 0x8, 0x800, 0x808, 0x1000000, 0x1000008, 0x1000800, 0x1000808, 0, 0x8, 0x800, 0x808, 0x1000000, 0x1000008, 0x1000800, 0x1000808);
|
||||||
|
var pc2bytes3 = new Array(0, 0x200000, 0x8000000, 0x8200000, 0x2000, 0x202000, 0x8002000, 0x8202000, 0x20000, 0x220000, 0x8020000, 0x8220000, 0x22000, 0x222000, 0x8022000, 0x8222000);
|
||||||
|
var pc2bytes4 = new Array(0, 0x40000, 0x10, 0x40010, 0, 0x40000, 0x10, 0x40010, 0x1000, 0x41000, 0x1010, 0x41010, 0x1000, 0x41000, 0x1010, 0x41010);
|
||||||
|
var pc2bytes5 = new Array(0, 0x400, 0x20, 0x420, 0, 0x400, 0x20, 0x420, 0x2000000, 0x2000400, 0x2000020, 0x2000420, 0x2000000, 0x2000400, 0x2000020, 0x2000420);
|
||||||
|
var pc2bytes6 = new Array(0, 0x10000000, 0x80000, 0x10080000, 0x2, 0x10000002, 0x80002, 0x10080002, 0, 0x10000000, 0x80000, 0x10080000, 0x2, 0x10000002, 0x80002, 0x10080002);
|
||||||
|
var pc2bytes7 = new Array(0, 0x10000, 0x800, 0x10800, 0x20000000, 0x20010000, 0x20000800, 0x20010800, 0x20000, 0x30000, 0x20800, 0x30800, 0x20020000, 0x20030000, 0x20020800, 0x20030800);
|
||||||
|
var pc2bytes8 = new Array(0, 0x40000, 0, 0x40000, 0x2, 0x40002, 0x2, 0x40002, 0x2000000, 0x2040000, 0x2000000, 0x2040000, 0x2000002, 0x2040002, 0x2000002, 0x2040002);
|
||||||
|
var pc2bytes9 = new Array(0, 0x10000000, 0x8, 0x10000008, 0, 0x10000000, 0x8, 0x10000008, 0x400, 0x10000400, 0x408, 0x10000408, 0x400, 0x10000400, 0x408, 0x10000408);
|
||||||
|
var pc2bytes10 = new Array(0, 0x20, 0, 0x20, 0x100000, 0x100020, 0x100000, 0x100020, 0x2000, 0x2020, 0x2000, 0x2020, 0x102000, 0x102020, 0x102000, 0x102020);
|
||||||
|
var pc2bytes11 = new Array(0, 0x1000000, 0x200, 0x1000200, 0x200000, 0x1200000, 0x200200, 0x1200200, 0x4000000, 0x5000000, 0x4000200, 0x5000200, 0x4200000, 0x5200000, 0x4200200, 0x5200200);
|
||||||
|
var pc2bytes12 = new Array(0, 0x1000, 0x8000000, 0x8001000, 0x80000, 0x81000, 0x8080000, 0x8081000, 0x10, 0x1010, 0x8000010, 0x8001010, 0x80010, 0x81010, 0x8080010, 0x8081010);
|
||||||
|
var pc2bytes13 = new Array(0, 0x4, 0x100, 0x104, 0, 0x4, 0x100, 0x104, 0x1, 0x5, 0x101, 0x105, 0x1, 0x5, 0x101, 0x105);
|
||||||
|
|
||||||
|
//how many iterations (1 for des, 3 for triple des)
|
||||||
|
var iterations = key.length > 8 ? 3 : 1; //changed by Paul 16/6/2007 to use Triple DES for 9+ byte keys
|
||||||
|
//stores the return keys
|
||||||
|
var keys = new Array(32 * iterations);
|
||||||
|
//now define the left shifts which need to be done
|
||||||
|
var shifts = new Array(0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0);
|
||||||
|
//other variables
|
||||||
|
var lefttemp, righttemp, m = 0, n = 0, temp;
|
||||||
|
|
||||||
|
for (var j = 0; j < iterations; j++) { //either 1 or 3 iterations
|
||||||
|
var left = (key.charCodeAt(m++) << 24) | (key.charCodeAt(m++) << 16) | (key.charCodeAt(m++) << 8) | key.charCodeAt(m++);
|
||||||
|
var right = (key.charCodeAt(m++) << 24) | (key.charCodeAt(m++) << 16) | (key.charCodeAt(m++) << 8) | key.charCodeAt(m++);
|
||||||
|
|
||||||
|
var temp = ((left >>> 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4);
|
||||||
|
temp = ((right >>> -16) ^ left) & 0x0000ffff; left ^= temp; right ^= (temp << -16);
|
||||||
|
temp = ((left >>> 2) ^ right) & 0x33333333; right ^= temp; left ^= (temp << 2);
|
||||||
|
temp = ((right >>> -16) ^ left) & 0x0000ffff; left ^= temp; right ^= (temp << -16);
|
||||||
|
temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1);
|
||||||
|
temp = ((right >>> 8) ^ left) & 0x00ff00ff; left ^= temp; right ^= (temp << 8);
|
||||||
|
temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1);
|
||||||
|
|
||||||
|
//the right side needs to be shifted and to get the last four bits of the left side
|
||||||
|
temp = (left << 8) | ((right >>> 20) & 0x000000f0);
|
||||||
|
//left needs to be put upside down
|
||||||
|
left = (right << 24) | ((right << 8) & 0xff0000) | ((right >>> 8) & 0xff00) | ((right >>> 24) & 0xf0);
|
||||||
|
right = temp;
|
||||||
|
|
||||||
|
//now go through and perform these shifts on the left and right keys
|
||||||
|
for (var i = 0; i < shifts.length; i++) {
|
||||||
|
//shift the keys either one or two bits to the left
|
||||||
|
if (shifts[i]) { left = (left << 2) | (left >>> 26); right = (right << 2) | (right >>> 26); }
|
||||||
|
else { left = (left << 1) | (left >>> 27); right = (right << 1) | (right >>> 27); }
|
||||||
|
left &= -0xf; right &= -0xf;
|
||||||
|
|
||||||
|
//now apply PC-2, in such a way that E is easier when encrypting or decrypting
|
||||||
|
//this conversion will look like PC-2 except only the last 6 bits of each byte are used
|
||||||
|
//rather than 48 consecutive bits and the order of lines will be according to
|
||||||
|
//how the S selection functions will be applied: S2, S4, S6, S8, S1, S3, S5, S7
|
||||||
|
lefttemp = pc2bytes0[left >>> 28] | pc2bytes1[(left >>> 24) & 0xf]
|
||||||
|
| pc2bytes2[(left >>> 20) & 0xf] | pc2bytes3[(left >>> 16) & 0xf]
|
||||||
|
| pc2bytes4[(left >>> 12) & 0xf] | pc2bytes5[(left >>> 8) & 0xf]
|
||||||
|
| pc2bytes6[(left >>> 4) & 0xf];
|
||||||
|
righttemp = pc2bytes7[right >>> 28] | pc2bytes8[(right >>> 24) & 0xf]
|
||||||
|
| pc2bytes9[(right >>> 20) & 0xf] | pc2bytes10[(right >>> 16) & 0xf]
|
||||||
|
| pc2bytes11[(right >>> 12) & 0xf] | pc2bytes12[(right >>> 8) & 0xf]
|
||||||
|
| pc2bytes13[(right >>> 4) & 0xf];
|
||||||
|
temp = ((righttemp >>> 16) ^ lefttemp) & 0x0000ffff;
|
||||||
|
keys[n++] = lefttemp ^ temp; keys[n++] = righttemp ^ (temp << 16);
|
||||||
|
}
|
||||||
|
} //for each iterations
|
||||||
|
//return the keys we've created
|
||||||
|
return keys;
|
||||||
|
} //end of des_createKeys
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////
|
||||||
|
function stringToHexForDES(inputS) {
|
||||||
|
var s = des('$AZKOSS$', suffix8Blank(inputS), 1, 0);
|
||||||
|
var r = "";
|
||||||
|
var hexes = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f");
|
||||||
|
for (var i = 0; i < s.length; i++) { r += hexes[s.charCodeAt(i) >> 4] + hexes[s.charCodeAt(i) & 0xf]; }
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
function hexToStringForDES(h) {
|
||||||
|
var r = "";
|
||||||
|
for (var i = (h.substr(0, 2) == "0x") ? 2 : 0; i < h.length; i += 2) { r += String.fromCharCode(parseInt(h.substr(i, 2), 16)); }
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
function suffix8Blank(str) {
|
||||||
|
for (var i = 0; i < 8; i++) {
|
||||||
|
str += " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.stringToHexForDES = stringToHexForDES;
|
||||||
|
module.exports.hexToStringForDES = hexToStringForDES;
|
@ -0,0 +1,30 @@
|
|||||||
|
var log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
debug() {
|
||||||
|
if (!log) return
|
||||||
|
log.debug.apply(log, arguments)
|
||||||
|
},
|
||||||
|
info() {
|
||||||
|
if (!log) return
|
||||||
|
log.info.apply(log, arguments)
|
||||||
|
},
|
||||||
|
warn() {
|
||||||
|
if (!log) return
|
||||||
|
log.warn.apply(log, arguments)
|
||||||
|
},
|
||||||
|
error() {
|
||||||
|
if (!log) return
|
||||||
|
log.error.apply(log, arguments)
|
||||||
|
},
|
||||||
|
setFilterMsg(msg) { // 从基础库2.7.3开始支持
|
||||||
|
if (!log || !log.setFilterMsg) return
|
||||||
|
if (typeof msg !== 'string') return
|
||||||
|
log.setFilterMsg(msg)
|
||||||
|
},
|
||||||
|
addFilterMsg(msg) { // 从基础库2.8.1开始支持
|
||||||
|
if (!log || !log.addFilterMsg) return
|
||||||
|
if (typeof msg !== 'string') return
|
||||||
|
log.addFilterMsg(msg)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,214 @@
|
|||||||
|
var rotateLeft = function (lValue, iShiftBits) {
|
||||||
|
return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
|
||||||
|
}
|
||||||
|
|
||||||
|
var addUnsigned = function (lX, lY) {
|
||||||
|
var lX4, lY4, lX8, lY8, lResult;
|
||||||
|
lX8 = (lX & 0x80000000);
|
||||||
|
lY8 = (lY & 0x80000000);
|
||||||
|
lX4 = (lX & 0x40000000);
|
||||||
|
lY4 = (lY & 0x40000000);
|
||||||
|
lResult = (lX & 0x3FFFFFFF) + (lY & 0x3FFFFFFF);
|
||||||
|
if (lX4 & lY4) return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
|
||||||
|
if (lX4 | lY4) {
|
||||||
|
if (lResult & 0x40000000) return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
|
||||||
|
else return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
|
||||||
|
} else {
|
||||||
|
return (lResult ^ lX8 ^ lY8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var F = function (x, y, z) {
|
||||||
|
return (x & y) | ((~x) & z);
|
||||||
|
}
|
||||||
|
|
||||||
|
var G = function (x, y, z) {
|
||||||
|
return (x & z) | (y & (~z));
|
||||||
|
}
|
||||||
|
|
||||||
|
var H = function (x, y, z) {
|
||||||
|
return (x ^ y ^ z);
|
||||||
|
}
|
||||||
|
|
||||||
|
var I = function (x, y, z) {
|
||||||
|
return (y ^ (x | (~z)));
|
||||||
|
}
|
||||||
|
|
||||||
|
var FF = function (a, b, c, d, x, s, ac) {
|
||||||
|
a = addUnsigned(a, addUnsigned(addUnsigned(F(b, c, d), x), ac));
|
||||||
|
return addUnsigned(rotateLeft(a, s), b);
|
||||||
|
};
|
||||||
|
|
||||||
|
var GG = function (a, b, c, d, x, s, ac) {
|
||||||
|
a = addUnsigned(a, addUnsigned(addUnsigned(G(b, c, d), x), ac));
|
||||||
|
return addUnsigned(rotateLeft(a, s), b);
|
||||||
|
};
|
||||||
|
|
||||||
|
var HH = function (a, b, c, d, x, s, ac) {
|
||||||
|
a = addUnsigned(a, addUnsigned(addUnsigned(H(b, c, d), x), ac));
|
||||||
|
return addUnsigned(rotateLeft(a, s), b);
|
||||||
|
};
|
||||||
|
|
||||||
|
var II = function (a, b, c, d, x, s, ac) {
|
||||||
|
a = addUnsigned(a, addUnsigned(addUnsigned(I(b, c, d), x), ac));
|
||||||
|
return addUnsigned(rotateLeft(a, s), b);
|
||||||
|
};
|
||||||
|
|
||||||
|
var convertToWordArray = function (string) {
|
||||||
|
var lWordCount;
|
||||||
|
var lMessageLength = string.length;
|
||||||
|
var lNumberOfWordsTempOne = lMessageLength + 8;
|
||||||
|
var lNumberOfWordsTempTwo = (lNumberOfWordsTempOne - (lNumberOfWordsTempOne % 64)) / 64;
|
||||||
|
var lNumberOfWords = (lNumberOfWordsTempTwo + 1) * 16;
|
||||||
|
var lWordArray = Array(lNumberOfWords - 1);
|
||||||
|
var lBytePosition = 0;
|
||||||
|
var lByteCount = 0;
|
||||||
|
while (lByteCount < lMessageLength) {
|
||||||
|
lWordCount = (lByteCount - (lByteCount % 4)) / 4;
|
||||||
|
lBytePosition = (lByteCount % 4) * 8;
|
||||||
|
lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount) << lBytePosition));
|
||||||
|
lByteCount++;
|
||||||
|
}
|
||||||
|
lWordCount = (lByteCount - (lByteCount % 4)) / 4;
|
||||||
|
lBytePosition = (lByteCount % 4) * 8;
|
||||||
|
lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80 << lBytePosition);
|
||||||
|
lWordArray[lNumberOfWords - 2] = lMessageLength << 3;
|
||||||
|
lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29;
|
||||||
|
return lWordArray;
|
||||||
|
};
|
||||||
|
|
||||||
|
var wordToHex = function (lValue) {
|
||||||
|
var WordToHexValue = "",
|
||||||
|
WordToHexValueTemp = "",
|
||||||
|
lByte, lCount;
|
||||||
|
for (lCount = 0; lCount <= 3; lCount++) {
|
||||||
|
lByte = (lValue >>> (lCount * 8)) & 255;
|
||||||
|
WordToHexValueTemp = "0" + lByte.toString(16);
|
||||||
|
WordToHexValue = WordToHexValue + WordToHexValueTemp.substr(WordToHexValueTemp.length - 2, 2);
|
||||||
|
}
|
||||||
|
return WordToHexValue;
|
||||||
|
};
|
||||||
|
|
||||||
|
var uTF8Encode = function (string) {
|
||||||
|
string = string.replace(/\x0d\x0a/g, "\x0a");
|
||||||
|
var output = "";
|
||||||
|
for (var n = 0; n < string.length; n++) {
|
||||||
|
var c = string.charCodeAt(n);
|
||||||
|
if (c < 128) {
|
||||||
|
output += String.fromCharCode(c);
|
||||||
|
} else if ((c > 127) && (c < 2048)) {
|
||||||
|
output += String.fromCharCode((c >> 6) | 192);
|
||||||
|
output += String.fromCharCode((c & 63) | 128);
|
||||||
|
} else {
|
||||||
|
output += String.fromCharCode((c >> 12) | 224);
|
||||||
|
output += String.fromCharCode(((c >> 6) & 63) | 128);
|
||||||
|
output += String.fromCharCode((c & 63) | 128);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
};
|
||||||
|
|
||||||
|
function md5(string) {
|
||||||
|
var x = Array();
|
||||||
|
var k, AA, BB, CC, DD, a, b, c, d;
|
||||||
|
var S11 = 7,
|
||||||
|
S12 = 12,
|
||||||
|
S13 = 17,
|
||||||
|
S14 = 22;
|
||||||
|
var S21 = 5,
|
||||||
|
S22 = 9,
|
||||||
|
S23 = 14,
|
||||||
|
S24 = 20;
|
||||||
|
var S31 = 4,
|
||||||
|
S32 = 11,
|
||||||
|
S33 = 16,
|
||||||
|
S34 = 23;
|
||||||
|
var S41 = 6,
|
||||||
|
S42 = 10,
|
||||||
|
S43 = 15,
|
||||||
|
S44 = 21;
|
||||||
|
string = uTF8Encode(string);
|
||||||
|
x = convertToWordArray(string);
|
||||||
|
a = 0x67452301;
|
||||||
|
b = 0xEFCDAB89;
|
||||||
|
c = 0x98BADCFE;
|
||||||
|
d = 0x10325476;
|
||||||
|
for (k = 0; k < x.length; k += 16) {
|
||||||
|
AA = a;
|
||||||
|
BB = b;
|
||||||
|
CC = c;
|
||||||
|
DD = d;
|
||||||
|
a = FF(a, b, c, d, x[k + 0], S11, 0xD76AA478);
|
||||||
|
d = FF(d, a, b, c, x[k + 1], S12, 0xE8C7B756);
|
||||||
|
c = FF(c, d, a, b, x[k + 2], S13, 0x242070DB);
|
||||||
|
b = FF(b, c, d, a, x[k + 3], S14, 0xC1BDCEEE);
|
||||||
|
a = FF(a, b, c, d, x[k + 4], S11, 0xF57C0FAF);
|
||||||
|
d = FF(d, a, b, c, x[k + 5], S12, 0x4787C62A);
|
||||||
|
c = FF(c, d, a, b, x[k + 6], S13, 0xA8304613);
|
||||||
|
b = FF(b, c, d, a, x[k + 7], S14, 0xFD469501);
|
||||||
|
a = FF(a, b, c, d, x[k + 8], S11, 0x698098D8);
|
||||||
|
d = FF(d, a, b, c, x[k + 9], S12, 0x8B44F7AF);
|
||||||
|
c = FF(c, d, a, b, x[k + 10], S13, 0xFFFF5BB1);
|
||||||
|
b = FF(b, c, d, a, x[k + 11], S14, 0x895CD7BE);
|
||||||
|
a = FF(a, b, c, d, x[k + 12], S11, 0x6B901122);
|
||||||
|
d = FF(d, a, b, c, x[k + 13], S12, 0xFD987193);
|
||||||
|
c = FF(c, d, a, b, x[k + 14], S13, 0xA679438E);
|
||||||
|
b = FF(b, c, d, a, x[k + 15], S14, 0x49B40821);
|
||||||
|
a = GG(a, b, c, d, x[k + 1], S21, 0xF61E2562);
|
||||||
|
d = GG(d, a, b, c, x[k + 6], S22, 0xC040B340);
|
||||||
|
c = GG(c, d, a, b, x[k + 11], S23, 0x265E5A51);
|
||||||
|
b = GG(b, c, d, a, x[k + 0], S24, 0xE9B6C7AA);
|
||||||
|
a = GG(a, b, c, d, x[k + 5], S21, 0xD62F105D);
|
||||||
|
d = GG(d, a, b, c, x[k + 10], S22, 0x2441453);
|
||||||
|
c = GG(c, d, a, b, x[k + 15], S23, 0xD8A1E681);
|
||||||
|
b = GG(b, c, d, a, x[k + 4], S24, 0xE7D3FBC8);
|
||||||
|
a = GG(a, b, c, d, x[k + 9], S21, 0x21E1CDE6);
|
||||||
|
d = GG(d, a, b, c, x[k + 14], S22, 0xC33707D6);
|
||||||
|
c = GG(c, d, a, b, x[k + 3], S23, 0xF4D50D87);
|
||||||
|
b = GG(b, c, d, a, x[k + 8], S24, 0x455A14ED);
|
||||||
|
a = GG(a, b, c, d, x[k + 13], S21, 0xA9E3E905);
|
||||||
|
d = GG(d, a, b, c, x[k + 2], S22, 0xFCEFA3F8);
|
||||||
|
c = GG(c, d, a, b, x[k + 7], S23, 0x676F02D9);
|
||||||
|
b = GG(b, c, d, a, x[k + 12], S24, 0x8D2A4C8A);
|
||||||
|
a = HH(a, b, c, d, x[k + 5], S31, 0xFFFA3942);
|
||||||
|
d = HH(d, a, b, c, x[k + 8], S32, 0x8771F681);
|
||||||
|
c = HH(c, d, a, b, x[k + 11], S33, 0x6D9D6122);
|
||||||
|
b = HH(b, c, d, a, x[k + 14], S34, 0xFDE5380C);
|
||||||
|
a = HH(a, b, c, d, x[k + 1], S31, 0xA4BEEA44);
|
||||||
|
d = HH(d, a, b, c, x[k + 4], S32, 0x4BDECFA9);
|
||||||
|
c = HH(c, d, a, b, x[k + 7], S33, 0xF6BB4B60);
|
||||||
|
b = HH(b, c, d, a, x[k + 10], S34, 0xBEBFBC70);
|
||||||
|
a = HH(a, b, c, d, x[k + 13], S31, 0x289B7EC6);
|
||||||
|
d = HH(d, a, b, c, x[k + 0], S32, 0xEAA127FA);
|
||||||
|
c = HH(c, d, a, b, x[k + 3], S33, 0xD4EF3085);
|
||||||
|
b = HH(b, c, d, a, x[k + 6], S34, 0x4881D05);
|
||||||
|
a = HH(a, b, c, d, x[k + 9], S31, 0xD9D4D039);
|
||||||
|
d = HH(d, a, b, c, x[k + 12], S32, 0xE6DB99E5);
|
||||||
|
c = HH(c, d, a, b, x[k + 15], S33, 0x1FA27CF8);
|
||||||
|
b = HH(b, c, d, a, x[k + 2], S34, 0xC4AC5665);
|
||||||
|
a = II(a, b, c, d, x[k + 0], S41, 0xF4292244);
|
||||||
|
d = II(d, a, b, c, x[k + 7], S42, 0x432AFF97);
|
||||||
|
c = II(c, d, a, b, x[k + 14], S43, 0xAB9423A7);
|
||||||
|
b = II(b, c, d, a, x[k + 5], S44, 0xFC93A039);
|
||||||
|
a = II(a, b, c, d, x[k + 12], S41, 0x655B59C3);
|
||||||
|
d = II(d, a, b, c, x[k + 3], S42, 0x8F0CCC92);
|
||||||
|
c = II(c, d, a, b, x[k + 10], S43, 0xFFEFF47D);
|
||||||
|
b = II(b, c, d, a, x[k + 1], S44, 0x85845DD1);
|
||||||
|
a = II(a, b, c, d, x[k + 8], S41, 0x6FA87E4F);
|
||||||
|
d = II(d, a, b, c, x[k + 15], S42, 0xFE2CE6E0);
|
||||||
|
c = II(c, d, a, b, x[k + 6], S43, 0xA3014314);
|
||||||
|
b = II(b, c, d, a, x[k + 13], S44, 0x4E0811A1);
|
||||||
|
a = II(a, b, c, d, x[k + 4], S41, 0xF7537E82);
|
||||||
|
d = II(d, a, b, c, x[k + 11], S42, 0xBD3AF235);
|
||||||
|
c = II(c, d, a, b, x[k + 2], S43, 0x2AD7D2BB);
|
||||||
|
b = II(b, c, d, a, x[k + 9], S44, 0xEB86D391);
|
||||||
|
a = addUnsigned(a, AA);
|
||||||
|
b = addUnsigned(b, BB);
|
||||||
|
c = addUnsigned(c, CC);
|
||||||
|
d = addUnsigned(d, DD);
|
||||||
|
}
|
||||||
|
var tempValue = wordToHex(a) + wordToHex(b) + wordToHex(c) + wordToHex(d);
|
||||||
|
return tempValue.toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.md5 = md5
|
@ -0,0 +1,38 @@
|
|||||||
|
var app = getApp();
|
||||||
|
var mqUtils = require('mqttSubstribe.js');
|
||||||
|
var timerHolder = null;
|
||||||
|
var startMonitor = function(currentPage, type) {
|
||||||
|
var that = this;
|
||||||
|
var time = 35000;
|
||||||
|
app.page = currentPage;
|
||||||
|
// 获取c li
|
||||||
|
var client = app.client;
|
||||||
|
if (client && type == 0) { //
|
||||||
|
console.log("重新订阅主题");
|
||||||
|
mqUtils.doSubscribe();
|
||||||
|
}
|
||||||
|
// 线连接一次
|
||||||
|
// mqUtils.doConnect(page);
|
||||||
|
if (!timerHolder) {
|
||||||
|
timerHolder = setInterval(function() {
|
||||||
|
var client = app.client;
|
||||||
|
if (!client || !client.isConnected()) {
|
||||||
|
console.log(client);
|
||||||
|
console.log("mqtt----------检测连接")
|
||||||
|
mqUtils.doConnect();
|
||||||
|
} else {
|
||||||
|
console.log(" mqttMonitor 已连接....");
|
||||||
|
}
|
||||||
|
}, 1500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 取消订阅主题
|
||||||
|
var startCancel = function() {
|
||||||
|
mqUtils.unsubscribe();
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
startMonitor: startMonitor,
|
||||||
|
startCancel: startCancel,
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,141 @@
|
|||||||
|
var app = getApp();
|
||||||
|
const Paho = require('./paho-mqtt.js');
|
||||||
|
|
||||||
|
var mqttApi = {
|
||||||
|
/*---- 获取连接 ----*/
|
||||||
|
connect: function (cb) {
|
||||||
|
var that = this;
|
||||||
|
var client = app.mqttParam.client;
|
||||||
|
if (client && client.isConnected()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var userInfo = wx.getStorageSync("userInfo");
|
||||||
|
var randomString = "";
|
||||||
|
for (var i = 1; i <= 5; i++) {
|
||||||
|
randomString += parseInt(Math.random() * 10);
|
||||||
|
}
|
||||||
|
var clientId = "swxclientId-member-" + app.globalData.tenantId + "-" + app.globalData.shopNo + "-" + app.globalData.posNo + randomString;
|
||||||
|
client = new Paho.Client(app.mqttParam.wsServer, 0, clientId);
|
||||||
|
client.connect({
|
||||||
|
useSSL: false,
|
||||||
|
cleanSession: false,
|
||||||
|
keepAliveInterval: 2,
|
||||||
|
onSuccess: function () {
|
||||||
|
app.mqttParam.client = client;
|
||||||
|
client.onMessageArrived = function (msg) {
|
||||||
|
console.log("----------订阅消息", msg);
|
||||||
|
var title = msg.destinationName;
|
||||||
|
if (title.indexOf('pay') > -1) {
|
||||||
|
if (typeof app.globalFunction.scanPayCodeCallBack === 'function') {
|
||||||
|
app.globalFunction.scanPayCodeCallBack(msg.payloadString);
|
||||||
|
}
|
||||||
|
} else if (title.indexOf('couponconsume') > -1) {
|
||||||
|
app.globalFunction.consumeCouponCallBack(msg.payloadString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
client.onConnectionLost = function (responseObject) {
|
||||||
|
if (typeof app.globalFunction.onConnectionLost === 'function') {
|
||||||
|
return app.globalData.onConnectionLost(responseObject)
|
||||||
|
}
|
||||||
|
if (responseObject.errorCode !== 0) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof cb === 'function') {
|
||||||
|
cb();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
/*----订阅 ----*/
|
||||||
|
subscribe: function (filter, subscribeOptions) {
|
||||||
|
var that = this;
|
||||||
|
var client = app.mqttParam.client;
|
||||||
|
if (client && client.isConnected()) {
|
||||||
|
client.subscribe(filter, {
|
||||||
|
qos: 2
|
||||||
|
});
|
||||||
|
console.log("mqtt订阅成功")
|
||||||
|
} else {
|
||||||
|
that.connect(function () {
|
||||||
|
client = app.mqttParam.client;
|
||||||
|
if (client && client.isConnected()) {
|
||||||
|
client.subscribe(filter, {
|
||||||
|
qos: 2
|
||||||
|
});
|
||||||
|
console.log("mqtt订阅成功")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/*----废弃订阅 ----*/
|
||||||
|
unsubscribe: function (filter, subscribeOptions) {
|
||||||
|
var client = app.mqttParam.client;
|
||||||
|
if (client && client.isConnected()) {
|
||||||
|
client.unsubscribe(filter);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/*----发布消息----*/
|
||||||
|
publish: function (topic, message, qos = 0, retained = false) {
|
||||||
|
// 发布
|
||||||
|
var that = this;
|
||||||
|
var client = app.mqttParam.client;
|
||||||
|
if (client && client.isConnected()) {
|
||||||
|
var message = new Paho.Message(message);
|
||||||
|
message.destinationName = topic;
|
||||||
|
message.qos = qos;
|
||||||
|
message.retained = retained;
|
||||||
|
return client.send(message);
|
||||||
|
} else {
|
||||||
|
that.connect(function () {
|
||||||
|
client = app.mqttParam.client;
|
||||||
|
if (client && client.isConnected()) {
|
||||||
|
var message = new Paho.Message(message);
|
||||||
|
message.destinationName = topic;
|
||||||
|
message.qos = qos;
|
||||||
|
message.retained = retained;
|
||||||
|
return client.send(message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
wx.showToast({
|
||||||
|
title: '发送失败',
|
||||||
|
icon: 'success',
|
||||||
|
duration: 2000
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
setOnMessageArrived: function (onMessageArrived) {
|
||||||
|
if (typeof onMessageArrived === 'function') {
|
||||||
|
onMessageArrived();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
startReconnect: function () {
|
||||||
|
// 重连机制
|
||||||
|
var that = this;
|
||||||
|
var interval = app.interval;
|
||||||
|
clearInterval(interval);
|
||||||
|
interval = setInterval(function () {
|
||||||
|
var client = app.mqttParam.client;
|
||||||
|
if (client != null) {
|
||||||
|
that.connect();
|
||||||
|
}
|
||||||
|
}, 10000);
|
||||||
|
},
|
||||||
|
|
||||||
|
setOnConnectionLost: function (onConnectionLost) {
|
||||||
|
if (typeof onConnectionLost === 'function') {
|
||||||
|
onConnectionLost();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
api: mqttApi
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
const showMsg = function(title, msg) {
|
||||||
|
wx.showModal({
|
||||||
|
title: title,
|
||||||
|
content: msg,
|
||||||
|
showCancel: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
showMsg: showMsg
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,375 @@
|
|||||||
|
var $ = {
|
||||||
|
extend: function(flag, p, c) {
|
||||||
|
var c = c || {};
|
||||||
|
for (var i in p) {
|
||||||
|
if (!p.hasOwnProperty(i)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (typeof p[i] === 'object') {
|
||||||
|
c[i] = (p[i].constructor === Array) ? [] : {};
|
||||||
|
deepCopy(p[i], c[i]);
|
||||||
|
} else {
|
||||||
|
c[i] = p[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return c;
|
||||||
|
},
|
||||||
|
each: function(object, callback, args) {
|
||||||
|
var name, i = 0,
|
||||||
|
length = object.length,
|
||||||
|
isObj = length === undefined || typeof object == 'function';
|
||||||
|
|
||||||
|
if (args) {
|
||||||
|
if (isObj) {
|
||||||
|
for (name in object) {
|
||||||
|
if (callback.apply(object[name], args) === false) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (; i < length;) {
|
||||||
|
if (callback.apply(object[i++], args) === false) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A special, fast, case for the most common use of each
|
||||||
|
} else {
|
||||||
|
if (isObj) {
|
||||||
|
for (name in object) {
|
||||||
|
if (callback.call(object[name], name, object[name]) === false) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (; i < length;) {
|
||||||
|
if (callback.call(object[i], i, object[i++]) === false) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function sha1(x, blen) {
|
||||||
|
function add32(a, b) {
|
||||||
|
var lsw = (a & 0xFFFF) + (b & 0xFFFF);
|
||||||
|
var msw = (a >> 16) + (b >> 16) + (lsw >> 16);
|
||||||
|
return (msw << 16) | (lsw & 0xFFFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
function AA(a, b, c, d, e) {
|
||||||
|
b = (b >>> 27) | (b << 5);
|
||||||
|
var lsw = (a & 0xFFFF) + (b & 0xFFFF) + (c & 0xFFFF) + (d & 0xFFFF) + (e & 0xFFFF);
|
||||||
|
var msw = (a >> 16) + (b >> 16) + (c >> 16) + (d >> 16) + (e >> 16) + (lsw >> 16);
|
||||||
|
return (msw << 16) | (lsw & 0xFFFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
function RR(w, j) {
|
||||||
|
var n = w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16];
|
||||||
|
return (n >>> 31) | (n << 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
var len = blen * 8;
|
||||||
|
x[len >> 5] |= 0x80 << (24 - len % 32);
|
||||||
|
x[((len + 64 >> 9) << 4) + 15] = len;
|
||||||
|
var w = new Array(80);
|
||||||
|
|
||||||
|
var k1 = 0x5A827999;
|
||||||
|
var k2 = 0x6ED9EBA1;
|
||||||
|
var k3 = 0x8F1BBCDC;
|
||||||
|
var k4 = 0xCA62C1D6;
|
||||||
|
|
||||||
|
var h0 = 0x67452301;
|
||||||
|
var h1 = 0xEFCDAB89;
|
||||||
|
var h2 = 0x98BADCFE;
|
||||||
|
var h3 = 0x10325476;
|
||||||
|
var h4 = 0xC3D2E1F0;
|
||||||
|
|
||||||
|
for (var i = 0; i < x.length; i += 16) {
|
||||||
|
var j = 0;
|
||||||
|
var t;
|
||||||
|
var a = h0;
|
||||||
|
var b = h1;
|
||||||
|
var c = h2;
|
||||||
|
var d = h3;
|
||||||
|
var e = h4;
|
||||||
|
while (j < 16) {
|
||||||
|
w[j] = x[i + j];
|
||||||
|
t = AA(e, a, d ^ (b & (c ^ d)), w[j], k1);
|
||||||
|
e = d;
|
||||||
|
d = c;
|
||||||
|
c = (b >>> 2) | (b << 30);
|
||||||
|
b = a;
|
||||||
|
a = t;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
while (j < 20) {
|
||||||
|
w[j] = RR(w, j);
|
||||||
|
t = AA(e, a, d ^ (b & (c ^ d)), w[j], k1);
|
||||||
|
e = d;
|
||||||
|
d = c;
|
||||||
|
c = (b >>> 2) | (b << 30);
|
||||||
|
b = a;
|
||||||
|
a = t;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
while (j < 40) {
|
||||||
|
w[j] = RR(w, j);
|
||||||
|
t = AA(e, a, b ^ c ^ d, w[j], k2);
|
||||||
|
e = d;
|
||||||
|
d = c;
|
||||||
|
c = (b >>> 2) | (b << 30);
|
||||||
|
b = a;
|
||||||
|
a = t;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
while (j < 60) {
|
||||||
|
w[j] = RR(w, j);
|
||||||
|
t = AA(e, a, (b & c) | (d & (b | c)), w[j], k3);
|
||||||
|
e = d;
|
||||||
|
d = c;
|
||||||
|
c = (b >>> 2) | (b << 30);
|
||||||
|
b = a;
|
||||||
|
a = t;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
while (j < 80) {
|
||||||
|
w[j] = RR(w, j);
|
||||||
|
t = AA(e, a, b ^ c ^ d, w[j], k4);
|
||||||
|
e = d;
|
||||||
|
d = c;
|
||||||
|
c = (b >>> 2) | (b << 30);
|
||||||
|
b = a;
|
||||||
|
a = t;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
h0 = add32(h0, a);
|
||||||
|
h1 = add32(h1, b);
|
||||||
|
h2 = add32(h2, c);
|
||||||
|
h3 = add32(h3, d);
|
||||||
|
h4 = add32(h4, e);
|
||||||
|
}
|
||||||
|
return [h0, h1, h2, h3, h4];
|
||||||
|
}
|
||||||
|
|
||||||
|
var encoding = {
|
||||||
|
strToBe32s: function(str) {
|
||||||
|
var be = [];
|
||||||
|
var len = Math.floor(str.length / 4);
|
||||||
|
var i, j;
|
||||||
|
for (i = 0, j = 0; i < len; i++, j += 4) {
|
||||||
|
be[i] = ((str.charCodeAt(j) & 0xff) << 24) | ((str.charCodeAt(j + 1) & 0xff) << 16) | ((str.charCodeAt(j + 2) & 0xff) << 8) | (str.charCodeAt(j + 3) & 0xff);
|
||||||
|
}
|
||||||
|
while (j < str.length) {
|
||||||
|
be[j >> 2] |= (str.charCodeAt(j) & 0xff) << (24 - (j * 8) % 32);
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
return be;
|
||||||
|
},
|
||||||
|
|
||||||
|
be32sToHex: function(be) {
|
||||||
|
var hex = '0123456789ABCDEF';
|
||||||
|
var str = '';
|
||||||
|
for (var i = 0; i < be.length * 4; i++) {
|
||||||
|
str += hex.charAt((be[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xF) + hex.charAt((be[i >> 2] >> ((3 - i % 4) * 8)) & 0xF);
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
$.encoding = encoding;
|
||||||
|
var digests = {
|
||||||
|
hexSha1Str: function(str) {
|
||||||
|
return $.encoding.be32sToHex($.digests.sha1Str(str));
|
||||||
|
},
|
||||||
|
sha1Str: function(str) {
|
||||||
|
return sha1($.encoding.strToBe32s(str), str.length);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
$.digests = digests;
|
||||||
|
|
||||||
|
var utils = {
|
||||||
|
inArray: function(needle, array, bool) {
|
||||||
|
if (typeof needle == "string" || typeof needle == "number") {
|
||||||
|
var len = array.length;
|
||||||
|
for (var i = 0; i < len; i++) {
|
||||||
|
if (needle === array[i]) {
|
||||||
|
if (bool) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
$.utils = utils;
|
||||||
|
|
||||||
|
var api = function(openAppKey, openAppSecret, url) {
|
||||||
|
return {
|
||||||
|
test1: function() {
|
||||||
|
console.log("test1");
|
||||||
|
},
|
||||||
|
sign: function(params, ignores) {
|
||||||
|
var timestamp = (new Date()).valueOf();
|
||||||
|
var appSecret = openAppSecret;
|
||||||
|
var defaults = {
|
||||||
|
appKey: openAppKey,
|
||||||
|
v: '1.0',
|
||||||
|
format: 'json',
|
||||||
|
locale: 'zh-CN',
|
||||||
|
client: 'weixin',
|
||||||
|
timestamp: timestamp
|
||||||
|
};
|
||||||
|
var options = $.extend(true, defaults, params);
|
||||||
|
var paramNames = [];
|
||||||
|
$.each(options, function(key, value) {
|
||||||
|
if (!$.utils.inArray(key, ignores, false)) {
|
||||||
|
paramNames.push(key);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
paramNames = paramNames.sort();
|
||||||
|
|
||||||
|
var stringBuilder = [];
|
||||||
|
stringBuilder.push(appSecret);
|
||||||
|
|
||||||
|
$.each(paramNames, function(inx, value) {
|
||||||
|
stringBuilder.push(value);
|
||||||
|
stringBuilder.push(options[value]);
|
||||||
|
});
|
||||||
|
|
||||||
|
stringBuilder.push(appSecret);
|
||||||
|
options.sign = $.digests.hexSha1Str(stringBuilder.join(""));
|
||||||
|
|
||||||
|
return options;
|
||||||
|
},
|
||||||
|
ajax: function(params, ignores, success, error) {
|
||||||
|
var data = this.sign(params, ignores);
|
||||||
|
wx.request({
|
||||||
|
url: url,
|
||||||
|
method: 'POST',
|
||||||
|
data: data,
|
||||||
|
dataType: 'json',
|
||||||
|
header: {
|
||||||
|
'content-type': 'application/x-www-form-urlencoded'
|
||||||
|
},
|
||||||
|
success: function(json) {
|
||||||
|
if (json.errorToken) {
|
||||||
|
if (typeof error == 'function') {
|
||||||
|
error.call(error, json);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (typeof success == 'function') {
|
||||||
|
success.call(success, json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fail: function() {
|
||||||
|
if (typeof error == 'function') {
|
||||||
|
var json = {
|
||||||
|
"errorToken": "@@$-ERROR_TOKEN$-@@",
|
||||||
|
"code": "0",
|
||||||
|
"message": "网络好像有问题,无法访问云端服务!",
|
||||||
|
"solution": "请检查本机网络是否能正常访问互联网",
|
||||||
|
"subErrors": ""
|
||||||
|
};
|
||||||
|
error.call(error, json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 会员授权
|
||||||
|
*/
|
||||||
|
var memberApi = function (openAppKey, openAppSecret, url) {
|
||||||
|
return {
|
||||||
|
test1: function () {
|
||||||
|
console.log("test1");
|
||||||
|
},
|
||||||
|
sign: function (params, ignores) {
|
||||||
|
var timestamp = (new Date()).valueOf();
|
||||||
|
var appSecret = openAppSecret;
|
||||||
|
var defaults = {
|
||||||
|
appKey: openAppKey,
|
||||||
|
v: '1.0',
|
||||||
|
format: 'json',
|
||||||
|
locale: 'zh-CN',
|
||||||
|
client: 'web',
|
||||||
|
timestamp: timestamp
|
||||||
|
};
|
||||||
|
var options = $.extend(true, defaults, params);
|
||||||
|
var paramNames = [];
|
||||||
|
$.each(options, function (key, value) {
|
||||||
|
if (!$.utils.inArray(key, ignores, false)) {
|
||||||
|
paramNames.push(key);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
paramNames = paramNames.sort();
|
||||||
|
|
||||||
|
var stringBuilder = [];
|
||||||
|
stringBuilder.push(appSecret);
|
||||||
|
|
||||||
|
$.each(paramNames, function (inx, value) {
|
||||||
|
stringBuilder.push(value);
|
||||||
|
stringBuilder.push(options[value]);
|
||||||
|
});
|
||||||
|
|
||||||
|
stringBuilder.push(appSecret);
|
||||||
|
options.sign = $.digests.hexSha1Str(stringBuilder.join(""));
|
||||||
|
|
||||||
|
return options;
|
||||||
|
},
|
||||||
|
ajax: function (params, ignores, success, error) {
|
||||||
|
var data = this.sign(params, ignores);
|
||||||
|
wx.request({
|
||||||
|
url: url,
|
||||||
|
method: 'POST',
|
||||||
|
data: data,
|
||||||
|
dataType: 'json',
|
||||||
|
header: {
|
||||||
|
'content-type': 'application/x-www-form-urlencoded'
|
||||||
|
},
|
||||||
|
success: function (json) {
|
||||||
|
if (json.errorToken) {
|
||||||
|
if (typeof error == 'function') {
|
||||||
|
error.call(error, json);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (typeof success == 'function') {
|
||||||
|
success.call(success, json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fail: function () {
|
||||||
|
if (typeof error == 'function') {
|
||||||
|
var json = {
|
||||||
|
"errorToken": "@@$-ERROR_TOKEN$-@@",
|
||||||
|
"code": "0",
|
||||||
|
"message": "网络好像有问题,无法访问云端服务!",
|
||||||
|
"solution": "请检查本机网络是否能正常访问互联网",
|
||||||
|
"subErrors": ""
|
||||||
|
};
|
||||||
|
error.call(error, json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
api: api,
|
||||||
|
memberApi: memberApi
|
||||||
|
};
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,135 @@
|
|||||||
|
var jsapi = require('api.js');
|
||||||
|
var util = require('util.js');
|
||||||
|
var app = getApp();
|
||||||
|
|
||||||
|
var pointInfo = {
|
||||||
|
/*--------随机字符串-------*/
|
||||||
|
randomString: function() {
|
||||||
|
var randomString = "";
|
||||||
|
for (var i = 1; i <= 5; i++) {
|
||||||
|
randomString += parseInt(Math.random() * 10);
|
||||||
|
}
|
||||||
|
return randomString;
|
||||||
|
},
|
||||||
|
/*--------自动生成订单号-------*/
|
||||||
|
generateTradeNo: function() {
|
||||||
|
return util.formatTimeV(new Date(), 'yyMMddhhmmss') + app.globalData.shopNo + this.randomString();
|
||||||
|
},
|
||||||
|
|
||||||
|
/*--------查询积分商品列表-------*/
|
||||||
|
queryPointProductList: function(type) {
|
||||||
|
var data = {
|
||||||
|
"wid": app.globalData.memberWid,
|
||||||
|
"method": "query.point.product.list",
|
||||||
|
"pageType": type
|
||||||
|
};
|
||||||
|
return jsapi.cardApi.ajax(data, []);
|
||||||
|
},
|
||||||
|
/*--------查询积分商品详情-------*/
|
||||||
|
queryPointProductDetail: function (id, schemeId) {
|
||||||
|
var data = {
|
||||||
|
"method": "query.point.product.detail",
|
||||||
|
"productId": id,
|
||||||
|
"schemeId": schemeId
|
||||||
|
};
|
||||||
|
return jsapi.cardApi.ajax(data, []);
|
||||||
|
},
|
||||||
|
|
||||||
|
/*--------创建积分商城订单-------*/
|
||||||
|
createPointProductOrder: function (payAmount, payPoint, schemeId, payType, addressList, num, imageUrl, remark) {
|
||||||
|
var data = {
|
||||||
|
"method": "weixin.point.product.order.create",
|
||||||
|
"wid": app.globalData.memberWid,
|
||||||
|
"cardNo": wx.getStorageSync("cardNo"),
|
||||||
|
"mobile": wx.getStorageSync("cardInfo").mobile,
|
||||||
|
"openid": wx.getStorageSync("openId"),
|
||||||
|
"outTradeNo": this.generateTradeNo(),
|
||||||
|
"payAmount": payAmount,
|
||||||
|
"payPoint": payPoint,
|
||||||
|
"createTime": util.formatTimeV(new Date(), 'yyyy-MM-dd hh:mm:ss'),
|
||||||
|
"schemeId": schemeId,
|
||||||
|
"payType": payType,
|
||||||
|
"num": num,
|
||||||
|
"imageUrl": imageUrl,
|
||||||
|
"groupNo": app.globalData.groupNo,
|
||||||
|
"remark":remark
|
||||||
|
};
|
||||||
|
var ignores = ["mobile", "payType", "remark", "imageUrl"];
|
||||||
|
if (addressList){
|
||||||
|
if (addressList.receiveMobile){
|
||||||
|
ignores.push("receiveMobile");
|
||||||
|
data.receiveMobile = addressList.receiveMobile
|
||||||
|
}
|
||||||
|
if (addressList.receiveAddress) {
|
||||||
|
ignores.push("receiveAddress");
|
||||||
|
data.receiveAddress = addressList.receiveAddress
|
||||||
|
}
|
||||||
|
if (addressList.receiveDoor) {
|
||||||
|
ignores.push("receiveDoor");
|
||||||
|
data.receiveDoor = addressList.receiveDoor
|
||||||
|
}
|
||||||
|
if (addressList.receiveName) {
|
||||||
|
ignores.push("receiveName");
|
||||||
|
data.receiveName = addressList.receiveName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return jsapi.cardApi.ajax(data, ignores);
|
||||||
|
},
|
||||||
|
/*--------拉取微信预支付参数-------*/
|
||||||
|
pullOrderParam: function (outTradeNo, payAmount, type, schemeId, point, num) {
|
||||||
|
var data = {
|
||||||
|
"method": "weixin.unifiedorder.point",
|
||||||
|
"wid": app.globalData.memberWid,
|
||||||
|
"outTradeNo": outTradeNo,
|
||||||
|
"openid": wx.getStorageSync("openId"),
|
||||||
|
"spbillCreateIp": "127.0.0.1",
|
||||||
|
"type": type,
|
||||||
|
"groupNo": app.globalData.groupNo,
|
||||||
|
"sourceSign": "wechat",
|
||||||
|
"posNo": app.globalData.posNo,
|
||||||
|
"shopNo": app.globalData.shopNo,
|
||||||
|
"cardNo": wx.getStorageSync("cardNo"),
|
||||||
|
"workerNo": app.globalData.workerNo,
|
||||||
|
"memberId": wx.getStorageSync("memberId"),
|
||||||
|
"schemeId": schemeId,
|
||||||
|
"body": "积分商城下单"
|
||||||
|
};
|
||||||
|
var ignores = ["body", "payType"];
|
||||||
|
return jsapi.cardApi.ajax(data, ignores);
|
||||||
|
},
|
||||||
|
/*--------查询订单-------*/
|
||||||
|
queryPointOrder: function(orderNo) {
|
||||||
|
var data = {
|
||||||
|
"method": "query.point.product.order",
|
||||||
|
"wid": app.globalData.memberWid,
|
||||||
|
"orderNo": orderNo
|
||||||
|
};
|
||||||
|
var ignores = [];
|
||||||
|
return jsapi.cardApi.ajax(data, ignores);
|
||||||
|
},
|
||||||
|
/*--------查询积分商城订单列表-------*/
|
||||||
|
queryPointOrderList: function (pageSize, pageNum) {
|
||||||
|
var data = {
|
||||||
|
"method": "query.point.product.order.list",
|
||||||
|
"cardNo": wx.getStorageSync("cardNo"),
|
||||||
|
"pageSize": pageSize,
|
||||||
|
"pageNum": pageNum
|
||||||
|
};
|
||||||
|
var ignores = [];
|
||||||
|
return jsapi.cardApi.ajax(data, ignores);
|
||||||
|
},
|
||||||
|
/*--------查询积分商城订单详情-------*/
|
||||||
|
queryPointOrderDetail: function (orderNo) {
|
||||||
|
var data = {
|
||||||
|
"method": "query.point.product.order.detail",
|
||||||
|
"orderNo": orderNo
|
||||||
|
};
|
||||||
|
var ignores = [];
|
||||||
|
return jsapi.cardApi.ajax(data, ignores);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
pointInfo: pointInfo
|
||||||
|
}
|
@ -0,0 +1,876 @@
|
|||||||
|
var ERROR_CONF = {
|
||||||
|
KEY_ERR: 311,
|
||||||
|
KEY_ERR_MSG: 'key格式错误',
|
||||||
|
PARAM_ERR: 310,
|
||||||
|
PARAM_ERR_MSG: '请求参数信息有误',
|
||||||
|
SYSTEM_ERR: 600,
|
||||||
|
SYSTEM_ERR_MSG: '系统错误',
|
||||||
|
WX_ERR_CODE: 1000,
|
||||||
|
WX_OK_CODE: 200
|
||||||
|
};
|
||||||
|
var BASE_URL = 'https://apis.map.qq.com/ws/';
|
||||||
|
var URL_SEARCH = BASE_URL + 'place/v1/search';
|
||||||
|
var URL_SUGGESTION = BASE_URL + 'place/v1/suggestion';
|
||||||
|
var URL_GET_GEOCODER = BASE_URL + 'geocoder/v1/';
|
||||||
|
var URL_CITY_LIST = BASE_URL + 'district/v1/list';
|
||||||
|
var URL_AREA_LIST = BASE_URL + 'district/v1/getchildren';
|
||||||
|
var URL_DISTANCE = BASE_URL + 'distance/v1/';
|
||||||
|
var URL_DIRECTION = BASE_URL + 'direction/v1/';
|
||||||
|
var MODE = {
|
||||||
|
driving: 'driving',
|
||||||
|
transit: 'transit'
|
||||||
|
};
|
||||||
|
var EARTH_RADIUS = 6378136.49;
|
||||||
|
var Utils = {
|
||||||
|
safeAdd(x, y) {
|
||||||
|
var lsw = (x & 0xffff) + (y & 0xffff);
|
||||||
|
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
|
||||||
|
return (msw << 16) | (lsw & 0xffff)
|
||||||
|
},
|
||||||
|
bitRotateLeft(num, cnt) {
|
||||||
|
return (num << cnt) | (num >>> (32 - cnt))
|
||||||
|
},
|
||||||
|
md5cmn(q, a, b, x, s, t) {
|
||||||
|
return this.safeAdd(this.bitRotateLeft(this.safeAdd(this.safeAdd(a, q), this.safeAdd(x, t)), s), b)
|
||||||
|
},
|
||||||
|
md5ff(a, b, c, d, x, s, t) {
|
||||||
|
return this.md5cmn((b & c) | (~b & d), a, b, x, s, t)
|
||||||
|
},
|
||||||
|
md5gg(a, b, c, d, x, s, t) {
|
||||||
|
return this.md5cmn((b & d) | (c & ~d), a, b, x, s, t)
|
||||||
|
},
|
||||||
|
md5hh(a, b, c, d, x, s, t) {
|
||||||
|
return this.md5cmn(b ^ c ^ d, a, b, x, s, t)
|
||||||
|
},
|
||||||
|
md5ii(a, b, c, d, x, s, t) {
|
||||||
|
return this.md5cmn(c ^ (b | ~d), a, b, x, s, t)
|
||||||
|
},
|
||||||
|
binlMD5(x, len) {
|
||||||
|
x[len >> 5] |= 0x80 << (len % 32);
|
||||||
|
x[((len + 64) >>> 9 << 4) + 14] = len;
|
||||||
|
var i;
|
||||||
|
var olda;
|
||||||
|
var oldb;
|
||||||
|
var oldc;
|
||||||
|
var oldd;
|
||||||
|
var a = 1732584193;
|
||||||
|
var b = -271733879;
|
||||||
|
var c = -1732584194;
|
||||||
|
var d = 271733878;
|
||||||
|
for (i = 0; i < x.length; i += 16) {
|
||||||
|
olda = a;
|
||||||
|
oldb = b;
|
||||||
|
oldc = c;
|
||||||
|
oldd = d;
|
||||||
|
a = this.md5ff(a, b, c, d, x[i], 7, -680876936);
|
||||||
|
d = this.md5ff(d, a, b, c, x[i + 1], 12, -389564586);
|
||||||
|
c = this.md5ff(c, d, a, b, x[i + 2], 17, 606105819);
|
||||||
|
b = this.md5ff(b, c, d, a, x[i + 3], 22, -1044525330);
|
||||||
|
a = this.md5ff(a, b, c, d, x[i + 4], 7, -176418897);
|
||||||
|
d = this.md5ff(d, a, b, c, x[i + 5], 12, 1200080426);
|
||||||
|
c = this.md5ff(c, d, a, b, x[i + 6], 17, -1473231341);
|
||||||
|
b = this.md5ff(b, c, d, a, x[i + 7], 22, -45705983);
|
||||||
|
a = this.md5ff(a, b, c, d, x[i + 8], 7, 1770035416);
|
||||||
|
d = this.md5ff(d, a, b, c, x[i + 9], 12, -1958414417);
|
||||||
|
c = this.md5ff(c, d, a, b, x[i + 10], 17, -42063);
|
||||||
|
b = this.md5ff(b, c, d, a, x[i + 11], 22, -1990404162);
|
||||||
|
a = this.md5ff(a, b, c, d, x[i + 12], 7, 1804603682);
|
||||||
|
d = this.md5ff(d, a, b, c, x[i + 13], 12, -40341101);
|
||||||
|
c = this.md5ff(c, d, a, b, x[i + 14], 17, -1502002290);
|
||||||
|
b = this.md5ff(b, c, d, a, x[i + 15], 22, 1236535329);
|
||||||
|
a = this.md5gg(a, b, c, d, x[i + 1], 5, -165796510);
|
||||||
|
d = this.md5gg(d, a, b, c, x[i + 6], 9, -1069501632);
|
||||||
|
c = this.md5gg(c, d, a, b, x[i + 11], 14, 643717713);
|
||||||
|
b = this.md5gg(b, c, d, a, x[i], 20, -373897302);
|
||||||
|
a = this.md5gg(a, b, c, d, x[i + 5], 5, -701558691);
|
||||||
|
d = this.md5gg(d, a, b, c, x[i + 10], 9, 38016083);
|
||||||
|
c = this.md5gg(c, d, a, b, x[i + 15], 14, -660478335);
|
||||||
|
b = this.md5gg(b, c, d, a, x[i + 4], 20, -405537848);
|
||||||
|
a = this.md5gg(a, b, c, d, x[i + 9], 5, 568446438);
|
||||||
|
d = this.md5gg(d, a, b, c, x[i + 14], 9, -1019803690);
|
||||||
|
c = this.md5gg(c, d, a, b, x[i + 3], 14, -187363961);
|
||||||
|
b = this.md5gg(b, c, d, a, x[i + 8], 20, 1163531501);
|
||||||
|
a = this.md5gg(a, b, c, d, x[i + 13], 5, -1444681467);
|
||||||
|
d = this.md5gg(d, a, b, c, x[i + 2], 9, -51403784);
|
||||||
|
c = this.md5gg(c, d, a, b, x[i + 7], 14, 1735328473);
|
||||||
|
b = this.md5gg(b, c, d, a, x[i + 12], 20, -1926607734);
|
||||||
|
a = this.md5hh(a, b, c, d, x[i + 5], 4, -378558);
|
||||||
|
d = this.md5hh(d, a, b, c, x[i + 8], 11, -2022574463);
|
||||||
|
c = this.md5hh(c, d, a, b, x[i + 11], 16, 1839030562);
|
||||||
|
b = this.md5hh(b, c, d, a, x[i + 14], 23, -35309556);
|
||||||
|
a = this.md5hh(a, b, c, d, x[i + 1], 4, -1530992060);
|
||||||
|
d = this.md5hh(d, a, b, c, x[i + 4], 11, 1272893353);
|
||||||
|
c = this.md5hh(c, d, a, b, x[i + 7], 16, -155497632);
|
||||||
|
b = this.md5hh(b, c, d, a, x[i + 10], 23, -1094730640);
|
||||||
|
a = this.md5hh(a, b, c, d, x[i + 13], 4, 681279174);
|
||||||
|
d = this.md5hh(d, a, b, c, x[i], 11, -358537222);
|
||||||
|
c = this.md5hh(c, d, a, b, x[i + 3], 16, -722521979);
|
||||||
|
b = this.md5hh(b, c, d, a, x[i + 6], 23, 76029189);
|
||||||
|
a = this.md5hh(a, b, c, d, x[i + 9], 4, -640364487);
|
||||||
|
d = this.md5hh(d, a, b, c, x[i + 12], 11, -421815835);
|
||||||
|
c = this.md5hh(c, d, a, b, x[i + 15], 16, 530742520);
|
||||||
|
b = this.md5hh(b, c, d, a, x[i + 2], 23, -995338651);
|
||||||
|
a = this.md5ii(a, b, c, d, x[i], 6, -198630844);
|
||||||
|
d = this.md5ii(d, a, b, c, x[i + 7], 10, 1126891415);
|
||||||
|
c = this.md5ii(c, d, a, b, x[i + 14], 15, -1416354905);
|
||||||
|
b = this.md5ii(b, c, d, a, x[i + 5], 21, -57434055);
|
||||||
|
a = this.md5ii(a, b, c, d, x[i + 12], 6, 1700485571);
|
||||||
|
d = this.md5ii(d, a, b, c, x[i + 3], 10, -1894986606);
|
||||||
|
c = this.md5ii(c, d, a, b, x[i + 10], 15, -1051523);
|
||||||
|
b = this.md5ii(b, c, d, a, x[i + 1], 21, -2054922799);
|
||||||
|
a = this.md5ii(a, b, c, d, x[i + 8], 6, 1873313359);
|
||||||
|
d = this.md5ii(d, a, b, c, x[i + 15], 10, -30611744);
|
||||||
|
c = this.md5ii(c, d, a, b, x[i + 6], 15, -1560198380);
|
||||||
|
b = this.md5ii(b, c, d, a, x[i + 13], 21, 1309151649);
|
||||||
|
a = this.md5ii(a, b, c, d, x[i + 4], 6, -145523070);
|
||||||
|
d = this.md5ii(d, a, b, c, x[i + 11], 10, -1120210379);
|
||||||
|
c = this.md5ii(c, d, a, b, x[i + 2], 15, 718787259);
|
||||||
|
b = this.md5ii(b, c, d, a, x[i + 9], 21, -343485551);
|
||||||
|
a = this.safeAdd(a, olda);
|
||||||
|
b = this.safeAdd(b, oldb);
|
||||||
|
c = this.safeAdd(c, oldc);
|
||||||
|
d = this.safeAdd(d, oldd)
|
||||||
|
}
|
||||||
|
return [a, b, c, d]
|
||||||
|
},
|
||||||
|
binl2rstr(input) {
|
||||||
|
var i;
|
||||||
|
var output = '';
|
||||||
|
var length32 = input.length * 32;
|
||||||
|
for (i = 0; i < length32; i += 8) {
|
||||||
|
output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xff)
|
||||||
|
}
|
||||||
|
return output
|
||||||
|
},
|
||||||
|
rstr2binl(input) {
|
||||||
|
var i;
|
||||||
|
var output = [];
|
||||||
|
output[(input.length >> 2) - 1] = undefined;
|
||||||
|
for (i = 0; i < output.length; i += 1) {
|
||||||
|
output[i] = 0
|
||||||
|
}
|
||||||
|
var length8 = input.length * 8;
|
||||||
|
for (i = 0; i < length8; i += 8) {
|
||||||
|
output[i >> 5] |= (input.charCodeAt(i / 8) & 0xff) << (i % 32)
|
||||||
|
}
|
||||||
|
return output
|
||||||
|
},
|
||||||
|
rstrMD5(s) {
|
||||||
|
return this.binl2rstr(this.binlMD5(this.rstr2binl(s), s.length * 8))
|
||||||
|
},
|
||||||
|
rstrHMACMD5(key, data) {
|
||||||
|
var i;
|
||||||
|
var bkey = this.rstr2binl(key);
|
||||||
|
var ipad = [];
|
||||||
|
var opad = [];
|
||||||
|
var hash;
|
||||||
|
ipad[15] = opad[15] = undefined;
|
||||||
|
if (bkey.length > 16) {
|
||||||
|
bkey = this.binlMD5(bkey, key.length * 8)
|
||||||
|
}
|
||||||
|
for (i = 0; i < 16; i += 1) {
|
||||||
|
ipad[i] = bkey[i] ^ 0x36363636;
|
||||||
|
opad[i] = bkey[i] ^ 0x5c5c5c5c
|
||||||
|
}
|
||||||
|
hash = this.binlMD5(ipad.concat(this.rstr2binl(data)), 512 + data.length * 8);
|
||||||
|
return this.binl2rstr(this.binlMD5(opad.concat(hash), 512 + 128))
|
||||||
|
},
|
||||||
|
rstr2hex(input) {
|
||||||
|
var hexTab = '0123456789abcdef';
|
||||||
|
var output = '';
|
||||||
|
var x;
|
||||||
|
var i;
|
||||||
|
for (i = 0; i < input.length; i += 1) {
|
||||||
|
x = input.charCodeAt(i);
|
||||||
|
output += hexTab.charAt((x >>> 4) & 0x0f) + hexTab.charAt(x & 0x0f)
|
||||||
|
}
|
||||||
|
return output
|
||||||
|
},
|
||||||
|
str2rstrUTF8(input) {
|
||||||
|
return unescape(encodeURIComponent(input))
|
||||||
|
},
|
||||||
|
rawMD5(s) {
|
||||||
|
return this.rstrMD5(this.str2rstrUTF8(s))
|
||||||
|
},
|
||||||
|
hexMD5(s) {
|
||||||
|
return this.rstr2hex(this.rawMD5(s))
|
||||||
|
},
|
||||||
|
rawHMACMD5(k, d) {
|
||||||
|
return this.rstrHMACMD5(this.str2rstrUTF8(k), str2rstrUTF8(d))
|
||||||
|
},
|
||||||
|
hexHMACMD5(k, d) {
|
||||||
|
return this.rstr2hex(this.rawHMACMD5(k, d))
|
||||||
|
},
|
||||||
|
md5(string, key, raw) {
|
||||||
|
if (!key) {
|
||||||
|
if (!raw) {
|
||||||
|
return this.hexMD5(string)
|
||||||
|
}
|
||||||
|
return this.rawMD5(string)
|
||||||
|
}
|
||||||
|
if (!raw) {
|
||||||
|
return this.hexHMACMD5(key, string)
|
||||||
|
}
|
||||||
|
return this.rawHMACMD5(key, string)
|
||||||
|
},
|
||||||
|
getSig(requestParam, sk, feature, mode) {
|
||||||
|
var sig = null;
|
||||||
|
var requestArr = [];
|
||||||
|
Object.keys(requestParam).sort().forEach(function (key) {
|
||||||
|
requestArr.push(key + '=' + requestParam[key])
|
||||||
|
});
|
||||||
|
if (feature == 'search') {
|
||||||
|
sig = '/ws/place/v1/search?' + requestArr.join('&') + sk
|
||||||
|
}
|
||||||
|
if (feature == 'suggest') {
|
||||||
|
sig = '/ws/place/v1/suggestion?' + requestArr.join('&') + sk
|
||||||
|
}
|
||||||
|
if (feature == 'reverseGeocoder') {
|
||||||
|
sig = '/ws/geocoder/v1/?' + requestArr.join('&') + sk
|
||||||
|
}
|
||||||
|
if (feature == 'geocoder') {
|
||||||
|
sig = '/ws/geocoder/v1/?' + requestArr.join('&') + sk
|
||||||
|
}
|
||||||
|
if (feature == 'getCityList') {
|
||||||
|
sig = '/ws/district/v1/list?' + requestArr.join('&') + sk
|
||||||
|
}
|
||||||
|
if (feature == 'getDistrictByCityId') {
|
||||||
|
sig = '/ws/district/v1/getchildren?' + requestArr.join('&') + sk
|
||||||
|
}
|
||||||
|
if (feature == 'calculateDistance') {
|
||||||
|
sig = '/ws/distance/v1/?' + requestArr.join('&') + sk
|
||||||
|
}
|
||||||
|
if (feature == 'direction') {
|
||||||
|
sig = '/ws/direction/v1/' + mode + '?' + requestArr.join('&') + sk
|
||||||
|
}
|
||||||
|
sig = this.md5(sig);
|
||||||
|
return sig
|
||||||
|
},
|
||||||
|
location2query(data) {
|
||||||
|
if (typeof data == 'string') {
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
var query = '';
|
||||||
|
for (var i = 0; i < data.length; i++) {
|
||||||
|
var d = data[i];
|
||||||
|
if (!!query) {
|
||||||
|
query += ';'
|
||||||
|
}
|
||||||
|
if (d.location) {
|
||||||
|
query = query + d.location.lat + ',' + d.location.lng
|
||||||
|
}
|
||||||
|
if (d.latitude && d.longitude) {
|
||||||
|
query = query + d.latitude + ',' + d.longitude
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return query
|
||||||
|
},
|
||||||
|
rad(d) {
|
||||||
|
return d * Math.PI / 180.0
|
||||||
|
},
|
||||||
|
getEndLocation(location) {
|
||||||
|
var to = location.split(';');
|
||||||
|
var endLocation = [];
|
||||||
|
for (var i = 0; i < to.length; i++) {
|
||||||
|
endLocation.push({
|
||||||
|
lat: parseFloat(to[i].split(',')[0]),
|
||||||
|
lng: parseFloat(to[i].split(',')[1])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return endLocation
|
||||||
|
},
|
||||||
|
getDistance(latFrom, lngFrom, latTo, lngTo) {
|
||||||
|
var radLatFrom = this.rad(latFrom);
|
||||||
|
var radLatTo = this.rad(latTo);
|
||||||
|
var a = radLatFrom - radLatTo;
|
||||||
|
var b = this.rad(lngFrom) - this.rad(lngTo);
|
||||||
|
var distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLatFrom) * Math.cos(radLatTo) * Math.pow(Math.sin(b / 2), 2)));
|
||||||
|
distance = distance * EARTH_RADIUS;
|
||||||
|
distance = Math.round(distance * 10000) / 10000;
|
||||||
|
return parseFloat(distance.toFixed(0))
|
||||||
|
},
|
||||||
|
getWXLocation(success, fail, complete) {
|
||||||
|
wx.getLocation({
|
||||||
|
type: 'gcj02',
|
||||||
|
success: success,
|
||||||
|
fail: fail,
|
||||||
|
complete: complete
|
||||||
|
})
|
||||||
|
},
|
||||||
|
getLocationParam(location) {
|
||||||
|
if (typeof location == 'string') {
|
||||||
|
var locationArr = location.split(',');
|
||||||
|
if (locationArr.length === 2) {
|
||||||
|
location = {
|
||||||
|
latitude: location.split(',')[0],
|
||||||
|
longitude: location.split(',')[1]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
location = {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return location
|
||||||
|
},
|
||||||
|
polyfillParam(param) {
|
||||||
|
param.success = param.success || function () {};
|
||||||
|
param.fail = param.fail || function () {};
|
||||||
|
param.complete = param.complete || function () {}
|
||||||
|
},
|
||||||
|
checkParamKeyEmpty(param, key) {
|
||||||
|
if (!param[key]) {
|
||||||
|
var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + key + '参数格式有误');
|
||||||
|
param.fail(errconf);
|
||||||
|
param.complete(errconf);
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
},
|
||||||
|
checkKeyword(param) {
|
||||||
|
return !this.checkParamKeyEmpty(param, 'keyword')
|
||||||
|
},
|
||||||
|
checkLocation(param) {
|
||||||
|
var location = this.getLocationParam(param.location);
|
||||||
|
if (!location || !location.latitude || !location.longitude) {
|
||||||
|
var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + ' location参数格式有误');
|
||||||
|
param.fail(errconf);
|
||||||
|
param.complete(errconf);
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
buildErrorConfig(errCode, errMsg) {
|
||||||
|
return {
|
||||||
|
status: errCode,
|
||||||
|
message: errMsg
|
||||||
|
}
|
||||||
|
},
|
||||||
|
handleData(param, data, feature) {
|
||||||
|
if (feature == 'search') {
|
||||||
|
var searchResult = data.data;
|
||||||
|
var searchSimplify = [];
|
||||||
|
for (var i = 0; i < searchResult.length; i++) {
|
||||||
|
searchSimplify.push({
|
||||||
|
id: searchResult[i].id || null,
|
||||||
|
title: searchResult[i].title || null,
|
||||||
|
latitude: searchResult[i].location && searchResult[i].location.lat || null,
|
||||||
|
longitude: searchResult[i].location && searchResult[i].location.lng || null,
|
||||||
|
address: searchResult[i].address || null,
|
||||||
|
category: searchResult[i].category || null,
|
||||||
|
tel: searchResult[i].tel || null,
|
||||||
|
adcode: searchResult[i].ad_info && searchResult[i].ad_info.adcode || null,
|
||||||
|
city: searchResult[i].ad_info && searchResult[i].ad_info.city || null,
|
||||||
|
district: searchResult[i].ad_info && searchResult[i].ad_info.district || null,
|
||||||
|
province: searchResult[i].ad_info && searchResult[i].ad_info.province || null
|
||||||
|
})
|
||||||
|
}
|
||||||
|
param.success(data, {
|
||||||
|
searchResult: searchResult,
|
||||||
|
searchSimplify: searchSimplify
|
||||||
|
})
|
||||||
|
} else if (feature == 'suggest') {
|
||||||
|
var suggestResult = data.data;
|
||||||
|
var suggestSimplify = [];
|
||||||
|
for (var i = 0; i < suggestResult.length; i++) {
|
||||||
|
suggestSimplify.push({
|
||||||
|
adcode: suggestResult[i].adcode || null,
|
||||||
|
address: suggestResult[i].address || null,
|
||||||
|
category: suggestResult[i].category || null,
|
||||||
|
city: suggestResult[i].city || null,
|
||||||
|
district: suggestResult[i].district || null,
|
||||||
|
id: suggestResult[i].id || null,
|
||||||
|
latitude: suggestResult[i].location && suggestResult[i].location.lat || null,
|
||||||
|
longitude: suggestResult[i].location && suggestResult[i].location.lng || null,
|
||||||
|
province: suggestResult[i].province || null,
|
||||||
|
title: suggestResult[i].title || null,
|
||||||
|
type: suggestResult[i].type || null
|
||||||
|
})
|
||||||
|
}
|
||||||
|
param.success(data, {
|
||||||
|
suggestResult: suggestResult,
|
||||||
|
suggestSimplify: suggestSimplify
|
||||||
|
})
|
||||||
|
} else if (feature == 'reverseGeocoder') {
|
||||||
|
var reverseGeocoderResult = data.result;
|
||||||
|
var reverseGeocoderSimplify = {
|
||||||
|
address: reverseGeocoderResult.address || null,
|
||||||
|
latitude: reverseGeocoderResult.location && reverseGeocoderResult.location.lat || null,
|
||||||
|
longitude: reverseGeocoderResult.location && reverseGeocoderResult.location.lng || null,
|
||||||
|
adcode: reverseGeocoderResult.ad_info && reverseGeocoderResult.ad_info.adcode || null,
|
||||||
|
city: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.city || null,
|
||||||
|
district: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.district || null,
|
||||||
|
nation: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.nation || null,
|
||||||
|
province: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.province || null,
|
||||||
|
street: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.street || null,
|
||||||
|
street_number: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.street_number || null,
|
||||||
|
recommend: reverseGeocoderResult.formatted_addresses && reverseGeocoderResult.formatted_addresses.recommend || null,
|
||||||
|
rough: reverseGeocoderResult.formatted_addresses && reverseGeocoderResult.formatted_addresses.rough || null
|
||||||
|
};
|
||||||
|
if (reverseGeocoderResult.pois) {
|
||||||
|
var pois = reverseGeocoderResult.pois;
|
||||||
|
var poisSimplify = [];
|
||||||
|
for (var i = 0; i < pois.length; i++) {
|
||||||
|
poisSimplify.push({
|
||||||
|
id: pois[i].id || null,
|
||||||
|
title: pois[i].title || null,
|
||||||
|
latitude: pois[i].location && pois[i].location.lat || null,
|
||||||
|
longitude: pois[i].location && pois[i].location.lng || null,
|
||||||
|
address: pois[i].address || null,
|
||||||
|
category: pois[i].category || null,
|
||||||
|
adcode: pois[i].ad_info && pois[i].ad_info.adcode || null,
|
||||||
|
city: pois[i].ad_info && pois[i].ad_info.city || null,
|
||||||
|
district: pois[i].ad_info && pois[i].ad_info.district || null,
|
||||||
|
province: pois[i].ad_info && pois[i].ad_info.province || null
|
||||||
|
})
|
||||||
|
}
|
||||||
|
param.success(data, {
|
||||||
|
reverseGeocoderResult: reverseGeocoderResult,
|
||||||
|
reverseGeocoderSimplify: reverseGeocoderSimplify,
|
||||||
|
pois: pois,
|
||||||
|
poisSimplify: poisSimplify
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
param.success(data, {
|
||||||
|
reverseGeocoderResult: reverseGeocoderResult,
|
||||||
|
reverseGeocoderSimplify: reverseGeocoderSimplify
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else if (feature == 'geocoder') {
|
||||||
|
var geocoderResult = data.result;
|
||||||
|
var geocoderSimplify = {
|
||||||
|
title: geocoderResult.title || null,
|
||||||
|
latitude: geocoderResult.location && geocoderResult.location.lat || null,
|
||||||
|
longitude: geocoderResult.location && geocoderResult.location.lng || null,
|
||||||
|
adcode: geocoderResult.ad_info && geocoderResult.ad_info.adcode || null,
|
||||||
|
province: geocoderResult.address_components && geocoderResult.address_components.province || null,
|
||||||
|
city: geocoderResult.address_components && geocoderResult.address_components.city || null,
|
||||||
|
district: geocoderResult.address_components && geocoderResult.address_components.district || null,
|
||||||
|
street: geocoderResult.address_components && geocoderResult.address_components.street || null,
|
||||||
|
street_number: geocoderResult.address_components && geocoderResult.address_components.street_number || null,
|
||||||
|
level: geocoderResult.level || null
|
||||||
|
};
|
||||||
|
param.success(data, {
|
||||||
|
geocoderResult: geocoderResult,
|
||||||
|
geocoderSimplify: geocoderSimplify
|
||||||
|
})
|
||||||
|
} else if (feature == 'getCityList') {
|
||||||
|
var provinceResult = data.result[0];
|
||||||
|
var cityResult = data.result[1];
|
||||||
|
var districtResult = data.result[2];
|
||||||
|
param.success(data, {
|
||||||
|
provinceResult: provinceResult,
|
||||||
|
cityResult: cityResult,
|
||||||
|
districtResult: districtResult
|
||||||
|
})
|
||||||
|
} else if (feature == 'getDistrictByCityId') {
|
||||||
|
var districtByCity = data.result[0];
|
||||||
|
param.success(data, districtByCity)
|
||||||
|
} else if (feature == 'calculateDistance') {
|
||||||
|
var calculateDistanceResult = data.result.elements;
|
||||||
|
var distance = [];
|
||||||
|
for (var i = 0; i < calculateDistanceResult.length; i++) {
|
||||||
|
distance.push(calculateDistanceResult[i].distance)
|
||||||
|
}
|
||||||
|
param.success(data, {
|
||||||
|
calculateDistanceResult: calculateDistanceResult,
|
||||||
|
distance: distance
|
||||||
|
})
|
||||||
|
} else if (feature == 'direction') {
|
||||||
|
var direction = data.result.routes;
|
||||||
|
param.success(data, direction)
|
||||||
|
} else {
|
||||||
|
param.success(data)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
buildWxRequestConfig(param, options, feature) {
|
||||||
|
var that = this;
|
||||||
|
options.header = {
|
||||||
|
"content-type": "application/json"
|
||||||
|
};
|
||||||
|
options.method = 'GET';
|
||||||
|
options.success = function (res) {
|
||||||
|
var data = res.data;
|
||||||
|
if (data.status === 0) {
|
||||||
|
that.handleData(param, data, feature)
|
||||||
|
} else {
|
||||||
|
param.fail(data)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
options.fail = function (res) {
|
||||||
|
res.statusCode = ERROR_CONF.WX_ERR_CODE;
|
||||||
|
param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg))
|
||||||
|
};
|
||||||
|
options.complete = function (res) {
|
||||||
|
var statusCode = +res.statusCode;
|
||||||
|
switch (statusCode) {
|
||||||
|
case ERROR_CONF.WX_ERR_CODE: {
|
||||||
|
param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case ERROR_CONF.WX_OK_CODE: {
|
||||||
|
var data = res.data;
|
||||||
|
if (data.status === 0) {
|
||||||
|
param.complete(data)
|
||||||
|
} else {
|
||||||
|
param.complete(that.buildErrorConfig(data.status, data.message))
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
param.complete(that.buildErrorConfig(ERROR_CONF.SYSTEM_ERR, ERROR_CONF.SYSTEM_ERR_MSG))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return options
|
||||||
|
},
|
||||||
|
locationProcess(param, locationsuccess, locationfail, locationcomplete) {
|
||||||
|
var that = this;
|
||||||
|
locationfail = locationfail || function (res) {
|
||||||
|
res.statusCode = ERROR_CONF.WX_ERR_CODE;
|
||||||
|
param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg))
|
||||||
|
};
|
||||||
|
locationcomplete = locationcomplete || function (res) {
|
||||||
|
if (res.statusCode == ERROR_CONF.WX_ERR_CODE) {
|
||||||
|
param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if (!param.location) {
|
||||||
|
that.getWXLocation(locationsuccess, locationfail, locationcomplete)
|
||||||
|
} else if (that.checkLocation(param)) {
|
||||||
|
var location = Utils.getLocationParam(param.location);
|
||||||
|
locationsuccess(location)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
class QQMapWX {
|
||||||
|
constructor(options) {
|
||||||
|
if (!options.key) {
|
||||||
|
throw Error('key值不能为空')
|
||||||
|
}
|
||||||
|
this.key = options.key
|
||||||
|
};
|
||||||
|
search(options) {
|
||||||
|
var that = this;
|
||||||
|
options = options || {};
|
||||||
|
Utils.polyfillParam(options);
|
||||||
|
if (!Utils.checkKeyword(options)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var requestParam = {
|
||||||
|
keyword: options.keyword,
|
||||||
|
orderby: options.orderby || '_distance',
|
||||||
|
page_size: options.page_size || 10,
|
||||||
|
page_index: options.page_index || 1,
|
||||||
|
output: 'json',
|
||||||
|
key: that.key
|
||||||
|
};
|
||||||
|
if (options.address_format) {
|
||||||
|
requestParam.address_format = options.address_format
|
||||||
|
}
|
||||||
|
if (options.filter) {
|
||||||
|
requestParam.filter = options.filter
|
||||||
|
}
|
||||||
|
var distance = options.distance || "1000";
|
||||||
|
var auto_extend = options.auto_extend || 1;
|
||||||
|
var region = null;
|
||||||
|
var rectangle = null;
|
||||||
|
if (options.region) {
|
||||||
|
region = options.region
|
||||||
|
}
|
||||||
|
if (options.rectangle) {
|
||||||
|
rectangle = options.rectangle
|
||||||
|
}
|
||||||
|
var locationsuccess = function (result) {
|
||||||
|
if (region && !rectangle) {
|
||||||
|
requestParam.boundary = "region(" + region + "," + auto_extend + "," + result.latitude + "," + result.longitude + ")";
|
||||||
|
if (options.sig) {
|
||||||
|
requestParam.sig = Utils.getSig(requestParam, options.sig, 'search')
|
||||||
|
}
|
||||||
|
} else if (rectangle && !region) {
|
||||||
|
requestParam.boundary = "rectangle(" + rectangle + ")";
|
||||||
|
if (options.sig) {
|
||||||
|
requestParam.sig = Utils.getSig(requestParam, options.sig, 'search')
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
requestParam.boundary = "nearby(" + result.latitude + "," + result.longitude + "," + distance + "," + auto_extend + ")";
|
||||||
|
if (options.sig) {
|
||||||
|
requestParam.sig = Utils.getSig(requestParam, options.sig, 'search')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
wx.request(Utils.buildWxRequestConfig(options, {
|
||||||
|
url: URL_SEARCH,
|
||||||
|
data: requestParam
|
||||||
|
}, 'search'))
|
||||||
|
};
|
||||||
|
Utils.locationProcess(options, locationsuccess)
|
||||||
|
};
|
||||||
|
getSuggestion(options) {
|
||||||
|
var that = this;
|
||||||
|
options = options || {};
|
||||||
|
Utils.polyfillParam(options);
|
||||||
|
if (!Utils.checkKeyword(options)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var requestParam = {
|
||||||
|
keyword: options.keyword,
|
||||||
|
region: options.region || '全国',
|
||||||
|
region_fix: options.region_fix || 0,
|
||||||
|
policy: options.policy || 0,
|
||||||
|
page_size: options.page_size || 10,
|
||||||
|
page_index: options.page_index || 1,
|
||||||
|
get_subpois: options.get_subpois || 0,
|
||||||
|
output: 'json',
|
||||||
|
key: that.key
|
||||||
|
};
|
||||||
|
if (options.address_format) {
|
||||||
|
requestParam.address_format = options.address_format
|
||||||
|
}
|
||||||
|
if (options.filter) {
|
||||||
|
requestParam.filter = options.filter
|
||||||
|
}
|
||||||
|
if (options.location) {
|
||||||
|
var locationsuccess = function (result) {
|
||||||
|
requestParam.location = result.latitude + ',' + result.longitude;
|
||||||
|
if (options.sig) {
|
||||||
|
requestParam.sig = Utils.getSig(requestParam, options.sig, 'suggest')
|
||||||
|
}
|
||||||
|
wx.request(Utils.buildWxRequestConfig(options, {
|
||||||
|
url: URL_SUGGESTION,
|
||||||
|
data: requestParam
|
||||||
|
}, "suggest"))
|
||||||
|
};
|
||||||
|
Utils.locationProcess(options, locationsuccess)
|
||||||
|
} else {
|
||||||
|
if (options.sig) {
|
||||||
|
requestParam.sig = Utils.getSig(requestParam, options.sig, 'suggest')
|
||||||
|
}
|
||||||
|
wx.request(Utils.buildWxRequestConfig(options, {
|
||||||
|
url: URL_SUGGESTION,
|
||||||
|
data: requestParam
|
||||||
|
}, "suggest"))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
reverseGeocoder(options) {
|
||||||
|
var that = this;
|
||||||
|
options = options || {};
|
||||||
|
Utils.polyfillParam(options);
|
||||||
|
var requestParam = {
|
||||||
|
coord_type: options.coord_type || 5,
|
||||||
|
get_poi: options.get_poi || 0,
|
||||||
|
output: 'json',
|
||||||
|
key: that.key
|
||||||
|
};
|
||||||
|
if (options.poi_options) {
|
||||||
|
requestParam.poi_options = options.poi_options
|
||||||
|
}
|
||||||
|
var locationsuccess = function (result) {
|
||||||
|
requestParam.location = result.latitude + ',' + result.longitude;
|
||||||
|
if (options.sig) {
|
||||||
|
requestParam.sig = Utils.getSig(requestParam, options.sig, 'reverseGeocoder')
|
||||||
|
}
|
||||||
|
wx.request(Utils.buildWxRequestConfig(options, {
|
||||||
|
url: URL_GET_GEOCODER,
|
||||||
|
data: requestParam
|
||||||
|
}, 'reverseGeocoder'))
|
||||||
|
};
|
||||||
|
Utils.locationProcess(options, locationsuccess)
|
||||||
|
};
|
||||||
|
geocoder(options) {
|
||||||
|
var that = this;
|
||||||
|
options = options || {};
|
||||||
|
Utils.polyfillParam(options);
|
||||||
|
if (Utils.checkParamKeyEmpty(options, 'address')) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var requestParam = {
|
||||||
|
address: options.address,
|
||||||
|
output: 'json',
|
||||||
|
key: that.key
|
||||||
|
};
|
||||||
|
if (options.region) {
|
||||||
|
requestParam.region = options.region
|
||||||
|
}
|
||||||
|
if (options.sig) {
|
||||||
|
requestParam.sig = Utils.getSig(requestParam, options.sig, 'geocoder')
|
||||||
|
}
|
||||||
|
wx.request(Utils.buildWxRequestConfig(options, {
|
||||||
|
url: URL_GET_GEOCODER,
|
||||||
|
data: requestParam
|
||||||
|
}, 'geocoder'))
|
||||||
|
};
|
||||||
|
getCityList(options) {
|
||||||
|
var that = this;
|
||||||
|
options = options || {};
|
||||||
|
Utils.polyfillParam(options);
|
||||||
|
var requestParam = {
|
||||||
|
output: 'json',
|
||||||
|
key: that.key
|
||||||
|
};
|
||||||
|
if (options.sig) {
|
||||||
|
requestParam.sig = Utils.getSig(requestParam, options.sig, 'getCityList')
|
||||||
|
}
|
||||||
|
wx.request(Utils.buildWxRequestConfig(options, {
|
||||||
|
url: URL_CITY_LIST,
|
||||||
|
data: requestParam
|
||||||
|
}, 'getCityList'))
|
||||||
|
};
|
||||||
|
getDistrictByCityId(options) {
|
||||||
|
var that = this;
|
||||||
|
options = options || {};
|
||||||
|
Utils.polyfillParam(options);
|
||||||
|
if (Utils.checkParamKeyEmpty(options, 'id')) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var requestParam = {
|
||||||
|
id: options.id || '',
|
||||||
|
output: 'json',
|
||||||
|
key: that.key
|
||||||
|
};
|
||||||
|
if (options.sig) {
|
||||||
|
requestParam.sig = Utils.getSig(requestParam, options.sig, 'getDistrictByCityId')
|
||||||
|
}
|
||||||
|
wx.request(Utils.buildWxRequestConfig(options, {
|
||||||
|
url: URL_AREA_LIST,
|
||||||
|
data: requestParam
|
||||||
|
}, 'getDistrictByCityId'))
|
||||||
|
};
|
||||||
|
calculateDistance(options) {
|
||||||
|
var that = this;
|
||||||
|
options = options || {};
|
||||||
|
Utils.polyfillParam(options);
|
||||||
|
if (Utils.checkParamKeyEmpty(options, 'to')) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var requestParam = {
|
||||||
|
mode: options.mode || 'walking',
|
||||||
|
to: Utils.location2query(options.to),
|
||||||
|
output: 'json',
|
||||||
|
key: that.key
|
||||||
|
};
|
||||||
|
if (options.from) {
|
||||||
|
options.location = options.from
|
||||||
|
}
|
||||||
|
if (requestParam.mode == 'straight') {
|
||||||
|
var locationsuccess = function (result) {
|
||||||
|
var locationTo = Utils.getEndLocation(requestParam.to);
|
||||||
|
var data = {
|
||||||
|
message: "query ok",
|
||||||
|
result: {
|
||||||
|
elements: []
|
||||||
|
},
|
||||||
|
status: 0
|
||||||
|
};
|
||||||
|
for (var i = 0; i < locationTo.length; i++) {
|
||||||
|
data.result.elements.push({
|
||||||
|
distance: Utils.getDistance(result.latitude, result.longitude, locationTo[i].lat, locationTo[i].lng),
|
||||||
|
duration: 0,
|
||||||
|
from: {
|
||||||
|
lat: result.latitude,
|
||||||
|
lng: result.longitude
|
||||||
|
},
|
||||||
|
to: {
|
||||||
|
lat: locationTo[i].lat,
|
||||||
|
lng: locationTo[i].lng
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
var calculateResult = data.result.elements;
|
||||||
|
var distanceResult = [];
|
||||||
|
for (var i = 0; i < calculateResult.length; i++) {
|
||||||
|
distanceResult.push(calculateResult[i].distance)
|
||||||
|
}
|
||||||
|
return options.success(data, {
|
||||||
|
calculateResult: calculateResult,
|
||||||
|
distanceResult: distanceResult
|
||||||
|
})
|
||||||
|
};
|
||||||
|
Utils.locationProcess(options, locationsuccess)
|
||||||
|
} else {
|
||||||
|
var locationsuccess = function (result) {
|
||||||
|
requestParam.from = result.latitude + ',' + result.longitude;
|
||||||
|
if (options.sig) {
|
||||||
|
requestParam.sig = Utils.getSig(requestParam, options.sig, 'calculateDistance')
|
||||||
|
}
|
||||||
|
wx.request(Utils.buildWxRequestConfig(options, {
|
||||||
|
url: URL_DISTANCE,
|
||||||
|
data: requestParam
|
||||||
|
}, 'calculateDistance'))
|
||||||
|
};
|
||||||
|
Utils.locationProcess(options, locationsuccess)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
direction(options) {
|
||||||
|
var that = this;
|
||||||
|
options = options || {};
|
||||||
|
Utils.polyfillParam(options);
|
||||||
|
if (Utils.checkParamKeyEmpty(options, 'to')) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var requestParam = {
|
||||||
|
output: 'json',
|
||||||
|
key: that.key
|
||||||
|
};
|
||||||
|
if (typeof options.to == 'string') {
|
||||||
|
requestParam.to = options.to
|
||||||
|
} else {
|
||||||
|
requestParam.to = options.to.latitude + ',' + options.to.longitude
|
||||||
|
}
|
||||||
|
var SET_URL_DIRECTION = null;
|
||||||
|
options.mode = options.mode || MODE.driving;
|
||||||
|
SET_URL_DIRECTION = URL_DIRECTION + options.mode;
|
||||||
|
if (options.from) {
|
||||||
|
options.location = options.from
|
||||||
|
}
|
||||||
|
if (options.mode == MODE.driving) {
|
||||||
|
if (options.from_poi) {
|
||||||
|
requestParam.from_poi = options.from_poi
|
||||||
|
}
|
||||||
|
if (options.heading) {
|
||||||
|
requestParam.heading = options.heading
|
||||||
|
}
|
||||||
|
if (options.speed) {
|
||||||
|
requestParam.speed = options.speed
|
||||||
|
}
|
||||||
|
if (options.accuracy) {
|
||||||
|
requestParam.accuracy = options.accuracy
|
||||||
|
}
|
||||||
|
if (options.road_type) {
|
||||||
|
requestParam.road_type = options.road_type
|
||||||
|
}
|
||||||
|
if (options.to_poi) {
|
||||||
|
requestParam.to_poi = options.to_poi
|
||||||
|
}
|
||||||
|
if (options.from_track) {
|
||||||
|
requestParam.from_track = options.from_track
|
||||||
|
}
|
||||||
|
if (options.waypoints) {
|
||||||
|
requestParam.waypoints = options.waypoints
|
||||||
|
}
|
||||||
|
if (options.policy) {
|
||||||
|
requestParam.policy = options.policy
|
||||||
|
}
|
||||||
|
if (options.plate_number) {
|
||||||
|
requestParam.plate_number = options.plate_number
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (options.mode == MODE.transit) {
|
||||||
|
if (options.departure_time) {
|
||||||
|
requestParam.departure_time = options.departure_time
|
||||||
|
}
|
||||||
|
if (options.policy) {
|
||||||
|
requestParam.policy = options.policy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var locationsuccess = function (result) {
|
||||||
|
requestParam.from = result.latitude + ',' + result.longitude;
|
||||||
|
if (options.sig) {
|
||||||
|
requestParam.sig = Utils.getSig(requestParam, options.sig, 'direction', options.mode)
|
||||||
|
}
|
||||||
|
wx.request(Utils.buildWxRequestConfig(options, {
|
||||||
|
url: SET_URL_DIRECTION,
|
||||||
|
data: requestParam
|
||||||
|
}, 'direction'))
|
||||||
|
};
|
||||||
|
Utils.locationProcess(options, locationsuccess)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
module.exports = QQMapWX;
|
@ -0,0 +1,778 @@
|
|||||||
|
var QR = (function () {
|
||||||
|
|
||||||
|
// alignment pattern
|
||||||
|
var adelta = [
|
||||||
|
0, 11, 15, 19, 23, 27, 31, // force 1 pat
|
||||||
|
16, 18, 20, 22, 24, 26, 28, 20, 22, 24, 24, 26, 28, 28, 22, 24, 24,
|
||||||
|
26, 26, 28, 28, 24, 24, 26, 26, 26, 28, 28, 24, 26, 26, 26, 28, 28
|
||||||
|
];
|
||||||
|
|
||||||
|
// version block
|
||||||
|
var vpat = [
|
||||||
|
0xc94, 0x5bc, 0xa99, 0x4d3, 0xbf6, 0x762, 0x847, 0x60d,
|
||||||
|
0x928, 0xb78, 0x45d, 0xa17, 0x532, 0x9a6, 0x683, 0x8c9,
|
||||||
|
0x7ec, 0xec4, 0x1e1, 0xfab, 0x08e, 0xc1a, 0x33f, 0xd75,
|
||||||
|
0x250, 0x9d5, 0x6f0, 0x8ba, 0x79f, 0xb0b, 0x42e, 0xa64,
|
||||||
|
0x541, 0xc69
|
||||||
|
];
|
||||||
|
|
||||||
|
// final format bits with mask: level << 3 | mask
|
||||||
|
var fmtword = [
|
||||||
|
0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976, //L
|
||||||
|
0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0, //M
|
||||||
|
0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed, //Q
|
||||||
|
0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b //H
|
||||||
|
];
|
||||||
|
|
||||||
|
// 4 per version: number of blocks 1,2; data width; ecc width
|
||||||
|
var eccblocks = [
|
||||||
|
1, 0, 19, 7, 1, 0, 16, 10, 1, 0, 13, 13, 1, 0, 9, 17,
|
||||||
|
1, 0, 34, 10, 1, 0, 28, 16, 1, 0, 22, 22, 1, 0, 16, 28,
|
||||||
|
1, 0, 55, 15, 1, 0, 44, 26, 2, 0, 17, 18, 2, 0, 13, 22,
|
||||||
|
1, 0, 80, 20, 2, 0, 32, 18, 2, 0, 24, 26, 4, 0, 9, 16,
|
||||||
|
1, 0, 108, 26, 2, 0, 43, 24, 2, 2, 15, 18, 2, 2, 11, 22,
|
||||||
|
2, 0, 68, 18, 4, 0, 27, 16, 4, 0, 19, 24, 4, 0, 15, 28,
|
||||||
|
2, 0, 78, 20, 4, 0, 31, 18, 2, 4, 14, 18, 4, 1, 13, 26,
|
||||||
|
2, 0, 97, 24, 2, 2, 38, 22, 4, 2, 18, 22, 4, 2, 14, 26,
|
||||||
|
2, 0, 116, 30, 3, 2, 36, 22, 4, 4, 16, 20, 4, 4, 12, 24,
|
||||||
|
2, 2, 68, 18, 4, 1, 43, 26, 6, 2, 19, 24, 6, 2, 15, 28,
|
||||||
|
4, 0, 81, 20, 1, 4, 50, 30, 4, 4, 22, 28, 3, 8, 12, 24,
|
||||||
|
2, 2, 92, 24, 6, 2, 36, 22, 4, 6, 20, 26, 7, 4, 14, 28,
|
||||||
|
4, 0, 107, 26, 8, 1, 37, 22, 8, 4, 20, 24, 12, 4, 11, 22,
|
||||||
|
3, 1, 115, 30, 4, 5, 40, 24, 11, 5, 16, 20, 11, 5, 12, 24,
|
||||||
|
5, 1, 87, 22, 5, 5, 41, 24, 5, 7, 24, 30, 11, 7, 12, 24,
|
||||||
|
5, 1, 98, 24, 7, 3, 45, 28, 15, 2, 19, 24, 3, 13, 15, 30,
|
||||||
|
1, 5, 107, 28, 10, 1, 46, 28, 1, 15, 22, 28, 2, 17, 14, 28,
|
||||||
|
5, 1, 120, 30, 9, 4, 43, 26, 17, 1, 22, 28, 2, 19, 14, 28,
|
||||||
|
3, 4, 113, 28, 3, 11, 44, 26, 17, 4, 21, 26, 9, 16, 13, 26,
|
||||||
|
3, 5, 107, 28, 3, 13, 41, 26, 15, 5, 24, 30, 15, 10, 15, 28,
|
||||||
|
4, 4, 116, 28, 17, 0, 42, 26, 17, 6, 22, 28, 19, 6, 16, 30,
|
||||||
|
2, 7, 111, 28, 17, 0, 46, 28, 7, 16, 24, 30, 34, 0, 13, 24,
|
||||||
|
4, 5, 121, 30, 4, 14, 47, 28, 11, 14, 24, 30, 16, 14, 15, 30,
|
||||||
|
6, 4, 117, 30, 6, 14, 45, 28, 11, 16, 24, 30, 30, 2, 16, 30,
|
||||||
|
8, 4, 106, 26, 8, 13, 47, 28, 7, 22, 24, 30, 22, 13, 15, 30,
|
||||||
|
10, 2, 114, 28, 19, 4, 46, 28, 28, 6, 22, 28, 33, 4, 16, 30,
|
||||||
|
8, 4, 122, 30, 22, 3, 45, 28, 8, 26, 23, 30, 12, 28, 15, 30,
|
||||||
|
3, 10, 117, 30, 3, 23, 45, 28, 4, 31, 24, 30, 11, 31, 15, 30,
|
||||||
|
7, 7, 116, 30, 21, 7, 45, 28, 1, 37, 23, 30, 19, 26, 15, 30,
|
||||||
|
5, 10, 115, 30, 19, 10, 47, 28, 15, 25, 24, 30, 23, 25, 15, 30,
|
||||||
|
13, 3, 115, 30, 2, 29, 46, 28, 42, 1, 24, 30, 23, 28, 15, 30,
|
||||||
|
17, 0, 115, 30, 10, 23, 46, 28, 10, 35, 24, 30, 19, 35, 15, 30,
|
||||||
|
17, 1, 115, 30, 14, 21, 46, 28, 29, 19, 24, 30, 11, 46, 15, 30,
|
||||||
|
13, 6, 115, 30, 14, 23, 46, 28, 44, 7, 24, 30, 59, 1, 16, 30,
|
||||||
|
12, 7, 121, 30, 12, 26, 47, 28, 39, 14, 24, 30, 22, 41, 15, 30,
|
||||||
|
6, 14, 121, 30, 6, 34, 47, 28, 46, 10, 24, 30, 2, 64, 15, 30,
|
||||||
|
17, 4, 122, 30, 29, 14, 46, 28, 49, 10, 24, 30, 24, 46, 15, 30,
|
||||||
|
4, 18, 122, 30, 13, 32, 46, 28, 48, 14, 24, 30, 42, 32, 15, 30,
|
||||||
|
20, 4, 117, 30, 40, 7, 47, 28, 43, 22, 24, 30, 10, 67, 15, 30,
|
||||||
|
19, 6, 118, 30, 18, 31, 47, 28, 34, 34, 24, 30, 20, 61, 15, 30
|
||||||
|
];
|
||||||
|
|
||||||
|
// Galois field log table
|
||||||
|
var glog = [
|
||||||
|
0xff, 0x00, 0x01, 0x19, 0x02, 0x32, 0x1a, 0xc6, 0x03, 0xdf, 0x33, 0xee, 0x1b, 0x68, 0xc7, 0x4b,
|
||||||
|
0x04, 0x64, 0xe0, 0x0e, 0x34, 0x8d, 0xef, 0x81, 0x1c, 0xc1, 0x69, 0xf8, 0xc8, 0x08, 0x4c, 0x71,
|
||||||
|
0x05, 0x8a, 0x65, 0x2f, 0xe1, 0x24, 0x0f, 0x21, 0x35, 0x93, 0x8e, 0xda, 0xf0, 0x12, 0x82, 0x45,
|
||||||
|
0x1d, 0xb5, 0xc2, 0x7d, 0x6a, 0x27, 0xf9, 0xb9, 0xc9, 0x9a, 0x09, 0x78, 0x4d, 0xe4, 0x72, 0xa6,
|
||||||
|
0x06, 0xbf, 0x8b, 0x62, 0x66, 0xdd, 0x30, 0xfd, 0xe2, 0x98, 0x25, 0xb3, 0x10, 0x91, 0x22, 0x88,
|
||||||
|
0x36, 0xd0, 0x94, 0xce, 0x8f, 0x96, 0xdb, 0xbd, 0xf1, 0xd2, 0x13, 0x5c, 0x83, 0x38, 0x46, 0x40,
|
||||||
|
0x1e, 0x42, 0xb6, 0xa3, 0xc3, 0x48, 0x7e, 0x6e, 0x6b, 0x3a, 0x28, 0x54, 0xfa, 0x85, 0xba, 0x3d,
|
||||||
|
0xca, 0x5e, 0x9b, 0x9f, 0x0a, 0x15, 0x79, 0x2b, 0x4e, 0xd4, 0xe5, 0xac, 0x73, 0xf3, 0xa7, 0x57,
|
||||||
|
0x07, 0x70, 0xc0, 0xf7, 0x8c, 0x80, 0x63, 0x0d, 0x67, 0x4a, 0xde, 0xed, 0x31, 0xc5, 0xfe, 0x18,
|
||||||
|
0xe3, 0xa5, 0x99, 0x77, 0x26, 0xb8, 0xb4, 0x7c, 0x11, 0x44, 0x92, 0xd9, 0x23, 0x20, 0x89, 0x2e,
|
||||||
|
0x37, 0x3f, 0xd1, 0x5b, 0x95, 0xbc, 0xcf, 0xcd, 0x90, 0x87, 0x97, 0xb2, 0xdc, 0xfc, 0xbe, 0x61,
|
||||||
|
0xf2, 0x56, 0xd3, 0xab, 0x14, 0x2a, 0x5d, 0x9e, 0x84, 0x3c, 0x39, 0x53, 0x47, 0x6d, 0x41, 0xa2,
|
||||||
|
0x1f, 0x2d, 0x43, 0xd8, 0xb7, 0x7b, 0xa4, 0x76, 0xc4, 0x17, 0x49, 0xec, 0x7f, 0x0c, 0x6f, 0xf6,
|
||||||
|
0x6c, 0xa1, 0x3b, 0x52, 0x29, 0x9d, 0x55, 0xaa, 0xfb, 0x60, 0x86, 0xb1, 0xbb, 0xcc, 0x3e, 0x5a,
|
||||||
|
0xcb, 0x59, 0x5f, 0xb0, 0x9c, 0xa9, 0xa0, 0x51, 0x0b, 0xf5, 0x16, 0xeb, 0x7a, 0x75, 0x2c, 0xd7,
|
||||||
|
0x4f, 0xae, 0xd5, 0xe9, 0xe6, 0xe7, 0xad, 0xe8, 0x74, 0xd6, 0xf4, 0xea, 0xa8, 0x50, 0x58, 0xaf
|
||||||
|
];
|
||||||
|
|
||||||
|
// Galios field exponent table
|
||||||
|
var gexp = [
|
||||||
|
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1d, 0x3a, 0x74, 0xe8, 0xcd, 0x87, 0x13, 0x26,
|
||||||
|
0x4c, 0x98, 0x2d, 0x5a, 0xb4, 0x75, 0xea, 0xc9, 0x8f, 0x03, 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0,
|
||||||
|
0x9d, 0x27, 0x4e, 0x9c, 0x25, 0x4a, 0x94, 0x35, 0x6a, 0xd4, 0xb5, 0x77, 0xee, 0xc1, 0x9f, 0x23,
|
||||||
|
0x46, 0x8c, 0x05, 0x0a, 0x14, 0x28, 0x50, 0xa0, 0x5d, 0xba, 0x69, 0xd2, 0xb9, 0x6f, 0xde, 0xa1,
|
||||||
|
0x5f, 0xbe, 0x61, 0xc2, 0x99, 0x2f, 0x5e, 0xbc, 0x65, 0xca, 0x89, 0x0f, 0x1e, 0x3c, 0x78, 0xf0,
|
||||||
|
0xfd, 0xe7, 0xd3, 0xbb, 0x6b, 0xd6, 0xb1, 0x7f, 0xfe, 0xe1, 0xdf, 0xa3, 0x5b, 0xb6, 0x71, 0xe2,
|
||||||
|
0xd9, 0xaf, 0x43, 0x86, 0x11, 0x22, 0x44, 0x88, 0x0d, 0x1a, 0x34, 0x68, 0xd0, 0xbd, 0x67, 0xce,
|
||||||
|
0x81, 0x1f, 0x3e, 0x7c, 0xf8, 0xed, 0xc7, 0x93, 0x3b, 0x76, 0xec, 0xc5, 0x97, 0x33, 0x66, 0xcc,
|
||||||
|
0x85, 0x17, 0x2e, 0x5c, 0xb8, 0x6d, 0xda, 0xa9, 0x4f, 0x9e, 0x21, 0x42, 0x84, 0x15, 0x2a, 0x54,
|
||||||
|
0xa8, 0x4d, 0x9a, 0x29, 0x52, 0xa4, 0x55, 0xaa, 0x49, 0x92, 0x39, 0x72, 0xe4, 0xd5, 0xb7, 0x73,
|
||||||
|
0xe6, 0xd1, 0xbf, 0x63, 0xc6, 0x91, 0x3f, 0x7e, 0xfc, 0xe5, 0xd7, 0xb3, 0x7b, 0xf6, 0xf1, 0xff,
|
||||||
|
0xe3, 0xdb, 0xab, 0x4b, 0x96, 0x31, 0x62, 0xc4, 0x95, 0x37, 0x6e, 0xdc, 0xa5, 0x57, 0xae, 0x41,
|
||||||
|
0x82, 0x19, 0x32, 0x64, 0xc8, 0x8d, 0x07, 0x0e, 0x1c, 0x38, 0x70, 0xe0, 0xdd, 0xa7, 0x53, 0xa6,
|
||||||
|
0x51, 0xa2, 0x59, 0xb2, 0x79, 0xf2, 0xf9, 0xef, 0xc3, 0x9b, 0x2b, 0x56, 0xac, 0x45, 0x8a, 0x09,
|
||||||
|
0x12, 0x24, 0x48, 0x90, 0x3d, 0x7a, 0xf4, 0xf5, 0xf7, 0xf3, 0xfb, 0xeb, 0xcb, 0x8b, 0x0b, 0x16,
|
||||||
|
0x2c, 0x58, 0xb0, 0x7d, 0xfa, 0xe9, 0xcf, 0x83, 0x1b, 0x36, 0x6c, 0xd8, 0xad, 0x47, 0x8e, 0x00
|
||||||
|
];
|
||||||
|
|
||||||
|
// Working buffers:
|
||||||
|
// data input and ecc append, image working buffer, fixed part of image, run lengths for badness
|
||||||
|
var strinbuf=[], eccbuf=[], qrframe=[], framask=[], rlens=[];
|
||||||
|
// Control values - width is based on version, last 4 are from table.
|
||||||
|
var version, width, neccblk1, neccblk2, datablkw, eccblkwid;
|
||||||
|
var ecclevel = 2;
|
||||||
|
// set bit to indicate cell in qrframe is immutable. symmetric around diagonal
|
||||||
|
function setmask(x, y)
|
||||||
|
{
|
||||||
|
var bt;
|
||||||
|
if (x > y) {
|
||||||
|
bt = x;
|
||||||
|
x = y;
|
||||||
|
y = bt;
|
||||||
|
}
|
||||||
|
// y*y = 1+3+5...
|
||||||
|
bt = y;
|
||||||
|
bt *= y;
|
||||||
|
bt += y;
|
||||||
|
bt >>= 1;
|
||||||
|
bt += x;
|
||||||
|
framask[bt] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// enter alignment pattern - black to qrframe, white to mask (later black frame merged to mask)
|
||||||
|
function putalign(x, y)
|
||||||
|
{
|
||||||
|
var j;
|
||||||
|
|
||||||
|
qrframe[x + width * y] = 1;
|
||||||
|
for (j = -2; j < 2; j++) {
|
||||||
|
qrframe[(x + j) + width * (y - 2)] = 1;
|
||||||
|
qrframe[(x - 2) + width * (y + j + 1)] = 1;
|
||||||
|
qrframe[(x + 2) + width * (y + j)] = 1;
|
||||||
|
qrframe[(x + j + 1) + width * (y + 2)] = 1;
|
||||||
|
}
|
||||||
|
for (j = 0; j < 2; j++) {
|
||||||
|
setmask(x - 1, y + j);
|
||||||
|
setmask(x + 1, y - j);
|
||||||
|
setmask(x - j, y - 1);
|
||||||
|
setmask(x + j, y + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//========================================================================
|
||||||
|
// Reed Solomon error correction
|
||||||
|
// exponentiation mod N
|
||||||
|
function modnn(x)
|
||||||
|
{
|
||||||
|
while (x >= 255) {
|
||||||
|
x -= 255;
|
||||||
|
x = (x >> 8) + (x & 255);
|
||||||
|
}
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
var genpoly = [];
|
||||||
|
|
||||||
|
// Calculate and append ECC data to data block. Block is in strinbuf, indexes to buffers given.
|
||||||
|
function appendrs(data, dlen, ecbuf, eclen)
|
||||||
|
{
|
||||||
|
var i, j, fb;
|
||||||
|
|
||||||
|
for (i = 0; i < eclen; i++)
|
||||||
|
strinbuf[ecbuf + i] = 0;
|
||||||
|
for (i = 0; i < dlen; i++) {
|
||||||
|
fb = glog[strinbuf[data + i] ^ strinbuf[ecbuf]];
|
||||||
|
if (fb != 255) /* fb term is non-zero */
|
||||||
|
for (j = 1; j < eclen; j++)
|
||||||
|
strinbuf[ecbuf + j - 1] = strinbuf[ecbuf + j] ^ gexp[modnn(fb + genpoly[eclen - j])];
|
||||||
|
else
|
||||||
|
for( j = ecbuf ; j < ecbuf + eclen; j++ )
|
||||||
|
strinbuf[j] = strinbuf[j + 1];
|
||||||
|
strinbuf[ ecbuf + eclen - 1] = fb == 255 ? 0 : gexp[modnn(fb + genpoly[0])];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//========================================================================
|
||||||
|
// Frame data insert following the path rules
|
||||||
|
|
||||||
|
// check mask - since symmetrical use half.
|
||||||
|
function ismasked(x, y)
|
||||||
|
{
|
||||||
|
var bt;
|
||||||
|
if (x > y) {
|
||||||
|
bt = x;
|
||||||
|
x = y;
|
||||||
|
y = bt;
|
||||||
|
}
|
||||||
|
bt = y;
|
||||||
|
bt += y * y;
|
||||||
|
bt >>= 1;
|
||||||
|
bt += x;
|
||||||
|
return framask[bt];
|
||||||
|
}
|
||||||
|
|
||||||
|
//========================================================================
|
||||||
|
// Apply the selected mask out of the 8.
|
||||||
|
function applymask(m)
|
||||||
|
{
|
||||||
|
var x, y, r3x, r3y;
|
||||||
|
|
||||||
|
switch (m) {
|
||||||
|
case 0:
|
||||||
|
for (y = 0; y < width; y++)
|
||||||
|
for (x = 0; x < width; x++)
|
||||||
|
if (!((x + y) & 1) && !ismasked(x, y))
|
||||||
|
qrframe[x + y * width] ^= 1;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
for (y = 0; y < width; y++)
|
||||||
|
for (x = 0; x < width; x++)
|
||||||
|
if (!(y & 1) && !ismasked(x, y))
|
||||||
|
qrframe[x + y * width] ^= 1;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
for (y = 0; y < width; y++)
|
||||||
|
for (r3x = 0, x = 0; x < width; x++, r3x++) {
|
||||||
|
if (r3x == 3)
|
||||||
|
r3x = 0;
|
||||||
|
if (!r3x && !ismasked(x, y))
|
||||||
|
qrframe[x + y * width] ^= 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
for (r3y = 0, y = 0; y < width; y++, r3y++) {
|
||||||
|
if (r3y == 3)
|
||||||
|
r3y = 0;
|
||||||
|
for (r3x = r3y, x = 0; x < width; x++, r3x++) {
|
||||||
|
if (r3x == 3)
|
||||||
|
r3x = 0;
|
||||||
|
if (!r3x && !ismasked(x, y))
|
||||||
|
qrframe[x + y * width] ^= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
for (y = 0; y < width; y++)
|
||||||
|
for (r3x = 0, r3y = ((y >> 1) & 1), x = 0; x < width; x++, r3x++) {
|
||||||
|
if (r3x == 3) {
|
||||||
|
r3x = 0;
|
||||||
|
r3y = !r3y;
|
||||||
|
}
|
||||||
|
if (!r3y && !ismasked(x, y))
|
||||||
|
qrframe[x + y * width] ^= 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
for (r3y = 0, y = 0; y < width; y++, r3y++) {
|
||||||
|
if (r3y == 3)
|
||||||
|
r3y = 0;
|
||||||
|
for (r3x = 0, x = 0; x < width; x++, r3x++) {
|
||||||
|
if (r3x == 3)
|
||||||
|
r3x = 0;
|
||||||
|
if (!((x & y & 1) + !(!r3x | !r3y)) && !ismasked(x, y))
|
||||||
|
qrframe[x + y * width] ^= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
for (r3y = 0, y = 0; y < width; y++, r3y++) {
|
||||||
|
if (r3y == 3)
|
||||||
|
r3y = 0;
|
||||||
|
for (r3x = 0, x = 0; x < width; x++, r3x++) {
|
||||||
|
if (r3x == 3)
|
||||||
|
r3x = 0;
|
||||||
|
if (!(((x & y & 1) + (r3x && (r3x == r3y))) & 1) && !ismasked(x, y))
|
||||||
|
qrframe[x + y * width] ^= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
for (r3y = 0, y = 0; y < width; y++, r3y++) {
|
||||||
|
if (r3y == 3)
|
||||||
|
r3y = 0;
|
||||||
|
for (r3x = 0, x = 0; x < width; x++, r3x++) {
|
||||||
|
if (r3x == 3)
|
||||||
|
r3x = 0;
|
||||||
|
if (!(((r3x && (r3x == r3y)) + ((x + y) & 1)) & 1) && !ismasked(x, y))
|
||||||
|
qrframe[x + y * width] ^= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Badness coefficients.
|
||||||
|
var N1 = 3, N2 = 3, N3 = 40, N4 = 10;
|
||||||
|
|
||||||
|
// Using the table of the length of each run, calculate the amount of bad image
|
||||||
|
// - long runs or those that look like finders; called twice, once each for X and Y
|
||||||
|
function badruns(length)
|
||||||
|
{
|
||||||
|
var i;
|
||||||
|
var runsbad = 0;
|
||||||
|
for (i = 0; i <= length; i++)
|
||||||
|
if (rlens[i] >= 5)
|
||||||
|
runsbad += N1 + rlens[i] - 5;
|
||||||
|
// BwBBBwB as in finder
|
||||||
|
for (i = 3; i < length - 1; i += 2)
|
||||||
|
if (rlens[i - 2] == rlens[i + 2]
|
||||||
|
&& rlens[i + 2] == rlens[i - 1]
|
||||||
|
&& rlens[i - 1] == rlens[i + 1]
|
||||||
|
&& rlens[i - 1] * 3 == rlens[i]
|
||||||
|
// white around the black pattern? Not part of spec
|
||||||
|
&& (rlens[i - 3] == 0 // beginning
|
||||||
|
|| i + 3 > length // end
|
||||||
|
|| rlens[i - 3] * 3 >= rlens[i] * 4 || rlens[i + 3] * 3 >= rlens[i] * 4)
|
||||||
|
)
|
||||||
|
runsbad += N3;
|
||||||
|
return runsbad;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate how bad the masked image is - blocks, imbalance, runs, or finders.
|
||||||
|
function badcheck()
|
||||||
|
{
|
||||||
|
var x, y, h, b, b1;
|
||||||
|
var thisbad = 0;
|
||||||
|
var bw = 0;
|
||||||
|
|
||||||
|
// blocks of same color.
|
||||||
|
for (y = 0; y < width - 1; y++)
|
||||||
|
for (x = 0; x < width - 1; x++)
|
||||||
|
if ((qrframe[x + width * y] && qrframe[(x + 1) + width * y]
|
||||||
|
&& qrframe[x + width * (y + 1)] && qrframe[(x + 1) + width * (y + 1)]) // all black
|
||||||
|
|| !(qrframe[x + width * y] || qrframe[(x + 1) + width * y]
|
||||||
|
|| qrframe[x + width * (y + 1)] || qrframe[(x + 1) + width * (y + 1)])) // all white
|
||||||
|
thisbad += N2;
|
||||||
|
|
||||||
|
// X runs
|
||||||
|
for (y = 0; y < width; y++) {
|
||||||
|
rlens[0] = 0;
|
||||||
|
for (h = b = x = 0; x < width; x++) {
|
||||||
|
if ((b1 = qrframe[x + width * y]) == b)
|
||||||
|
rlens[h]++;
|
||||||
|
else
|
||||||
|
rlens[++h] = 1;
|
||||||
|
b = b1;
|
||||||
|
bw += b ? 1 : -1;
|
||||||
|
}
|
||||||
|
thisbad += badruns(h);
|
||||||
|
}
|
||||||
|
|
||||||
|
// black/white imbalance
|
||||||
|
if (bw < 0)
|
||||||
|
bw = -bw;
|
||||||
|
|
||||||
|
var big = bw;
|
||||||
|
var count = 0;
|
||||||
|
big += big << 2;
|
||||||
|
big <<= 1;
|
||||||
|
while (big > width * width)
|
||||||
|
big -= width * width, count++;
|
||||||
|
thisbad += count * N4;
|
||||||
|
|
||||||
|
// Y runs
|
||||||
|
for (x = 0; x < width; x++) {
|
||||||
|
rlens[0] = 0;
|
||||||
|
for (h = b = y = 0; y < width; y++) {
|
||||||
|
if ((b1 = qrframe[x + width * y]) == b)
|
||||||
|
rlens[h]++;
|
||||||
|
else
|
||||||
|
rlens[++h] = 1;
|
||||||
|
b = b1;
|
||||||
|
}
|
||||||
|
thisbad += badruns(h);
|
||||||
|
}
|
||||||
|
return thisbad;
|
||||||
|
}
|
||||||
|
|
||||||
|
function genframe(instring)
|
||||||
|
{
|
||||||
|
var x, y, k, t, v, i, j, m;
|
||||||
|
|
||||||
|
// find the smallest version that fits the string
|
||||||
|
t = instring.length;
|
||||||
|
version = 0;
|
||||||
|
do {
|
||||||
|
version++;
|
||||||
|
k = (ecclevel - 1) * 4 + (version - 1) * 16;
|
||||||
|
neccblk1 = eccblocks[k++];
|
||||||
|
neccblk2 = eccblocks[k++];
|
||||||
|
datablkw = eccblocks[k++];
|
||||||
|
eccblkwid = eccblocks[k];
|
||||||
|
k = datablkw * (neccblk1 + neccblk2) + neccblk2 - 3 + (version <= 9);
|
||||||
|
if (t <= k)
|
||||||
|
break;
|
||||||
|
} while (version < 40);
|
||||||
|
|
||||||
|
// FIXME - insure that it fits insted of being truncated
|
||||||
|
width = 17 + 4 * version;
|
||||||
|
|
||||||
|
// allocate, clear and setup data structures
|
||||||
|
v = datablkw + (datablkw + eccblkwid) * (neccblk1 + neccblk2) + neccblk2;
|
||||||
|
for( t = 0; t < v; t++ )
|
||||||
|
eccbuf[t] = 0;
|
||||||
|
strinbuf = instring.slice(0);
|
||||||
|
|
||||||
|
for( t = 0; t < width * width; t++ )
|
||||||
|
qrframe[t] = 0;
|
||||||
|
|
||||||
|
for( t = 0 ; t < (width * (width + 1) + 1) / 2; t++)
|
||||||
|
framask[t] = 0;
|
||||||
|
|
||||||
|
// insert finders - black to frame, white to mask
|
||||||
|
for (t = 0; t < 3; t++) {
|
||||||
|
k = 0;
|
||||||
|
y = 0;
|
||||||
|
if (t == 1)
|
||||||
|
k = (width - 7);
|
||||||
|
if (t == 2)
|
||||||
|
y = (width - 7);
|
||||||
|
qrframe[(y + 3) + width * (k + 3)] = 1;
|
||||||
|
for (x = 0; x < 6; x++) {
|
||||||
|
qrframe[(y + x) + width * k] = 1;
|
||||||
|
qrframe[y + width * (k + x + 1)] = 1;
|
||||||
|
qrframe[(y + 6) + width * (k + x)] = 1;
|
||||||
|
qrframe[(y + x + 1) + width * (k + 6)] = 1;
|
||||||
|
}
|
||||||
|
for (x = 1; x < 5; x++) {
|
||||||
|
setmask(y + x, k + 1);
|
||||||
|
setmask(y + 1, k + x + 1);
|
||||||
|
setmask(y + 5, k + x);
|
||||||
|
setmask(y + x + 1, k + 5);
|
||||||
|
}
|
||||||
|
for (x = 2; x < 4; x++) {
|
||||||
|
qrframe[(y + x) + width * (k + 2)] = 1;
|
||||||
|
qrframe[(y + 2) + width * (k + x + 1)] = 1;
|
||||||
|
qrframe[(y + 4) + width * (k + x)] = 1;
|
||||||
|
qrframe[(y + x + 1) + width * (k + 4)] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// alignment blocks
|
||||||
|
if (version > 1) {
|
||||||
|
t = adelta[version];
|
||||||
|
y = width - 7;
|
||||||
|
for (;;) {
|
||||||
|
x = width - 7;
|
||||||
|
while (x > t - 3) {
|
||||||
|
putalign(x, y);
|
||||||
|
if (x < t)
|
||||||
|
break;
|
||||||
|
x -= t;
|
||||||
|
}
|
||||||
|
if (y <= t + 9)
|
||||||
|
break;
|
||||||
|
y -= t;
|
||||||
|
putalign(6, y);
|
||||||
|
putalign(y, 6);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// single black
|
||||||
|
qrframe[8 + width * (width - 8)] = 1;
|
||||||
|
|
||||||
|
// timing gap - mask only
|
||||||
|
for (y = 0; y < 7; y++) {
|
||||||
|
setmask(7, y);
|
||||||
|
setmask(width - 8, y);
|
||||||
|
setmask(7, y + width - 7);
|
||||||
|
}
|
||||||
|
for (x = 0; x < 8; x++) {
|
||||||
|
setmask(x, 7);
|
||||||
|
setmask(x + width - 8, 7);
|
||||||
|
setmask(x, width - 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
// reserve mask-format area
|
||||||
|
for (x = 0; x < 9; x++)
|
||||||
|
setmask(x, 8);
|
||||||
|
for (x = 0; x < 8; x++) {
|
||||||
|
setmask(x + width - 8, 8);
|
||||||
|
setmask(8, x);
|
||||||
|
}
|
||||||
|
for (y = 0; y < 7; y++)
|
||||||
|
setmask(8, y + width - 7);
|
||||||
|
|
||||||
|
// timing row/col
|
||||||
|
for (x = 0; x < width - 14; x++)
|
||||||
|
if (x & 1) {
|
||||||
|
setmask(8 + x, 6);
|
||||||
|
setmask(6, 8 + x);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
qrframe[(8 + x) + width * 6] = 1;
|
||||||
|
qrframe[6 + width * (8 + x)] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// version block
|
||||||
|
if (version > 6) {
|
||||||
|
t = vpat[version - 7];
|
||||||
|
k = 17;
|
||||||
|
for (x = 0; x < 6; x++)
|
||||||
|
for (y = 0; y < 3; y++, k--)
|
||||||
|
if (1 & (k > 11 ? version >> (k - 12) : t >> k)) {
|
||||||
|
qrframe[(5 - x) + width * (2 - y + width - 11)] = 1;
|
||||||
|
qrframe[(2 - y + width - 11) + width * (5 - x)] = 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
setmask(5 - x, 2 - y + width - 11);
|
||||||
|
setmask(2 - y + width - 11, 5 - x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sync mask bits - only set above for white spaces, so add in black bits
|
||||||
|
for (y = 0; y < width; y++)
|
||||||
|
for (x = 0; x <= y; x++)
|
||||||
|
if (qrframe[x + width * y])
|
||||||
|
setmask(x, y);
|
||||||
|
|
||||||
|
// convert string to bitstream
|
||||||
|
// 8 bit data to QR-coded 8 bit data (numeric or alphanum, or kanji not supported)
|
||||||
|
v = strinbuf.length;
|
||||||
|
|
||||||
|
// string to array
|
||||||
|
for( i = 0 ; i < v; i++ )
|
||||||
|
eccbuf[i] = strinbuf.charCodeAt(i);
|
||||||
|
strinbuf = eccbuf.slice(0);
|
||||||
|
|
||||||
|
// calculate max string length
|
||||||
|
x = datablkw * (neccblk1 + neccblk2) + neccblk2;
|
||||||
|
if (v >= x - 2) {
|
||||||
|
v = x - 2;
|
||||||
|
if (version > 9)
|
||||||
|
v--;
|
||||||
|
}
|
||||||
|
|
||||||
|
// shift and repack to insert length prefix
|
||||||
|
i = v;
|
||||||
|
if (version > 9) {
|
||||||
|
strinbuf[i + 2] = 0;
|
||||||
|
strinbuf[i + 3] = 0;
|
||||||
|
while (i--) {
|
||||||
|
t = strinbuf[i];
|
||||||
|
strinbuf[i + 3] |= 255 & (t << 4);
|
||||||
|
strinbuf[i + 2] = t >> 4;
|
||||||
|
}
|
||||||
|
strinbuf[2] |= 255 & (v << 4);
|
||||||
|
strinbuf[1] = v >> 4;
|
||||||
|
strinbuf[0] = 0x40 | (v >> 12);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
strinbuf[i + 1] = 0;
|
||||||
|
strinbuf[i + 2] = 0;
|
||||||
|
while (i--) {
|
||||||
|
t = strinbuf[i];
|
||||||
|
strinbuf[i + 2] |= 255 & (t << 4);
|
||||||
|
strinbuf[i + 1] = t >> 4;
|
||||||
|
}
|
||||||
|
strinbuf[1] |= 255 & (v << 4);
|
||||||
|
strinbuf[0] = 0x40 | (v >> 4);
|
||||||
|
}
|
||||||
|
// fill to end with pad pattern
|
||||||
|
i = v + 3 - (version < 10);
|
||||||
|
while (i < x) {
|
||||||
|
strinbuf[i++] = 0xec;
|
||||||
|
// buffer has room if (i == x) break;
|
||||||
|
strinbuf[i++] = 0x11;
|
||||||
|
}
|
||||||
|
|
||||||
|
// calculate and append ECC
|
||||||
|
|
||||||
|
// calculate generator polynomial
|
||||||
|
genpoly[0] = 1;
|
||||||
|
for (i = 0; i < eccblkwid; i++) {
|
||||||
|
genpoly[i + 1] = 1;
|
||||||
|
for (j = i; j > 0; j--)
|
||||||
|
genpoly[j] = genpoly[j]
|
||||||
|
? genpoly[j - 1] ^ gexp[modnn(glog[genpoly[j]] + i)] : genpoly[j - 1];
|
||||||
|
genpoly[0] = gexp[modnn(glog[genpoly[0]] + i)];
|
||||||
|
}
|
||||||
|
for (i = 0; i <= eccblkwid; i++)
|
||||||
|
genpoly[i] = glog[genpoly[i]]; // use logs for genpoly[] to save calc step
|
||||||
|
|
||||||
|
// append ecc to data buffer
|
||||||
|
k = x;
|
||||||
|
y = 0;
|
||||||
|
for (i = 0; i < neccblk1; i++) {
|
||||||
|
appendrs(y, datablkw, k, eccblkwid);
|
||||||
|
y += datablkw;
|
||||||
|
k += eccblkwid;
|
||||||
|
}
|
||||||
|
for (i = 0; i < neccblk2; i++) {
|
||||||
|
appendrs(y, datablkw + 1, k, eccblkwid);
|
||||||
|
y += datablkw + 1;
|
||||||
|
k += eccblkwid;
|
||||||
|
}
|
||||||
|
// interleave blocks
|
||||||
|
y = 0;
|
||||||
|
for (i = 0; i < datablkw; i++) {
|
||||||
|
for (j = 0; j < neccblk1; j++)
|
||||||
|
eccbuf[y++] = strinbuf[i + j * datablkw];
|
||||||
|
for (j = 0; j < neccblk2; j++)
|
||||||
|
eccbuf[y++] = strinbuf[(neccblk1 * datablkw) + i + (j * (datablkw + 1))];
|
||||||
|
}
|
||||||
|
for (j = 0; j < neccblk2; j++)
|
||||||
|
eccbuf[y++] = strinbuf[(neccblk1 * datablkw) + i + (j * (datablkw + 1))];
|
||||||
|
for (i = 0; i < eccblkwid; i++)
|
||||||
|
for (j = 0; j < neccblk1 + neccblk2; j++)
|
||||||
|
eccbuf[y++] = strinbuf[x + i + j * eccblkwid];
|
||||||
|
strinbuf = eccbuf;
|
||||||
|
|
||||||
|
// pack bits into frame avoiding masked area.
|
||||||
|
x = y = width - 1;
|
||||||
|
k = v = 1; // up, minus
|
||||||
|
/* inteleaved data and ecc codes */
|
||||||
|
m = (datablkw + eccblkwid) * (neccblk1 + neccblk2) + neccblk2;
|
||||||
|
for (i = 0; i < m; i++) {
|
||||||
|
t = strinbuf[i];
|
||||||
|
for (j = 0; j < 8; j++, t <<= 1) {
|
||||||
|
if (0x80 & t)
|
||||||
|
qrframe[x + width * y] = 1;
|
||||||
|
do { // find next fill position
|
||||||
|
if (v)
|
||||||
|
x--;
|
||||||
|
else {
|
||||||
|
x++;
|
||||||
|
if (k) {
|
||||||
|
if (y != 0)
|
||||||
|
y--;
|
||||||
|
else {
|
||||||
|
x -= 2;
|
||||||
|
k = !k;
|
||||||
|
if (x == 6) {
|
||||||
|
x--;
|
||||||
|
y = 9;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (y != width - 1)
|
||||||
|
y++;
|
||||||
|
else {
|
||||||
|
x -= 2;
|
||||||
|
k = !k;
|
||||||
|
if (x == 6) {
|
||||||
|
x--;
|
||||||
|
y -= 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
v = !v;
|
||||||
|
} while (ismasked(x, y));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// save pre-mask copy of frame
|
||||||
|
strinbuf = qrframe.slice(0);
|
||||||
|
t = 0; // best
|
||||||
|
y = 30000; // demerit
|
||||||
|
// for instead of while since in original arduino code
|
||||||
|
// if an early mask was "good enough" it wouldn't try for a better one
|
||||||
|
// since they get more complex and take longer.
|
||||||
|
for (k = 0; k < 8; k++) {
|
||||||
|
applymask(k); // returns black-white imbalance
|
||||||
|
x = badcheck();
|
||||||
|
if (x < y) { // current mask better than previous best?
|
||||||
|
y = x;
|
||||||
|
t = k;
|
||||||
|
}
|
||||||
|
if (t == 7)
|
||||||
|
break; // don't increment i to a void redoing mask
|
||||||
|
qrframe = strinbuf.slice(0); // reset for next pass
|
||||||
|
}
|
||||||
|
if (t != k) // redo best mask - none good enough, last wasn't t
|
||||||
|
applymask(t);
|
||||||
|
|
||||||
|
// add in final mask/ecclevel bytes
|
||||||
|
y = fmtword[t + ((ecclevel - 1) << 3)];
|
||||||
|
// low byte
|
||||||
|
for (k = 0; k < 8; k++, y >>= 1)
|
||||||
|
if (y & 1) {
|
||||||
|
qrframe[(width - 1 - k) + width * 8] = 1;
|
||||||
|
if (k < 6)
|
||||||
|
qrframe[8 + width * k] = 1;
|
||||||
|
else
|
||||||
|
qrframe[8 + width * (k + 1)] = 1;
|
||||||
|
}
|
||||||
|
// high byte
|
||||||
|
for (k = 0; k < 7; k++, y >>= 1)
|
||||||
|
if (y & 1) {
|
||||||
|
qrframe[8 + width * (width - 7 + k)] = 1;
|
||||||
|
if (k)
|
||||||
|
qrframe[(6 - k) + width * 8] = 1;
|
||||||
|
else
|
||||||
|
qrframe[7 + width * 8] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// return image
|
||||||
|
return qrframe;
|
||||||
|
}
|
||||||
|
|
||||||
|
var _canvas = null,
|
||||||
|
_size = null;
|
||||||
|
|
||||||
|
var api = {
|
||||||
|
|
||||||
|
get ecclevel () {
|
||||||
|
return ecclevel;
|
||||||
|
},
|
||||||
|
|
||||||
|
set ecclevel (val) {
|
||||||
|
ecclevel = val;
|
||||||
|
},
|
||||||
|
|
||||||
|
get size () {
|
||||||
|
return _size;
|
||||||
|
},
|
||||||
|
|
||||||
|
set size (val) {
|
||||||
|
_size = val
|
||||||
|
},
|
||||||
|
|
||||||
|
get canvas () {
|
||||||
|
return _canvas;
|
||||||
|
},
|
||||||
|
|
||||||
|
set canvas (el) {
|
||||||
|
_canvas = el;
|
||||||
|
},
|
||||||
|
|
||||||
|
getFrame: function (string) {
|
||||||
|
return genframe(string);
|
||||||
|
},
|
||||||
|
|
||||||
|
draw: function (string, canvas, size, ecc) {
|
||||||
|
|
||||||
|
ecclevel = ecc || ecclevel;
|
||||||
|
canvas = canvas || _canvas;
|
||||||
|
|
||||||
|
if (!canvas) {
|
||||||
|
console.warn('No canvas provided to draw QR code in!')
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
size = size || _size || Math.min(canvas.width, canvas.height);
|
||||||
|
|
||||||
|
var frame = genframe(string),
|
||||||
|
ctx = canvas.ctx,
|
||||||
|
px = Math.round(size / (width + 8));
|
||||||
|
|
||||||
|
var roundedSize = px * (width + 8),
|
||||||
|
offset = Math.floor((size - roundedSize) / 2);
|
||||||
|
|
||||||
|
size = roundedSize;
|
||||||
|
|
||||||
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
|
ctx.setFillStyle('#000000');
|
||||||
|
|
||||||
|
for (var i = 0; i < width; i++) {
|
||||||
|
for (var j = 0; j < width; j++) {
|
||||||
|
if (frame[j * width + i]) {
|
||||||
|
ctx.fillRect(px * (4 + i) + offset, px * (4 + j) + offset, px, px);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ctx.draw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
api: api
|
||||||
|
}
|
||||||
|
|
||||||
|
})()
|
@ -0,0 +1,91 @@
|
|||||||
|
const formatTime = date => {
|
||||||
|
const year = date.getFullYear()
|
||||||
|
const month = date.getMonth() + 1
|
||||||
|
const day = date.getDate()
|
||||||
|
const hour = date.getHours()
|
||||||
|
const minute = date.getMinutes()
|
||||||
|
const second = date.getSeconds()
|
||||||
|
|
||||||
|
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
|
||||||
|
}
|
||||||
|
|
||||||
|
const formatNumber = n => {
|
||||||
|
n = n.toString()
|
||||||
|
return n[1] ? n : '0' + n
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatTimeV(date, fmt) {
|
||||||
|
var o = {
|
||||||
|
"M+": date.getMonth() + 1, // 月份
|
||||||
|
"d+": date.getDate(), // 日
|
||||||
|
"h+": date.getHours(), // 小时
|
||||||
|
"m+": date.getMinutes(), // 分
|
||||||
|
"s+": date.getSeconds(), // 秒
|
||||||
|
"q+": Math.floor((date.getMonth() + 3) / 3), // 季度
|
||||||
|
"S": date.getMilliseconds()
|
||||||
|
// 毫秒
|
||||||
|
};
|
||||||
|
if (/(y+)/.test(fmt))
|
||||||
|
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
|
||||||
|
for (var k in o)
|
||||||
|
if (new RegExp("(" + k + ")").test(fmt))
|
||||||
|
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
|
||||||
|
return fmt;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setNavTitle4Color(title, color,bgColor) {
|
||||||
|
var bc = '#000000';
|
||||||
|
if (bgColor){
|
||||||
|
bc = bgColor;
|
||||||
|
}
|
||||||
|
wx.setNavigationBarColor({
|
||||||
|
frontColor: bc,
|
||||||
|
backgroundColor: color,
|
||||||
|
})
|
||||||
|
wx.setNavigationBarTitle({
|
||||||
|
title: title
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function showFailureMsg(error) {
|
||||||
|
var title = "系统繁忙!!!";
|
||||||
|
if (error.errMessage) {
|
||||||
|
title = error.errMessage
|
||||||
|
} else
|
||||||
|
if (error.message) {
|
||||||
|
title = error.message
|
||||||
|
}
|
||||||
|
wx.showToast({
|
||||||
|
title: title,
|
||||||
|
icon: "none"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function showSuccessMsg(msg) {
|
||||||
|
var title = "操作成功";
|
||||||
|
if (msg){
|
||||||
|
title = msg;
|
||||||
|
}
|
||||||
|
wx.showToast({
|
||||||
|
title: title
|
||||||
|
})
|
||||||
|
}
|
||||||
|
function showWaring(msg) {
|
||||||
|
var title = "警告";
|
||||||
|
if (msg) {
|
||||||
|
title = msg;
|
||||||
|
}
|
||||||
|
wx.showToast({
|
||||||
|
title: title,
|
||||||
|
icon: "none"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
formatTime: formatTime,
|
||||||
|
formatTimeV: formatTimeV,
|
||||||
|
setNavTitle4Color: setNavTitle4Color,
|
||||||
|
showFailureMsg: showFailureMsg,
|
||||||
|
showSuccessMsg: showSuccessMsg,
|
||||||
|
showWaring: showWaring
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
var postfix = '_deadtime';
|
||||||
|
|
||||||
|
/**存储数据 并指定有效时间 */
|
||||||
|
function put(k, v, t) {
|
||||||
|
// console.log(k);
|
||||||
|
wx.setStorageSync(k, v)
|
||||||
|
var seconds = parseInt(t);
|
||||||
|
if (seconds > 0) {
|
||||||
|
var timestamp = Date.parse(new Date());
|
||||||
|
timestamp = timestamp / 1000 + seconds;
|
||||||
|
// console.log(timestamp);
|
||||||
|
wx.setStorageSync(k + postfix, timestamp + "")
|
||||||
|
} else {
|
||||||
|
wx.removeStorageSync(k + postfix)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**获取数据 1:校验是否在有效期 2:true 返回指定keyvalue 否则返回 def*/
|
||||||
|
function get(k, def) {
|
||||||
|
var deadtime = parseInt(wx.getStorageSync(k + postfix))
|
||||||
|
if (deadtime) {
|
||||||
|
if (parseInt(deadtime) < Date.parse(new Date()) / 1000) {
|
||||||
|
if (def) { return def; } else { return; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var res = wx.getStorageSync(k);
|
||||||
|
if (res) {
|
||||||
|
return res;
|
||||||
|
} else {
|
||||||
|
return def;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**删除指定key 以及指定key有效时间缓存 */
|
||||||
|
function remove(k) {
|
||||||
|
wx.removeStorageSync(k);
|
||||||
|
wx.removeStorageSync(k + postfix);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**清空缓存 */
|
||||||
|
function clear() {
|
||||||
|
wx.clearStorageSync();
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
put: put,
|
||||||
|
get: get,
|
||||||
|
remove: remove,
|
||||||
|
clear: clear,
|
||||||
|
}
|
@ -0,0 +1,308 @@
|
|||||||
|
/**
|
||||||
|
* html2Json 改造来自: https://github.com/Jxck/html2json
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* author: Di (微信小程序开发工程师)
|
||||||
|
* organization: WeAppDev(微信小程序开发论坛)(http://weappdev.com)
|
||||||
|
* 垂直微信小程序开发交流社区
|
||||||
|
*
|
||||||
|
* github地址: https://github.com/icindy/wxParse
|
||||||
|
*
|
||||||
|
* for: 微信小程序富文本解析
|
||||||
|
* detail : http://weappdev.com/t/wxparse-alpha0-1-html-markdown/184
|
||||||
|
*/
|
||||||
|
|
||||||
|
var __placeImgeUrlHttps = "https";
|
||||||
|
var __emojisReg = '';
|
||||||
|
var __emojisBaseSrc = '';
|
||||||
|
var __emojis = {};
|
||||||
|
var wxDiscode = require('./wxDiscode.js');
|
||||||
|
var HTMLParser = require('./htmlparser.js');
|
||||||
|
// Empty Elements - HTML 5
|
||||||
|
var empty = makeMap("area,base,basefont,br,col,frame,hr,img,input,link,meta,param,embed,command,keygen,source,track,wbr");
|
||||||
|
// Block Elements - HTML 5
|
||||||
|
var block = makeMap("br,a,code,address,article,applet,aside,audio,blockquote,button,canvas,center,dd,del,dir,div,dl,dt,fieldset,figcaption,figure,footer,form,frameset,h1,h2,h3,h4,h5,h6,header,hgroup,hr,iframe,ins,isindex,li,map,menu,noframes,noscript,object,ol,output,p,pre,section,script,table,tbody,td,tfoot,th,thead,tr,ul,video");
|
||||||
|
|
||||||
|
// Inline Elements - HTML 5
|
||||||
|
var inline = makeMap("abbr,acronym,applet,b,basefont,bdo,big,button,cite,del,dfn,em,font,i,iframe,img,input,ins,kbd,label,map,object,q,s,samp,script,select,small,span,strike,strong,sub,sup,textarea,tt,u,var");
|
||||||
|
|
||||||
|
// Elements that you can, intentionally, leave open
|
||||||
|
// (and which close themselves)
|
||||||
|
var closeSelf = makeMap("colgroup,dd,dt,li,options,p,td,tfoot,th,thead,tr");
|
||||||
|
|
||||||
|
// Attributes that have their values filled in disabled="disabled"
|
||||||
|
var fillAttrs = makeMap("checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected");
|
||||||
|
|
||||||
|
// Special Elements (can contain anything)
|
||||||
|
var special = makeMap("wxxxcode-style,script,style,view,scroll-view,block");
|
||||||
|
function makeMap(str) {
|
||||||
|
var obj = {}, items = str.split(",");
|
||||||
|
for (var i = 0; i < items.length; i++)
|
||||||
|
obj[items[i]] = true;
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
function q(v) {
|
||||||
|
return '"' + v + '"';
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeDOCTYPE(html) {
|
||||||
|
return html
|
||||||
|
.replace(/<\?xml.*\?>\n/, '')
|
||||||
|
.replace(/<.*!doctype.*\>\n/, '')
|
||||||
|
.replace(/<.*!DOCTYPE.*\>\n/, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
function trimHtml(html) {
|
||||||
|
return html
|
||||||
|
.replace(/\r?\n+/g, '')
|
||||||
|
.replace(/<!--.*?-->/ig, '')
|
||||||
|
.replace(/\/\*.*?\*\//ig, '')
|
||||||
|
.replace(/[ ]+</ig, '<')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function html2json(html, bindName) {
|
||||||
|
//处理字符串
|
||||||
|
html = removeDOCTYPE(html);
|
||||||
|
html = trimHtml(html);
|
||||||
|
html = wxDiscode.strDiscode(html);
|
||||||
|
//生成node节点
|
||||||
|
var bufArray = [];
|
||||||
|
var results = {
|
||||||
|
node: bindName,
|
||||||
|
nodes: [],
|
||||||
|
images:[],
|
||||||
|
imageUrls:[]
|
||||||
|
};
|
||||||
|
var index = 0;
|
||||||
|
HTMLParser(html, {
|
||||||
|
start: function (tag, attrs, unary) {
|
||||||
|
//debug(tag, attrs, unary);
|
||||||
|
// node for this element
|
||||||
|
var node = {
|
||||||
|
node: 'element',
|
||||||
|
tag: tag,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (bufArray.length === 0) {
|
||||||
|
node.index = index.toString()
|
||||||
|
index += 1
|
||||||
|
} else {
|
||||||
|
var parent = bufArray[0];
|
||||||
|
if (parent.nodes === undefined) {
|
||||||
|
parent.nodes = [];
|
||||||
|
}
|
||||||
|
node.index = parent.index + '.' + parent.nodes.length
|
||||||
|
}
|
||||||
|
|
||||||
|
if (block[tag]) {
|
||||||
|
node.tagType = "block";
|
||||||
|
} else if (inline[tag]) {
|
||||||
|
node.tagType = "inline";
|
||||||
|
} else if (closeSelf[tag]) {
|
||||||
|
node.tagType = "closeSelf";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (attrs.length !== 0) {
|
||||||
|
node.attr = attrs.reduce(function (pre, attr) {
|
||||||
|
var name = attr.name;
|
||||||
|
var value = attr.value;
|
||||||
|
if (name == 'class') {
|
||||||
|
console.dir(value);
|
||||||
|
// value = value.join("")
|
||||||
|
node.classStr = value;
|
||||||
|
}
|
||||||
|
// has multi attibutes
|
||||||
|
// make it array of attribute
|
||||||
|
if (name == 'style') {
|
||||||
|
console.dir(value);
|
||||||
|
// value = value.join("")
|
||||||
|
node.styleStr = value;
|
||||||
|
}
|
||||||
|
if (name == 'click') {
|
||||||
|
console.dir(value);
|
||||||
|
console.error(" click ")
|
||||||
|
node.clickStr = value;
|
||||||
|
}
|
||||||
|
if (value.match(/ /)) {
|
||||||
|
value = value.split(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// if attr already exists
|
||||||
|
// merge it
|
||||||
|
if (pre[name]) {
|
||||||
|
if (Array.isArray(pre[name])) {
|
||||||
|
// already array, push to last
|
||||||
|
pre[name].push(value);
|
||||||
|
} else {
|
||||||
|
// single value, make it array
|
||||||
|
pre[name] = [pre[name], value];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// not exist, put it
|
||||||
|
pre[name] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pre;
|
||||||
|
}, {});
|
||||||
|
}
|
||||||
|
|
||||||
|
//对img添加额外数据
|
||||||
|
if (node.tag === 'img') {
|
||||||
|
node.imgIndex = results.images.length;
|
||||||
|
var imgUrl = node.attr.src;
|
||||||
|
if (imgUrl[0] == '') {
|
||||||
|
imgUrl.splice(0, 1);
|
||||||
|
}
|
||||||
|
imgUrl = wxDiscode.urlToHttpUrl(imgUrl, __placeImgeUrlHttps);
|
||||||
|
node.attr.src = imgUrl;
|
||||||
|
node.from = bindName;
|
||||||
|
results.images.push(node);
|
||||||
|
results.imageUrls.push(imgUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理font标签样式属性
|
||||||
|
if (node.tag === 'font') {
|
||||||
|
var fontSize = ['x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', '-webkit-xxx-large'];
|
||||||
|
var styleAttrs = {
|
||||||
|
'color': 'color',
|
||||||
|
'face': 'font-family',
|
||||||
|
'size': 'font-size'
|
||||||
|
};
|
||||||
|
if (!node.attr.style) node.attr.style = [];
|
||||||
|
if (!node.styleStr) node.styleStr = '';
|
||||||
|
for (var key in styleAttrs) {
|
||||||
|
if (node.attr[key]) {
|
||||||
|
var value = key === 'size' ? fontSize[node.attr[key]-1] : node.attr[key];
|
||||||
|
node.attr.style.push(styleAttrs[key]);
|
||||||
|
node.attr.style.push(value);
|
||||||
|
node.styleStr += styleAttrs[key] + ': ' + value + ';';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//临时记录source资源
|
||||||
|
if(node.tag === 'source'){
|
||||||
|
results.source = node.attr.src;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (unary) {
|
||||||
|
// if this tag doesn't have end tag
|
||||||
|
// like <img src="hoge.png"/>
|
||||||
|
// add to parents
|
||||||
|
var parent = bufArray[0] || results;
|
||||||
|
if (parent.nodes === undefined) {
|
||||||
|
parent.nodes = [];
|
||||||
|
}
|
||||||
|
parent.nodes.push(node);
|
||||||
|
} else {
|
||||||
|
bufArray.unshift(node);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
end: function (tag) {
|
||||||
|
//debug(tag);
|
||||||
|
// merge into parent tag
|
||||||
|
var node = bufArray.shift();
|
||||||
|
if (node.tag !== tag) console.error('invalid state: mismatch end tag');
|
||||||
|
|
||||||
|
//当有缓存source资源时于于video补上src资源
|
||||||
|
if(node.tag === 'video' && results.source){
|
||||||
|
node.attr.src = results.source;
|
||||||
|
delete results.source;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bufArray.length === 0) {
|
||||||
|
results.nodes.push(node);
|
||||||
|
} else {
|
||||||
|
var parent = bufArray[0];
|
||||||
|
if (parent.nodes === undefined) {
|
||||||
|
parent.nodes = [];
|
||||||
|
}
|
||||||
|
parent.nodes.push(node);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
chars: function (text) {
|
||||||
|
//debug(text);
|
||||||
|
var node = {
|
||||||
|
node: 'text',
|
||||||
|
text: text,
|
||||||
|
textArray:transEmojiStr(text)
|
||||||
|
};
|
||||||
|
|
||||||
|
if (bufArray.length === 0) {
|
||||||
|
node.index = index.toString()
|
||||||
|
index += 1
|
||||||
|
results.nodes.push(node);
|
||||||
|
} else {
|
||||||
|
var parent = bufArray[0];
|
||||||
|
if (parent.nodes === undefined) {
|
||||||
|
parent.nodes = [];
|
||||||
|
}
|
||||||
|
node.index = parent.index + '.' + parent.nodes.length
|
||||||
|
parent.nodes.push(node);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
comment: function (text) {
|
||||||
|
//debug(text);
|
||||||
|
// var node = {
|
||||||
|
// node: 'comment',
|
||||||
|
// text: text,
|
||||||
|
// };
|
||||||
|
// var parent = bufArray[0];
|
||||||
|
// if (parent.nodes === undefined) {
|
||||||
|
// parent.nodes = [];
|
||||||
|
// }
|
||||||
|
// parent.nodes.push(node);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return results;
|
||||||
|
};
|
||||||
|
|
||||||
|
function transEmojiStr(str){
|
||||||
|
// var eReg = new RegExp("["+__reg+' '+"]");
|
||||||
|
// str = str.replace(/\[([^\[\]]+)\]/g,':$1:')
|
||||||
|
|
||||||
|
var emojiObjs = [];
|
||||||
|
//如果正则表达式为空
|
||||||
|
if(__emojisReg.length == 0 || !__emojis){
|
||||||
|
var emojiObj = {}
|
||||||
|
emojiObj.node = "text";
|
||||||
|
emojiObj.text = str;
|
||||||
|
array = [emojiObj];
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
//这个地方需要调整
|
||||||
|
str = str.replace(/\[([^\[\]]+)\]/g,':$1:')
|
||||||
|
var eReg = new RegExp("[:]");
|
||||||
|
var array = str.split(eReg);
|
||||||
|
for(var i = 0; i < array.length; i++){
|
||||||
|
var ele = array[i];
|
||||||
|
var emojiObj = {};
|
||||||
|
if(__emojis[ele]){
|
||||||
|
emojiObj.node = "element";
|
||||||
|
emojiObj.tag = "emoji";
|
||||||
|
emojiObj.text = __emojis[ele];
|
||||||
|
emojiObj.baseSrc= __emojisBaseSrc;
|
||||||
|
}else{
|
||||||
|
emojiObj.node = "text";
|
||||||
|
emojiObj.text = ele;
|
||||||
|
}
|
||||||
|
emojiObjs.push(emojiObj);
|
||||||
|
}
|
||||||
|
|
||||||
|
return emojiObjs;
|
||||||
|
}
|
||||||
|
|
||||||
|
function emojisInit(reg='',baseSrc="/wxParse/emojis/",emojis){
|
||||||
|
__emojisReg = reg;
|
||||||
|
__emojisBaseSrc=baseSrc;
|
||||||
|
__emojis=emojis;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
html2json: html2json,
|
||||||
|
emojisInit:emojisInit
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,192 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* htmlParser改造自: https://github.com/blowsie/Pure-JavaScript-HTML5-Parser
|
||||||
|
*
|
||||||
|
* author: Di (微信小程序开发工程师)
|
||||||
|
* organization: WeAppDev(微信小程序开发论坛)(http://weappdev.com)
|
||||||
|
* 垂直微信小程序开发交流社区
|
||||||
|
*
|
||||||
|
* github地址: https://github.com/icindy/wxParse
|
||||||
|
*
|
||||||
|
* for: 微信小程序富文本解析
|
||||||
|
* detail : http://weappdev.com/t/wxparse-alpha0-1-html-markdown/184
|
||||||
|
*/
|
||||||
|
// Regular Expressions for parsing tags and attributes
|
||||||
|
var startTag = /^<([-A-Za-z0-9_]+)((?:\s+[a-zA-Z_:][-a-zA-Z0-9_:.]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/,
|
||||||
|
endTag = /^<\/([-A-Za-z0-9_]+)[^>]*>/,
|
||||||
|
attr = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g;
|
||||||
|
|
||||||
|
// Empty Elements - HTML 5
|
||||||
|
var empty = makeMap("area,base,basefont,br,col,frame,hr,img,input,link,meta,param,embed,command,keygen,source,track,wbr");
|
||||||
|
|
||||||
|
// Block Elements - HTML 5
|
||||||
|
var block = makeMap("a,address,code,article,applet,aside,audio,blockquote,button,canvas,center,dd,del,dir,div,dl,dt,fieldset,figcaption,figure,footer,form,frameset,h1,h2,h3,h4,h5,h6,header,hgroup,hr,iframe,ins,isindex,li,map,menu,noframes,noscript,object,ol,output,p,pre,section,script,table,tbody,td,tfoot,th,thead,tr,ul,video");
|
||||||
|
|
||||||
|
// Inline Elements - HTML 5
|
||||||
|
var inline = makeMap("abbr,acronym,applet,b,basefont,bdo,big,br,button,cite,del,dfn,em,font,i,iframe,img,input,ins,kbd,label,map,object,q,s,samp,script,select,small,span,strike,strong,sub,sup,textarea,tt,u,var");
|
||||||
|
|
||||||
|
// Elements that you can, intentionally, leave open
|
||||||
|
// (and which close themselves)
|
||||||
|
var closeSelf = makeMap("colgroup,dd,dt,li,options,p,td,tfoot,th,thead,tr");
|
||||||
|
|
||||||
|
// Attributes that have their values filled in disabled="disabled"
|
||||||
|
var fillAttrs = makeMap("checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected");
|
||||||
|
|
||||||
|
// Special Elements (can contain anything)
|
||||||
|
var special = makeMap("wxxxcode-style,script,style,view,scroll-view,block");
|
||||||
|
|
||||||
|
function HTMLParser(html, handler) {
|
||||||
|
var index, chars, match, stack = [], last = html;
|
||||||
|
stack.last = function () {
|
||||||
|
return this[this.length - 1];
|
||||||
|
};
|
||||||
|
|
||||||
|
while (html) {
|
||||||
|
chars = true;
|
||||||
|
|
||||||
|
// Make sure we're not in a script or style element
|
||||||
|
if (!stack.last() || !special[stack.last()]) {
|
||||||
|
|
||||||
|
// Comment
|
||||||
|
if (html.indexOf("<!--") == 0) {
|
||||||
|
index = html.indexOf("-->");
|
||||||
|
|
||||||
|
if (index >= 0) {
|
||||||
|
if (handler.comment)
|
||||||
|
handler.comment(html.substring(4, index));
|
||||||
|
html = html.substring(index + 3);
|
||||||
|
chars = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// end tag
|
||||||
|
} else if (html.indexOf("</") == 0) {
|
||||||
|
match = html.match(endTag);
|
||||||
|
|
||||||
|
if (match) {
|
||||||
|
html = html.substring(match[0].length);
|
||||||
|
match[0].replace(endTag, parseEndTag);
|
||||||
|
chars = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// start tag
|
||||||
|
} else if (html.indexOf("<") == 0) {
|
||||||
|
match = html.match(startTag);
|
||||||
|
|
||||||
|
if (match) {
|
||||||
|
html = html.substring(match[0].length);
|
||||||
|
match[0].replace(startTag, parseStartTag);
|
||||||
|
chars = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (chars) {
|
||||||
|
index = html.indexOf("<");
|
||||||
|
var text = ''
|
||||||
|
while (index === 0) {
|
||||||
|
text += "<";
|
||||||
|
html = html.substring(1);
|
||||||
|
index = html.indexOf("<");
|
||||||
|
}
|
||||||
|
text += index < 0 ? html : html.substring(0, index);
|
||||||
|
html = index < 0 ? "" : html.substring(index);
|
||||||
|
|
||||||
|
if (handler.chars)
|
||||||
|
handler.chars(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
html = html.replace(new RegExp("([\\s\\S]*?)<\/" + stack.last() + "[^>]*>"), function (all, text) {
|
||||||
|
text = text.replace(/<!--([\s\S]*?)-->|<!\[CDATA\[([\s\S]*?)]]>/g, "$1$2");
|
||||||
|
if (handler.chars)
|
||||||
|
handler.chars(text);
|
||||||
|
|
||||||
|
return "";
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
parseEndTag("", stack.last());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (html == last)
|
||||||
|
throw "Parse Error: " + html;
|
||||||
|
last = html;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up any remaining tags
|
||||||
|
parseEndTag();
|
||||||
|
|
||||||
|
function parseStartTag(tag, tagName, rest, unary) {
|
||||||
|
tagName = tagName.toLowerCase();
|
||||||
|
|
||||||
|
if (block[tagName]) {
|
||||||
|
while (stack.last() && inline[stack.last()]) {
|
||||||
|
parseEndTag("", stack.last());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (closeSelf[tagName] && stack.last() == tagName) {
|
||||||
|
parseEndTag("", tagName);
|
||||||
|
}
|
||||||
|
|
||||||
|
unary = empty[tagName] || !!unary;
|
||||||
|
|
||||||
|
if (!unary)
|
||||||
|
stack.push(tagName);
|
||||||
|
|
||||||
|
if (handler.start) {
|
||||||
|
var attrs = [];
|
||||||
|
|
||||||
|
rest.replace(attr, function (match, name) {
|
||||||
|
var value = arguments[2] ? arguments[2] :
|
||||||
|
arguments[3] ? arguments[3] :
|
||||||
|
arguments[4] ? arguments[4] :
|
||||||
|
fillAttrs[name] ? name : "";
|
||||||
|
|
||||||
|
attrs.push({
|
||||||
|
name: name,
|
||||||
|
value: value,
|
||||||
|
escaped: value.replace(/(^|[^\\])"/g, '$1\\\"') //"
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
if (handler.start) {
|
||||||
|
handler.start(tagName, attrs, unary);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseEndTag(tag, tagName) {
|
||||||
|
// If no tag name is provided, clean shop
|
||||||
|
if (!tagName)
|
||||||
|
var pos = 0;
|
||||||
|
|
||||||
|
// Find the closest opened tag of the same type
|
||||||
|
else {
|
||||||
|
tagName = tagName.toLowerCase();
|
||||||
|
for (var pos = stack.length - 1; pos >= 0; pos--)
|
||||||
|
if (stack[pos] == tagName)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (pos >= 0) {
|
||||||
|
// Close all the open elements, up the stack
|
||||||
|
for (var i = stack.length - 1; i >= pos; i--)
|
||||||
|
if (handler.end)
|
||||||
|
handler.end(stack[i]);
|
||||||
|
|
||||||
|
// Remove the open elements from the stack
|
||||||
|
stack.length = pos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
function makeMap(str) {
|
||||||
|
var obj = {}, items = str.split(",");
|
||||||
|
for (var i = 0; i < items.length; i++)
|
||||||
|
obj[items[i]] = true;
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = HTMLParser;
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"usingComponents": {}
|
||||||
|
}
|
@ -0,0 +1,206 @@
|
|||||||
|
|
||||||
|
/**
|
||||||
|
* author: Di (微信小程序开发工程师)
|
||||||
|
* organization: WeAppDev(微信小程序开发论坛)(http://weappdev.com)
|
||||||
|
* 垂直微信小程序开发交流社区
|
||||||
|
*
|
||||||
|
* github地址: https://github.com/icindy/wxParse
|
||||||
|
*
|
||||||
|
* for: 微信小程序富文本解析
|
||||||
|
* detail : http://weappdev.com/t/wxparse-alpha0-1-html-markdown/184
|
||||||
|
*/
|
||||||
|
|
||||||
|
.wxParse{
|
||||||
|
margin: 0 5px;
|
||||||
|
font-family: Helvetica,sans-serif;
|
||||||
|
font-size: 28rpx;
|
||||||
|
color: #666;
|
||||||
|
line-height: 1.8;
|
||||||
|
}
|
||||||
|
view{
|
||||||
|
/* word-break:break-all; overflow:auto; */
|
||||||
|
}
|
||||||
|
.wxParse-inline{
|
||||||
|
display: inline;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
/*//标题 */
|
||||||
|
.wxParse-div{margin: 0;padding: 0;}
|
||||||
|
.wxParse-h1{ font-size:2em; margin: .67em 0 }
|
||||||
|
.wxParse-h2{ font-size:1.5em; margin: .75em 0 }
|
||||||
|
.wxParse-h3{ font-size:1.17em; margin: .83em 0 }
|
||||||
|
.wxParse-h4{ margin: 1.12em 0}
|
||||||
|
.wxParse-h5 { font-size:.83em; margin: 1.5em 0 }
|
||||||
|
.wxParse-h6{ font-size:.75em; margin: 1.67em 0 }
|
||||||
|
|
||||||
|
.wxParse-h1 {
|
||||||
|
font-size: 18px;
|
||||||
|
font-weight: 400;
|
||||||
|
margin-bottom: .9em;
|
||||||
|
}
|
||||||
|
.wxParse-h2 {
|
||||||
|
font-size: 16px;
|
||||||
|
font-weight: 400;
|
||||||
|
margin-bottom: .34em;
|
||||||
|
}
|
||||||
|
.wxParse-h3 {
|
||||||
|
font-weight: 400;
|
||||||
|
font-size: 15px;
|
||||||
|
margin-bottom: .34em;
|
||||||
|
}
|
||||||
|
.wxParse-h4 {
|
||||||
|
font-weight: 400;
|
||||||
|
font-size: 14px;
|
||||||
|
margin-bottom: .24em;
|
||||||
|
}
|
||||||
|
.wxParse-h5 {
|
||||||
|
font-weight: 400;
|
||||||
|
font-size: 13px;
|
||||||
|
margin-bottom: .14em;
|
||||||
|
}
|
||||||
|
.wxParse-h6 {
|
||||||
|
font-weight: 400;
|
||||||
|
font-size: 12px;
|
||||||
|
margin-bottom: .04em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wxParse-h1, .wxParse-h2, .wxParse-h3, .wxParse-h4, .wxParse-h5, .wxParse-h6, .wxParse-b, .wxParse-strong { font-weight: bolder }
|
||||||
|
|
||||||
|
.wxParse-i,.wxParse-cite,.wxParse-em,.wxParse-var,.wxParse-address{font-style:italic}
|
||||||
|
.wxParse-pre,.wxParse-tt,.wxParse-code,.wxParse-kbd,.wxParse-samp{font-family:monospace}
|
||||||
|
.wxParse-pre{white-space:pre}
|
||||||
|
.wxParse-big{font-size:1.17em}
|
||||||
|
.wxParse-small,.wxParse-sub,.wxParse-sup{font-size:.83em}
|
||||||
|
.wxParse-sub{vertical-align:sub}
|
||||||
|
.wxParse-sup{vertical-align:super}
|
||||||
|
.wxParse-s,.wxParse-strike,.wxParse-del{text-decoration:line-through}
|
||||||
|
/*wxparse-自定义个性化的css样式*/
|
||||||
|
/*增加video的css样式*/
|
||||||
|
.wxParse-strong,.wxParse-s{display: inline}
|
||||||
|
.wxParse-a{
|
||||||
|
color: deepskyblue;
|
||||||
|
word-break:break-all;
|
||||||
|
overflow:auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wxParse-video{
|
||||||
|
text-align: center;
|
||||||
|
margin: 10px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wxParse-video-video{
|
||||||
|
width:100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wxParse-img{
|
||||||
|
/*background-color: #efefef;*/
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wxParse-blockquote {
|
||||||
|
margin: 0;
|
||||||
|
padding:10px 0 10px 5px;
|
||||||
|
font-family:Courier, Calibri,"宋体";
|
||||||
|
background:#f5f5f5;
|
||||||
|
border-left: 3px solid #dbdbdb;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wxParse-code,.wxParse-wxxxcode-style{
|
||||||
|
display: inline;
|
||||||
|
background:#f5f5f5;
|
||||||
|
}
|
||||||
|
.wxParse-ul{
|
||||||
|
margin: 20rpx 10rpx;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wxParse-li,.wxParse-li-inner{
|
||||||
|
display: flex;
|
||||||
|
align-items: baseline;
|
||||||
|
margin: 10rpx 0;
|
||||||
|
}
|
||||||
|
.wxParse-li-text{
|
||||||
|
|
||||||
|
align-items: center;
|
||||||
|
line-height: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wxParse-li-circle{
|
||||||
|
display: inline-flex;
|
||||||
|
width: 5px;
|
||||||
|
height: 5px;
|
||||||
|
background-color: #333;
|
||||||
|
margin-right: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wxParse-li-square{
|
||||||
|
display: inline-flex;
|
||||||
|
width: 10rpx;
|
||||||
|
height: 10rpx;
|
||||||
|
background-color: #333;
|
||||||
|
margin-right: 5px;
|
||||||
|
}
|
||||||
|
.wxParse-li-ring{
|
||||||
|
display: inline-flex;
|
||||||
|
width: 10rpx;
|
||||||
|
height: 10rpx;
|
||||||
|
border: 2rpx solid #333;
|
||||||
|
border-radius: 50%;
|
||||||
|
background-color: #fff;
|
||||||
|
margin-right: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*.wxParse-table{
|
||||||
|
width: 100%;
|
||||||
|
height: 400px;
|
||||||
|
}
|
||||||
|
.wxParse-thead,.wxParse-tfoot,.wxParse-tr{
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
}
|
||||||
|
.wxParse-th,.wxParse-td{
|
||||||
|
display: flex;
|
||||||
|
width: 580px;
|
||||||
|
overflow: auto;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
.wxParse-u {
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
.wxParse-hide{
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
.WxEmojiView{
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
.wxEmoji{
|
||||||
|
width: 16px;
|
||||||
|
height:16px;
|
||||||
|
}
|
||||||
|
.wxParse-tr{
|
||||||
|
display: flex;
|
||||||
|
border-right:1px solid #e0e0e0;
|
||||||
|
border-bottom:1px solid #e0e0e0;
|
||||||
|
border-top:1px solid #e0e0e0;
|
||||||
|
}
|
||||||
|
.wxParse-th,
|
||||||
|
.wxParse-td{
|
||||||
|
flex:1;
|
||||||
|
padding:5px;
|
||||||
|
font-size:28rpx;
|
||||||
|
border-left:1px solid #e0e0e0;
|
||||||
|
word-break: break-all;
|
||||||
|
}
|
||||||
|
.wxParse-td:last{
|
||||||
|
border-top:1px solid #e0e0e0;
|
||||||
|
}
|
||||||
|
.wxParse-th{
|
||||||
|
background:#f0f0f0;
|
||||||
|
border-top:1px solid #e0e0e0;
|
||||||
|
}
|
||||||
|
.wxParse-del{
|
||||||
|
display: inline;
|
||||||
|
}
|
||||||
|
.wxParse-figure {
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
var Promise = require('../plugins/es6-promise.js')
|
||||||
|
function wxPromisify(fn) {
|
||||||
|
return function (obj = {}) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
obj.success = function (res) {
|
||||||
|
//成功
|
||||||
|
resolve(res)
|
||||||
|
}
|
||||||
|
obj.fail = function (res) {
|
||||||
|
//失败
|
||||||
|
reject(res)
|
||||||
|
}
|
||||||
|
fn(obj)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//无论promise对象最后状态如何都会执行
|
||||||
|
Promise.prototype.finally = function (callback) {
|
||||||
|
let P = this.constructor;
|
||||||
|
return this.then(
|
||||||
|
value => P.resolve(callback()).then(() => value),
|
||||||
|
reason => P.resolve(callback()).then(() => { throw reason })
|
||||||
|
);
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* 微信请求get方法
|
||||||
|
* url
|
||||||
|
* data 以对象的格式传入
|
||||||
|
*/
|
||||||
|
function getRequest(url, data) {
|
||||||
|
var getRequest = wxPromisify(wx.request)
|
||||||
|
return getRequest({
|
||||||
|
url: url,
|
||||||
|
method: 'GET',
|
||||||
|
data: data,
|
||||||
|
header: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信请求post方法封装
|
||||||
|
* url
|
||||||
|
* data 以对象的格式传入
|
||||||
|
*/
|
||||||
|
function postRequest(url, data) {
|
||||||
|
var postRequest = wxPromisify(wx.request)
|
||||||
|
return postRequest({
|
||||||
|
url: url,
|
||||||
|
method: 'POST',
|
||||||
|
data: data,
|
||||||
|
header: {
|
||||||
|
"content-type": "application/x-www-form-urlencoded"
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
postRequest: postRequest,
|
||||||
|
getRequest: getRequest
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
var barcode = require('./barcode');
|
||||||
|
var qrcode = require('./qrcode');
|
||||||
|
|
||||||
|
function convert_length(length) {
|
||||||
|
return Math.round(wx.getSystemInfoSync().windowWidth * length / 750);
|
||||||
|
}
|
||||||
|
|
||||||
|
function barc(id, code, width, height) {
|
||||||
|
barcode.code128(wx.createCanvasContext(id), code, convert_length(width), convert_length(height))
|
||||||
|
}
|
||||||
|
|
||||||
|
function qrc(id, code, width, height) {
|
||||||
|
qrcode.api.draw(code, {
|
||||||
|
ctx: wx.createCanvasContext(id),
|
||||||
|
width: convert_length(width),
|
||||||
|
height: convert_length(height)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
barcode: barc,
|
||||||
|
qrcode: qrc
|
||||||
|
}
|
Loading…
Reference in New Issue