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.

557 lines
15 KiB

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