294 lines
11 KiB
JavaScript
294 lines
11 KiB
JavaScript
const app = getApp();
|
|
import $ from "../../utils/request";
|
|
|
|
Component({
|
|
data: {
|
|
BLUE_STATE: {},
|
|
device: null,
|
|
connectState: "",
|
|
|
|
userId: "",
|
|
realname: "",
|
|
sex: {
|
|
index: "",
|
|
data: [{
|
|
id: 1,
|
|
name: '男'
|
|
}, {
|
|
id: 2,
|
|
name: '女'
|
|
}]
|
|
},
|
|
birthday: {
|
|
label: "1980年01月01日",
|
|
value: "1980-01-01"
|
|
},
|
|
height: {
|
|
index: 50,
|
|
data: Array.from({ length: 200 - 120 + 1 }, (_, i) => i + 120)
|
|
},
|
|
weight: {
|
|
index: [60, 0],
|
|
data: [Array.from({ length: 120 + 1 }, (_, i) => i + 10), Array.from({ length: 10 }, (_, i) => i)]
|
|
},
|
|
},
|
|
|
|
// 存储回调函数的引用,以便在 detached 时取消监听
|
|
_connectStateWatcher: null,
|
|
|
|
lifetimes: {
|
|
attached() {
|
|
console.log(`[Component] connectState 获取: ${app.globalData.ppScale.device.connectState}`);
|
|
this.setData({
|
|
BLUE_STATE: app.globalData.ppScale.plugin.BLUE_STATE,
|
|
connectState: app.globalData.ppScale.device.connectState,
|
|
device: app.globalData.ppScale.device.connection
|
|
})
|
|
this._connectStateWatcher = (newValue, oldValue) => {
|
|
console.log(`[Component] connectState 变化: ${oldValue} -> ${newValue}`);
|
|
this.setData({
|
|
connectState: newValue || ''
|
|
});
|
|
};
|
|
app.watch('ppScale.device.connectState', this._connectStateWatcher);
|
|
|
|
let userInfo = wx.getStorageSync("userInfo") || null;
|
|
if(userInfo) {
|
|
let [year, month, day] = userInfo.birthday.split("-");
|
|
|
|
this.setData({
|
|
userId: userInfo.userId,
|
|
realname: userInfo.realname,
|
|
["sex.index"]: this.data.sex.data.findIndex(item => {return item.id === userInfo.sex}),
|
|
birthday: {
|
|
label: year + "年" + month + "月" + day + "日",
|
|
value: userInfo.birthday
|
|
},
|
|
["height.index"]: this.data.height.data.findIndex(item => {return item === userInfo.height}),
|
|
})
|
|
|
|
if(userInfo.weight) {
|
|
const integerPart = Math.floor(userInfo.weight);
|
|
const decimalPart = Math.round((userInfo.weight - integerPart) * 10);
|
|
const integerIndex = this.data.weight.data[0].indexOf(integerPart);
|
|
const decimalIndex = this.data.weight.data[1].indexOf(decimalPart);
|
|
this.setData({
|
|
["weight.index"]: [integerIndex !== -1 ? integerIndex : null, decimalIndex !== -1 ? decimalIndex : null],
|
|
})
|
|
}
|
|
}
|
|
},
|
|
detached() {
|
|
// 4. 在组件销毁时取消监听,防止内存泄漏
|
|
if (this._connectStateWatcher) {
|
|
app.unwatch('ppScale.device.connectState', this._connectStateWatcher);
|
|
}
|
|
}
|
|
},
|
|
|
|
// 组件的方法
|
|
methods: {
|
|
// 二次连接设备
|
|
connectedDevice() {
|
|
this.triggerEvent('connectedDeviceEvent', {
|
|
device: this.data.device
|
|
});
|
|
},
|
|
|
|
// 姓名输入
|
|
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
|
|
}
|
|
})
|
|
},
|
|
|
|
// 身高选择
|
|
heightChange(e) {
|
|
this.setData({
|
|
['height.index']: e.detail.value
|
|
})
|
|
},
|
|
|
|
// 体重选择
|
|
weightChange(e) {
|
|
console.log(e.detail.value)
|
|
this.setData({
|
|
['weight.index']: e.detail.value
|
|
})
|
|
},
|
|
|
|
setUserInfo() {
|
|
if(this.data.connectState == this.data.BLUE_STATE.CONNECTSUCCESS) {
|
|
if(this.data.userId) {
|
|
app.globalData.ppScale.activeProtocol.dataSyncUserInfo({
|
|
userID: this.data.userId,
|
|
userName: this.data.realname,
|
|
memberID: "",
|
|
age: this.getAge(this.data.birthday.value),
|
|
gender: this.data.sex.data[this.data.sex.index].id,
|
|
height: this.data.height.data[this.data.height.index],
|
|
isAthleteMode: 0,
|
|
currentWeight: this.data.weight.data[0][this.data.weight.index[0]] + '.' + this.data.weight.data[1][this.data.weight.index[1]],
|
|
deviceHeaderIndex: 0,
|
|
targetWeight: "",
|
|
idealWeight: "",
|
|
recentData: [],
|
|
}, (res) => {
|
|
if(res == 0) {
|
|
this.triggerEvent('deviceEvent', {
|
|
status: true
|
|
});
|
|
} else {
|
|
console.log("dataSyncUserInfo", res)
|
|
wx.showToast({
|
|
title: "用户信息下发失败,请重试。",
|
|
icon: "none"
|
|
})
|
|
}
|
|
})
|
|
} else {
|
|
let realname = this.data.realname;
|
|
let sex = this.data.sex;
|
|
let birthday = this.data.birthday;
|
|
let height = this.data.height;
|
|
let weight = this.data.weight;
|
|
if(!realname) {
|
|
wx.showToast({
|
|
icon: "none",
|
|
title: "姓名不能为空"
|
|
})
|
|
return false;
|
|
}
|
|
if(sex.index === "") {
|
|
wx.showToast({
|
|
icon: "none",
|
|
title: "性别不能为空"
|
|
})
|
|
return false;
|
|
}
|
|
if(!birthday.value) {
|
|
wx.showToast({
|
|
icon: "none",
|
|
title: "生日不能为空"
|
|
})
|
|
return false;
|
|
}
|
|
if(height.index === "") {
|
|
wx.showToast({
|
|
icon: "none",
|
|
title: "身高不能为空"
|
|
})
|
|
return false;
|
|
}
|
|
if(weight.index[0] === null && weight.index[1] === null) {
|
|
console.log(weight)
|
|
wx.showToast({
|
|
icon: "none",
|
|
title: "体重不能为空"
|
|
})
|
|
return false;
|
|
}
|
|
let sn = app.globalData.ppScale.device.mac.replace(/:/g, '');
|
|
$.ajax("weighingScale/edit/user", {
|
|
sn: sn,
|
|
realname: realname,
|
|
sex: sex.data[sex.index].id,
|
|
birthday: birthday.value,
|
|
height: height.data[height.index],
|
|
weight: weight.data[0][weight.index[0]] + '.' + weight.data[1][weight.index[1]]
|
|
}, "POST", true, "保存中...").then(res => {
|
|
$.ajax("weighingScale/select/user", {
|
|
sn: sn
|
|
}, "GET", true, "更新用户信息中...").then(res_ => {
|
|
wx.setStorageSync("userInfo", res_.result);
|
|
wx.showToast({
|
|
icon: "none",
|
|
title: res.message
|
|
})
|
|
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) => {
|
|
if(res == 0) {
|
|
this.triggerEvent('deviceEvent', {
|
|
status: true
|
|
});
|
|
} else {
|
|
console.log("dataSyncUserInfo", res)
|
|
wx.showToast({
|
|
title: "用户信息下发失败,请重试。",
|
|
icon: "none"
|
|
})
|
|
}
|
|
})
|
|
})
|
|
})
|
|
}
|
|
} else {
|
|
wx.showToast({
|
|
title: "设备连接失败,请重试。",
|
|
icon: "none"
|
|
})
|
|
}
|
|
},
|
|
|
|
// 根据生日获取年龄
|
|
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;
|
|
},
|
|
}
|
|
}); |