|
|
// pages/login/login.js
|
|
|
Page({
|
|
|
data: {
|
|
|
ID: '', // 用户输入的 ID
|
|
|
password: '', // 用户输入的密码
|
|
|
errorMessage: '' // 错误信息提示
|
|
|
},
|
|
|
|
|
|
// 处理 ID 输入
|
|
|
onIDInput(e) {
|
|
|
this.setData({
|
|
|
ID: e.detail.value // 将输入的ID存储到 data 中
|
|
|
});
|
|
|
this.clearErrorMessage(); // 清除错误提示
|
|
|
},
|
|
|
|
|
|
// 处理密码输入
|
|
|
onPasswordInput(e) {
|
|
|
this.setData({
|
|
|
password: e.detail.value // 将输入的密码存储到 data 中
|
|
|
});
|
|
|
this.clearErrorMessage(); // 清除错误提示
|
|
|
},
|
|
|
|
|
|
// 清除错误信息
|
|
|
clearErrorMessage() {
|
|
|
if (this.data.errorMessage) {
|
|
|
this.setData({ errorMessage: '' });
|
|
|
}
|
|
|
},
|
|
|
|
|
|
// 检查管理员状态
|
|
|
checkAdminStatus() {
|
|
|
const token = getApp().globalData.token || wx.getStorageSync('token');
|
|
|
if (!token) {
|
|
|
this.setData({ errorMessage: 'Token 丢失,请重新登录' });
|
|
|
return;
|
|
|
}
|
|
|
console.log("Checking admin status with token:", token); // 输出 token 确认
|
|
|
|
|
|
wx.request({
|
|
|
header: {
|
|
|
'Authorization': `${token}`, // 使用存储的 token
|
|
|
'content-type': 'application/x-www-form-urlencoded'
|
|
|
},
|
|
|
url: 'http://192.168.144.1:8080/user/is_admin', // 管理员状态查询接口
|
|
|
method: 'GET',
|
|
|
success: (res) => {
|
|
|
console.log("Admin status response:", res); // 输出请求返回数据
|
|
|
if (res.data.data) {
|
|
|
wx.navigateTo({
|
|
|
url: '/pages/admin/admin' // 管理员页面
|
|
|
});
|
|
|
} else {
|
|
|
wx.switchTab({
|
|
|
url: '/pages/home/home' // 普通用户主页
|
|
|
});
|
|
|
}
|
|
|
},
|
|
|
fail: (error) => {
|
|
|
console.log("Admin status check failed", error);
|
|
|
this.setData({
|
|
|
errorMessage: '无法验证管理员状态,请稍后再试'
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
submitUserInfo() {
|
|
|
const { ID, password } = this.data;
|
|
|
|
|
|
if (!ID || !password) {
|
|
|
this.setData({
|
|
|
errorMessage: 'ID和密码不能为空'
|
|
|
});
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
wx.request({
|
|
|
header: {
|
|
|
'content-type': 'application/x-www-form-urlencoded'
|
|
|
},
|
|
|
url: 'http://192.168.144.1:8080/user/login',
|
|
|
method: 'POST',
|
|
|
data: {
|
|
|
'username': ID,
|
|
|
'password': password
|
|
|
},
|
|
|
success: (res) => {
|
|
|
if (res.data.code == 0) {
|
|
|
wx.showToast({
|
|
|
title: '登录成功',
|
|
|
icon: 'success'
|
|
|
});
|
|
|
|
|
|
// 检查 getApp() 和 globalData 是否定义
|
|
|
const appInstance = getApp();
|
|
|
if (appInstance && appInstance.globalData) {
|
|
|
appInstance.globalData.token = res.data.data;
|
|
|
wx.setStorageSync('token', res.data.data);
|
|
|
} else {
|
|
|
console.error("App instance or globalData is not initialized");
|
|
|
}
|
|
|
|
|
|
this.checkAdminStatus();
|
|
|
} else {
|
|
|
this.setData({
|
|
|
errorMessage: res.data.message || '登录失败,请检查ID和密码'
|
|
|
});
|
|
|
}
|
|
|
},
|
|
|
fail: (error) => {
|
|
|
console.log('登录失败', error);
|
|
|
this.setData({
|
|
|
errorMessage: '网络请求失败,请稍后再试'
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
// 跳转到注册页面
|
|
|
goToRegister() {
|
|
|
wx.redirectTo({
|
|
|
url: '/pages/register/register' // 跳转到注册页面
|
|
|
});
|
|
|
}
|
|
|
});
|