You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
213 lines
4.8 KiB
213 lines
4.8 KiB
const request = require('../utils/request')
|
|
Page({
|
|
data: {
|
|
// 账号密码
|
|
account: '',
|
|
password: '',
|
|
// 显示密码切换
|
|
showPassword: false,
|
|
// 错误状态
|
|
accountError: false,
|
|
accountErrorMsg: '',
|
|
passwordError: false,
|
|
passwordErrorMsg: '',
|
|
// 加载状态
|
|
isLoading: false
|
|
},
|
|
|
|
// 处理账号输入
|
|
handleAccountInput(e) {
|
|
const account = e.detail.value.trim();
|
|
this.setData({
|
|
account,
|
|
accountError: false,
|
|
accountErrorMsg: ''
|
|
});
|
|
},
|
|
|
|
// 处理密码输入
|
|
handlePasswordInput(e) {
|
|
const password = e.detail.value;
|
|
this.setData({
|
|
password,
|
|
passwordError: false,
|
|
passwordErrorMsg: ''
|
|
});
|
|
},
|
|
|
|
// 切换密码显示状态
|
|
togglePassword() {
|
|
this.setData({
|
|
showPassword: !this.data.showPassword
|
|
});
|
|
},
|
|
|
|
// 表单验证
|
|
validateForm() {
|
|
let isValid = true;
|
|
const { account, password } = this.data;
|
|
|
|
// 验证账号
|
|
if (!account) {
|
|
this.setData({
|
|
accountError: true,
|
|
accountErrorMsg: '请输入账号'
|
|
});
|
|
isValid = false;
|
|
} else if (account.length < 4) {
|
|
this.setData({
|
|
accountError: true,
|
|
accountErrorMsg: '账号长度不能少于4位'
|
|
});
|
|
isValid = false;
|
|
}
|
|
|
|
// 验证密码
|
|
if (!password) {
|
|
this.setData({
|
|
passwordError: true,
|
|
passwordErrorMsg: '请输入密码'
|
|
});
|
|
isValid = false;
|
|
} else if (password.length < 6) {
|
|
this.setData({
|
|
passwordError: true,
|
|
passwordErrorMsg: '密码长度不能少于6位'
|
|
});
|
|
isValid = false;
|
|
}
|
|
|
|
return isValid;
|
|
},
|
|
|
|
// 处理登录
|
|
handleLogin() {
|
|
// 表单验证
|
|
if (!this.validateForm()) {
|
|
return;
|
|
}
|
|
|
|
const { account, password } = this.data;
|
|
|
|
// 显示加载状态
|
|
this.setData({
|
|
isLoading: true
|
|
});
|
|
|
|
wx.request({
|
|
url: 'http://localhost:48080/admin-api/system/auth/login', // 仅为示例,并非真实的接口地址
|
|
method: 'POST', // 请求方法,可以是 GET, POST, PUT, DELETE 等
|
|
data: {
|
|
"tenantName": "芋道源码",
|
|
"username": account,
|
|
"password": password,
|
|
"rememberMe": true
|
|
},
|
|
header: {
|
|
'Tenant-Id':1,
|
|
'content-type': 'application/json' // 默认值,也可以根据后端要求设置其他值,如 'application/x-www-form-urlencoded'
|
|
},
|
|
success :(res)=> {
|
|
console.log(res.data)
|
|
// 处理返回数据
|
|
if(res.data.code==0){
|
|
|
|
wx.setStorageSync('token', res.data.data.accessToken);
|
|
console.log(wx.getStorageSync('token'))
|
|
// 跳转到主界面
|
|
wx.switchTab({
|
|
url: '/pages/index/index',
|
|
success: () => {
|
|
console.log('执行成功')
|
|
this.setData({
|
|
isLoading: false
|
|
});
|
|
}
|
|
});
|
|
}else{
|
|
this.setData({
|
|
passwordError: true,
|
|
passwordErrorMsg: res.data.msg,
|
|
isLoading: false
|
|
});
|
|
// 震动反馈
|
|
wx.vibrateShort();
|
|
}
|
|
},
|
|
fail (err) {
|
|
console.error(err)
|
|
|
|
},
|
|
complete () {
|
|
// 请求完成时执行,无论成功或失败
|
|
}
|
|
})
|
|
},
|
|
|
|
// 微信快捷登录
|
|
wxLogin(e) {
|
|
// 检查用户是否授权
|
|
if (e.detail.userInfo) {
|
|
// 用户同意授权
|
|
this.setData({
|
|
isLoading: true
|
|
});
|
|
|
|
// 模拟微信登录过程
|
|
setTimeout(() => {
|
|
// 保存用户信息
|
|
wx.setStorageSync('userInfo', {
|
|
nickname: e.detail.userInfo.nickName,
|
|
avatar: e.detail.userInfo.avatarUrl,
|
|
loginTime: new Date().getTime(),
|
|
isWechatLogin: true
|
|
});
|
|
|
|
// 跳转到主界面
|
|
wx.redirectTo({
|
|
url: '/pages/index/index',
|
|
success: () => {
|
|
this.setData({
|
|
isLoading: false
|
|
});
|
|
}
|
|
});
|
|
}, 1500);
|
|
} else {
|
|
// 用户拒绝授权
|
|
wx.showToast({
|
|
title: '授权失败,无法登录',
|
|
icon: 'none',
|
|
duration: 2000
|
|
});
|
|
}
|
|
},
|
|
|
|
// 跳转到忘记密码页面
|
|
navigateToForgot() {
|
|
wx.navigateTo({
|
|
url: '/pages/forgot-password/forgot-password'
|
|
});
|
|
},
|
|
|
|
// 跳转到注册页面
|
|
navigateToRegister() {
|
|
wx.navigateTo({
|
|
url: '/pages/register/register'
|
|
});
|
|
},
|
|
|
|
onLoad() {
|
|
// 检查是否已登录
|
|
const userInfo = wx.getStorageSync('userInfo');
|
|
if (userInfo && userInfo.loginTime) {
|
|
// 如果30天内登录过,自动跳转
|
|
const now = new Date().getTime();
|
|
if (now - userInfo.loginTime < 30 * 24 * 60 * 60 * 1000) {
|
|
wx.redirectTo({
|
|
url: '/pages/index/index'
|
|
});
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|