2026-05-07 18:04:49 +08:00

273 lines
6.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.

// pages/login/login.js
const { post } = require('../../../utils/http');
const { addMyCount, addMyCountWithToken, getUserAccount, getOpenId } = require('../../../utils/serve/user');
const tabService = require("../../../utils/tab-serve")
Page({
data: {
currentRole: 'teacher', // 当前选择的角色:user/teacher/parent
agreed: false, // 是否同意协议
top:24,//胶囊距离顶部top
height:32,//胶囊高度
// 教师登录相关
teacherPhone: "",
teacherPassword: "",
rememberPassword: false,
// 家长登录相关
studentName: "",
studentId: "",
isLoading:true,
params:'',
haveId:''
},
// 切换角色
switchRole: function(e) {
if(this.data.haveId.includes(e.currentTarget.dataset.id)){
return wx.showToast({
title: '已有该身份,无需绑定',
icon:'none'
})
}
this.setData({
currentRole: e.currentTarget.dataset.role
});
},
back(){
wx.showToast({ title: "请先同意用户协议", icon: "none" });
},
// 切换协议勾选状态
agree(){
this.setData({
agreed:!this.data.agreed
})
},
// 微信登录获取手机号
getPhoneNumber: function(e) {
if (e.detail.errMsg !== "getPhoneNumber:ok") {
wx.showToast({ title: "获取手机号失败", icon: "none" });
return;
}
if (!this.data.agreed) {
wx.showToast({ title: "请先同意用户协议", icon: "none" });
return;
}
wx.showLoading({ title: "登录中..." });
wx.login({
success: (loginRes) => {
if (loginRes.code) {
const params = {
code: loginRes.code,
encryptedData: e.detail.encryptedData,
iv: e.detail.iv,
role: this.data.currentRole
};
this.wechatLogin(params);
} else {
wx.hideLoading();
wx.showToast({ title: "登录失败", icon: "none" });
}
},
fail: (err) => {
wx.hideLoading();
wx.showToast({ title: "登录失败", icon: "none" });
console.error("wx.login失败:", err);
}
});
},
//查看隐私协议
goRrivacy(){
wx.navigateTo({
url: '/subpackage/login/privacy',
})
},
// 教师手机号输入
onTeacherPhoneInput: function(e) {
this.setData({ teacherPhone: e.detail.value });
},
// 教师密码输入
onTeacherPasswordInput: function(e) {
this.setData({ teacherPassword: e.detail.value });
},
// 记住密码
onRememberChange: function(e) {
this.setData({ rememberPassword: e.detail.value.length > 0 });
},
// 教师登录
onTeacherLogin: function() {
const that = this
const { teacherPhone, teacherPassword } = this.data;
if (!teacherPhone || !teacherPassword) {
wx.showToast({ title: "请输入手机号和密码", icon: "none" });
return;
}
const regex = /^1[3-9]\d{9}$/;
if(!regex.test(teacherPhone)) {
wx.showToast({ title: "请输入正确的手机号", icon: "none" });
return;
}
if (!this.data.agreed) {
wx.showToast({ title: "请先同意用户协议", icon: "none" });
return;
}
wx.showLoading({ title: "登录中..." });
wx.login({
success: async (loginData) => {
const opid = await getOpenId(loginData.code)
post("User/Login", {
roleId: 2,
key: teacherPhone,
password: teacherPassword,
openId:opid.data
}).then(res => {
wx.hideLoading();
if (res.success) {
that.handleLoginSuccess(res.data.token,res.data.user_Id);
//
} else {
wx.showToast({
title: res.errorMsg || "登录失败",
icon: "none"
});
}
}).catch(err => {
wx.hideLoading();
wx.showToast({
title: "网络错误",
icon: "none"
});
console.error("教师登录失败:", err);
});
},
})
},
// 家长学生姓名输入
onStudentNameInput: function(e) {
this.setData({ studentName: e.detail.value });
},
// 家长学生学号输入
onStudentIdInput: function(e) {
this.setData({ studentId: e.detail.value });
},
// 家长登录
onParentLogin: function() {
const { studentName, studentId } = this.data;
if (!studentName || !studentId) {
wx.showToast({ title: "请输入学生姓名和学号", icon: "none" });
return;
}
wx.showLoading({ title: "登录中..." });
post("User/Login", {
roleId:3,
key: studentName,
password: studentId
}).then(res => {
wx.hideLoading();
if (res.success) {
this.handleLoginSuccess(res.data.token,res.data.user_Id);
} else {
wx.showToast({ title: res.message || "登录失败", icon: "none" });
}
}).catch(err => {
wx.hideLoading();
wx.showToast({ title: "网络错误", icon: "none" });
console.error("家长登录失败:", err);
});
},
// 微信登录请求
wechatLogin: function(params) {
post("User/WxLogin", params)
.then(res => {
wx.hideLoading();
if (res.success) {
this.handleLoginSuccess(res.data.token,res.data.user_Id);
} else {
wx.showToast({ title: res.message || "登录失败", icon: "none" });
}
})
.catch(err => {
wx.hideLoading();
wx.showToast({ title: "网络错误", icon: "none" });
console.error("微信登录失败:", err);
});
},
// 登录成功处理
handleLoginSuccess:async function (token,id) {
this.data.accountList.forEach(async (e,index) =>{
const res = await addMyCountWithToken({
"roleId": this.data.currentRole== 'user' ? 1 : this.data.currentRole== 'teacher'?2:3,
"userId": id,
"token": token
},e.token)
if(!res.success) return
console.log('=============>',index)
})
wx.setStorageSync('token', token);
wx.showToast({ title: "添加成功", icon: "success" });
// 根据角色跳转
const redirectMap = {
user: '/pages/student/train/train',
teacher: "/pages/teacher/resource/resource",
parent: "/pages/parent/train/train"
};
tabService.updateRole(this,this.data.currentRole)
wx.setStorageSync('theme', this.data.currentRole)
setTimeout(() => {
wx.switchTab({
url: redirectMap[this.data.currentRole],
})
}, 1500);
},
// 跳转到注册页面
onRegister: function() {
wx.navigateTo({ url: "/pages/register/register" });
},
onLoad: async function (option) {
const {haveId , list} = option
console.log(list)
const res = await getUserAccount()
let currentRole = 'user'
if(haveId.includes('1')){
currentRole = 'teacher'
if(haveId.includes('2')){
currentRole = 'parent'
}
}
this.setData({
haveId,
currentRole,
accountList:res.data
})
},
});