468 lines
11 KiB
JavaScript
468 lines
11 KiB
JavaScript
const app = getApp();
|
|
import $ from "../../utils/request";
|
|
|
|
Page({
|
|
data: {
|
|
deviceConnect: false,
|
|
|
|
initBluetooth: "0",
|
|
customerType: "1",
|
|
deviceInfo: null,
|
|
|
|
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.getDevice();
|
|
|
|
app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", (res) => {
|
|
console.log('userInfo ===》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("userInfo ===》codeUpdateMTU", res);
|
|
|
|
this.setData({
|
|
deviceConnect: true
|
|
})
|
|
});
|
|
});
|
|
}
|
|
},
|
|
|
|
// 获取已经绑定的设备列表
|
|
getDevice() {
|
|
let params = {};
|
|
$.ajax("weighingScale/list/device", params, "GET", false).then((res) => {
|
|
if(res.result && res.result.length) {
|
|
this.setData({
|
|
deviceInfo: res.result[0]
|
|
})
|
|
this.initBluetooth();
|
|
} else {
|
|
wx.showModal({
|
|
title: "提示",
|
|
content: "当前暂未绑定设备,需要绑定吗?",
|
|
confirmText: "去绑定",
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
wx.navigateTo({
|
|
url: "/pages/searchDevice/searchDevice"
|
|
})
|
|
} else if (res.cancel) {
|
|
wx.navigateBack({
|
|
delta: 1
|
|
})
|
|
}
|
|
}
|
|
})
|
|
}
|
|
})
|
|
},
|
|
|
|
// 初始化蓝牙
|
|
initBluetooth() {
|
|
wx.openBluetoothAdapter({
|
|
success: () => {
|
|
this.getDeviceSettingList();
|
|
},
|
|
fail: (err) => {
|
|
if (err.errCode === 10001) {
|
|
wx.showModal({
|
|
title: "提示",
|
|
content: "请打开手机蓝牙",
|
|
});
|
|
}
|
|
},
|
|
});
|
|
},
|
|
|
|
getDeviceSettingList() {
|
|
let seviceList = [];
|
|
app.globalData.ppScale.device.setting.map(item => {
|
|
if(this.data.deviceInfo.type === item.deviceName) {
|
|
seviceList.push(item);
|
|
}
|
|
})
|
|
if(seviceList && seviceList.length) {
|
|
app.globalData.ppScale.plugin.Blue.setDeviceSetting(seviceList);
|
|
app.globalData.ppScale.plugin.Blue.start(seviceList[0].deviceName, false);
|
|
app.globalData.ppScale.plugin.bus.subscribe("devicesList", (res) => {
|
|
console.log("deviceInfo ===> devicesList", res);
|
|
let fIndex = res.findIndex(item => item.deviceId === this.data.deviceInfo.deviceId);
|
|
if (fIndex >= 0) {
|
|
app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery();
|
|
app.globalData.ppScale.plugin.Blue.createBLEConnection(res[fIndex]);
|
|
return;
|
|
}
|
|
});
|
|
}
|
|
},
|
|
|
|
// 姓名输入
|
|
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.getDeviceSettingList();
|
|
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: "删除失败,请靠近设备并点亮后再试"
|
|
})
|
|
app.globalData.ppScale.plugin.Blue.stop();
|
|
this.getDeviceSettingList();
|
|
}
|
|
},
|
|
|
|
// 获取家庭成员信息详情
|
|
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();
|
|
}
|
|
}
|
|
}) |