560 lines
13 KiB
JavaScript
560 lines
13 KiB
JavaScript
// 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: 'teacher', // 当前选择的角色:user/teacher/parent
|
||
agreed: false, // 是否同意协议
|
||
|
||
top: 24, //胶囊距离顶部top
|
||
height: 32, //胶囊高度
|
||
|
||
redeemCode:'',
|
||
isLookTeach:false,
|
||
isSureLogin:false,
|
||
isMe:true,
|
||
teamIsMe:false,
|
||
|
||
// 教师登录相关
|
||
teacherPhone: "",
|
||
teacherPassword: "",
|
||
rememberPassword: false,
|
||
trainList: [
|
||
{
|
||
img: 'fit.png',
|
||
url: 'pages/parent/train/train'
|
||
},{
|
||
img: 'small.png',
|
||
url: 'pages/parent/train/train'
|
||
},
|
||
{
|
||
img: 'mid.png',
|
||
url: 'pages/parent/train/train'
|
||
},
|
||
],
|
||
|
||
// 家长登录相关
|
||
studentName: "",
|
||
studentId: "",
|
||
isLoading: true,
|
||
params: ''
|
||
},
|
||
|
||
goLoginn(){
|
||
this.setData({
|
||
isSureLogin:true
|
||
})
|
||
// wx.showModal({
|
||
// title: '体验完整功能需要去登录哦~',
|
||
// confirmText:'去登录',
|
||
// complete: (res) => {
|
||
// if (res.confirm) {
|
||
// this.setData({
|
||
// isSureLogin:true
|
||
// })
|
||
// }
|
||
// }
|
||
// })
|
||
},
|
||
|
||
ai(){
|
||
this.setData({
|
||
isSureLogin:true
|
||
})
|
||
},
|
||
|
||
changeTab(e) {
|
||
const value = e.currentTarget.id
|
||
if (value == 'me' || value == 'team') {
|
||
this.setData({
|
||
isMe: value == 'me'
|
||
})
|
||
} else {
|
||
this.setData({
|
||
teamIsMe: value == 'myCreat',
|
||
})
|
||
}
|
||
},
|
||
|
||
// 切换角色
|
||
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: "登录中..."
|
||
});
|
||
wx.login({
|
||
success: async (loginData) => {
|
||
const opid = await getOpenId(loginData.code)
|
||
post("User/Login", {
|
||
roleId: 3,
|
||
key: studentName,
|
||
password: studentId,
|
||
openId: opid.data
|
||
}).then(res => {
|
||
wx.hideLoading();
|
||
if (res.success) {
|
||
wx.setStorageSync('roleId', res.data.role_Id)
|
||
this.handleLoginSuccess(res.data.token);
|
||
} else {
|
||
wx.showToast({
|
||
title: res.errorMsg || "登录失败",
|
||
icon: "none"
|
||
});
|
||
}
|
||
}).catch(err => {
|
||
wx.hideLoading();
|
||
wx.showToast({
|
||
title: err.errorMsg || "网络错误",
|
||
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"
|
||
});
|
||
// 根据角色跳转
|
||
if(this.data.redeemCode != ''){
|
||
wx.redirectTo({
|
||
url: '/subpackage/user/redeem-class/redeem-class?redeemCode='+this.data.redeemCode,
|
||
})
|
||
}else if(this.data.isLookTeach){
|
||
wx.switchTab({
|
||
url:'/pages/student/teach/teach'
|
||
})
|
||
}else{
|
||
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],
|
||
})
|
||
}, 1300);
|
||
}
|
||
},
|
||
|
||
// 跳转到注册页面
|
||
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)
|
||
if(that.data.redeemCode != ''){
|
||
wx.redirectTo({
|
||
url: '/subpackage/user/redeem-class/redeem-class?redeemCode='+that.data.redeemCode,
|
||
})
|
||
}else if(that.data.isLookTeach){
|
||
wx.switchTab({
|
||
url:'/pages/student/teach/teach'
|
||
})
|
||
}else{
|
||
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) {
|
||
const button = wx.getMenuButtonBoundingClientRect();
|
||
this.setData({
|
||
height: button.height,
|
||
top: button.top
|
||
});
|
||
console.log(this.getTabBar)
|
||
console.log('参数', option)
|
||
|
||
if(option.redeemCode){
|
||
this.setData({
|
||
redeemCode : option.redeemCode
|
||
})
|
||
}
|
||
|
||
if(option.lookTeach){
|
||
this.setData({
|
||
isLookTeach:true
|
||
})
|
||
}
|
||
|
||
//判断是否已登录,直接跳转首页
|
||
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') != '') {
|
||
if(option.redeemCode || option.lookTeach){ //领取会员课程||查看会员课程
|
||
utils.refreshToken()
|
||
if(wx.getStorageSync('theme') != 'user'){
|
||
wx.clearStorageSync()
|
||
this.setData({
|
||
isLoading: false
|
||
})
|
||
}else{
|
||
if(option.lookTeach){
|
||
wx.switchTab({
|
||
url:'/pages/student/teach/teach'
|
||
})
|
||
}else{
|
||
wx.redirectTo({
|
||
url: '/subpackage/user/redeem-class/redeem-class?redeemCode='+option.redeemCode,
|
||
})
|
||
}
|
||
}
|
||
}else{
|
||
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.setData({
|
||
isLoading: false
|
||
})
|
||
}
|
||
|
||
|
||
if(option.q){ //扫描普通二维码进入小程序 1.登录 2.一体机登录
|
||
console.log(decodeURIComponent(option.q))
|
||
const paramObj = utils.parseQueryParams(decodeURIComponent(option.q))
|
||
console.log(paramObj)
|
||
if(paramObj.opt == 'teacherLogin' && paramObj.code && paramObj.schoolCode){
|
||
console.log('codecode============================')
|
||
console.log()
|
||
console.log('codecode============================')
|
||
const app = getApp()
|
||
app.globalData.schoolCode = paramObj.schoolCode
|
||
app.globalData.code = paramObj.code
|
||
}
|
||
}
|
||
|
||
|
||
},
|
||
}); |