feat(mobile): 解决移动端authorization为空问题并完善加密登录功能
主要修复: - 修复登录流程逻辑顺序,先保存Token再调用getUserInfo API - 解决纯JavaScript RSA加密兼容性问题,替代JSEncrypt - 创建Token测试工具,便于调试和状态检查 - 优化请求头配置,符合后端API要求 技术改进: - 新增encrypt-pure.js:纯JavaScript RSA加密实现 - 新增token-test.js:Token存储和状态测试工具 - 优化request.js:增强Token获取和调试功能 - 更新login/index.vue:修复登录流程和添加测试功能 解决问题: - ✅ authorization请求头不再为空 - ✅ AES+RSA双重加密登录正常工作 - ✅ uni-app环境下RSA加密兼容性问题已解决 - ✅ 完整的登录认证流程验证通过
This commit is contained in:
1 parent
975a4f2e33
commit
5589cba249
8 files changed
+1862
-107
No files matched your search
@@ -37,6 +37,7 @@
|
||||
:border="true"
|
||||
:custom-style="inputStyle"
|
||||
placeholder-style="color: #999; font-size: 16px"
|
||||
@confirm="onPasswordConfirm"
|
||||
>
|
||||
<template #prefix>
|
||||
<text class="iconfont icon-lock input-prefix-icon"></text>
|
||||
@@ -44,13 +45,33 @@
|
||||
</u-input>
|
||||
</view>
|
||||
<!-- 登录按钮 -->
|
||||
<u-button type="primary" :custom-style="buttonStyle" @click="onLogin">
|
||||
<view class="login-btn-content">
|
||||
<!-- <text class="iconfont icon-login btn-icon"></text> -->
|
||||
<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>
|
||||
@@ -58,12 +79,19 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { api, userStorage } from '@/utils/request';
|
||||
import { runAllTests } from '@/utils/encrypt-test';
|
||||
import { testTokenStorage, getCurrentTokenInfo, clearAllLoginInfo } from '@/utils/token-test';
|
||||
|
||||
export default {
|
||||
name: 'LoginPage',
|
||||
data() {
|
||||
return {
|
||||
username: '',
|
||||
password: '',
|
||||
username: 'admin',
|
||||
password: 'admin123',
|
||||
loading: false,
|
||||
tenantList: [],
|
||||
tenantId: '000000',
|
||||
// uview-plus 输入框样式
|
||||
inputStyle: {
|
||||
height: '44px',
|
||||
@@ -88,17 +116,241 @@ export default {
|
||||
},
|
||||
};
|
||||
},
|
||||
async onLoad() {
|
||||
// 检查是否已登录
|
||||
if (userStorage.isLoggedIn()) {
|
||||
uni.reLaunch({
|
||||
url: '/pages/index/index',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 初始化租户列表
|
||||
await this.initTenantList();
|
||||
},
|
||||
methods: {
|
||||
onLogin() {
|
||||
if (!this.username) {
|
||||
/**
|
||||
* 初始化租户列表
|
||||
*/
|
||||
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) {
|
||||
if (!this.password.trim()) {
|
||||
uni.showToast({ title: '请输入密码', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
uni.showToast({ title: '仅原型演示,无实际登录', icon: 'success' });
|
||||
|
||||
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);
|
||||
|
||||
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',
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 表单输入处理
|
||||
*/
|
||||
onUsernameInput(value) {
|
||||
this.username = value;
|
||||
},
|
||||
|
||||
onPasswordInput(value) {
|
||||
this.password = value;
|
||||
},
|
||||
|
||||
/**
|
||||
* 键盘回车事件
|
||||
*/
|
||||
onPasswordConfirm() {
|
||||
this.onLogin();
|
||||
},
|
||||
|
||||
/**
|
||||
* 测试加密功能
|
||||
*/
|
||||
async testEncryption() {
|
||||
console.log('🧪 开始测试加密功能...');
|
||||
try {
|
||||
uni.showLoading({ title: '测试中...' });
|
||||
|
||||
const result = await runAllTests();
|
||||
|
||||
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 = testTokenStorage();
|
||||
getCurrentTokenInfo();
|
||||
|
||||
uni.showToast({
|
||||
title: result ? 'Token测试通过' : 'Token测试失败',
|
||||
icon: result ? 'success' : 'none',
|
||||
duration: 2000,
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 检查当前Token状态
|
||||
*/
|
||||
checkTokenStatus() {
|
||||
const info = getCurrentTokenInfo();
|
||||
const message = info
|
||||
? `Token: ${info.hasToken ? '存在' : '无'} | 登录: ${info.isLoggedIn ? '是' : '否'}`
|
||||
: 'Token检查失败';
|
||||
|
||||
uni.showModal({
|
||||
title: 'Token状态',
|
||||
content: message,
|
||||
showCancel: false,
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 清除登录信息
|
||||
*/
|
||||
clearLogin() {
|
||||
clearAllLoginInfo();
|
||||
uni.showToast({
|
||||
title: '已清除登录信息',
|
||||
icon: 'success',
|
||||
duration: 1500,
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user