adultDeviceApp/tools/tools.js

297 lines
9.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import $store from '@/store'
export default {
msg,
str2hex,
hex2str,
ab2hex,
inArray,
getAge,
getTime,
getDate,
getMonth,
GetDateStr,
handlePages,
getBluetoothAdapter
}
function inArray(arr, key, val) {
if (!arr || !arr.length || typeof arr != 'object' || !Array.isArray(arr)) {
return -1
}
for (let i = 0; i < arr.length; i++) {
if (!key) {
if (arr[i] == val) {
return i
}
} else if (arr[i][key] === val) {
return i
}
}
return -1;
}
function msg(str) {
uni.showToast({
title: str,
icon: 'none'
})
}
function ab2hex(buffer, split) {
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join(split);
}
function hex2str(arr) {
let decoder = new TextDecoder('utf8')
let uint8 = new Uint8Array(arr)
let res = decoder.decode(uint8)
return res
}
function str2hex(str) {
let encoder = new TextEncoder('utf8')
return encoder.encode(str)
}
function getBluetoothAdapter(err) {
if (err.errMsg == "openBluetoothAdapter:fail auth denied" || err.errMsg ===
"openBluetoothAdapter:fail auth deny" ||
err.errMsg === "openBluetoothAdapter:fail authorize no response"
) {
uni.showModal({
title: "提示",
content: "需要您授权使用手机蓝牙",
showCancel: false,
success(modalSuccess) {
uni.openSetting({
success(settingdata) {
if (settingdata.authSetting["scope.bluetooth"]) {
uni.openBluetoothAdapter({
success: e => {
uni.showToast({
title: "获取权限成功,请继续去测量",
icon: "none"
})
$store.commit("changeBluetooth", true);
},
fail: err => {
uni.showToast({
title: "请打开手机蓝牙",
icon: "none",
duration: 1000,
})
console.log('初始化蓝牙失败:' + err.errMsg);
}
});
} else {
uni.showToast({
title: "获取权限失败,将无法使用手机蓝牙进行测量",
icon: "none"
})
}
}
})
}
})
} else {
uni.showToast({
title: "请打开手机蓝牙",
icon: "none",
duration: 1000,
})
}
}
function getDate(type) {
const date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
month = month > 9 ? month : '0' + month;;
day = day > 9 ? day : '0' + day;
if (type === 'tow') {
year = year - 2;
return `${year}-${month}-${day}`;
}
if (type === 'start') {
year = year;
return `${year}-${month}-${day}`;
}
if (type === 'end') {
year = year + 60;
return `${year}-${month}-${day}`;
}
if (type == "m") {
if (month == "01" || month == "03" || month == "05" || month == "07" || month == "08" || month == "10" ||
month == "12") {
return year + "/" + month + "/01" + "~" + year + "/" + month + "/31";
} else if (month == "02") {
if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) {
return year + "/" + month + "/01" + "~" + year + "/" + month + "/29";
} else {
return year + "/" + month + "/01" + "~" + year + "/" + month + "/28";
};
} else {
return year + "/" + month + "/01" + "~" + year + "/" + month + "/30";
};
}
}
function GetDateStr(AddDayCount) {
var dd = new Date();
dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期
var y = dd.getFullYear();
var m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1); //获取当前月份的日期不足10补0
var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate(); //获取当前几号不足10补0
return y + "-" + m + "-" + d;
}
// 获取当前日期
function getTime() {
var date = new Date()
var todate =
((date.getMonth() + 1) < 10 ? ('0' + (date.getMonth() + 1)) : date.getMonth() +
1) + '月' + (date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate() + '日')
return todate
}
// 根据出生日期获取年龄
function getAge(str) {
var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})/);
if (r == null) return false;
var d = new Date(r[1], r[3] - 1, r[4]);
var returnStr = "输入的日期格式错误!";
if (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]) {
var date = new Date();
var yearNow = date.getFullYear();
var monthNow = date.getMonth() + 1;
var dayNow = date.getDate();
var largeMonths = [1, 3, 5, 7, 8, 10, 12], //大月, 用于计算天,只在年月都为零时,天数有效
lastMonth = monthNow - 1 > 0 ? monthNow - 1 : 12, // 上一个月的月份
isLeapYear = false, // 是否是闰年
daysOFMonth = 0; // 当前日期的上一个月多少天
if ((yearNow % 4 === 0 && yearNow % 100 !== 0) || yearNow % 400 === 0) { // 是否闰年, 用于计算天,只在年月都为零时,天数有效
isLeapYear = true;
}
if (largeMonths.indexOf(lastMonth) > -1) {
daysOFMonth = 31;
} else if (lastMonth === 2) {
if (isLeapYear) {
daysOFMonth = 29;
} else {
daysOFMonth = 28;
}
} else {
daysOFMonth = 30;
}
var Y = yearNow - parseInt(r[1]);
var M = monthNow - parseInt(r[3]);
var D = dayNow - parseInt(r[4]);
if (D < 0) {
D = D + daysOFMonth; //借一个月
M--;
}
if (M < 0) { // 借一年 12个月
Y--;
M = M + 12; //
}
if (Y < 0) {
returnStr = "出生日期有误!";
} else if (Y === 0) {
if (M === 0) {
returnStr = D + "天";
} else {
returnStr = M + "个月";
}
} else {
if (M === 0) {
returnStr = Y + "岁";
} else {
returnStr = Y + "岁" + M + "个月";
}
}
}
return returnStr;
}
function getMonth(dates, months) {
var d = new Date(dates.substring(0, 10));
let year = d.getFullYear();
var month = d.getMonth() + 1;
if (Math.abs(months) > 12) {
months = months % 12;
};
if (months != 0) {
if (month + months > 12) {
year++;
month = (month + months) % 12;
} else if (month + months < 1) {
year--;
month = 12 + month + months;
} else {
month = month + months;
};
};
month = month < 10 ? "0" + month : month;
var date = d.getDate();
if (month == "01" || month == "03" || month == "05" || month == "07" || month == "08" || month == "10" ||
month == "12") {
return year + "/" + month + "/01" + "~" + year + "/" + month + "/31";
} else if (month == "02") {
if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) {
return year + '/' + month + "/01" + "~" + year + "/" + year + "/" + month + "/29";
} else {
return year + '/' + month + "/01" + "~" + year + "/" + month + "/28";
};
} else {
return year + '/' + month + "/01" + "~" + year + "/" + month + "/30";
};
};
// 页面跳转
function handlePages(type, deviceId) {
if (type == 1) {
uni.redirectTo({
url: "/BLEPages/adult/PCD01pro?deviceId=" + deviceId
})
return
}
if (type == 4) {
uni.redirectTo({
url: "/BLEPages/adult/PCL01?deviceId=" + deviceId
})
return
}
if (type == 8) {
uni.redirectTo({
url: "/BLEPages/adult/H01pro?deviceId=" + deviceId
})
return
}
if (type == 14) {
uni.redirectTo({
url: "/BLEPages/adult/F01B?deviceId=" + deviceId
})
return
}
if (type == 21) {
uni.redirectTo({
url: "/BLEPages/adult/H09B?deviceId=" + deviceId
})
return
}
}