ai-watch-app/utils/utils.js

692 lines
15 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 w_md5 from "./w_md5.js";
import store from "../store/index.js";
export const copyThis = (str) => {
uni.setClipboardData({
data: str, //要被复制的内容
success: () => { //复制成功的回调函数
uni.showToast({ //提示
title: "复制成功"
})
}
});
}
export const goPath = (path,type) => {
uni.navigateTo({
url:path,
fail(e) {
console.log(e)
}
})
}
export const md5 = (bStr,type = 2) => {
type = parseInt(type)
let str = ''
switch(type){
case 0:
str = w_md5.hex_md5_16(bStr)//16位小写
break;
case 1:
str = w_md5.hex_md5_16Upper(bStr)//16位大写
break;
case 2:
str = w_md5.hex_md5_32(bStr);//32位小写
break;
case 3:
str = w_md5.hex_md5_32Upper(bStr);//32位大写
break;
}
return str
}
export const getPwd = (str) => {
if(str == ''){
return ''
}
return md5('bmsc-'+str)
}
export const getVersion = () => {
// 获取当前app的版本
let version = ''
// #ifdef APP-PLUS
const systemInfo = uni.getSystemInfoSync();
version = systemInfo.appWgtVersion;
// #endif
return version;
}
export const back = () => {
uni.navigateBack({
delta: 1
})
}
export const getBodyInfoData = () => {
let bodyDataExample = {
BMI:{
name:"BMI",
tags:[
{
name:'偏瘦',
colorIdx:0,
val:18.5,
icon_key:'icon-down-arrow'
},
{
name:'完美',
colorIdx:1,
val:24,
icon_key:'icon-success'
},
{
name:'超重',
colorIdx:2,
val:28,
icon_key:"icon-up-arrow"
},
{
name:'肥胖',
colorIdx:3,
val:0,
icon_key:"icon-up-arrow"
}
],
unit:'',// 计量单位
icon:'https://img.agrimedia.cn/-_BMI-tuya.png'
},
bodyFatPercentage:{
name:"体脂率",
tags:[
{
name:'偏瘦',
colorIdx:0,
val:18,
icon_key:'icon-down-arrow'
},
{
name:'完美',
colorIdx:1,
val:23,
icon_key:'icon-success'
},
{
name:'超重',
colorIdx:2,
val:30,
icon_key:"icon-up-arrow"
},
{
name:'肥胖',
colorIdx:3,
val:0,
icon_key:"icon-up-arrow"
}
],
unit:'%',// 计量单位
icon:'https://img.agrimedia.cn/%E4%BD%93%E8%84%82%E7%8E%871-tuya.png'
},
leanBodyMass:{
name:"去脂体重",
tags:[
{
name:'低',
colorIdx:0,
val:45.8,
icon_key:'icon-down-arrow'
},
{
name:'标准',
colorIdx:1,
val:55.9,
icon_key:'icon-success'
},
{
name:'完美',
colorIdx:4,
val:0,
icon_key:"icon-success"
},
],
unit:'千克',// 计量单位
icon:'https://img.agrimedia.cn/%E5%8E%BB%E8%84%82%E4%BD%93%E9%87%8D-tuya.png'
},
muscleRate:{
name:"肌肉率",
tags:[
{
name:'低',
colorIdx:0,
val:68.1,
icon_key:'icon-down-arrow'
},
{
name:'标准',
colorIdx:1,
val:84.8,
icon_key:'icon-success'
},
{
name:'完美',
colorIdx:4,
val:0,
icon_key:"icon-success"
},
],
unit:'%',// 计量单位
icon:'https://img.agrimedia.cn/Surgery-tuya.png'
},
muscleMass:{
name:"肌肉量",
tags:[
{
name:'低',
colorIdx:0,
val:40.8,
icon_key:'icon-down-arrow'
},
{
name:'标准',
colorIdx:1,
val:50.8,
icon_key:'icon-success'
},
{
name:'完美',
colorIdx:4,
val:0,
icon_key:"icon-success"
},
],
unit:'千克',// 计量单位
icon:'https://img.agrimedia.cn/watch-app/exercise-tuya.png'
},
subcutaneousFat:{
name:"皮下脂肪",
tags:[
{
name:'低',
colorIdx:0,
val:8.6,
icon_key:'icon-down-arrow'
},
{
name:'标准',
colorIdx:1,
val:16.7,
icon_key:'icon-success'
},
{
name:'高',
colorIdx:2,
val:0,
icon_key:"icon-up-arrow"
},
],
unit:'%',// 计量单位
icon:'https://img.agrimedia.cn/008%E7%9A%AE%E4%B8%8B%E8%84%82%E8%82%AA-tuya.png'
},
bodyMoisture:{
name:"体内水分",
tags:[
{
name:'低',
colorIdx:0,
val:53.4,
icon_key:'icon-down-arrow'
},
{
name:'标准',
colorIdx:1,
val:66.6,
icon_key:'icon-success'
},
{
name:'完美',
colorIdx:4,
val:0,
icon_key:"icon-up-arrow"
},
],
unit:'%',// 计量单位
icon:'https://img.agrimedia.cn/watch-app/%E6%B0%B42-tuya.png'
},
skeletalMuscleRate:{
name:"骨骼肌",
tags:[
{
name:'低',
colorIdx:0,
val:25,
icon_key:'icon-down-arrow'
},
{
name:'标准',
colorIdx:1,
val:35,
icon_key:'icon-success'
},
{
name:'完美',
colorIdx:4,
val:0,
icon_key:"icon-success"
},
],
unit:'%',// 计量单位
icon:'https://img.agrimedia.cn/watch-app/%E9%AA%A8%E9%AA%BC%E8%82%8C%E7%8E%87-tuya.png'
},
boneMass:{
name:"骨重",
tags:[
{
name:'低',
colorIdx:0,
val:2.9,
icon_key:'icon-down-arrow'
},
{
name:'标准',
colorIdx:1,
val:3.7,
icon_key:'icon-success'
},
{
name:'完美',
colorIdx:4,
val:0,
icon_key:"icon-up-arrow"
},
],
unit:'千克',// 计量单位
icon:'https://img.agrimedia.cn/%E9%AA%A8%E9%87%8D.png'
},
proteinAmount:{
name:"蛋白质",
tags:[
{
name:'低',
colorIdx:0,
val:14.1,
icon_key:'icon-down-arrow'
},
{
name:'标准',
colorIdx:1,
val:17.7,
icon_key:'icon-success'
},
{
name:'完美',
colorIdx:4,
val:0,
icon_key:"icon-up-arrow"
},
],
unit:'%',// 计量单位
icon:'https://img.agrimedia.cn/%E8%9B%8B%E7%99%BD%E8%B4%A8-tuya.png'
},
basalMetabolicRate:{
name:"基础代谢",
tags:[
{
name:'低',
colorIdx:0,
val:1619,
icon_key:'icon-down-arrow'
},
{
name:'完美',
colorIdx:4,
val:0,
icon_key:"icon-success"
},
],
unit:'千卡',// 计量单位
icon:'https://img.agrimedia.cn/%E5%9F%BA%E7%A1%80%E4%BB%A3%E8%B0%A2.png'
},
}
return bodyDataExample;
}
export const getBodyKeyInfo = (val,key_name) => {
let colorArr = [
"#5990f5",
"#29ce70",
"#fcb041",
"#fa6820",
"#1da30f"
]
let back = {}
let data = getBodyInfoData()[key_name].tags
data.map((v,i) => {
if(i == 0){
if(val < v.val){
back = v
back.prev_val = 0
back.idx = i
back.color = colorArr[i]
back.margin = ((val / (v.val - 0)) * 100).toString() + '%'
}
}else{
if(val >= data[i-1].val && (val < v.val || v.val == 0)){
back = v
back.prev_val = data[i-1].val
back.idx = i
back.color = colorArr[i]
if(v.val == 0){
back.margin = '20%'
}else{
back.margin = (((val - data[i-1].val) / (v.val - data[i-1].val)) * 100).toString() + '%'
}
}
}
})
return back
}
export const makeSign = (obj,serverTime) => {
obj = JSON.parse(JSON.stringify(obj));
let app_key = 'm58c97';
let app_secret = 'e9c0a04382655dfgb3c98d11477ad547';
// let serverTime = (store.getters['api/getServerTime'] == 0?(parseInt(Date.now() /1000)):store.getters['api/getServerTime'])
obj.app_key = app_key
obj.timestamp = serverTime
let arr = Object.keys(obj)
arr.sort()
let o = {}
arr.forEach((v,i) => {
o[v] = obj[v]
})
let arr2 = Object.keys(o)
let str = ''
arr2.forEach((v,i) => {
if(o[v] !== ''){
if (typeof(o[v]) == 'object') {
o[v] = JSON.stringify(o[v]);
}
str += v + '=' + o[v] + '&'
}
})
if(str != ''){
str = str.substr(0,str.length - 1)
}
let md = md5(str + app_secret)
return md
}
// APP下执行
export const DeviceType = () => {
let platform = uni.getSystemInfoSync().platform;
if (platform == 'ios') {
return 'ios'
} else if (platform == 'android'){
return 'android'
}
}
// 地址 标题名 分页跳转类型
export const EasyPay = (url, title, pageType) => {
// #ifdef APP-PLUS
uni.navigateTo({
url: `/pages/user/pay_webview/pay_webview?title=${title}&urlLink=${encodeURIComponent(url)}&pageType=${pageType}`
})
// #endif
// #ifdef H5
location.href = url;
// #endif
}
// 保存图片
export const saveImg = (img) => {
let arr = [];
arr.push(img);
uni.previewImage({
urls: arr , //预览图片的地址,必须要数组形式
longPressActions: {
itemList:['保存图片'],
success: res=> {
uni.downloadFile({
url: img,
success(data) {
if (data.statusCode == 200) {
uni.saveImageToPhotosAlbum({
filePath:data.tempFilePath,
success: function() {
uni.showToast({
title: '保存成功',
icon: "success"
})
}
})
}
}
})
}
}
})
}
// 数组去空
export const trimSpace = (array) => {
var newArr = [];
for (var i = 0; i < array.length; i++) {
if(array[i] !== undefined && array[i] !== null && array[i] !== '') {
newArr.push(array[i]);
}
}
return newArr;
}
// 时间转换(时间格式2021-07-02 121212 显示为几分钟前,几小时前等)
export const timeChange = (dateTimeStamp) => {
var dateTimeStamp = Date.parse(dateTimeStamp.replace(/-/gi, "/"));
var minute = 1000 * 60; //把分,时,天,周,半个月,一个月用毫秒表示
var hour = minute * 60;
var day = hour * 24;
var week = day * 7;
var halfamonth = day * 15;
var month = day * 30;
var now = new Date().getTime(); //获取当前时间毫秒
var diffValue = now - dateTimeStamp; //时间差
if (diffValue < 0) {
return '刚刚';
}
var minC = diffValue / minute; //计算时间差的分,时,天,周,月
var hourC = diffValue / hour;
var dayC = diffValue / day;
var weekC = diffValue / week;
var monthC = diffValue / month;
var result = "2";
if (monthC >= 1 && monthC <= 3) {
result = " " + parseInt(monthC) + "月前"
} else if (weekC >= 1 && weekC <= 4) {
result = " " + parseInt(weekC) + "周前"
} else if (dayC >= 1 && dayC <= 6) {
result = " " + parseInt(dayC) + "天前"
} else if (hourC >= 1 && hourC <= 23) {
result = " " + parseInt(hourC) + "小时前"
} else if (minC >= 1 && minC <= 59) {
result = " " + parseInt(minC) + "分钟前"
} else if (diffValue >= 0 && diffValue <= minute) {
result = "刚刚"
} else {
var datetime = new Date();
datetime.setTime(dateTimeStamp);
var Nyear = datetime.getFullYear(); {}
var Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
var Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
var Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
var Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
var Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
result = Nyear + "-" + Nmonth + "-" + Ndate
}
return result;
}
//检测utf16emoji表情 转换为实体字符以供后台存储
export const utf16toEntities = (str) => {
var patt = /[\ud800-\udbff][\udc00-\udfff]/g;
str = str.replace(patt, function(char) {
var H, L, code;
if (char.length === 2) { //辅助平面字符(我们需要做处理的一类)
H = char.charCodeAt(0); // 取出高位
L = char.charCodeAt(1); // 取出低位
code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00; // 转换算法
return "&#" + code + ";";
} else {
return char;
}
});
return str;
}
// base64转二进制文件
export const base64ToBlob = (urlData, type) => {
let arr = urlData.split(',');
let mime = arr[0].match(/:(.*?);/)[1] || type;
// 去掉url的头并转化为byte
let bytes = window.atob(arr[1]);
// 处理异常,将ascii码小于0的转换为大于0
let ab = new ArrayBuffer(bytes.length);
// 生成视图直接针对内存8位无符号整数长度1个字节
let ia = new Uint8Array(ab);
for (let i = 0; i < bytes.length; i++) {
ia[i] = bytes.charCodeAt(i);
}
return new Blob([ab], {
type: mime
});
}
export const userLeave = (value) => {
if (value == 2) {
return '管理员'
} else if (value == 1) {
return '群主'
} else if (value == 0) {
return '普通群员'
}
}
// 时间戳转日期
export const timestampToTime = (timestamp) => {
// 时间戳为10位需*1000时间戳为13位不需乘1000
var date = new Date(timestamp * 1000);
var Y = date.getFullYear() + "-";
var M = (date.getMonth() + 1 < 10? "0" + (date.getMonth() + 1): date.getMonth() + 1) + "-";
var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
var h = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
var m = (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) + ":";
var s = (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds());
return Y + M + D + h + m + s;
}
export const isDateTimeBetween = (startDateTime, endDateTime, currentDateTime) => {
const startDate = new Date(startDateTime);
const endDate = new Date(endDateTime);
const currentDate = new Date(currentDateTime);
if (startDate <= endDate) {
return currentDate >= startDate && currentDate <= endDate;
} else {
// 处理跨天的情况
return currentDate >= startDate || currentDate <= endDate;
}
}
export const areAllIdsEmpty = (arr) => {
// 遍历数组中的每个对象
for (let i = 0; i < arr.length; i++) {
console.log(arr[i].ids)
// 如果对象的ids属性不是空数组则立即返回true
if (arr[i].ids && arr[i].ids.length > 0) {
return true; // 至少有一个ids不是空的返回true
}
}
}
export const getSunmiSerial = () => {
var sunmi_serial = '';
// #ifdef APP-PLUS
const dataKey = 'getDeviceSn'; // 同上面设置的key
sunmi_serial = uni.getStorageSync(dataKey)
if(!sunmi_serial){
var Build = plus.android.importClass("android.os.Build");
var SystemProperties = plus.android.importClass("android.os.SystemProperties");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
sunmi_serial = Build.getSerial()
// sunmi_serial = SystemProperties.get('ro.sunmi.serial')
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
sunmi_serial = Build.getSerial()
} else{
sunmi_serial = Build.SERIAL
}
uni.setStorageSync(dataKey,sunmi_serial)
}
// #endif
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// sunmi_serial = Build.getSerial()
// sunmi_serial = SystemProperties.get('ro.sunmi.serial')
// } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// sunmi_serial = Build.getSerial()
// } else{
// sunmi_serial = Build.SERIAL
// }
return sunmi_serial
}
export const getArrMaxValue = (arr) => {
if (!arr || arr.length === 0) {
return '--'; // 值是0或空就不显示
}
var max = arr[0];
for (var i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
export default {
copyThis,
goPath,
md5,
makeSign,
getPwd,
DeviceType,
EasyPay,
saveImg,
trimSpace,
timeChange,
utf16toEntities,
userLeave,
timestampToTime,
isDateTimeBetween,
areAllIdsEmpty,
getArrMaxValue,
getBodyInfoData,
getBodyKeyInfo,
getVersion,
back,
getSunmiSerial
}