zs.sdk.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. var conf = require("./zs.sdk.conf");
  2. var sdk = (function() {
  3. var self = {};
  4. var adUrl = "https://ad.ali-yun.wang/api/";
  5. /**
  6. * ********************************************************
  7. */
  8. var rotateLeft = function (lValue, iShiftBits) {
  9. return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
  10. }
  11. var addUnsigned = function (lX, lY) {
  12. var lX4, lY4, lX8, lY8, lResult;
  13. lX8 = (lX & 0x80000000);
  14. lY8 = (lY & 0x80000000);
  15. lX4 = (lX & 0x40000000);
  16. lY4 = (lY & 0x40000000);
  17. lResult = (lX & 0x3FFFFFFF) + (lY & 0x3FFFFFFF);
  18. if (lX4 & lY4) return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
  19. if (lX4 | lY4) {
  20. if (lResult & 0x40000000) return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
  21. else return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
  22. } else {
  23. return (lResult ^ lX8 ^ lY8);
  24. }
  25. }
  26. var F = function (x, y, z) {
  27. return (x & y) | ((~x) & z);
  28. }
  29. var G = function (x, y, z) {
  30. return (x & z) | (y & (~z));
  31. }
  32. var H = function (x, y, z) {
  33. return (x ^ y ^ z);
  34. }
  35. var I = function (x, y, z) {
  36. return (y ^ (x | (~z)));
  37. }
  38. var FF = function (a, b, c, d, x, s, ac) {
  39. a = addUnsigned(a, addUnsigned(addUnsigned(F(b, c, d), x), ac));
  40. return addUnsigned(rotateLeft(a, s), b);
  41. };
  42. var GG = function (a, b, c, d, x, s, ac) {
  43. a = addUnsigned(a, addUnsigned(addUnsigned(G(b, c, d), x), ac));
  44. return addUnsigned(rotateLeft(a, s), b);
  45. };
  46. var HH = function (a, b, c, d, x, s, ac) {
  47. a = addUnsigned(a, addUnsigned(addUnsigned(H(b, c, d), x), ac));
  48. return addUnsigned(rotateLeft(a, s), b);
  49. };
  50. var II = function (a, b, c, d, x, s, ac) {
  51. a = addUnsigned(a, addUnsigned(addUnsigned(I(b, c, d), x), ac));
  52. return addUnsigned(rotateLeft(a, s), b);
  53. };
  54. var convertToWordArray = function (string) {
  55. var lWordCount;
  56. var lMessageLength = string.length;
  57. var lNumberOfWordsTempOne = lMessageLength + 8;
  58. var lNumberOfWordsTempTwo = (lNumberOfWordsTempOne - (lNumberOfWordsTempOne % 64)) / 64;
  59. var lNumberOfWords = (lNumberOfWordsTempTwo + 1) * 16;
  60. var lWordArray = Array(lNumberOfWords - 1);
  61. var lBytePosition = 0;
  62. var lByteCount = 0;
  63. while (lByteCount < lMessageLength) {
  64. lWordCount = (lByteCount - (lByteCount % 4)) / 4;
  65. lBytePosition = (lByteCount % 4) * 8;
  66. lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount) << lBytePosition));
  67. lByteCount++;
  68. }
  69. lWordCount = (lByteCount - (lByteCount % 4)) / 4;
  70. lBytePosition = (lByteCount % 4) * 8;
  71. lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80 << lBytePosition);
  72. lWordArray[lNumberOfWords - 2] = lMessageLength << 3;
  73. lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29;
  74. return lWordArray;
  75. };
  76. var wordToHex = function (lValue) {
  77. var WordToHexValue = "",
  78. WordToHexValueTemp = "",
  79. lByte, lCount;
  80. for (lCount = 0; lCount <= 3; lCount++) {
  81. lByte = (lValue >>> (lCount * 8)) & 255;
  82. WordToHexValueTemp = "0" + lByte.toString(16);
  83. WordToHexValue = WordToHexValue + WordToHexValueTemp.substr(WordToHexValueTemp.length - 2, 2);
  84. }
  85. return WordToHexValue;
  86. };
  87. var uTF8Encode = function (string) {
  88. string = string.replace(/\x0d\x0a/g, "\x0a");
  89. var output = "";
  90. for (var n = 0; n < string.length; n++) {
  91. var c = string.charCodeAt(n);
  92. if (c < 128) {
  93. output += String.fromCharCode(c);
  94. } else if ((c > 127) && (c < 2048)) {
  95. output += String.fromCharCode((c >> 6) | 192);
  96. output += String.fromCharCode((c & 63) | 128);
  97. } else {
  98. output += String.fromCharCode((c >> 12) | 224);
  99. output += String.fromCharCode(((c >> 6) & 63) | 128);
  100. output += String.fromCharCode((c & 63) | 128);
  101. }
  102. }
  103. return output;
  104. };
  105. var md5 = function(string) {
  106. var x = Array();
  107. var k, AA, BB, CC, DD, a, b, c, d;
  108. var S11 = 7,
  109. S12 = 12,
  110. S13 = 17,
  111. S14 = 22;
  112. var S21 = 5,
  113. S22 = 9,
  114. S23 = 14,
  115. S24 = 20;
  116. var S31 = 4,
  117. S32 = 11,
  118. S33 = 16,
  119. S34 = 23;
  120. var S41 = 6,
  121. S42 = 10,
  122. S43 = 15,
  123. S44 = 21;
  124. string = uTF8Encode(string);
  125. x = convertToWordArray(string);
  126. a = 0x67452301;
  127. b = 0xEFCDAB89;
  128. c = 0x98BADCFE;
  129. d = 0x10325476;
  130. for (k = 0; k < x.length; k += 16) {
  131. AA = a;
  132. BB = b;
  133. CC = c;
  134. DD = d;
  135. a = FF(a, b, c, d, x[k + 0], S11, 0xD76AA478);
  136. d = FF(d, a, b, c, x[k + 1], S12, 0xE8C7B756);
  137. c = FF(c, d, a, b, x[k + 2], S13, 0x242070DB);
  138. b = FF(b, c, d, a, x[k + 3], S14, 0xC1BDCEEE);
  139. a = FF(a, b, c, d, x[k + 4], S11, 0xF57C0FAF);
  140. d = FF(d, a, b, c, x[k + 5], S12, 0x4787C62A);
  141. c = FF(c, d, a, b, x[k + 6], S13, 0xA8304613);
  142. b = FF(b, c, d, a, x[k + 7], S14, 0xFD469501);
  143. a = FF(a, b, c, d, x[k + 8], S11, 0x698098D8);
  144. d = FF(d, a, b, c, x[k + 9], S12, 0x8B44F7AF);
  145. c = FF(c, d, a, b, x[k + 10], S13, 0xFFFF5BB1);
  146. b = FF(b, c, d, a, x[k + 11], S14, 0x895CD7BE);
  147. a = FF(a, b, c, d, x[k + 12], S11, 0x6B901122);
  148. d = FF(d, a, b, c, x[k + 13], S12, 0xFD987193);
  149. c = FF(c, d, a, b, x[k + 14], S13, 0xA679438E);
  150. b = FF(b, c, d, a, x[k + 15], S14, 0x49B40821);
  151. a = GG(a, b, c, d, x[k + 1], S21, 0xF61E2562);
  152. d = GG(d, a, b, c, x[k + 6], S22, 0xC040B340);
  153. c = GG(c, d, a, b, x[k + 11], S23, 0x265E5A51);
  154. b = GG(b, c, d, a, x[k + 0], S24, 0xE9B6C7AA);
  155. a = GG(a, b, c, d, x[k + 5], S21, 0xD62F105D);
  156. d = GG(d, a, b, c, x[k + 10], S22, 0x2441453);
  157. c = GG(c, d, a, b, x[k + 15], S23, 0xD8A1E681);
  158. b = GG(b, c, d, a, x[k + 4], S24, 0xE7D3FBC8);
  159. a = GG(a, b, c, d, x[k + 9], S21, 0x21E1CDE6);
  160. d = GG(d, a, b, c, x[k + 14], S22, 0xC33707D6);
  161. c = GG(c, d, a, b, x[k + 3], S23, 0xF4D50D87);
  162. b = GG(b, c, d, a, x[k + 8], S24, 0x455A14ED);
  163. a = GG(a, b, c, d, x[k + 13], S21, 0xA9E3E905);
  164. d = GG(d, a, b, c, x[k + 2], S22, 0xFCEFA3F8);
  165. c = GG(c, d, a, b, x[k + 7], S23, 0x676F02D9);
  166. b = GG(b, c, d, a, x[k + 12], S24, 0x8D2A4C8A);
  167. a = HH(a, b, c, d, x[k + 5], S31, 0xFFFA3942);
  168. d = HH(d, a, b, c, x[k + 8], S32, 0x8771F681);
  169. c = HH(c, d, a, b, x[k + 11], S33, 0x6D9D6122);
  170. b = HH(b, c, d, a, x[k + 14], S34, 0xFDE5380C);
  171. a = HH(a, b, c, d, x[k + 1], S31, 0xA4BEEA44);
  172. d = HH(d, a, b, c, x[k + 4], S32, 0x4BDECFA9);
  173. c = HH(c, d, a, b, x[k + 7], S33, 0xF6BB4B60);
  174. b = HH(b, c, d, a, x[k + 10], S34, 0xBEBFBC70);
  175. a = HH(a, b, c, d, x[k + 13], S31, 0x289B7EC6);
  176. d = HH(d, a, b, c, x[k + 0], S32, 0xEAA127FA);
  177. c = HH(c, d, a, b, x[k + 3], S33, 0xD4EF3085);
  178. b = HH(b, c, d, a, x[k + 6], S34, 0x4881D05);
  179. a = HH(a, b, c, d, x[k + 9], S31, 0xD9D4D039);
  180. d = HH(d, a, b, c, x[k + 12], S32, 0xE6DB99E5);
  181. c = HH(c, d, a, b, x[k + 15], S33, 0x1FA27CF8);
  182. b = HH(b, c, d, a, x[k + 2], S34, 0xC4AC5665);
  183. a = II(a, b, c, d, x[k + 0], S41, 0xF4292244);
  184. d = II(d, a, b, c, x[k + 7], S42, 0x432AFF97);
  185. c = II(c, d, a, b, x[k + 14], S43, 0xAB9423A7);
  186. b = II(b, c, d, a, x[k + 5], S44, 0xFC93A039);
  187. a = II(a, b, c, d, x[k + 12], S41, 0x655B59C3);
  188. d = II(d, a, b, c, x[k + 3], S42, 0x8F0CCC92);
  189. c = II(c, d, a, b, x[k + 10], S43, 0xFFEFF47D);
  190. b = II(b, c, d, a, x[k + 1], S44, 0x85845DD1);
  191. a = II(a, b, c, d, x[k + 8], S41, 0x6FA87E4F);
  192. d = II(d, a, b, c, x[k + 15], S42, 0xFE2CE6E0);
  193. c = II(c, d, a, b, x[k + 6], S43, 0xA3014314);
  194. b = II(b, c, d, a, x[k + 13], S44, 0x4E0811A1);
  195. a = II(a, b, c, d, x[k + 4], S41, 0xF7537E82);
  196. d = II(d, a, b, c, x[k + 11], S42, 0xBD3AF235);
  197. c = II(c, d, a, b, x[k + 2], S43, 0x2AD7D2BB);
  198. b = II(b, c, d, a, x[k + 9], S44, 0xEB86D391);
  199. a = addUnsigned(a, AA);
  200. b = addUnsigned(b, BB);
  201. c = addUnsigned(c, CC);
  202. d = addUnsigned(d, DD);
  203. }
  204. var tempValue = wordToHex(a) + wordToHex(b) + wordToHex(c) + wordToHex(d);
  205. return tempValue.toLowerCase();
  206. }
  207. /**
  208. * *******************************************************************************
  209. */
  210. var _object2Query = function(obj) {
  211. var args = []
  212. for (var k in obj)
  213. args.push(k + "=" + obj[k])
  214. return args.join("&"); // 返回对象
  215. }
  216. /**
  217. * var crypto = require('crypto');
  218. *
  219. * var params = {
  220. openid: openId,
  221. key: key,
  222. }
  223. * @param {参数} params 对象
  224. */
  225. var _buildSign = function(params, isSecret) {
  226. isSecret = isSecret || true;
  227. var sortedKeys = Object.keys(params).sort();
  228. var signParam = '';
  229. for (var i = 0; i < sortedKeys.length; i++) {
  230. signParam += sortedKeys[i] + ":" + params[sortedKeys[i]];
  231. }
  232. if (isSecret)
  233. signParam += conf.secret;
  234. // signParam = signParam.toLowerCase();
  235. var md5sign = md5(signParam);
  236. md5sign = md5sign.toLowerCase();
  237. return md5sign;
  238. }
  239. var request = function(url, data, method, success, fail, complete) {
  240. if (typeof wx === 'undefined') {
  241. var xhr = new XMLHttpRequest();
  242. xhr.onreadystatechange = function () {
  243. if (xhr.readyState == 4) {
  244. var response = xhr.responseText;
  245. if (xhr.status >= 200 && xhr.status < 400) {
  246. var result = {};
  247. try {
  248. result = JSON.parse(response)
  249. } catch (e) {
  250. console.error('json parse error ', response)
  251. if (fail)
  252. fail(e);
  253. }
  254. if (success)
  255. success(result);
  256. } else {
  257. console.error('error ', response)
  258. if (fail)
  259. fail(response);
  260. }
  261. } else {
  262. }
  263. };
  264. xhr.timeout = 3000;
  265. xhr.ontimeout = function (event) {
  266. console.error('error ', event)
  267. if (fail)
  268. fail(event);
  269. }
  270. xhr.open(method, url, true);
  271. if (method == "POST") {
  272. xhr.open('POST', url);
  273. xhr.setRequestHeader("Content-Type", 'application/x-www-form-urlencoded');
  274. xhr.send(_object2Query(data));
  275. } else {
  276. xhr.send();
  277. }
  278. } else {
  279. wx.request({
  280. url: url,
  281. data: data,
  282. header: {
  283. 'content-type': 'application/json'
  284. },
  285. method: method,
  286. success: function (res) {
  287. if (success)
  288. success(res.data);
  289. },
  290. fail: function (res) {
  291. if (fail)
  292. fail(res);
  293. },
  294. complete: function (res) {
  295. if (complete)
  296. complete(res);
  297. }
  298. })
  299. }
  300. }
  301. var postExportAppLog = function(toid, openid) {
  302. var url = adUrl + "appad_new/collect";
  303. var currentTime = Math.round(new Date().getTime() / 1000).toString();
  304. var signParams = {
  305. user_id: openid,
  306. from_id: conf.appId,
  307. to_id: toid,
  308. timestamp: currentTime,
  309. };
  310. var sign = _buildSign(signParams);
  311. var data = Object.assign({}, signParams, {
  312. sign: sign,
  313. });
  314. request(url, data, 'POST',
  315. function () { },
  316. function () {
  317. console.log('appad_new/collect fail');
  318. },
  319. function () {
  320. console.log('appad_new/collect complete');
  321. });
  322. }
  323. var setStorageSync = function(key, value) {
  324. self.zsStorage = self.zsStorage || {};
  325. self.zsStorage[key] = value;
  326. }
  327. var getStorageSync = function(key) {
  328. self.zsStorage = self.zsStorage || {};
  329. return self.zsStorage[key];
  330. }
  331. var getCache = function(key, expire) {
  332. if (!expire) {
  333. return getStorageSync(key);
  334. }
  335. else {
  336. var lastCacheTime = getStorageSync(key + "_time");
  337. if (lastCacheTime == null || Date.now() - Number(lastCacheTime) < expire) {
  338. return getStorageSync(key);
  339. }
  340. else {
  341. return null;
  342. }
  343. }
  344. }
  345. var setCache = function(key, value) {
  346. setStorageSync(key, value);
  347. setStorageSync(key + "_time", Date.now());
  348. }
  349. var isFun = function(fun) {
  350. if (typeof fun == 'function')
  351. return true
  352. return false
  353. }
  354. /**
  355. * 跳转成功之后数据上报
  356. * @param {number} row 从loadAd接口中返回的数组项 @example indexLeft[0]
  357. * @param {string} userid 小游戏中的用户Id
  358. */
  359. var collect = function (row, userid) {
  360. if (row.app_type == "3") {
  361. var value = getStorageSync(row.appid);
  362. if (value == null) {
  363. setStorageSync(row.appid, 1);
  364. }
  365. else {
  366. setStorageSync(row.appid, Number(value) + 1);
  367. }
  368. }
  369. if (typeof wx === 'undefined') {
  370. return ;
  371. }
  372. postExportAppLog(row.app_id, userid);
  373. }
  374. self.adCbList = [];
  375. self.inAdRequest = false;
  376. /**
  377. * 获取广告数据
  378. * @param {*} callback
  379. * @returns more 更多好玩 个人中心的广告 现已经不用了
  380. * promotion 首页推广 首页开始按钮下的广告
  381. * indexFloat 首页浮动广告 首页右上的广告
  382. * indexLeft 首页侧栏
  383. * gameFloat 游戏页浮动广告
  384. * endPage 结束页广告
  385. */
  386. self.loadAd = function (callback) {
  387. var cacheExpire = 1000 * 60 * 10;
  388. var cache = getCache("zsAd", cacheExpire);
  389. if (cache) {
  390. callback(cache);
  391. }
  392. else if (this.inAdRequest) {
  393. this.adCbList.push(callback);
  394. }
  395. else {
  396. this.inAdRequest = true;
  397. this.adCbList.push(callback);
  398. var url = adUrl + "appad_new/index";
  399. var currentTime = Math.round(new Date().getTime() / 1000).toString();
  400. var signParams = {
  401. appid: conf.appId,
  402. timestamp: currentTime,
  403. };
  404. var sign = _buildSign(signParams);
  405. var data = Object.assign({}, signParams, { sign:sign });
  406. request(url, data, 'POST',
  407. function (res) {
  408. self.inAdRequest = false;
  409. for (var z in res.data) {
  410. var arr = res.data[z];
  411. arr.sort(function() {return Math.random() > 0.5 ? 1 : -1;});
  412. }
  413. var retValue = {
  414. /**
  415. * 个人中心的广告 现已经不用了
  416. */
  417. more: res.data["position-1"] || [],
  418. /**
  419. * 首页开始按钮下的广告
  420. */
  421. promotion: res.data["position-2"] || [],
  422. /**
  423. * 首页右上的浮动广告
  424. */
  425. indexFloat: res.data["position-3"] || [],
  426. /**
  427. * 首页右上的浮动广告
  428. */
  429. banner: res.data["position-4"] || [],
  430. /**
  431. * 首页侧栏
  432. */
  433. indexLeft: res.data["position-7"] || [],
  434. /**
  435. * 游戏页浮动广告
  436. */
  437. gameFloat: res.data["position-8"] || [],
  438. /**
  439. * 结束页广告
  440. */
  441. endPage: res.data["position-9"] || [],
  442. /**
  443. * 首页左侧浮动广告
  444. */
  445. indexLeftFloat: res.data["position-11"] || [],
  446. /**
  447. * 返回页广告
  448. */
  449. backAd: res.data["position-12"] || [],
  450. /**
  451. * ios跳转列表
  452. */
  453. iosLinkAd: res.data["position-13"] || [],
  454. }
  455. setCache("zsAd", retValue);
  456. for (var index = 0; index < self.adCbList.length; index++) {
  457. if (isFun(self.adCbList[index]))self.adCbList[index](retValue);
  458. }
  459. self.adCbList = [];
  460. },
  461. function (error) {
  462. self.inAdRequest = false;
  463. console.log('requestAdData fail');
  464. var retValue = {
  465. /**
  466. * 个人中心的广告 现已经不用了
  467. */
  468. more: [],
  469. /**
  470. * 首页开始按钮下的广告
  471. */
  472. promotion: [],
  473. /**
  474. * 首页右上的浮动广告
  475. */
  476. indexFloat: [],
  477. /**
  478. * 首页右上的浮动广告
  479. */
  480. banner: [],
  481. /**
  482. * 首页侧栏
  483. */
  484. indexLeft: [],
  485. /**
  486. * 游戏页浮动广告
  487. */
  488. gameFloat: [],
  489. /**
  490. * 结束页广告
  491. */
  492. endPage: [],
  493. /**
  494. * 首页左侧浮动广告
  495. */
  496. indexLeftFloat: [],
  497. /**
  498. * 返回页广告
  499. */
  500. backAd: [],
  501. /**
  502. * ios跳转列表
  503. */
  504. iosLinkAd: [],
  505. }
  506. for (var index = 0; index < self.adCbList.length; index++) {
  507. if (isFun(self.adCbList[index]))self.adCbList[index](retValue);
  508. }
  509. self.adCbList = [];
  510. },
  511. function (res) {
  512. console.log('requestAdData complete');
  513. }
  514. );
  515. }
  516. }
  517. /**
  518. * 跳转小程序
  519. * @param {*} row 从loadAd接口中返回的数组项 @example indexLeft[0]
  520. * @param {*} openid 小游戏中的用户openid
  521. * @param {function} success 接口调用成功的回调函数
  522. * @param {function} fail 接口调用失败的回调函数
  523. * @param {function} complete 接口调用结束的回调函数(调用成功、失败都会执行)
  524. */
  525. self.navigate2Mini = function (row, openid, success, fail, complete) {
  526. var targetMini = row;
  527. if (typeof wx === 'undefined') {
  528. if (isFun(fail))
  529. fail();
  530. if (isFun(complete))
  531. complete();
  532. return;
  533. }
  534. targetMini.extraData = targetMini.extraData || {};
  535. wx.navigateToMiniProgram({
  536. appId: targetMini.appid,
  537. path: targetMini.link_path,
  538. extraData: targetMini.extraData,
  539. success: function (e) {
  540. collect(targetMini, openid);
  541. if (isFun(success))
  542. success();
  543. },
  544. fail: function (e) {
  545. if (isFun(fail))
  546. fail();
  547. },
  548. complete: function (e) {
  549. if (isFun(complete))
  550. complete();
  551. }
  552. })
  553. }
  554. return self;
  555. })();
  556. var theEnv = (typeof window !== 'undefined' ? window : global)
  557. theEnv.zs = theEnv.zs || {};
  558. theEnv.zs.sdk = sdk;
  559. (function(){
  560. var moduleName = sdk;
  561. if (typeof module !== 'undefined' && typeof exports === 'object') {
  562. module.exports = moduleName;
  563. } else if (typeof define === 'function' && (define.amd || define.cmd)) {
  564. define(function () { return moduleName; });
  565. } else {
  566. this.moduleName = moduleName;
  567. }
  568. }).call(
  569. function () {
  570. return this || (typeof window !== 'undefined' ? window : global);
  571. });