Files
bodyWeight/components/configureDevice_2_addUser/configureDevice_2_addUser.js
T

258 lines
8.7 KiB
JavaScript

import $ from "../../utils/request";
const app = getApp();
Component({
properties: {
},
data: {
animationData: null,
modalStatus: false,
// 是否是第一个用户(主用户),true 时只能选员工
isFirstUser: false,
// 用户类型:employee=员工,family=家庭用户
userType: 'employee',
// 员工模式
workNo: "",
userInfo: null,
// 家庭用户模式 - 手动填写
familyInfo: {
name: '',
sex: 1,
birthday: '',
height: '',
weight: ''
},
// 性别选择器选项
genderOptions: ['男', '女'],
genderIndex: 0,
},
lifetimes: {
// 在组件实例进入页面节点树时执行
attached() {
},
// 在组件实例被从页面节点树移除时执行
detached() {
},
},
methods: {
// 显示对话框
showModal(isFirstUser = false) {
this.setData({ isFirstUser: !!isFirstUser });
// 先瞬间将弹窗移到屏幕下方(duration:0,不产生过渡)
let initAnim = wx.createAnimation({ duration: 0 });
initAnim.translateY(1200).step();
this.setData({ animationData: initAnim.export(), modalStatus: true });
// 留一帧让渲染生效,再执行滑入动画
setTimeout(() => {
let animation = wx.createAnimation({ duration: 300, timingFunction: 'ease' });
animation.translateY(0).step();
this.setData({ animationData: animation.export() });
}, 50);
},
// 隐藏对话框
hideModal() {
// 执行滑出动画
let animation = wx.createAnimation({ duration: 300, timingFunction: 'ease' });
animation.translateY(1200).step();
this.setData({ animationData: animation.export() });
// 等动画结束后再隐藏节点
setTimeout(() => {
this.setData({ modalStatus: false });
}, 300);
},
// 关闭弹窗并重置所有数据
closeModal() {
this.setData({
isFirstUser: false,
userType: 'employee',
workNo: "",
userInfo: null,
familyInfo: {
name: '',
sex: 1,
birthday: '',
height: '',
weight: ''
},
genderIndex: 0,
});
this.hideModal();
},
// 切换用户类型,同时清空所有已填数据
switchUserType(e) {
// 主用户模式下禁止切换
if (this.data.isFirstUser) return;
const type = e.currentTarget.dataset.type;
if (type === this.data.userType) return;
this.setData({
userType: type,
workNo: "",
userInfo: null,
familyInfo: {
name: '',
sex: 1,
birthday: '',
height: '',
weight: ''
},
genderIndex: 0,
});
},
// 监听工号输入(员工模式),输入满 8 位时自动查询
onWorkNoInput(e) {
const value = e.detail.value;
this.setData({ workNo: value, userInfo: null });
if (value.trim().length === 8) {
this.searchUser();
}
},
// 监听身高输入(员工模式)
onHeightInput(e) {
this.setData({ 'userInfo.height': e.detail.value });
},
// 监听体重输入(员工模式)
onWeightInput(e) {
this.setData({ 'userInfo.weight': e.detail.value });
},
// 监听家庭用户姓名输入
onFamilyNameInput(e) {
this.setData({ 'familyInfo.name': e.detail.value });
},
// 监听家庭用户性别选择
onFamilyGenderChange(e) {
const index = e.detail.value;
// 1=男,2=女
this.setData({
genderIndex: index,
'familyInfo.sex': index === '0' || index === 0 ? 1 : 2
});
},
// 监听家庭用户生日选择
onFamilyBirthdayChange(e) {
this.setData({ 'familyInfo.birthday': e.detail.value });
},
// 监听家庭用户身高输入
onFamilyHeightInput(e) {
this.setData({ 'familyInfo.height': e.detail.value });
},
// 监听家庭用户体重输入
onFamilyWeightInput(e) {
this.setData({ 'familyInfo.weight': e.detail.value });
},
// 根据工号查询用户信息(员工模式)
searchUser() {
let workNo = this.data.workNo.trim();
if (!workNo) {
wx.showToast({
title: "请输入工号",
icon: "none"
})
return;
}
let sn = app.globalData.ppScale.device.mac.replace(/:/g, '');
$.ajax("weighingScale/v2/getUserInfoByWkno", {
workNo: workNo,
sn: sn
}, "GET", true, "正在查询...").then(res => {
if (res.result) {
this.setData({ userInfo: res.result });
} else {
this.setData({ userInfo: null });
wx.showToast({
title: "未查询到该工号的用户信息",
icon: "none"
})
}
}).catch((err) => {
console.log(err);
this.setData({ userInfo: null });
wx.showToast({
title: (err && err.message) || "查询失败",
icon: "none"
})
})
},
// 确定提交
submit() {
const sn = app.globalData.ppScale.device.mac.replace(/:/g, '');
if (this.data.userType === 'employee') {
// 员工模式:必须先查询到用户信息
if (!this.data.userInfo) {
wx.showToast({ title: "请先查询用户信息", icon: "none" });
return;
}
let userInfo = this.data.userInfo;
if (!userInfo.height) {
wx.showToast({ title: "请输入身高", icon: "none" });
return;
}
if (!userInfo.weight) {
wx.showToast({ title: "请输入体重", icon: "none" });
return;
}
userInfo.sn = sn;
$.ajax("weighingScale/v2/edit/user", userInfo, "POST", true, "正在添加...").then(res => {
wx.showToast({ title: "添加成功", icon: "success" });
this.triggerEvent('returnDate', { userInfo });
this.closeModal();
}).catch(() => {
wx.showToast({ title: "添加失败,请重试", icon: "none" });
});
} else {
// 家庭用户模式:校验手动填写的字段
const familyInfo = this.data.familyInfo;
if (!familyInfo.name.trim()) {
wx.showToast({ title: "请输入姓名", icon: "none" });
return;
}
if (!familyInfo.birthday) {
wx.showToast({ title: "请选择生日", icon: "none" });
return;
}
if (!familyInfo.height) {
wx.showToast({ title: "请输入身高", icon: "none" });
return;
}
if (!familyInfo.weight) {
wx.showToast({ title: "请输入体重", icon: "none" });
return;
}
const submitData = {
realname: familyInfo.name.trim(),
sex: familyInfo.sex,
birthday: familyInfo.birthday,
height: familyInfo.height,
weight: familyInfo.weight,
sn: sn
};
$.ajax("weighingScale/v2/edit/user", submitData, "POST", true, "正在添加...").then(res => {
wx.showToast({ title: "添加成功", icon: "success" });
this.triggerEvent('returnDate', { userInfo: submitData });
this.closeModal();
}).catch(() => {
wx.showToast({ title: "添加失败,请重试", icon: "none" });
});
}
}
}
});