adultDeviceApp/tools/tools.js

297 lines
7.2 KiB
JavaScript
Raw Normal View History

2022-05-03 21:35:39 +08:00
import $store from '@/store'
export default {
2022-05-11 09:24:06 +08:00
msg,
str2hex,
hex2str,
ab2hex,
inArray,
getAge,
getTime,
getDate,
getMonth,
GetDateStr,
handlePages,
getBluetoothAdapter
2022-05-03 21:35:39 +08:00
}
function inArray(arr, key, val) {
2022-05-11 09:24:06 +08:00
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;
2022-05-03 21:35:39 +08:00
}
function msg(str) {
2022-05-11 09:24:06 +08:00
uni.showToast({
title: str,
icon: 'none'
})
2022-05-03 21:35:39 +08:00
}
function ab2hex(buffer, split) {
2022-05-11 09:24:06 +08:00
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join(split);
2022-05-03 21:35:39 +08:00
}
function hex2str(arr) {
2022-05-11 09:24:06 +08:00
let decoder = new TextDecoder('utf8')
let uint8 = new Uint8Array(arr)
let res = decoder.decode(uint8)
return res
2022-05-03 21:35:39 +08:00
}
function str2hex(str) {
2022-05-11 09:24:06 +08:00
let encoder = new TextEncoder('utf8')
return encoder.encode(str)
2022-05-03 21:35:39 +08:00
}
function getBluetoothAdapter(err) {
2022-05-11 09:24:06 +08:00
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,
})
}
2022-05-03 21:35:39 +08:00
}
function getDate(type) {
2022-05-11 09:24:06 +08:00
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";
};
}
2022-05-03 21:35:39 +08:00
}
function GetDateStr(AddDayCount) {
2022-05-11 09:24:06 +08:00
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;
2022-05-03 21:35:39 +08:00
}
// 获取当前日期
function getTime() {
2022-05-11 09:24:06 +08:00
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
2022-05-03 21:35:39 +08:00
}
// 根据出生日期获取年龄
function getAge(str) {
2022-05-11 09:24:06 +08:00
var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})/);
if (r == null) return false;
2022-05-03 21:35:39 +08:00
2022-05-11 09:24:06 +08:00
var d = new Date(r[1], r[3] - 1, r[4]);
var returnStr = "输入的日期格式错误!";
2022-05-03 21:35:39 +08:00
2022-05-11 09:24:06 +08:00
if (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]) {
2022-05-03 21:35:39 +08:00
2022-05-11 09:24:06 +08:00
var date = new Date();
var yearNow = date.getFullYear();
var monthNow = date.getMonth() + 1;
var dayNow = date.getDate();
2022-05-03 21:35:39 +08:00
2022-05-11 09:24:06 +08:00
var largeMonths = [1, 3, 5, 7, 8, 10, 12], //大月, 用于计算天,只在年月都为零时,天数有效
lastMonth = monthNow - 1 > 0 ? monthNow - 1 : 12, // 上一个月的月份
isLeapYear = false, // 是否是闰年
daysOFMonth = 0; // 当前日期的上一个月多少天
2022-05-03 21:35:39 +08:00
2022-05-11 09:24:06 +08:00
if ((yearNow % 4 === 0 && yearNow % 100 !== 0) || yearNow % 400 === 0) { // 是否闰年, 用于计算天,只在年月都为零时,天数有效
isLeapYear = true;
}
2022-05-03 21:35:39 +08:00
2022-05-11 09:24:06 +08:00
if (largeMonths.indexOf(lastMonth) > -1) {
daysOFMonth = 31;
} else if (lastMonth === 2) {
if (isLeapYear) {
daysOFMonth = 29;
} else {
daysOFMonth = 28;
}
} else {
daysOFMonth = 30;
}
2022-05-03 21:35:39 +08:00
2022-05-11 09:24:06 +08:00
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; //
}
2022-05-03 21:35:39 +08:00
2022-05-11 09:24:06 +08:00
if (Y < 0) {
returnStr = "出生日期有误!";
2022-05-03 21:35:39 +08:00
2022-05-11 09:24:06 +08:00
} else if (Y === 0) {
if (M === 0) {
returnStr = D + "天";
} else {
returnStr = M + "个月";
}
} else {
if (M === 0) {
returnStr = Y + "岁";
} else {
returnStr = Y + "岁" + M + "个月";
}
}
}
return returnStr;
2022-05-03 21:35:39 +08:00
}
function getMonth(dates, months) {
2022-05-11 09:24:06 +08:00
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";
};
2022-05-03 21:35:39 +08:00
};
// 页面跳转
function handlePages(type, deviceId) {
2022-05-11 09:24:06 +08:00
if (type == 1) {
uni.redirectTo({
url: "/BLEPages/adult/PCD01pro?deviceId=" + deviceId
})
return
}
if (type == 4 || type == 16) {
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
}
2022-05-03 21:35:39 +08:00
}