2025-06-09 17:19:11 +08:00

424 lines
9.5 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 {
getOpenId,getUserAccountWithToken
} = require('../../utils/serve/user');
const tabService = require("../../utils/tab-serve")
const utils = require("../../utils/utils")
const app = getApp()
Page({
data: {
currentRole: 'user', // 当前选择的角色user/teacher/parent
agreed: false, // 是否同意协议
top: 24, //胶囊距离顶部top
height: 32, //胶囊高度
// 教师登录相关
teacherPhone: "",
teacherPassword: "",
rememberPassword: false,
// 家长登录相关
studentName: "",
studentId: "",
isLoading: true,
params: ''
},
// 切换角色
switchRole: function (e) {
this.setData({
currentRole: e.currentTarget.dataset.role
});
},
//跳转隐私协议
goToPrivacy() {
wx.openPrivacyContract({
success: () => {
console.log('打开成功');
}, // 打开成功
fail: () => {
uni.showToast({
title: '打开失败,稍后重试',
icon: 'none'
})
} // 打开失败
})
},
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) {
wx.setStorageSync('roleId', res.data.role_Id)
that.handleLoginSuccess(res.data.token);
//
} 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;
}
if (!this.data.agreed) {
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);
} else {
wx.showToast({
title: res.message || "登录失败",
icon: "none"
});
}
}).catch(err => {
wx.hideLoading();
wx.showToast({
title: "网络错误",
icon: "none"
});
console.error("家长登录失败:", err);
});
},
// 微信登录请求
wechatLogin: function (params) {
const that = this
post("User/WxFirstLogin", params)
.then(res => {
wx.hideLoading();
console.log(' ')
console.log(' ')
console.log('登录================>', params)
console.log('params================>', params)
console.log(' ')
console.log(' ')
if (res.success) {
that.handleLoginSuccess(res.data.token);
} else {
console.error('登录失败================>', params)
console.error('登录失败================>', params)
console.error('登录失败================>', params)
wx.showToast({
title: res.message || "登录失败",
icon: "none"
});
}
})
.catch(err => {
wx.hideLoading();
wx.showToast({
title: "网络错误",
icon: "none"
});
console.error("微信登录失败:", err);
});
},
// 登录成功处理
handleLoginSuccess: function (token) {
if (app.globalData.isUserLogout) {
app.globalData.isUserLogout = false
}
if (!token) return wx.showToast({
title: '网络错误,请重试',
icon: 'none'
})
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"
});
},
//自动登录
autoLogin: (that) => {
wx.login({
success: async (loginRes) => {
console.log('自动登录====>', loginRes)
if (loginRes.code) {
const res = await post("User/WxLogin", {
code: loginRes.code,
})
console.log('loginCode登录===》', res)
if (res.success && res.data.token) {
tabService.updateRole(that, 'user')
wx.setStorageSync('theme', 'user')
wx.setStorageSync('token', res.data.token)
wx.switchTab({
url: '/pages/student/train/train',
})
} else {
that.setData({
isLoading: false
})
}
} else {
that.setData({
isLoading: false
})
}
},
fail: (err) => {
that.setData({
isLoading: false
})
}
});
},
onLoad: function async (option) {
console.log(this.getTabBar)
//判断是否已登录,直接跳转首页
if (option.opt != null && option.opt == 'join') {
wx.setStorageSync('groupId', option.groupId)
}
if (app.globalData.isUserLogout) { //用户手动退出登录
this.setData({
isLoading: false
})
} else if (wx.getStorageInfoSync('token') != '' && wx.getStorageSync('theme') != '') {
utils.refreshToken()
const redirectMap = {
user: '/pages/student/train/train',
teacher: "/pages/teacher/resource/resource",
parent: "/pages/parent/train/train"
};
const theme = wx.getStorageSync('theme')
tabService.updateRole(this, theme)
console.log(redirectMap[theme])
wx.switchTab({
url: redirectMap[theme],
})
} else {
this.autoLogin(this)
}
const button = wx.getMenuButtonBoundingClientRect();
this.setData({
height: button.height,
top: button.top
});
},
});