第一版

This commit is contained in:
17792275749
2025-03-25 20:52:15 +08:00
commit 04d373b84e
88 changed files with 4539 additions and 0 deletions
+475
View File
@@ -0,0 +1,475 @@
const app = getApp();
import $ from "../../utils/request";
Page({
data: {
deviceConnect: false,
initBluetooth: "0",
customerType: "1",
id: "",
realname: "",
sex: {
index: null,
data: [{
id: 1,
name: '男'
}, {
id: 2,
name: '女'
}]
},
birthday: {
label: "1980年01月01日",
value: "1980-01-01"
},
phone: "",
height: {
index: 50,
data: Array.from({ length: 200 - 120 + 1 }, (_, i) => i + 120)
},
weight: {
index: 20,
data: Array.from({ length: 100 - 50 + 1 }, (_, i) => i + 50)
},
},
onLoad(options) {
// initBluetooth 是否初始化蓝牙和设备 1 是 / 0 否
// customerType 用户类型 1 自己 / 0 他人
// id 用户ID 当customerType为0时,家庭成员的用户ID / 为1时始终为空字符串
this.setData({
id: options.id ? options.id : null,
initBluetooth: options.initBluetooth,
customerType: options.customerType,
})
if(options.customerType === "0") {
wx.setNavigationBarTitle({
title: options.id ? '编辑成员档案' : '新增成员档案'
})
if(options.id) {
this.subUserInfo()
}
} else {
let userInfo = wx.getStorageSync("userInfo") || null;
if (userInfo && options.initBluetooth === "1") {
let [year, month, day] = userInfo.birthday.split("-");
this.setData({
realname: userInfo.realname,
["sex.index"]: this.data.sex.data.findIndex(item => {return item.name === userInfo.sex_dictText}),
birthday: {
label: year + "年" + month + "月" + day + "日",
value: userInfo.birthday
},
phone: userInfo.phone,
["height.index"]: this.data.height.data.findIndex(item => {return item === userInfo.height}),
["weight.index"]: this.data.weight.data.findIndex(item => {return item === userInfo.weight}),
})
}
}
if(options.initBluetooth === "1") {
this.initBluetooth();
app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", (res) => {
console.log('deviceInfo ===》deviceConnect', res);
app.globalData.ppScale.plugin.ScaleAction.startDataProgress();
app.globalData.ppScale.activeProtocol = app.globalData.ppScale.plugin.ScaleAction.getActiveProtocol();
app.globalData.ppScale.activeProtocol.codeUpdateMTU((res) => {
console.log("deviceInfo ===》codeUpdateMTU", res);
$.ajax("weighingScale/list/device", {}, "GET", false).then((res) => {
if(res.result && res.result.length) {
let deviceConnect = res.result.some(item => item.macAddress === app.globalData.ppScale.device.mac);
if(deviceConnect) {
this.setData({
deviceConnect: true
})
} else {
app.globalData.ppScale.plugin.Blue.disconnect();
}
}
})
});
});
}
},
// 初始化蓝牙
initBluetooth() {
wx.openBluetoothAdapter({
success: () => {
this.getDeviceSettingList();
},
fail: (err) => {
if (err.errCode === 10001) {
wx.showModal({
title: "提示",
content: "请打开手机蓝牙",
});
}
},
});
},
async refreshToken(fn, flag) {
let bodyToken = wx.getStorageSync('bodyToken');
let bodyTokenTime = wx.getStorageSync('bodyTokenTime');
if (!bodyToken || bodyTokenTime * 1000 < +new Date() || flag) {
let res = await app.globalData.ppScale.plugin.refreshToken({
url: "https://uniquehealth.lefuenergy.com",
data: {
"appKey": app.globalData.ppScale.options.key,
"appSecret": app.globalData.ppScale.options.secret
}
});
if (res.data.code == 200) {
wx.setStorageSync('bodyToken', res.data.data.token);
wx.setStorageSync('bodyTokenTime', res.data.data.expireTime);
fn();
}
} else {
fn();
}
},
getDeviceSettingList() {
let bodyToken = wx.getStorageSync("bodyToken");
app.globalData.ppScale.plugin.getDeviceSettingList({
url: "https://uniquehealth.lefuenergy.com",
data: {
appKey: app.globalData.ppScale.options.key
},
header: {
"token": bodyToken,
"Accept-Language": wx.getStorageSync("lang") || 'zh'
}
}).then((res) => {
if (res.data.code == 200) {
app.globalData.ppScale.plugin.Blue.setDeviceSetting(res.data.data);
app.globalData.ppScale.device.setting = res.data.data;
let deviceNames = res.data.data.map(item => item.deviceName);
app.globalData.ppScale.plugin.Blue.start(deviceNames, false);
app.globalData.ppScale.plugin.bus.subscribe("devicesList", (res_) => {
app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery();
app.globalData.ppScale.plugin.Blue.createBLEConnection(res_[0]);
});
} else if (res.data.code == 401 || res.data.code == 4008) {
// 401 token过期 / 4008 token为空
// 重新请求token解析数据
this.refreshToken(() => {
this.getDeviceSettingList();
}, true);
}
});
},
// 姓名输入
realnameInput(e) {
let realname = e.detail.value.replace(/\s+/g, '');
this.setData({
realname: realname
})
},
// 性别选择
sexChange(e) {
let sex = e.detail.value;
console.log(sex);
this.setData({
['sex.index']: e.detail.value
})
if(this.data.height.index === null) {
this.setData({
['height.index']: sex == 0 ? 50 : 40
})
}
if(this.data.weight.index === null) {
this.setData({
['weight.index']: sex == 0 ? 20 : 10
})
}
},
// 生日选择
birthdayChange(e) {
let v = e.detail.value;
let [year, month, day] = v.split("-");
this.setData({
birthday: {
label: year + "年" + month + "月" + day + "日",
value: v
}
})
},
// 获取手机号
getPhoneNumber(e) {
let params = {
code: e.detail.code,
thirdId: wx.getStorageSync("userInfo").thirdId
};
$.ajax("weighingScale/getPhone", params, "POST").then(res => {
wx.setStorageSync("userInfo", res.result.user);
wx.setStorageSync("token", res.result.token);
this.setData({
phone: res.result.user.phone
})
})
},
// 身高选择
heightChange(e) {
this.setData({
['height.index']: e.detail.value
})
},
// 体重选择
weightChange(e) {
this.setData({
['weight.index']: e.detail.value
})
},
// 提交判断
submit() {
let realname = this.data.realname;
let sex = this.data.sex;
let birthday = this.data.birthday;
let phone = this.data.phone;
let height = this.data.height;
let weight = this.data.weight;
if(this.data.initBluetooth === "1" && !this.data.deviceConnect) {
wx.showToast({
icon: "none",
title: "请靠近设备并点亮后再试"
})
this.initBluetooth();
return false;
}
if(!realname) {
wx.showToast({
icon: "none",
title: "姓名不能为空"
})
return false;
}
if(sex.index === null) {
wx.showToast({
icon: "none",
title: "性别不能为空"
})
return false;
}
if(!birthday.value) {
wx.showToast({
icon: "none",
title: "生日不能为空"
})
return false;
}
if(this.data.customerType === "1" && !phone) {
wx.showToast({
icon: "none",
title: "手机号不能为空"
})
return false;
}
if(height.index === null) {
wx.showToast({
icon: "none",
title: "身高不能为空"
})
return false;
}
if(weight.index === null) {
wx.showToast({
icon: "none",
title: "体重不能为空"
})
return false;
}
if(this.data.customerType === "1") {
this.submitMe({
realname: realname,
sex: sex.data[sex.index].id,
birthday: birthday.value,
phone: phone,
height: height.data[height.index],
weight: weight.data[weight.index]
});
} else {
this.submitUser({
id: this.data.id,
realname: realname,
sex: sex.data[sex.index].id,
birthday: birthday.value,
height: height.data[height.index],
weight: weight.data[weight.index]
});
}
},
// 提交自己的信息
submitMe(params) {
$.ajax("weighingScale/edit/user", params, "POST", true, "保存中...").then(res => {
$.ajax("weighingScale/select/user", {}, "GET", true, "更新用户信息中...").then(res_ => {
wx.setStorageSync("userInfo", res_.result);
wx.showToast({
icon: "none",
title: res.message
})
if(this.data.initBluetooth === "1") {
app.globalData.ppScale.activeProtocol.dataSyncUserInfo({
userID: res_.result.id,
userName: res_.result.realname,
memberID: "",
age: this.getAge(res_.result.birthday), //
gender: res_.result.sex, //
height: res_.result.height, //
isAthleteMode: 0,
currentWeight: res_.result.weight, //
deviceHeaderIndex: 0,
targetWeight: "",
idealWeight: "",
recentData: [],
}, (res) => {
console.log("dataSyncUserInfo", res)
app.globalData.ppScale.plugin.Blue.stop();
setTimeout(() => {
wx.navigateBack({
delta: 1
})
}, 1500)
})
} else {
setTimeout(() => {
wx.navigateBack({
delta: 1
})
}, 1500)
}
})
})
},
// 提交家庭成员的信息
submitUser(params) {
$.ajax("weighingScale/edit/subUser", params, "POST", true, "保存中...").then(res => {
wx.showToast({
icon: "none",
title: res.message
})
app.globalData.ppScale.activeProtocol.dataSyncUserInfo({
userID: wx.getStorageSync("userInfo").id,
userName: params.realname,
memberID: res.result,
age: this.getAge(params.birthday), //
gender: params.sex, //
height: params.height, //
isAthleteMode: 0,
currentWeight: params.weight, //
deviceHeaderIndex: 0,
targetWeight: "",
idealWeight: "",
recentData: [],
}, (res) => {
console.log("dataSyncUserInfo", res)
app.globalData.ppScale.plugin.Blue.stop();
setTimeout(() => {
wx.navigateBack({
delta: 1
})
}, 1500)
})
})
},
// 删除家庭成员
delUser() {
if(this.data.deviceConnect) {
wx.showModal({
title: "提示",
content: "你要定要删除此成员吗?",
success: (res) => {
if (res.confirm) {
let params = {
id: this.data.id
};
console.log(params);
$.ajax("weighingScale/del/subUser", params, "POST", true, "删除中...").then(res => {
wx.showToast({
icon: "none",
title: res.message
})
app.globalData.ppScale.activeProtocol.dataDeleteUser({
userID: wx.getStorageSync("userInfo").id,
memberID: this.data.id,
}, (res) => {
console.log("dataDeleteUser", res);
app.globalData.ppScale.plugin.Blue.stop();
setTimeout(() => {
wx.navigateBack({
delta: 1
})
}, 1500);
})
})
}
}
})
} else {
wx.showToast({
icon: "none",
title: "删除失败,请靠近设备并点亮后再试"
})
this.initBluetooth();
}
},
// 获取家庭成员信息详情
subUserInfo() {
let params = {
userId: this.data.id
};
$.ajax("weighingScale/select/subUser", params, "GET", false).then(res => {
let [year, month, day] = res.result.birthday.split("-");
this.setData({
realname: res.result.realname,
["sex.index"]: this.data.sex.data.findIndex(item => {return item.name === res.result.sex_dictText}),
birthday: {
label: year + "年" + month + "月" + day + "日",
value: res.result.birthday
},
["height.index"]: this.data.height.data.findIndex(item => {return item === res.result.height}),
["weight.index"]: this.data.weight.data.findIndex(item => {return item === res.result.weight}),
})
})
},
// 根据生日获取年龄
getAge(birthDateString) {
const birthDate = new Date(birthDateString);
const today = new Date();
let age = today.getFullYear() - birthDate.getFullYear();
const monthDiff = today.getMonth() - birthDate.getMonth();
const dayDiff = today.getDate() - birthDate.getDate();
// 如果当前月份小于出生月份,或者同月但当前日期小于出生日期,年龄需要减 1
if (monthDiff < 0 || (monthDiff === 0 && dayDiff < 0)) {
age--;
}
return age;
},
onUnload() {
if(this.data.initBluetooth === "1") {
app.globalData.ppScale.plugin.Blue.stop();
}
}
})