You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

558 lines
15 KiB

1 year ago
1 year ago
1 year ago
  1. // import ICINLocker from "./ICINPakage/ICINLocker"
  2. // const WXAPI = require('./request')
  3. // import moment from "./moment"
  4. // var bleall = require('./bleall');
  5. // import {
  6. // BLE
  7. // } from "./btls/ble";
  8. const app = getApp();
  9. // const emitter = app.globalData.emitter
  10. var md5 = require("md5");
  11. function formatTime(date) {
  12. const year = date.getFullYear()
  13. const month = date.getMonth() + 1
  14. const day = date.getDate()
  15. const hour = date.getHours()
  16. const minute = date.getMinutes()
  17. const second = date.getSeconds()
  18. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  19. }
  20. function formatNumber(n) {
  21. n = n.toString()
  22. return n[1] ? n : '0' + n
  23. }
  24. function uint8ArrayToString(fileData) {
  25. let dataString = "";
  26. for (let i = 0; i < fileData.length; i++) {
  27. dataString += String.fromCharCode(fileData[i]);
  28. }
  29. return dataString
  30. }
  31. function stringToUint8Array(string) {
  32. // if (options.stream) {
  33. // throw new Error(`Failed to encode: the 'stream' option is unsupported.`);
  34. // }
  35. let pos = 0;
  36. const len = string.length;
  37. const out = [];
  38. let at = 0; // output position
  39. let tlen = Math.max(32, len + (len >> 1) + 7); // 1.5x size
  40. let target = new Uint8Array((tlen >> 3) << 3); // ... but at 8 byte offset
  41. while (pos < len) {
  42. let value = string.charCodeAt(pos++);
  43. if (value >= 0xd800 && value <= 0xdbff) {
  44. // high surrogate
  45. if (pos < len) {
  46. const extra = string.charCodeAt(pos);
  47. if ((extra & 0xfc00) === 0xdc00) {
  48. ++pos;
  49. value = ((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000;
  50. }
  51. }
  52. if (value >= 0xd800 && value <= 0xdbff) {
  53. continue; // drop lone surrogate
  54. }
  55. }
  56. // expand the buffer if we couldn't write 4 bytes
  57. if (at + 4 > target.length) {
  58. tlen += 8; // minimum extra
  59. tlen *= (1.0 + (pos / string.length) * 2); // take 2x the remaining
  60. tlen = (tlen >> 3) << 3; // 8 byte offset
  61. const update = new Uint8Array(tlen);
  62. update.set(target);
  63. target = update;
  64. }
  65. if ((value & 0xffffff80) === 0) { // 1-byte
  66. target[at++] = value; // ASCII
  67. continue;
  68. } else if ((value & 0xfffff800) === 0) { // 2-byte
  69. target[at++] = ((value >> 6) & 0x1f) | 0xc0;
  70. } else if ((value & 0xffff0000) === 0) { // 3-byte
  71. target[at++] = ((value >> 12) & 0x0f) | 0xe0;
  72. target[at++] = ((value >> 6) & 0x3f) | 0x80;
  73. } else if ((value & 0xffe00000) === 0) { // 4-byte
  74. target[at++] = ((value >> 18) & 0x07) | 0xf0;
  75. target[at++] = ((value >> 12) & 0x3f) | 0x80;
  76. target[at++] = ((value >> 6) & 0x3f) | 0x80;
  77. } else {
  78. continue;
  79. }
  80. target[at++] = (value & 0x3f) | 0x80;
  81. }
  82. return target.subarray(0, at);;
  83. }
  84. // function stringToUint8Array(str) {
  85. // let arr = [];
  86. // for (let i = 0, j = str.length; i < j; ++i) {
  87. // arr.push(str.charCodeAt(i));
  88. // }
  89. // let tmpUint8Array = new Uint8Array(arr);
  90. // return tmpUint8Array
  91. // }
  92. // function strToBinary(str){
  93. // var result = [];
  94. // var list = str.split("");
  95. // for(var i=0;i<list.length;i++){
  96. // if(i != 0){
  97. // result.push(" ");
  98. // }
  99. // var item = list[i];
  100. // var binaryStr = item.charCodeAt().toString(2);
  101. // result.push(binartStr);
  102. // }
  103. // return result.join("");
  104. // }
  105. function stringToBytesWithPadding(str, length, padding) {
  106. //try {
  107. // 使用指定的字符集将此字符串编码为byte序列并存到一个byte数组中
  108. var len = str.length;
  109. // var bb = str.charCodeAt();
  110. let bb = [];
  111. for (let i = 0, j = str.length; i < j; ++i) {
  112. bb.push(str[i].charCodeAt());
  113. }
  114. console.log(bb, 'bb')
  115. var bytes = new Array(length);
  116. for (var i = 0; i < length; i++) {
  117. if (i < len)
  118. bytes[i] = bb[i];
  119. else
  120. bytes[i] = padding;
  121. }
  122. return bytes;
  123. }
  124. function stringToUint8ArrayWithPadding(str, length, padding) {
  125. let arr = [];
  126. for (let i = 0, j = str.length; i < j; ++i) {
  127. arr.push(str.charCodeAt(i));
  128. }
  129. for (let i = str.length; i < length; i++) {
  130. arr.push(padding);
  131. }
  132. let tmpUint8Array = new Uint8Array(arr);
  133. return tmpUint8Array
  134. }
  135. // 进制转换
  136. function dec2hex(str) {
  137. var dec = str.toString().split(""),
  138. sum = [],
  139. hex = [],
  140. i,
  141. s;
  142. while (dec.length) {
  143. s = 1 * dec.shift();
  144. for (i = 0; s || i < sum.length; i++) {
  145. s += (sum[i] || 0) * 10;
  146. sum[i] = s % 16;
  147. s = (s - sum[i]) / 16;
  148. }
  149. }
  150. while (sum.length) {
  151. hex.push(sum.pop().toString(16));
  152. }
  153. return hex.join("");
  154. }
  155. // 微信官方给的ArrayBuffer转十六进制示例
  156. function ab2hex(buffer) {
  157. let hexArr = Array.prototype.map.call(
  158. new Uint8Array(buffer),
  159. function (bit) {
  160. return ('00' + bit.toString(16)).slice(-2)
  161. }
  162. )
  163. return hexArr.join(',');
  164. }
  165. //转成可展会的文字
  166. function hexCharCodeToStr(hexCharCodeStr) {
  167. let trimedStr = hexCharCodeStr.trim();
  168. let rawStr = trimedStr.substr(0, 2).toLowerCase() === '0x' ? trimedStr.substr(2) : trimedStr;
  169. let len = rawStr.length;
  170. let curCharCode;
  171. let resultStr = [];
  172. for (let i = 0; i < len; i = i + 2) {
  173. curCharCode = parseInt(rawStr.substr(i, 2), 16);
  174. resultStr.push(String.fromCharCode(curCharCode));
  175. }
  176. return resultStr.join('');
  177. }
  178. //转换成需要的格式
  179. function buf2string(buffer) {
  180. let arr = Array.prototype.map.call(new Uint8Array(buffer), x => x)
  181. return arr.map((char, i) => {
  182. return String.fromCharCode(char);
  183. }).join('');
  184. }
  185. function decodeHexNibble(c) {
  186. if (c >= 48 && c <= 57) {
  187. return c - 48;
  188. } else if (c >= 65 && c <= 70) {
  189. return c - 65 + 10;
  190. } else {
  191. return c >= 97 && c <= 102 ? c - 97 + 10 : -1;
  192. }
  193. }
  194. function decodeHexByte(s, pos) {
  195. let hi = decodeHexNibble(s.charCodeAt(pos));
  196. let lo = decodeHexNibble(s.charCodeAt(pos + 1));
  197. return ((hi << 4) + lo);
  198. }
  199. function hexStringToBytesWithPadding(str, byteLen, padding) {
  200. let len = str.length
  201. let length = 2 * byteLen
  202. if (length >= len && (length & 1) == 0) {
  203. if (length == 0) {
  204. return [0];
  205. } else {
  206. let bytes = new Uint8Array(length >>> 1);
  207. for (let i = 0; i < len; i += 2) {
  208. bytes[i >>> 1] = decodeHexByte(str, i);
  209. }
  210. for (let i = 0; i < byteLen - len / 2; i++)
  211. bytes[len / 2 + i] = padding;
  212. return bytes;
  213. }
  214. } else
  215. return [0];
  216. }
  217. function timeStringToBytes(timeStr, timeLen) {
  218. let s = timeStr;
  219. if (s.length < timeLen) {
  220. let tmp = timeLen - s.length
  221. for (let i = 0; i < tmp; i++)
  222. s = "0" + s;
  223. } else
  224. s = timeStr.substr(s.length - timeLen, s.length);
  225. let start = 0
  226. let length = s.length
  227. if (length >= 0 && (length & 1) == 0) {
  228. if (length == 0) {
  229. return new byte[0];
  230. } else {
  231. let bytes = new Uint8Array(length >>> 1);
  232. for (let i = 0; i < length; i += 2) {
  233. bytes[i >>> 1] = decodeHexByte(s, start + i);
  234. }
  235. return bytes;
  236. }
  237. } else
  238. return [0];
  239. }
  240. function time40ToBytes(dt) {
  241. var cs = dt.substring(0, 4) + "/" + dt.substring(4, 6) + "/" + dt.substring(6, 8) + " " + dt.substring(8, 10) + ":" + dt.substring(10, 12) + ":" + dt.substring(12, 14);
  242. var milliTs = new Date(cs);
  243. var milliTs1 = milliTs.getTime()
  244. var secondTs = parseInt(milliTs1 / 1000)
  245. console.log(cs, milliTs1)
  246. console.log(secondTs)
  247. let ts = secondTs.toString(16)
  248. let t = timeStringToBytes(ts, 10)
  249. console.log(t)
  250. return t;
  251. }
  252. function strToBinary(str) {
  253. var result = [];
  254. var list = str.split("");
  255. var binaryStr = ''
  256. for (var i = 0; i < list.length; i++) {
  257. if (i != 0) {
  258. result.push(" ");
  259. }
  260. var item = list[i];
  261. binaryStr = item.charCodeAt().toString(2);
  262. result.push(binaryStr);
  263. }
  264. return result.join("");
  265. }
  266. //当前时间戳
  267. function gettimestamp() {
  268. var timestamp = Date.parse(new Date());
  269. return timestamp / 1000
  270. }
  271. function gettimestamp1() {
  272. var timestamp = new Date().getTime()
  273. return timestamp
  274. }
  275. //随机生成N位数字(0到9) 获取随机数
  276. function getRandomNumber(n) {
  277. var arr = new Array(n); //用于存放随机数
  278. var randomNumber = ''; //存放随机数
  279. for (i = 0; i < arr.length; i++)
  280. arr[i] = parseInt(Math.random() * 10);
  281. var flag = 0;
  282. for (i = 0; i < arr.length - 1; i++) {
  283. for (j = i + 1; j < arr.length; j++) {
  284. if (arr[i] == arr[j]) {
  285. flag = 1;
  286. break;
  287. }
  288. }
  289. if (flag) break;
  290. }
  291. for (var i = 0; i < arr.length; i++) {
  292. randomNumber += arr[i];
  293. }
  294. return randomNumber;
  295. }
  296. // 去重
  297. function uniqueArray(array, key) {
  298. var result = [array[0]];
  299. for (var i = 1; i < array.length; i++) {
  300. var item = array[i];
  301. var repeat = false;
  302. for (var j = 0; j < result.length; j++) {
  303. if (item[key] == result[j][key]) {
  304. repeat = true;
  305. break;
  306. }
  307. }
  308. if (!repeat) {
  309. result.push(item);
  310. }
  311. }
  312. return result;
  313. }
  314. // 树形数据转化
  315. function ztreeToElementTree(data, config) {
  316. var id = config.id || 'id' // 定义ztree数据存储id(自身的id)的key值,可以自定义但一般都是id、pid这种
  317. var pid = config.pid || 'pid' // 定义ztree数据存储pid(父id)的key值
  318. var children = config.children || 'children' // 定义生成的element tree存储子节点的key,可以在element中规定defaultProps: {children: 'children'}
  319. var idMap = {} // 将原始数据转化为以id为key的map
  320. var elementTree = [] // 输出的element tree数据
  321. data.forEach(item => {
  322. idMap[item.id] = item
  323. })
  324. data.forEach(item => {
  325. // debugger
  326. if (idMap[item.pid] != idMap[item.id] && idMap[item.pid]) { // 该item项有爸爸,放到对应的父节点下
  327. !idMap[item.pid].hasOwnProperty(children) && (idMap[item.pid][children] = [])
  328. idMap[item.pid][children].push(item)
  329. } else { // 根节点
  330. elementTree.push(item)
  331. }
  332. });
  333. return elementTree
  334. }
  335. /*
  336. ** str 需要处理的字符串
  337. ** frontLen 保留的前几位
  338. ** endLen 保留的后几位
  339. ** cha 替换的字符串
  340. */
  341. function plusXing(str, frontLen, endLen, cha) {
  342. let content = str.toString()
  343. var len = content.length - frontLen - endLen;
  344. var xing = '';
  345. for (var i = 0; i < len; i++) {
  346. xing += cha;
  347. }
  348. return content.substring(0, frontLen) + xing + content.substring(content.length - endLen);
  349. };
  350. function IntToBytesLittleEndian(number, length){
  351. var bytes = [];
  352. var i = 0;
  353. do {
  354. bytes[i++] = number & (255);
  355. number = number>>8;
  356. } while ( i < length )
  357. return bytes;
  358. // var bytes = [];
  359. // var i = length;
  360. // do {
  361. // bytes[--i] = number & (255);
  362. // number = number>>8;
  363. // } while (i)
  364. // return bytes;
  365. }
  366. function BytesToIntBigEndian(bytes){
  367. var val = 0;
  368. for (var i = bytes.length - 1; i >= 0; i--) {
  369. val += bytes[i];
  370. if (i != 0) {
  371. val = val << 8;
  372. }
  373. }
  374. return val;
  375. }
  376. function dec2hex1(str) {
  377. var dec = str.toString().split(""),
  378. sum = [],
  379. hex = [],
  380. i,
  381. s;
  382. while (dec.length) {
  383. s = 1 * dec.shift();
  384. for (i = 0; s || i < sum.length; i++) {
  385. s += (sum[i] || 0) * 10;
  386. sum[i] = s % 16;
  387. s = (s - sum[i]) / 16;
  388. }
  389. }
  390. while (sum.length) {
  391. hex.push(sum.pop().toString(16));
  392. }
  393. var card = hex.join("")
  394. var zero = '00000000';
  395. var tmp = 8 - card.length;
  396. var clength = zero.substring(0, tmp) + card
  397. return clength;
  398. }
  399. /*函数节流*/
  400. function throttle(fn, interval) {
  401. var enterTime = 0;//触发的时间
  402. var gapTime = interval || 300 ;//间隔时间,如果interval不传,则默认300ms
  403. return function() {
  404. var context = this;
  405. var backTime = new Date();//第一次函数return即触发的时间
  406. if (backTime - enterTime > gapTime) {
  407. fn.call(context,arguments);
  408. enterTime = backTime;//赋值给第一次触发的时间,这样就保存了第二次触发的时间
  409. }
  410. };
  411. }
  412. /*函数防抖*/
  413. function debounce(fn, interval) {
  414. var timer;
  415. var gapTime = interval || 1000;//间隔时间,如果interval不传,则默认1000ms
  416. return function() {
  417. clearTimeout(timer);
  418. var context = this;
  419. var args = arguments;//保存此处的arguments,因为setTimeout是全局的,arguments不是防抖函数需要的。
  420. timer = setTimeout(function() {
  421. fn.call(context,args);
  422. }, gapTime);
  423. };
  424. }
  425. function findIndexArray(data,id,indexArray){
  426. let arr = Array.from(indexArray);
  427. for(let i=0,len=data.length;i<len;i++){
  428. arr.push(data[i].name);
  429. if(data[i].id===id){
  430. return arr
  431. }
  432. let children=data[i].children;
  433. if(children&&children.length){
  434. let result=this.findIndexArray(children,id,arr);
  435. if(result) return result
  436. }
  437. arr.pop()
  438. }
  439. return false
  440. }
  441. var pako = require('pako');
  442. var CryptoJS = require("crypto-js");
  443. // var md5 = require("md5");
  444. const moment = require('./moment');
  445. function decrypt(word, keyStr) {
  446. let key = CryptoJS.enc.Utf8.parse(keyStr);
  447. let decrypt = CryptoJS.AES.decrypt(word, key, {
  448. mode: CryptoJS.mode.ECB,
  449. padding: CryptoJS.pad.Pkcs7
  450. });
  451. return CryptoJS.enc.Utf8.stringify(decrypt).toString()
  452. }
  453. function zlib_unzip(info) {
  454. var key=md5(String(info.currentTime)+"mdmdmdmdmdvsololololol")
  455. let b = info.list.split(',').map(function(x) {
  456. return parseInt(x);
  457. });
  458. let c = pako.inflate(b, {
  459. to: "string"
  460. });
  461. let e = decrypt(c,key)
  462. return JSON.parse(e)
  463. }
  464. function searchParaProcess(obj) {
  465. var myObj = new Object();
  466. for (var key in obj) {
  467. if (obj[key] != "" && obj[key] != 'Invalid date' && obj[key] != null) {
  468. myObj[key] = obj[key]
  469. }
  470. }
  471. return JSON.stringify(myObj);
  472. }
  473. module.exports = {
  474. // bluetoothUnlocking,
  475. // bluetoothConnectivity,
  476. // bluetoothOperation_ZH,
  477. decrypt,
  478. searchParaProcess,
  479. zlib_unzip,
  480. findIndexArray,
  481. throttle,
  482. debounce,
  483. IntToBytesLittleEndian,
  484. BytesToIntBigEndian,
  485. dec2hex,
  486. dec2hex1,
  487. plusXing,
  488. ztreeToElementTree,
  489. formatTime: formatTime,
  490. uint8ArrayToString: uint8ArrayToString,
  491. stringToUint8Array: stringToUint8Array,
  492. stringToUint8ArrayWithPadding: stringToUint8ArrayWithPadding,
  493. ab2hex: ab2hex,
  494. hexCharCodeToStr,
  495. hexCharCodeToStr: buf2string,
  496. decodeHexNibble,
  497. decodeHexNibble: decodeHexByte,
  498. hexStringToBytesWithPadding: hexStringToBytesWithPadding,
  499. timeStringToBytes: timeStringToBytes,
  500. time40ToBytes: time40ToBytes,
  501. gettimestamp,
  502. gettimestamp1,
  503. getRandomNumber,
  504. uniqueArray,
  505. strToBinary,
  506. stringToBytesWithPadding
  507. }