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 };