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

687 lines
21 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* @description 链接地址是否为视频
* @param path
* @return {boolean}
*/
const isVideo = (path) => {
return /\.(mp4|avi|wmv|mpg|mpeg|mov|rm|ram|swf|flv)/.test(path);
}
/**
* @description 图片预览
* @param url data-url="{url}"
*/
const previewImage = ({currentTarget: {dataset: { url }}}) => {
if (!url) return;
if (url.indexOf('http') === -1 || url.indexOf('https') === -1) {
url = 'https://oss.flossom.com' + url;
}
wx.previewImage({
urls: [url]
})
};
const 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(":")}`;
};
const formatNumber = (n) => {
n = n.toString();
return n[1] ? n : `0${n}`;
};
const setdata = (that, key, value) => {
that.setData({
[key]: value,
});
};
const filertext = (text) => {
return text.replace(/{/g, "").replace(/}/, "").replace(/"/g, "");
};
/**
* 处理富文本里的图片宽度自适应
* 1.去掉img标签里的style、width、height属性
* 2.img标签添加style属性max-width:100%;height:auto
* 3.修改所有style里的width属性为max-width:100%
* 4.去掉<br/>标签
* @param html
* @returns {void|string|*}
*/
const formatRichText = (html) => {
let newContent = html.replace(/<img[^>]*>/gi, (match) => {
match = match.replace(/style="[^"]+"/gi, '')
return match;
});
newContent = newContent.replace(/style="[^"]+"/gi, (match) => {
match = match.replace(/width:[^;]+;/gi, 'max-width:100%!important;')
return match;
});
newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%!important;height:auto!important;display:block;margin-top:0;margin-bottom:0;"');
return newContent;
}
const date = () => {
const year = new Date().getFullYear();
const month = new Date().getMonth() + 1;
const day = new Date().getDate();
return (
year +
"-" +
String(month).padStart("2", 0) +
"-" +
String(day).padStart("2", 0)
);
};
const getdates = (date) => {
date = date.replace(/-/g, '/'); // 部分iphone new Date不兼容[2023-01-01]格式,需要转换
const year = new Date(date).getFullYear();
const month = new Date(date).getMonth() + 1;
const day = new Date(date).getDate();
const hour = new Date(date).getHours();
const minute = new Date(date).getMinutes();
const second = new Date(date).getSeconds();
// return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second].map(formatNumber).join(':')}`
return `${[year, month, day].map(formatNumber).join("-")}`;
};
/**
* 获取当前年份的第一天和最后一天
* @returns {string} 例如 2019-01-01~2019-12-31
*/
const getYearFirstLastDay = () => {
let firstDay = new Date();
firstDay.setDate(1);
firstDay.setMonth(0);
let lastDay = new Date();
lastDay.setFullYear(lastDay.getFullYear() + 2);
lastDay.setDate(0);
lastDay.setMonth(-1);
firstDay = firstDay.Format("yyyy-MM-dd");
lastDay = lastDay.Format("yyyy-MM-dd");
return lastDay;
};
//日期格式化方法Format()方法
// 对Date的扩展将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2018-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2018-7-2 8:9:4.18
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
S: this.getMilliseconds(), //毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(
RegExp.$1,
(this.getFullYear() + "").substr(4 - RegExp.$1.length)
);
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
);
return fmt;
};
/**
*
* ArrayBuffer转16进制字符串
*/
const ab2hex = (buffer) => {
var hexArr = Array.prototype.map.call(new Uint8Array(buffer), function (bit) {
return ("00" + bit.toString(16)).slice(-2);
});
return hexArr.join("");
};
/**
*
*
*/
const hex2int = (hex) => {
var len = hex.length,
a = new Array(len),
code;
for (var i = 0; i < len; i++) {
code = hex.charCodeAt(i);
if (48 <= code && code < 58) {
code -= 48;
} else {
code = (code & 0xdf) - 65 + 10;
}
a[i] = code;
}
return a.reduce(function (acc, c) {
acc = 16 * acc + c;
return acc;
}, 0);
};
const keywordTofilter = (str) => {
let arr = [];
for (let i = 0, len = str.length; i < len; i += 2) {
arr.push(str.slice(i, i + 2));
}
for (let i = 0; i < arr.length; i++) {
if (i == 0 || i == arr.length - 1) {
continue;
}
if (arr[i] == "db") {
arr[i] = "dccb";
}
if (arr[i] == "dc") {
arr[i] = "dccc";
}
if (arr[i] == "de") {
arr[i] = "dcce";
}
}
return arr.join("");
};
const filterData = (str) => {
let data = str
.replace(/dccc/g, "dc")
.replace(/dccb/g, "db")
.replace(/dcce/g, "de");
return data;
};
const filterBleData = (str) => {
let arr = [];
let arr2 = [];
let arr3 = [];
for (let i = 0, len = str.length; i < len; i += 2) {
arr.push(str.slice(i, i + 2));
}
let index = 0;
arr.map((item, i) => {
if (item == "dc") {
if (i + 1 !== arr.length - 1) {
arr2[index].push("d" + arr[i + 1].substring(1, 2));
}
return false;
}
if (item == "de") {
arr2[index] = arr2[index].join("");
index += 1;
} else {
if (!Array.isArray(arr2[index])) {
arr2[index] = [];
}
if (i !== 0) {
if (arr[i - 1] !== "dc") {
arr2[index].push(item);
}
}
}
});
arr2.map((item) => {
// console.log(ccrc8('db'+item.substring(0,item.length-2)) , item.substring(item.length-2,item.length))
if (
ccrc8("db" + item.substring(0, item.length - 2)) ==
item.substring(item.length - 2, item.length)
) {
arr3.push("db" + item + "de");
}
});
return arr3;
};
const ccrc8 = (str) => {
let arr = [];
for (let i = 0, len = str.length; i < len; i += 2) {
arr.push("0x" + str.slice(i, i + 2));
}
const crc8_854_table = [
0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65, 157,
195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220, 35, 125,
159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98, 190, 224, 2,
92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255, 70, 24, 250, 164,
39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7, 219, 133, 103, 57,
186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154, 101, 59, 217, 135, 4,
90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36, 248, 166, 68, 26, 153,
199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185, 140, 210, 48, 110, 237,
179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205, 17, 79, 173, 243, 112, 46,
204, 146, 211, 141, 111, 49, 178, 236, 14, 80, 175, 241, 19, 77, 206, 144,
114, 44, 109, 51, 209, 143, 12, 82, 176, 238, 50, 108, 142, 208, 83, 13,
239, 177, 240, 174, 76, 18, 145, 207, 45, 115, 202, 148, 118, 40, 171, 245,
23, 73, 8, 86, 180, 234, 105, 55, 213, 139, 87, 9, 235, 181, 54, 104, 138,
212, 149, 203, 41, 119, 244, 170, 72, 22, 233, 183, 85, 11, 136, 214, 52,
106, 43, 117, 151, 201, 74, 20, 246, 168, 116, 42, 200, 150, 21, 75, 169,
247, 182, 232, 10, 84, 215, 137, 107, 53,
];
let crc = new Uint8Array(1);
crc = 0;
for (let i = 0; i < arr.length; i++) {
crc = crc8_854_table[crc ^ (arr[i] & 0xff & 0xff)];
}
return (crc & 0xff).toString(16).padStart("2", 0);
};
const s_to_hs = (s) => {
//计算分钟
//算法将秒数除以60然后下舍入既得到分钟数
var h;
h = Math.floor(s / 60);
//计算秒
//算法:取得秒%60的余数既得到秒数
s = s % 60;
//将变量转换为字符串
h += "";
s += "";
//如果只有一位数前面增加一个0
h = h.length == 1 ? "0" + h : h;
s = s.length == 1 ? "0" + s : s;
return h + ":" + s;
};
const s_to_h = (s) => {
//计算分钟
//算法将秒数除以60然后下舍入既得到分钟数
var h;
h = Math.floor(s / 60);
//计算秒
//算法:取得秒%60的余数既得到秒数
s = s % 60;
//将变量转换为字符串
h += "";
s += "";
//如果只有一位数前面增加一个0
h = h.length == 1 ? "0" + h : h;
s = s.length == 1 ? "0" + s : s;
return h;
};
const s_to_s = (s) => {
//计算分钟
//算法将秒数除以60然后下舍入既得到分钟数
var h;
h = Math.floor(s / 60);
//计算秒
//算法:取得秒%60的余数既得到秒数
s = s % 60;
//将变量转换为字符串
h += "";
s += "";
//如果只有一位数前面增加一个0
h = h.length == 1 ? "0" + h : h;
s = s.length == 1 ? "0" + s : s;
return s;
};
// 分秒转换成秒
const minSecToS = (minSecStr) => {
if (!minSecStr) return 0;
let strArr = minSecStr.split(":");
console.info(strArr, 'strArr')
return strArr.length ? parseInt(strArr[0]) * 60 + parseInt(strArr[1]) : 0;
}
// 将字符串转换成ArrayBufer
const string2buffer = (str) => {
let val = "";
if (!str) return;
let length = str.length;
let index = 0;
let array = [];
while (index < length) {
array.push(str.substring(index, index + 2));
index = index + 2;
}
val = array.join(",");
// 将16进制转化为ArrayBuffer
return new Uint8Array(
val.match(/[\da-f]{2}/gi).map(function (h) {
return parseInt(h, 16);
})
).buffer;
};
const checkEnd = (str) => {
let itotal = 0,
len = str.length,
num = 0;
while (num < len) {
let s = str.substring(num, num + 2);
itotal += parseInt(s, 16);
num = num + 2;
}
let mode = itotal % 256;
let shex = mode.toString(16);
let iLen = shex.length;
if (iLen < 2) {
shex = "0" + shex;
}
return shex.toUpperCase();
};
const getofflineData = () => {
//获取近7天的离线记录
let arr = [];
for (let i = 0; i <= 6; i++) {
let str1 = "dbf0a00201";
let num16 = i.toString(16).padStart("2", 0);
let str2 = ccrc8(`${str1}${num16}`);
arr.push(`${str1}${num16}${str2}de`);
}
return arr;
};
const getUpgradeData = (info) => {
//解析获取升级包更新数据
console.log(info);
let arr = [];
for (let i = 0, len = info.data.length; i < len; i += 480) {
arr.push({
i: info.data.slice(i, i + 480),
});
}
arr.map((item, index) => {
let num = Number(index + 1)
.toString(16)
.padStart("4", 0);
let baohaodiwei = num.substring(2, 4);
let baohaogaowei = num.substring(0, 2);
let data = `dbf0ab01F7${info.length_low.padStart('2',0)}${info.length_high.padStart('2',0)}${info.bao_low.padStart('2',0)}${
info.bao_high.padStart('2',0)
}${baohaodiwei.padStart('2',0)}${baohaogaowei.padStart('2',0)}${
index == arr.length - 1
? (arr[index].i.length / 2).toString(16).padStart("2", 0)
: "F0"
}${item.i}`;
item.i = `${data}${ccrc8(data)}de`;
});
return arr;
};
const checkEnd2 = (str) => {
const crc8_854_table = [
0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65, 157,
195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220, 35, 125,
159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98, 190, 224, 2,
92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255, 70, 24, 250, 164,
39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7, 219, 133, 103, 57,
186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154, 101, 59, 217, 135, 4,
90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36, 248, 166, 68, 26, 153,
199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185, 140, 210, 48, 110, 237,
179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205, 17, 79, 173, 243, 112, 46,
204, 146, 211, 141, 111, 49, 178, 236, 14, 80, 175, 241, 19, 77, 206, 144,
114, 44, 109, 51, 209, 143, 12, 82, 176, 238, 50, 108, 142, 208, 83, 13,
239, 177, 240, 174, 76, 18, 145, 207, 45, 115, 202, 148, 118, 40, 171, 245,
23, 73, 8, 86, 180, 234, 105, 55, 213, 139, 87, 9, 235, 181, 54, 104, 138,
212, 149, 203, 41, 119, 244, 170, 72, 22, 233, 183, 85, 11, 136, 214, 52,
106, 43, 117, 151, 201, 74, 20, 246, 168, 116, 42, 200, 150, 21, 75, 169,
247, 182, 232, 10, 84, 215, 137, 107, 53,
];
let crc = new Uint8Array(1);
crc = 0;
for (let i = 0; i < str.length; i++) {
crc = crc8_854_table[crc ^ (str[i] & 0xff & 0xff)];
}
return (crc & 0xff).toString(16).toUpperCase().padStart("2", 0);
};
const checkEnd3 = (str) => {
let arr = [];
for (let i = 0, len = str.length; i < len; i += 2) {
arr.push("0x" + str.slice(i, i + 2));
}
const crc8_854_table = [
0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65, 157,
195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220, 35, 125,
159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98, 190, 224, 2,
92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255, 70, 24, 250, 164,
39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7, 219, 133, 103, 57,
186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154, 101, 59, 217, 135, 4,
90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36, 248, 166, 68, 26, 153,
199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185, 140, 210, 48, 110, 237,
179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205, 17, 79, 173, 243, 112, 46,
204, 146, 211, 141, 111, 49, 178, 236, 14, 80, 175, 241, 19, 77, 206, 144,
114, 44, 109, 51, 209, 143, 12, 82, 176, 238, 50, 108, 142, 208, 83, 13,
239, 177, 240, 174, 76, 18, 145, 207, 45, 115, 202, 148, 118, 40, 171, 245,
23, 73, 8, 86, 180, 234, 105, 55, 213, 139, 87, 9, 235, 181, 54, 104, 138,
212, 149, 203, 41, 119, 244, 170, 72, 22, 233, 183, 85, 11, 136, 214, 52,
106, 43, 117, 151, 201, 74, 20, 246, 168, 116, 42, 200, 150, 21, 75, 169,
247, 182, 232, 10, 84, 215, 137, 107, 53,
];
let crc = new Uint8Array(1);
crc = 0;
for (let i = 0; i < arr.length; i++) {
crc = crc8_854_table[crc ^ (arr[i] & 0xff & 0xff)];
}
return (crc & 0xff).toString(16).toUpperCase().padStart("2", 0);
};
const chulidata = (str) => {
let data = str
.replace(/dccc/g, "dc")
.replace(/dccb/g, "db")
.replace(/dcce/g, "de");
return data;
};
const chulidata2 = (str) => {
let data = str
.replace(/DCCC/g, "dc")
.replace(/DCCB/g, "db")
.replace(/DCCE/g, "de");
return data;
};
const getimpedanceList = () => {
let arr = [];
for(let i = 1;i<=10;i++){
arr.push({
num: 0,
time: `00:${String(i).padStart('2',0)}`
})
}
return arr;
};
/**
* 处理上报数据,转换成对象
*/
const getreportData = (str) =>{
let data = {};
data.status = hex2int(str.substring(10,12));//工作状态
data.quantity = hex2int(str.substring(20,22));//电量
data.impedance1 = hex2int(str.substring(24,26));//阻抗低位数据
data.impedance2 = hex2int(str.substring(26,28));//阻抗低位数据
data.jiaoer1 = hex2int(str.substring(28,30));//每秒能量N*10 焦耳N: 0~25.5
data.mode = hex2int(str.substring(12,14));//模式
data.position = hex2int(str.substring(14,16));//档位
//TOREAD 当模式为水份模式
if(data.mode == 6){
data.waterLevel = hex2int(str.substring(16,18))
data.waterPercent = hex2int(str.substring(18,20))
//模式为水份测试时 档位1~3对应 0x01测试成功 0x02: 测试中 0x03: 测试错误
const testResultMap = ['TEST_UNKNOW_STATUS','TEST_SUCCESS','TEST_PROCESSING','TEST_FAIL'];
data.testResult = testResultMap[data.position]||'TEST_UNKNOW_STATUS'
}
return data;
}
/**
* height 高位
* low 低位
* 阻抗算法
* 阻抗低位数据 + 阻抗高位数据 * 256
* 如果以上公式 >=701 固定 701
* <=200 1档
* <=280 2挡
* <=360 3档
* <=440 4挡
* <=520 5挡
* <=600 6挡
* <=700 7挡
* >=701 8挡
*/
const DYNAMIC_BONES_OFFSETS = (height,low) =>{
let total = Number(low)+Number(height)*256;
if(total<=200){
return 1
}
if(total<=280){
return 2
}
if(total<=360){
return 3
}
if(total<=440){
return 4
}
if(total<=520){
return 5
}
if(total<=600){
return 6
}
if(total<=700){
return 7
}
if(total>=701){
return 8
}
}
const getTimeCode = () =>{
let year = new Date().getFullYear();
let month = String(new Date().getMonth() + 1).padStart("2", 0);
let day = String(new Date().getDate()).padStart("2", 0);
let hour = String(new Date().getHours()).padStart("2", 0);
let points = String(new Date().getMinutes()).padStart("2", 0);
let seconds = String(new Date().getSeconds()).padStart("2", 0);
let value = `dbf0a0010b${Number(String(year).substring(0, 2)).toString(16)}${Number(String(year).substring(2, 4)).toString(16)}${Number(month).toString(16).padStart("2", 0)}${Number(day).toString(16).padStart("2", 0)}${Number(hour).toString(16).padStart("2", 0)}${Number(points).toString(16).padStart("2", 0)}${Number(seconds).toString(16).padStart("2", 0)}00000000`;
value = `${value}${ccrc8(value)}de`;
return value;
}
/**
* @param currentVersionNo
* @param latestVersionNo
* @returns needToUpdate
*/
const isNeedToUpdate = (currentVersionNo,latestVersionNo)=>{
if (!currentVersionNo || !latestVersionNo) return false;
let needToUpdate = false;
//硬件版本号相同
let sameHardwareNo = latestVersionNo.substring(0, 2) == currentVersionNo.substring(0, 2);
//软件版本号当前版本比最新版的旧
let latestValue = parseInt(latestVersionNo.substring(2, 6), 16);
let currentValue = parseInt(currentVersionNo.substring(2, 6), 16);
let solftwareNoNeedToUpdate = latestValue > currentValue;
needToUpdate = sameHardwareNo && solftwareNoNeedToUpdate
return needToUpdate
}
/**
* @param compareVersion
* @returns result 1:SDKVersion > version 0:SDKVersion == version -1:SDKVersion < version
*/
const compareVersion = (version)=>{
const systemInfo = wx.getSystemInfoSync();
let SDKVersion = systemInfo.SDKVersion.split('.');
version = version.split('.');
const len = Math.max(SDKVersion.length, version.length)
while (SDKVersion.length < len) {
SDKVersion.push('0')
}
while (version.length < len) {
version.push('0')
}
for (let i = 0; i < len; i++) {
const num1 = parseInt(SDKVersion[i])
const num2 = parseInt(version[i])
if (num1 > num2) {
return 1
} else if (num1 < num2) {
return -1
}
}
return 0
}
/**
* @param checkSameKey
* @returns boolean
*/
const checkSameKey = (array1, array2, key)=>{
for (var i = 0; i < array1.length; i++) {
for (var j = 0; j < array2.length; j++) {
if (array1[i][key] === array2[j][key]) {
return true;
}
}
}
return false;
}
module.exports = {
isVideo,
previewImage,
formatTime,
setdata,
filertext,
formatRichText,
date,
getdates,
getYearFirstLastDay,
ab2hex,
hex2int,
filterData,
ccrc8, //crc8校验位
string2buffer, //将字符串转换成ArrayBufer
s_to_hs, //秒转换成分秒
checkEnd, //相加和
filterBleData,
getofflineData,
getUpgradeData,
keywordTofilter,
checkEnd2,
checkEnd3,
chulidata,
chulidata2,
getimpedanceList,
DYNAMIC_BONES_OFFSETS, //阻抗获取值
getreportData, //解析上报数据
s_to_h,
s_to_s,
minSecToS,
getTimeCode, //同步时间指令
isNeedToUpdate,
compareVersion,
checkSameKey
};