Files
bodyWeight/pages/setNetworkSuccessful/setNetworkSuccessful.js
T

128 lines
2.9 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.
const app = getApp();
import $ from "../../utils/request";
Page({
data: {
userInfo: null,
subUser: {
i: 0,
data: []
},
ssid: ""
},
onLoad(options) {
this.setData({
userInfo: wx.getStorageSync("userInfo"),
ssid: app.globalData.ppScale.wifi.ssid
});
this.getSubUser();
},
completeClick() {
let userInfo = this.data.userInfo;
// 设置主用户时userID为小程序的用户IDmemberID为空
app.globalData.ppScale.activeProtocol.dataSyncUserInfo({
userID: userInfo.id,
userName: userInfo.realname,
memberID: "",
age: this.getAge(userInfo.birthday), //
gender: userInfo.sex, //
height: userInfo.height, //
isAthleteMode: 0,
currentWeight: userInfo.weight, //
deviceHeaderIndex: 0,
targetWeight: "",
idealWeight: "",
recentData: [],
}, (res) => {
console.log("dataSyncUserInfo", res);
if(this.data.subUser.data && this.data.subUser.data.length) {
wx.showLoading({
title: "正在同步用户信息,请稍等...",
mask: true
});
this.setSubUser();
} else {
this.resetDevice();
app.globalData.ppScale.plugin.Blue.stop();
wx.switchTab({
url: "/pages/home/home"
})
}
})
},
setSubUser() {
let userInfo = this.data.userInfo;
let i = this.data.subUser.i;
let subUser = this.data.subUser.data;
if(i < subUser.length) {
app.globalData.ppScale.activeProtocol.dataSyncUserInfo({
userID: userInfo.id,
userName: subUser[i].realname,
memberID: subUser[i].id,
age: this.getAge(subUser[i].birthday), //
gender: subUser[i].sex, //
height: subUser[i].height, //
isAthleteMode: 0,
currentWeight: subUser[i].weight, //
deviceHeaderIndex: 0,
targetWeight: "",
idealWeight: "",
recentData: [],
}, (res) => {
console.log("dataSyncSubUserInfo", res);
this.setData({
['subUser.i']: i + 1
})
this.setSubUser();
})
} else {
this.resetDevice();
app.globalData.ppScale.plugin.Blue.stop();
wx.hideLoading();
wx.switchTab({
url: "/pages/home/home"
})
}
},
resetDevice() {
app.globalData.ppScale.device = {
list: [],
mac: null,
name: "",
connection: null
}
},
// 获取家庭成员列表
getSubUser() {
let params = {};
$.ajax("weighingScale/list/subUser", params, "GET", true, "正在获取成员列表...").then(res => {
this.setData({
['subUser.data']: res.result
})
})
},
// 根据生日获取年龄
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;
}
})