hcy 3 months ago
parent 99268232ad
commit d2b6c99c0d

@ -0,0 +1,127 @@
// 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' // 跳转到注册页面
});
}
});

@ -0,0 +1,3 @@
{
"usingComponents": {}
}

@ -0,0 +1,31 @@
<!-- pages/login/login.wxml -->
<view class="container">
<view class="title">登录</view>
<!-- 输入账号ID -->
<input
type="text"
placeholder="请输入账号ID"
bindinput="onIDInput"
value="{{ID}}"
/>
<!-- 输入密码 -->
<input
type="password"
placeholder="请输入密码"
bindinput="onPasswordInput"
value="{{password}}"
/>
<!-- 显示错误信息 -->
<view class="error">{{errorMessage}}</view>
<!-- 登录按钮 -->
<button bindtap="submitUserInfo">登录</button>
<!-- 跳转到注册页面 -->
<view class="message">
还没有账号?<text class="link" bindtap="goToRegister">点击注册</text>
</view>
</view>

@ -0,0 +1,80 @@
/* pages/login/login.wxss */
page {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f8f8f8;
}
.container {
width: 90%;
max-width: 400px;
background-color: #fff;
border-radius: 10px;
padding: 20px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}
.title {
text-align: center;
font-size: 24px;
font-weight: bold;
color: #333;
margin-bottom: 20px;
}
input {
width: 100%;
height: 40px;
padding: 0 10px;
margin-bottom: 15px;
border: 1px solid #ddd;
border-radius: 5px;
background-color: #fafafa;
}
input:focus {
border-color: #007aff;
background-color: #fff;
}
button {
width: 100%;
height: 45px;
background-color: #007aff;
color: #fff;
border: none;
border-radius: 5px;
font-size: 16px;
text-align: center;
line-height: 45px;
margin-top: 10px;
}
button:active {
background-color: #005bb5;
}
.admin-section {
margin-bottom: 15px;
}
.error {
color: red;
font-size: 14px;
margin-top: 5px;
}
.message {
margin-top: 10px;
text-align: center;
color: #666;
}
.link {
color: #007aff;
text-decoration: underline;
cursor: pointer;
}

@ -0,0 +1,66 @@
// pages/register/register.js
Page({
data: {
ID: '',
password: '',
errorMessage: ''
},
// 处理ID输入
onIDInput(e) {
this.setData({
ID: e.detail.value
});
},
// 处理密码输入
onPasswordInput(e) {
this.setData({
password: e.detail.value
});
},
// 处理注册逻辑
submitUserInfo() {
const { ID, password } = this.data;
// 验证 ID 和密码是否为空
if (!ID || !password) {
this.setData({
errorMessage: '账号ID和密码不能为空'
});
return;
}
// 发起注册请求,使用 x-www-form-urlencoded
wx.request({
header: {
'content-type': 'application/x-www-form-urlencoded' // 设置请求头
},
url: 'http://192.168.144.1:8080/user/register', // 替换为你的后端API地址
method: 'POST',
data: {
'username': ID,
'password': password
},
success(res) {
console.log('注册成功', res);
wx.showToast({
title: '注册成功',
icon: 'success'
});
// 注册成功后跳转到登录页面
wx.redirectTo({
url: '/pages/login/login'
});
},
});
},
// 跳转到登录页面
goToLogin() {
wx.redirectTo({
url: '/pages/login/login' // 登录页面的路径
});
}
});

@ -0,0 +1,3 @@
{
"usingComponents": {}
}

@ -0,0 +1,32 @@
<!-- pages/register/register.wxml -->
<view class="container">
<!-- 注册标题 -->
<view class="title">注册</view>
<!-- 输入账号ID号 -->
<input
type="text"
placeholder="请输入账号ID"
bindinput="onIDInput"
value="{{phone}}"
/>
<!-- 输入密码 -->
<input
type="password"
placeholder="请输入密码"
bindinput="onPasswordInput"
value="{{password}}"
/>
<!-- 显示错误信息 -->
<view class="error">{{errorMessage}}</view>
<!-- 注册按钮 -->
<button bindtap="submitUserInfo">注册</button>
<!-- 提示跳转到登录页面 -->
<view class="message">
已有账号?<text class="link" bindtap="goToLogin">点击登录</text>
</view>
</view>

@ -0,0 +1,76 @@
/* pages/register/register.wxss */
page {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f8f8f8;
}
.container {
width: 90%;
max-width: 400px;
background-color: #fff;
border-radius: 10px;
padding: 20px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}
.title {
text-align: center;
font-size: 24px;
font-weight: bold;
color: #333;
margin-bottom: 20px;
}
input {
width: 100%;
height: 40px;
padding: 0 10px;
margin-bottom: 15px;
border: 1px solid #ddd;
border-radius: 5px;
background-color: #fafafa;
}
input:focus {
border-color: #007aff;
background-color: #fff;
}
button {
width: 100%;
height: 45px;
background-color: #007aff;
color: #fff;
border: none;
border-radius: 5px;
font-size: 16px;
text-align: center;
line-height: 45px;
margin-top: 10px;
}
button:active {
background-color: #005bb5;
}
.error {
color: red;
font-size: 14px;
margin-top: 5px;
}
.message {
margin-top: 10px;
text-align: center;
color: #666;
}
.link {
color: #007aff;
text-decoration: underline;
cursor: pointer;
}
Loading…
Cancel
Save