examTeamApp/tools/tools.js

267 lines
6.4 KiB
JavaScript
Raw Normal View History

2024-05-02 15:59:36 +08:00
import $store from '@/store'
2024-07-08 10:50:07 +08:00
import $tools from '@/tools/tools.js'
2024-05-02 15:59:36 +08:00
export default {
msg,
2024-07-08 10:50:07 +08:00
str2Num,
2024-05-29 16:35:45 +08:00
gethms,
2024-05-02 15:59:36 +08:00
str2hex,
hex2str,
ab2hex,
inArray,
getMonth,
2024-05-29 16:35:45 +08:00
getDate,
2024-05-02 15:59:36 +08:00
GetDateStr,
PrefixZero,
2024-05-29 16:35:45 +08:00
validatePhoneEmail,
2024-07-08 10:50:07 +08:00
getBluetoothAdapter,
handleDevicesMac
2024-05-02 15:59:36 +08:00
}
2024-07-08 10:50:07 +08:00
// 蓝牙连接
function handleDevicesMac(device, acd_id) {
console.log("卡片设备", device, acd_id)
if (device == 'true') {
uni.openBluetoothAdapter({
success: e => {
$store.commit("changeBluetooth", true);
uni.navigateTo({
url: "/pages/devices/search?id=" + acd_id
})
console.log('初始化蓝牙成功:' + e.errMsg);
},
fail: err => {
console.log('初始化蓝牙失败:' + err.errMsg);
return $tools.getBluetoothAdapter(err)
}
});
} else {
$tools.msg("请先添加设备!")
setTimeout(function() {
uni.switchTab({
url: "/pages/business/business"
2024-06-13 18:03:50 +08:00
})
2024-07-08 10:50:07 +08:00
}, 500)
}
}
// 蓝牙连接失败
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 => {
2024-06-13 18:03:50 +08:00
uni.showToast({
2024-07-08 10:50:07 +08:00
title: "获取权限成功,请继续去测量",
2024-06-13 18:03:50 +08:00
icon: "none"
})
2024-07-08 10:50:07 +08:00
$store.commit("changeBluetooth", true);
},
fail: err => {
uni.showToast({
title: "请打开手机蓝牙",
icon: "none",
duration: 1000,
})
console.log('初始化蓝牙失败:' + err.errMsg);
2024-06-13 18:03:50 +08:00
}
2024-07-08 10:50:07 +08:00
});
} else {
uni.showToast({
title: "获取权限失败,将无法使用手机蓝牙进行测量",
icon: "none"
})
}
2024-06-13 18:03:50 +08:00
}
})
}
2024-07-08 10:50:07 +08:00
})
} else {
uni.showToast({
title: "请打开手机蓝牙",
icon: "none",
duration: 1000,
})
}
2024-06-13 18:03:50 +08:00
}
2024-05-02 15:59:36 +08:00
// 2进制位数不足补0
function PrefixZero(num, n) {
return (Array(n).join(0) + num).slice(-n);
}
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;
}
2024-05-29 16:35:45 +08:00
function validatePhoneEmail(input) {
const phoneRegex = /^(\+?\d{1,4})?[-\s.]?\(?(\d{3})\)?[-\s.]?(\d{3})[-\s.]?(\d{4})$/;
const emailRegex = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
const isPhone = phoneRegex.test(input);
const isEmail = emailRegex.test(input);
return {
isPhone,
isEmail
};
}
2024-05-02 15:59:36 +08:00
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)
}
2024-07-08 10:50:07 +08:00
function str2Num(str) {
var result = "";
for (let i = 0; i < str.length - 2; i++) {
result += str[i];
if (i % 2 === 1) result += ':';
}
return result + str.slice(-2)
}
2024-05-29 16:35:45 +08:00
// 跳绳分秒时间选择
function gethms(type) {
var mindata = []
var secondData = []
let timeList = []
for (var i = 1; i <= 59; i++) {
i = i > 9 ? i : '0' + i
mindata.push(i + '分');
}
for (var i = 0; i <= 59; i++) {
i = i > 9 ? i : '0' + i
secondData.push(i + '秒');
2024-05-02 15:59:36 +08:00
}
2024-05-29 16:35:45 +08:00
timeList[0] = mindata
timeList[1] = secondData
return timeList
2024-05-02 15:59:36 +08:00
}
2024-05-29 16:35:45 +08:00
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") {
2024-06-13 18:03:50 +08:00
return year + "-" + month + "-01" + "~" + year + "-" + month + "-31";
2024-05-29 16:35:45 +08:00
} else if (month == "02") {
if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) {
2024-06-13 18:03:50 +08:00
return year + '-' + month + "-01" + "~" + year + "-" + month + "-29";
2024-05-29 16:35:45 +08:00
} else {
2024-06-13 18:03:50 +08:00
return year + '-' + month + "-01" + "~" + year + "-" + month + "-28";
2024-05-29 16:35:45 +08:00
};
} else {
2024-06-13 18:03:50 +08:00
return year + '-' + month + "-01" + "~" + year + "-" + month + "-30";
2024-05-29 16:35:45 +08:00
};
};
2024-05-02 15:59:36 +08:00
function getDate(type) {
const date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
2024-05-29 16:35:45 +08:00
month = month > 9 ? month : '0' + month;
2024-05-02 15:59:36 +08:00
day = day > 9 ? day : '0' + day;
if (type === 'tow') {
year = year - 2;
2024-05-29 16:35:45 +08:00
return `${year}/${month}/${day}`;
2024-05-02 15:59:36 +08:00
}
if (type === 'start') {
year = year;
2024-06-13 18:03:50 +08:00
return `${year}-${month}-${day}`;
2024-05-02 15:59:36 +08:00
}
if (type === 'end') {
year = year + 60;
2024-06-13 18:03:50 +08:00
return `${year}-${month}-${day}`;
2024-05-29 16:35:45 +08:00
}
if (type === 'year') {
year = year;
return `${year}`;
}
if (type === 'month') {
year = year;
2024-06-13 18:03:50 +08:00
return `${year}-${month}`;
2024-05-02 15:59:36 +08:00
}
if (type == "m") {
if (month == "01" || month == "03" || month == "05" || month == "07" || month == "08" || month == "10" ||
month == "12") {
2024-06-13 18:03:50 +08:00
return year + "-" + month + "-01" + "~" + year + "-" + month + "-31";
2024-05-02 15:59:36 +08:00
} else if (month == "02") {
if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) {
2024-06-13 18:03:50 +08:00
return year + "-" + month + "-01" + "~" + year + "-" + month + "-29";
2024-05-02 15:59:36 +08:00
} else {
2024-06-13 18:03:50 +08:00
return year + "-" + month + "-01" + "~" + year + "-" + month + "-28";
2024-05-02 15:59:36 +08:00
};
} else {
2024-06-13 18:03:50 +08:00
return year + "-" + month + "-01" + "~" + year + "-" + month + "-30";
2024-05-02 15:59:36 +08:00
};
}
}
2024-05-29 16:35:45 +08:00
//获取AddDayCount天后的日期
2024-05-02 15:59:36 +08:00
function GetDateStr(AddDayCount) {
var dd = new Date();
2024-05-29 16:35:45 +08:00
dd.setDate(dd.getDate() + AddDayCount);
2024-05-02 15:59:36 +08:00
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
2024-06-13 18:03:50 +08:00
return y + "-" + m + "-" + d;
2024-05-29 16:35:45 +08:00
}