|
|
// import ICINLocker from "./ICINPakage/ICINLocker"
|
|
// const WXAPI = require('./request')
|
|
// import moment from "./moment"
|
|
// var bleall = require('./bleall');
|
|
// import {
|
|
// BLE
|
|
// } from "./btls/ble";
|
|
const app = getApp();
|
|
// const emitter = app.globalData.emitter
|
|
|
|
var md5 = require("md5");
|
|
function 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(':')
|
|
}
|
|
|
|
function formatNumber(n) {
|
|
n = n.toString()
|
|
return n[1] ? n : '0' + n
|
|
}
|
|
|
|
|
|
function uint8ArrayToString(fileData) {
|
|
let dataString = "";
|
|
for (let i = 0; i < fileData.length; i++) {
|
|
dataString += String.fromCharCode(fileData[i]);
|
|
}
|
|
return dataString
|
|
}
|
|
function stringToUint8Array(string) {
|
|
// if (options.stream) {
|
|
// throw new Error(`Failed to encode: the 'stream' option is unsupported.`);
|
|
// }
|
|
let pos = 0;
|
|
const len = string.length;
|
|
const out = [];
|
|
let at = 0; // output position
|
|
let tlen = Math.max(32, len + (len >> 1) + 7); // 1.5x size
|
|
let target = new Uint8Array((tlen >> 3) << 3); // ... but at 8 byte offset
|
|
while (pos < len) {
|
|
let value = string.charCodeAt(pos++);
|
|
if (value >= 0xd800 && value <= 0xdbff) {
|
|
// high surrogate
|
|
if (pos < len) {
|
|
const extra = string.charCodeAt(pos);
|
|
if ((extra & 0xfc00) === 0xdc00) {
|
|
++pos;
|
|
value = ((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000;
|
|
}
|
|
}
|
|
if (value >= 0xd800 && value <= 0xdbff) {
|
|
continue; // drop lone surrogate
|
|
}
|
|
}
|
|
// expand the buffer if we couldn't write 4 bytes
|
|
if (at + 4 > target.length) {
|
|
tlen += 8; // minimum extra
|
|
tlen *= (1.0 + (pos / string.length) * 2); // take 2x the remaining
|
|
tlen = (tlen >> 3) << 3; // 8 byte offset
|
|
const update = new Uint8Array(tlen);
|
|
update.set(target);
|
|
target = update;
|
|
}
|
|
if ((value & 0xffffff80) === 0) { // 1-byte
|
|
target[at++] = value; // ASCII
|
|
continue;
|
|
} else if ((value & 0xfffff800) === 0) { // 2-byte
|
|
target[at++] = ((value >> 6) & 0x1f) | 0xc0;
|
|
} else if ((value & 0xffff0000) === 0) { // 3-byte
|
|
target[at++] = ((value >> 12) & 0x0f) | 0xe0;
|
|
target[at++] = ((value >> 6) & 0x3f) | 0x80;
|
|
} else if ((value & 0xffe00000) === 0) { // 4-byte
|
|
target[at++] = ((value >> 18) & 0x07) | 0xf0;
|
|
target[at++] = ((value >> 12) & 0x3f) | 0x80;
|
|
target[at++] = ((value >> 6) & 0x3f) | 0x80;
|
|
} else {
|
|
continue;
|
|
}
|
|
target[at++] = (value & 0x3f) | 0x80;
|
|
}
|
|
return target.subarray(0, at);;
|
|
}
|
|
// function stringToUint8Array(str) {
|
|
// let arr = [];
|
|
// for (let i = 0, j = str.length; i < j; ++i) {
|
|
// arr.push(str.charCodeAt(i));
|
|
// }
|
|
// let tmpUint8Array = new Uint8Array(arr);
|
|
// return tmpUint8Array
|
|
// }
|
|
// function strToBinary(str){
|
|
// var result = [];
|
|
// var list = str.split("");
|
|
// for(var i=0;i<list.length;i++){
|
|
// if(i != 0){
|
|
// result.push(" ");
|
|
// }
|
|
// var item = list[i];
|
|
// var binaryStr = item.charCodeAt().toString(2);
|
|
// result.push(binartStr);
|
|
// }
|
|
// return result.join("");
|
|
// }
|
|
|
|
function stringToBytesWithPadding(str, length, padding) {
|
|
//try {
|
|
// 使用指定的字符集将此字符串编码为byte序列并存到一个byte数组中
|
|
var len = str.length;
|
|
// var bb = str.charCodeAt();
|
|
let bb = [];
|
|
for (let i = 0, j = str.length; i < j; ++i) {
|
|
bb.push(str[i].charCodeAt());
|
|
}
|
|
console.log(bb, 'bb')
|
|
var bytes = new Array(length);
|
|
for (var i = 0; i < length; i++) {
|
|
if (i < len)
|
|
bytes[i] = bb[i];
|
|
else
|
|
bytes[i] = padding;
|
|
}
|
|
return bytes;
|
|
}
|
|
|
|
|
|
function stringToUint8ArrayWithPadding(str, length, padding) {
|
|
let arr = [];
|
|
for (let i = 0, j = str.length; i < j; ++i) {
|
|
arr.push(str.charCodeAt(i));
|
|
}
|
|
|
|
for (let i = str.length; i < length; i++) {
|
|
arr.push(padding);
|
|
}
|
|
|
|
let tmpUint8Array = new Uint8Array(arr);
|
|
return tmpUint8Array
|
|
}
|
|
|
|
|
|
// 进制转换
|
|
function dec2hex(str) {
|
|
var dec = str.toString().split(""),
|
|
sum = [],
|
|
hex = [],
|
|
i,
|
|
s;
|
|
while (dec.length) {
|
|
s = 1 * dec.shift();
|
|
for (i = 0; s || i < sum.length; i++) {
|
|
s += (sum[i] || 0) * 10;
|
|
sum[i] = s % 16;
|
|
s = (s - sum[i]) / 16;
|
|
}
|
|
}
|
|
while (sum.length) {
|
|
hex.push(sum.pop().toString(16));
|
|
}
|
|
return hex.join("");
|
|
}
|
|
|
|
|
|
// 微信官方给的ArrayBuffer转十六进制示例
|
|
function ab2hex(buffer) {
|
|
let hexArr = Array.prototype.map.call(
|
|
new Uint8Array(buffer),
|
|
function (bit) {
|
|
return ('00' + bit.toString(16)).slice(-2)
|
|
}
|
|
)
|
|
return hexArr.join(',');
|
|
}
|
|
|
|
//转成可展会的文字
|
|
function hexCharCodeToStr(hexCharCodeStr) {
|
|
let trimedStr = hexCharCodeStr.trim();
|
|
let rawStr = trimedStr.substr(0, 2).toLowerCase() === '0x' ? trimedStr.substr(2) : trimedStr;
|
|
let len = rawStr.length;
|
|
let curCharCode;
|
|
let resultStr = [];
|
|
for (let i = 0; i < len; i = i + 2) {
|
|
curCharCode = parseInt(rawStr.substr(i, 2), 16);
|
|
resultStr.push(String.fromCharCode(curCharCode));
|
|
}
|
|
return resultStr.join('');
|
|
}
|
|
|
|
//转换成需要的格式
|
|
function buf2string(buffer) {
|
|
let arr = Array.prototype.map.call(new Uint8Array(buffer), x => x)
|
|
|
|
return arr.map((char, i) => {
|
|
return String.fromCharCode(char);
|
|
}).join('');
|
|
}
|
|
|
|
function decodeHexNibble(c) {
|
|
if (c >= 48 && c <= 57) {
|
|
return c - 48;
|
|
} else if (c >= 65 && c <= 70) {
|
|
return c - 65 + 10;
|
|
} else {
|
|
return c >= 97 && c <= 102 ? c - 97 + 10 : -1;
|
|
}
|
|
}
|
|
|
|
function decodeHexByte(s, pos) {
|
|
let hi = decodeHexNibble(s.charCodeAt(pos));
|
|
let lo = decodeHexNibble(s.charCodeAt(pos + 1));
|
|
return ((hi << 4) + lo);
|
|
}
|
|
|
|
function hexStringToBytesWithPadding(str, byteLen, padding) {
|
|
let len = str.length
|
|
let length = 2 * byteLen
|
|
|
|
if (length >= len && (length & 1) == 0) {
|
|
if (length == 0) {
|
|
return [0];
|
|
} else {
|
|
let bytes = new Uint8Array(length >>> 1);
|
|
|
|
for (let i = 0; i < len; i += 2) {
|
|
bytes[i >>> 1] = decodeHexByte(str, i);
|
|
}
|
|
|
|
for (let i = 0; i < byteLen - len / 2; i++)
|
|
bytes[len / 2 + i] = padding;
|
|
|
|
return bytes;
|
|
}
|
|
} else
|
|
return [0];
|
|
}
|
|
|
|
|
|
function timeStringToBytes(timeStr, timeLen) {
|
|
let s = timeStr;
|
|
if (s.length < timeLen) {
|
|
let tmp = timeLen - s.length
|
|
for (let i = 0; i < tmp; i++)
|
|
s = "0" + s;
|
|
} else
|
|
s = timeStr.substr(s.length - timeLen, s.length);
|
|
|
|
let start = 0
|
|
let length = s.length
|
|
|
|
if (length >= 0 && (length & 1) == 0) {
|
|
if (length == 0) {
|
|
return new byte[0];
|
|
} else {
|
|
let bytes = new Uint8Array(length >>> 1);
|
|
|
|
for (let i = 0; i < length; i += 2) {
|
|
bytes[i >>> 1] = decodeHexByte(s, start + i);
|
|
}
|
|
|
|
return bytes;
|
|
}
|
|
} else
|
|
return [0];
|
|
}
|
|
|
|
function time40ToBytes(dt) {
|
|
|
|
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);
|
|
|
|
var milliTs = new Date(cs);
|
|
var milliTs1 = milliTs.getTime()
|
|
var secondTs = parseInt(milliTs1 / 1000)
|
|
|
|
console.log(cs, milliTs1)
|
|
|
|
console.log(secondTs)
|
|
|
|
let ts = secondTs.toString(16)
|
|
let t = timeStringToBytes(ts, 10)
|
|
|
|
console.log(t)
|
|
|
|
return t;
|
|
}
|
|
|
|
function strToBinary(str) {
|
|
var result = [];
|
|
var list = str.split("");
|
|
var binaryStr = ''
|
|
for (var i = 0; i < list.length; i++) {
|
|
if (i != 0) {
|
|
result.push(" ");
|
|
}
|
|
var item = list[i];
|
|
binaryStr = item.charCodeAt().toString(2);
|
|
result.push(binaryStr);
|
|
}
|
|
return result.join("");
|
|
}
|
|
|
|
|
|
//当前时间戳
|
|
function gettimestamp() {
|
|
var timestamp = Date.parse(new Date());
|
|
return timestamp / 1000
|
|
}
|
|
|
|
function gettimestamp1() {
|
|
var timestamp = new Date().getTime()
|
|
return timestamp
|
|
}
|
|
//随机生成N位数字(0到9) 获取随机数
|
|
function getRandomNumber(n) {
|
|
var arr = new Array(n); //用于存放随机数
|
|
var randomNumber = ''; //存放随机数
|
|
for (i = 0; i < arr.length; i++)
|
|
arr[i] = parseInt(Math.random() * 10);
|
|
var flag = 0;
|
|
for (i = 0; i < arr.length - 1; i++) {
|
|
for (j = i + 1; j < arr.length; j++) {
|
|
if (arr[i] == arr[j]) {
|
|
flag = 1;
|
|
break;
|
|
}
|
|
}
|
|
if (flag) break;
|
|
}
|
|
for (var i = 0; i < arr.length; i++) {
|
|
randomNumber += arr[i];
|
|
}
|
|
return randomNumber;
|
|
}
|
|
|
|
// 去重
|
|
function uniqueArray(array, key) {
|
|
var result = [array[0]];
|
|
for (var i = 1; i < array.length; i++) {
|
|
var item = array[i];
|
|
var repeat = false;
|
|
for (var j = 0; j < result.length; j++) {
|
|
if (item[key] == result[j][key]) {
|
|
repeat = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!repeat) {
|
|
result.push(item);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
// 树形数据转化
|
|
function ztreeToElementTree(data, config) {
|
|
var id = config.id || 'id' // 定义ztree数据存储id(自身的id)的key值,可以自定义但一般都是id、pid这种
|
|
var pid = config.pid || 'pid' // 定义ztree数据存储pid(父id)的key值
|
|
var children = config.children || 'children' // 定义生成的element tree存储子节点的key,可以在element中规定defaultProps: {children: 'children'}
|
|
var idMap = {} // 将原始数据转化为以id为key的map
|
|
var elementTree = [] // 输出的element tree数据
|
|
data.forEach(item => {
|
|
idMap[item.id] = item
|
|
})
|
|
data.forEach(item => {
|
|
// debugger
|
|
if (idMap[item.pid] != idMap[item.id] && idMap[item.pid]) { // 该item项有爸爸,放到对应的父节点下
|
|
!idMap[item.pid].hasOwnProperty(children) && (idMap[item.pid][children] = [])
|
|
idMap[item.pid][children].push(item)
|
|
} else { // 根节点
|
|
elementTree.push(item)
|
|
}
|
|
});
|
|
return elementTree
|
|
}
|
|
/* 部分隐藏处理
|
|
** str 需要处理的字符串
|
|
** frontLen 保留的前几位
|
|
** endLen 保留的后几位
|
|
** cha 替换的字符串
|
|
*/
|
|
function plusXing(str, frontLen, endLen, cha) {
|
|
let content = str.toString()
|
|
var len = content.length - frontLen - endLen;
|
|
var xing = '';
|
|
for (var i = 0; i < len; i++) {
|
|
xing += cha;
|
|
}
|
|
return content.substring(0, frontLen) + xing + content.substring(content.length - endLen);
|
|
};
|
|
function IntToBytesLittleEndian(number, length){
|
|
var bytes = [];
|
|
var i = 0;
|
|
do {
|
|
bytes[i++] = number & (255);
|
|
number = number>>8;
|
|
} while ( i < length )
|
|
return bytes;
|
|
// var bytes = [];
|
|
// var i = length;
|
|
// do {
|
|
// bytes[--i] = number & (255);
|
|
// number = number>>8;
|
|
// } while (i)
|
|
// return bytes;
|
|
}
|
|
|
|
function BytesToIntBigEndian(bytes){
|
|
var val = 0;
|
|
for (var i = bytes.length - 1; i >= 0; i--) {
|
|
val += bytes[i];
|
|
if (i != 0) {
|
|
val = val << 8;
|
|
}
|
|
}
|
|
return val;
|
|
}
|
|
|
|
function dec2hex1(str) {
|
|
var dec = str.toString().split(""),
|
|
sum = [],
|
|
hex = [],
|
|
i,
|
|
s;
|
|
while (dec.length) {
|
|
s = 1 * dec.shift();
|
|
for (i = 0; s || i < sum.length; i++) {
|
|
s += (sum[i] || 0) * 10;
|
|
sum[i] = s % 16;
|
|
s = (s - sum[i]) / 16;
|
|
}
|
|
}
|
|
while (sum.length) {
|
|
hex.push(sum.pop().toString(16));
|
|
}
|
|
var card = hex.join("")
|
|
var zero = '00000000';
|
|
var tmp = 8 - card.length;
|
|
var clength = zero.substring(0, tmp) + card
|
|
return clength;
|
|
}
|
|
/*函数节流*/
|
|
function throttle(fn, interval) {
|
|
var enterTime = 0;//触发的时间
|
|
var gapTime = interval || 300 ;//间隔时间,如果interval不传,则默认300ms
|
|
return function() {
|
|
var context = this;
|
|
var backTime = new Date();//第一次函数return即触发的时间
|
|
if (backTime - enterTime > gapTime) {
|
|
fn.call(context,arguments);
|
|
enterTime = backTime;//赋值给第一次触发的时间,这样就保存了第二次触发的时间
|
|
}
|
|
};
|
|
}
|
|
|
|
/*函数防抖*/
|
|
function debounce(fn, interval) {
|
|
var timer;
|
|
var gapTime = interval || 1000;//间隔时间,如果interval不传,则默认1000ms
|
|
return function() {
|
|
clearTimeout(timer);
|
|
var context = this;
|
|
var args = arguments;//保存此处的arguments,因为setTimeout是全局的,arguments不是防抖函数需要的。
|
|
timer = setTimeout(function() {
|
|
fn.call(context,args);
|
|
}, gapTime);
|
|
};
|
|
}
|
|
function findIndexArray(data,id,indexArray){
|
|
let arr = Array.from(indexArray);
|
|
for(let i=0,len=data.length;i<len;i++){
|
|
arr.push(data[i].name);
|
|
if(data[i].id===id){
|
|
return arr
|
|
}
|
|
let children=data[i].children;
|
|
if(children&&children.length){
|
|
let result=this.findIndexArray(children,id,arr);
|
|
if(result) return result
|
|
}
|
|
arr.pop()
|
|
}
|
|
return false
|
|
}
|
|
var pako = require('pako');
|
|
var CryptoJS = require("crypto-js");
|
|
// var md5 = require("md5");
|
|
const moment = require('./moment');
|
|
function decrypt(word, keyStr) {
|
|
let key = CryptoJS.enc.Utf8.parse(keyStr);
|
|
let decrypt = CryptoJS.AES.decrypt(word, key, {
|
|
mode: CryptoJS.mode.ECB,
|
|
padding: CryptoJS.pad.Pkcs7
|
|
});
|
|
return CryptoJS.enc.Utf8.stringify(decrypt).toString()
|
|
}
|
|
function zlib_unzip(info) {
|
|
var key=md5(String(info.currentTime)+"mdmdmdmdmdvsololololol")
|
|
let b = info.list.split(',').map(function(x) {
|
|
return parseInt(x);
|
|
});
|
|
let c = pako.inflate(b, {
|
|
to: "string"
|
|
});
|
|
let e = decrypt(c,key)
|
|
return JSON.parse(e)
|
|
}
|
|
|
|
function searchParaProcess(obj) {
|
|
var myObj = new Object();
|
|
for (var key in obj) {
|
|
if (obj[key] != "" && obj[key] != 'Invalid date' && obj[key] != null) {
|
|
myObj[key] = obj[key]
|
|
}
|
|
}
|
|
return JSON.stringify(myObj);
|
|
}
|
|
|
|
module.exports = {
|
|
// bluetoothUnlocking,
|
|
// bluetoothConnectivity,
|
|
// bluetoothOperation_ZH,
|
|
decrypt,
|
|
searchParaProcess,
|
|
zlib_unzip,
|
|
findIndexArray,
|
|
throttle,
|
|
debounce,
|
|
IntToBytesLittleEndian,
|
|
BytesToIntBigEndian,
|
|
dec2hex,
|
|
dec2hex1,
|
|
plusXing,
|
|
ztreeToElementTree,
|
|
formatTime: formatTime,
|
|
uint8ArrayToString: uint8ArrayToString,
|
|
stringToUint8Array: stringToUint8Array,
|
|
stringToUint8ArrayWithPadding: stringToUint8ArrayWithPadding,
|
|
ab2hex: ab2hex,
|
|
hexCharCodeToStr,
|
|
hexCharCodeToStr: buf2string,
|
|
decodeHexNibble,
|
|
decodeHexNibble: decodeHexByte,
|
|
hexStringToBytesWithPadding: hexStringToBytesWithPadding,
|
|
timeStringToBytes: timeStringToBytes,
|
|
time40ToBytes: time40ToBytes,
|
|
gettimestamp,
|
|
gettimestamp1,
|
|
getRandomNumber,
|
|
uniqueArray,
|
|
strToBinary,
|
|
stringToBytesWithPadding
|
|
}
|