460 lines
14 KiB
Vue
460 lines
14 KiB
Vue
<template>
|
||
<view class="login-bg">
|
||
<view class="login-card">
|
||
<!-- Logo和标题区域 -->
|
||
<view class="login-header">
|
||
<view class="login-logo">
|
||
<image class="logo-img" src="/static/logo-2.png" mode="aspectFit" />
|
||
</view>
|
||
<view class="login-text">
|
||
<view class="login-title">智慧校园</view>
|
||
<view class="login-subtitle">杭州第一技师学院</view>
|
||
</view>
|
||
</view>
|
||
<!-- 登录表单 -->
|
||
<view class="login-form">
|
||
<!-- 账号输入框 -->
|
||
<view class="input-wrapper">
|
||
<u-input
|
||
v-model="username"
|
||
placeholder="教工号/学号"
|
||
type="text"
|
||
border="true"
|
||
:custom-style="inputStyle"
|
||
placeholder-style="color: #999; font-size: 16px"
|
||
>
|
||
<template #prefix>
|
||
<u-icon
|
||
name="account"
|
||
size="20"
|
||
color="#b0b8c9"
|
||
class="input-prefix-icon"
|
||
/>
|
||
</template>
|
||
</u-input>
|
||
</view>
|
||
<!-- 密码输入框 -->
|
||
<view class="input-wrapper">
|
||
<u-input
|
||
v-model="password"
|
||
placeholder="密码"
|
||
type="password"
|
||
border="true"
|
||
:custom-style="inputStyle"
|
||
placeholder-style="color: #999; font-size: 16px"
|
||
@confirm="onPasswordConfirm"
|
||
>
|
||
<template #prefix>
|
||
<u-icon
|
||
name="lock"
|
||
size="20"
|
||
color="#b0b8c9"
|
||
class="input-prefix-icon"
|
||
/>
|
||
</template>
|
||
</u-input>
|
||
</view>
|
||
<!-- 登录按钮 -->
|
||
<u-button
|
||
type="primary"
|
||
:custom-style="buttonStyle"
|
||
:loading="loading"
|
||
:disabled="loading"
|
||
@click="onLogin"
|
||
>
|
||
<view class="login-btn-content" v-if="!loading">
|
||
<text class="login-btn-text">登录</text>
|
||
</view>
|
||
<view class="login-btn-content" v-else>
|
||
<text class="login-btn-text">登录中...</text>
|
||
</view>
|
||
</u-button>
|
||
</view>
|
||
<!-- 测试按钮(可选) -->
|
||
<!-- <view style="margin-top: 16px; display: flex; flex-direction: column; gap: 8px">
|
||
<u-button text="测试加密功能" @click="testEncryption" type="info" size="small" />
|
||
<u-button text="测试Token存储" @click="testToken" type="warning" size="small" />
|
||
<u-button
|
||
text="检查Token状态"
|
||
@click="checkTokenStatus"
|
||
type="default"
|
||
size="small"
|
||
/>
|
||
<u-button text="清除登录信息" @click="clearLogin" type="error" size="small" />
|
||
</view> -->
|
||
<!-- 版权信息 -->
|
||
<view class="login-copyright">© 2025 杭州第一技师学院 All Rights Reserved.</view>
|
||
</view>
|
||
</view>
|
||
</template>
|
||
|
||
<script>
|
||
import { api } from '@/common/request';
|
||
import { userStorage, tokenTest } from '@/common/storage';
|
||
import { validateKeyPair } from '@/common/crypto';
|
||
|
||
export default {
|
||
name: 'LoginPage',
|
||
data() {
|
||
return {
|
||
username: '',
|
||
password: '',
|
||
loading: false,
|
||
tenantList: [],
|
||
tenantId: '000000',
|
||
// uview-ui 输入框样式
|
||
inputStyle: {
|
||
height: '44px',
|
||
borderRadius: '8px',
|
||
borderColor: '#e3e8f0',
|
||
backgroundColor: '#fff',
|
||
fontSize: '16px',
|
||
color: '#222',
|
||
padding: '0 12px 0 12px',
|
||
},
|
||
// uview-ui 按钮样式
|
||
buttonStyle: {
|
||
width: '100%',
|
||
height: '48px',
|
||
borderRadius: '8px',
|
||
backgroundColor: '#5d87ff',
|
||
marginTop: '8px',
|
||
boxShadow: '0 4px 12px 0 rgba(93, 135, 255, 0.3)',
|
||
border: 'none',
|
||
fontSize: '18px',
|
||
fontWeight: '600',
|
||
},
|
||
};
|
||
},
|
||
async onLoad() {
|
||
// 检查是否已登录
|
||
if (userStorage.isLoggedIn()) {
|
||
uni.reLaunch({
|
||
url: '/pages/index/index',
|
||
});
|
||
return;
|
||
}
|
||
|
||
// 初始化租户列表
|
||
await this.initTenantList();
|
||
},
|
||
methods: {
|
||
/**
|
||
* 初始化租户列表
|
||
*/
|
||
async initTenantList() {
|
||
try {
|
||
const { data } = await api.getTenantList(false);
|
||
|
||
if (data && data.voList && data.voList.length > 0) {
|
||
this.tenantList = data.voList;
|
||
this.tenantId = data.voList[0].tenantId;
|
||
}
|
||
} catch (error) {
|
||
console.error('获取租户列表失败:', error);
|
||
// 租户列表获取失败时使用默认值
|
||
this.tenantId = '000000';
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 用户登录
|
||
*/
|
||
async onLogin() {
|
||
// 表单验证
|
||
if (!this.username.trim()) {
|
||
uni.showToast({ title: '请输入账号', icon: 'none' });
|
||
return;
|
||
}
|
||
if (!this.password.trim()) {
|
||
uni.showToast({ title: '请输入密码', icon: 'none' });
|
||
return;
|
||
}
|
||
|
||
if (this.loading) return;
|
||
|
||
this.loading = true;
|
||
|
||
// 更新按钮样式为加载状态
|
||
this.buttonStyle = {
|
||
...this.buttonStyle,
|
||
backgroundColor: '#a3bfff',
|
||
};
|
||
|
||
try {
|
||
uni.showLoading({ title: '登录中...' });
|
||
|
||
// 构建登录参数
|
||
const loginData = {
|
||
tenantId: this.tenantId,
|
||
username: this.username.trim(),
|
||
password: this.password.trim(),
|
||
clientId: 'e5cd7e4891bf95d1d19206ce24a7b32e',
|
||
grantType: 'password',
|
||
};
|
||
|
||
console.log('登录请求参数:', loginData);
|
||
|
||
// 调用登录API
|
||
const loginResult = await api.login(loginData);
|
||
console.log('登录结果:', loginResult);
|
||
if (loginResult.code === 200) {
|
||
// 登录成功,先保存Token
|
||
console.log('✅ 登录成功,先保存Token');
|
||
|
||
// 临时保存Token,以便后续API调用
|
||
uni.setStorageSync('access_token', loginResult.data.access_token);
|
||
console.log(
|
||
'🎫 Token已保存:',
|
||
loginResult.data.access_token.substring(0, 20) + '...',
|
||
);
|
||
|
||
// 获取用户信息
|
||
const userInfoResult = await api.getUserInfo();
|
||
|
||
if (userInfoResult.code === 200) {
|
||
// 保存完整的登录信息和用户信息
|
||
userStorage.saveLoginInfo(loginResult.data, userInfoResult.data);
|
||
|
||
uni.hideLoading();
|
||
uni.showToast({
|
||
title: '登录成功',
|
||
icon: 'success',
|
||
duration: 1000,
|
||
});
|
||
|
||
// 延迟跳转到首页
|
||
setTimeout(() => {
|
||
uni.reLaunch({
|
||
url: '/pages/index/index',
|
||
});
|
||
}, 1000);
|
||
} else {
|
||
throw new Error(userInfoResult.msg || '获取用户信息失败');
|
||
}
|
||
} else {
|
||
throw new Error(loginResult.msg || '登录失败');
|
||
}
|
||
} catch (error) {
|
||
console.error('登录失败:', error);
|
||
|
||
uni.hideLoading();
|
||
|
||
let errorMessage = '登录失败';
|
||
if (error.msg) {
|
||
errorMessage = error.msg;
|
||
} else if (error.message) {
|
||
errorMessage = error.message;
|
||
} else if (typeof error === 'string') {
|
||
errorMessage = error;
|
||
}
|
||
|
||
uni.showToast({
|
||
title: errorMessage,
|
||
icon: 'none',
|
||
duration: 2000,
|
||
});
|
||
} finally {
|
||
this.loading = false;
|
||
|
||
// 恢复按钮样式
|
||
this.buttonStyle = {
|
||
...this.buttonStyle,
|
||
backgroundColor: '#5d87ff',
|
||
};
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 键盘回车事件
|
||
*/
|
||
onPasswordConfirm() {
|
||
this.onLogin();
|
||
},
|
||
|
||
/**
|
||
* 测试加密功能
|
||
*/
|
||
async testEncryption() {
|
||
console.log('🧪 开始测试加密功能...');
|
||
try {
|
||
uni.showLoading({ title: '测试中...' });
|
||
|
||
const result = validateKeyPair();
|
||
|
||
uni.hideLoading();
|
||
|
||
if (result) {
|
||
uni.showToast({
|
||
title: '加密测试通过',
|
||
icon: 'success',
|
||
duration: 2000,
|
||
});
|
||
} else {
|
||
uni.showToast({
|
||
title: '加密测试失败',
|
||
icon: 'none',
|
||
duration: 2000,
|
||
});
|
||
}
|
||
} catch (error) {
|
||
console.error('测试加密功能失败:', error);
|
||
uni.hideLoading();
|
||
uni.showToast({
|
||
title: '测试失败: ' + error.message,
|
||
icon: 'none',
|
||
duration: 2000,
|
||
});
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 测试Token存储
|
||
*/
|
||
testToken() {
|
||
console.log('🧪 开始Token存储测试...');
|
||
|
||
const result = tokenTest.testTokenStorage();
|
||
tokenTest.getCurrentTokenInfo();
|
||
|
||
uni.showToast({
|
||
title: result ? 'Token测试通过' : 'Token测试失败',
|
||
icon: result ? 'success' : 'none',
|
||
duration: 2000,
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 检查当前Token状态
|
||
*/
|
||
checkTokenStatus() {
|
||
const info = tokenTest.getCurrentTokenInfo();
|
||
const message = info
|
||
? `Token: ${info.hasToken ? '存在' : '无'} | 登录: ${info.isLoggedIn ? '是' : '否'}`
|
||
: 'Token检查失败';
|
||
|
||
uni.showModal({
|
||
title: 'Token状态',
|
||
content: message,
|
||
showCancel: false,
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 清除登录信息
|
||
*/
|
||
clearLogin() {
|
||
tokenTest.clearAllLoginInfo();
|
||
uni.showToast({
|
||
title: '已清除登录信息',
|
||
icon: 'success',
|
||
duration: 1500,
|
||
});
|
||
},
|
||
},
|
||
};
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
.login-bg {
|
||
min-height: 100vh;
|
||
// background: linear-gradient(135deg, #5d87ff 0%, #a3bfff 100%);
|
||
//background: linear-gradient(135deg, #ffffff 0%, #e5e9f1 100%);
|
||
background-image: url('/static/logo-01.jpg');
|
||
background-size: cover;
|
||
background-position: center;
|
||
display: flex;
|
||
justify-content: center;
|
||
align-items: center;
|
||
}
|
||
.login-card {
|
||
width: 80vw;
|
||
max-width: 320px;
|
||
background: rgba(255, 255, 255, 0.85);
|
||
border-radius: 24px;
|
||
box-shadow: 0 8px 32px 0 rgba(93, 135, 255, 0.15);
|
||
padding: 32px 24px 20px 24px;
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
}
|
||
.login-header {
|
||
display: flex;
|
||
align-items: center;
|
||
margin-bottom: 24px;
|
||
gap: 16px;
|
||
}
|
||
.login-logo {
|
||
flex-shrink: 0;
|
||
}
|
||
.logo-img {
|
||
width: 60px;
|
||
height: 60px;
|
||
border-radius: 12px;
|
||
background: #f4f6fa;
|
||
}
|
||
.login-text {
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: flex-start;
|
||
}
|
||
.login-title {
|
||
font-size: 22px;
|
||
font-weight: bold;
|
||
color: #222;
|
||
margin-bottom: 4px;
|
||
line-height: 1.2;
|
||
}
|
||
.login-subtitle {
|
||
font-size: 14px;
|
||
color: rgb(107 114 128 / var(--tw-text-opacity, 1));
|
||
line-height: 1.2;
|
||
}
|
||
.login-form {
|
||
width: 100%;
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 16px;
|
||
}
|
||
|
||
/* uview-ui 输入框容器 */
|
||
.input-wrapper {
|
||
position: relative;
|
||
width: 100%;
|
||
}
|
||
|
||
/* uview-ui 输入框前缀图标 */
|
||
.input-prefix-icon {
|
||
margin-right: 8px;
|
||
}
|
||
|
||
/* uview-ui 按钮内容布局 */
|
||
.login-btn-content {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
gap: 8px;
|
||
}
|
||
|
||
/* 按钮图标 */
|
||
.btn-icon {
|
||
width: 16px;
|
||
height: 16px;
|
||
filter: brightness(0) saturate(100%) invert(100%);
|
||
}
|
||
|
||
/* 按钮文字 */
|
||
.login-btn-text {
|
||
color: #fff;
|
||
font-size: 18px;
|
||
font-weight: 600;
|
||
letter-spacing: 0.5px;
|
||
}
|
||
.login-copyright {
|
||
margin-top: 28px;
|
||
font-size: 12px;
|
||
color: #b0b8c9;
|
||
text-align: center;
|
||
}
|
||
</style>
|