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
+313
-96
@@ -1,120 +1,337 @@
|
||||
import { encryptLoginData } from './encrypt-pure.js';
|
||||
|
||||
// API请求配置
|
||||
const baseURL =
|
||||
process.env.NODE_ENV === 'development'
|
||||
? 'http://localhost:3000/api'
|
||||
: 'https://your-api-domain.com/api'
|
||||
? '/api' // 开发环境使用代理
|
||||
: 'https://your-api-domain.com';
|
||||
|
||||
// 统一请求方法
|
||||
export const request = (options) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: baseURL + options.url,
|
||||
method: options.method || 'GET',
|
||||
data: options.data || {},
|
||||
header: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: getToken(),
|
||||
...options.header,
|
||||
},
|
||||
success: (res) => {
|
||||
if (res.statusCode === 200) {
|
||||
resolve(res.data)
|
||||
} else {
|
||||
reject(res)
|
||||
}
|
||||
},
|
||||
fail: (error) => {
|
||||
reject(error)
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
// 获取token
|
||||
const token = getToken();
|
||||
|
||||
uni.request({
|
||||
url: baseURL + options.url,
|
||||
method: options.method || 'GET',
|
||||
data: options.data || {},
|
||||
header: {
|
||||
'Content-Type': 'application/json',
|
||||
authorization: token,
|
||||
clientid: 'e5cd7e4891bf95d1d19206ce24a7b32e',
|
||||
referer: 'http://localhost/login?redirect=/index',
|
||||
...options.header,
|
||||
},
|
||||
success: (res) => {
|
||||
// 处理响应
|
||||
if (res.statusCode === 200) {
|
||||
// 检查业务状态码
|
||||
if (res.data && res.data.code !== undefined) {
|
||||
if (res.data.code === 200) {
|
||||
resolve(res.data);
|
||||
} else {
|
||||
// 业务错误
|
||||
uni.showToast({
|
||||
title: res.data.msg || '请求失败',
|
||||
icon: 'none',
|
||||
});
|
||||
reject(res.data);
|
||||
}
|
||||
} else {
|
||||
resolve(res.data);
|
||||
}
|
||||
} else {
|
||||
// HTTP状态码错误
|
||||
uni.showToast({
|
||||
title: `请求失败: ${res.statusCode}`,
|
||||
icon: 'none',
|
||||
});
|
||||
reject(res);
|
||||
}
|
||||
},
|
||||
fail: (error) => {
|
||||
uni.showToast({
|
||||
title: '网络请求失败',
|
||||
icon: 'none',
|
||||
});
|
||||
reject(error);
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// 获取token
|
||||
function getToken() {
|
||||
try {
|
||||
const token = uni.getStorageSync('token')
|
||||
return token ? `Bearer ${token}` : ''
|
||||
} catch (e) {
|
||||
return ''
|
||||
}
|
||||
try {
|
||||
const token = uni.getStorageSync('access_token');
|
||||
console.log(
|
||||
'🔍 获取Token:',
|
||||
token ? `Bearer ${token.substring(0, 20)}...` : '❌ Token为空',
|
||||
);
|
||||
return token ? `Bearer ${token}` : '';
|
||||
} catch (e) {
|
||||
console.error('❌ 获取Token失败:', e);
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
// API方法封装
|
||||
export const api = {
|
||||
// 用户登录
|
||||
login: (data) =>
|
||||
request({
|
||||
url: '/auth/login',
|
||||
method: 'POST',
|
||||
data,
|
||||
}),
|
||||
// 用户登录
|
||||
login: async (data) => {
|
||||
try {
|
||||
console.log('🔐 开始加密登录流程...');
|
||||
|
||||
// 获取用户信息
|
||||
getUserInfo: () =>
|
||||
request({
|
||||
url: '/user/info',
|
||||
}),
|
||||
// 加密登录数据
|
||||
const encryptResult = encryptLoginData({
|
||||
username: data.username,
|
||||
password: data.password,
|
||||
tenantId: data.tenantId || '000000',
|
||||
clientId: data.clientId || 'e5cd7e4891bf95d1d19206ce24a7b32e',
|
||||
grantType: data.grantType || 'password',
|
||||
});
|
||||
|
||||
// 获取首页数据
|
||||
getHomeData: () =>
|
||||
request({
|
||||
url: '/home/data',
|
||||
}),
|
||||
}
|
||||
// 如果encryptLoginData返回的是Promise,需要await
|
||||
const actualEncryptResult = encryptResult.then ? await encryptResult : encryptResult;
|
||||
|
||||
if (!actualEncryptResult.success) {
|
||||
console.error('❌ 登录数据加密失败:', actualEncryptResult.error);
|
||||
throw new Error(actualEncryptResult.error);
|
||||
}
|
||||
|
||||
console.log('✅ 登录数据加密成功,发送加密请求...');
|
||||
|
||||
// 使用加密数据和自定义header发送请求
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: baseURL + '/auth/login',
|
||||
method: 'POST',
|
||||
data: actualEncryptResult.encryptedData, // 发送加密字符串,不是JSON对象
|
||||
header: {
|
||||
...actualEncryptResult.headers,
|
||||
referer: 'http://localhost/login?redirect=/index', // 添加正确格式的referer
|
||||
},
|
||||
success: (res) => {
|
||||
console.log('🔐 加密登录响应:', res);
|
||||
if (res.statusCode === 200) {
|
||||
if (res.data && res.data.code !== undefined) {
|
||||
if (res.data.code === 200) {
|
||||
console.log('✅ 加密登录成功');
|
||||
resolve(res.data);
|
||||
} else {
|
||||
console.log('❌ 登录失败:', res.data.msg);
|
||||
uni.showToast({
|
||||
title: res.data.msg || '登录失败',
|
||||
icon: 'none',
|
||||
});
|
||||
reject(res.data);
|
||||
}
|
||||
} else {
|
||||
resolve(res.data);
|
||||
}
|
||||
} else {
|
||||
console.log('❌ HTTP状态错误:', res.statusCode);
|
||||
uni.showToast({
|
||||
title: `请求失败: ${res.statusCode}`,
|
||||
icon: 'none',
|
||||
});
|
||||
reject(res);
|
||||
}
|
||||
},
|
||||
fail: (error) => {
|
||||
console.log('❌ 登录请求失败:', error);
|
||||
uni.showToast({
|
||||
title: '网络请求失败',
|
||||
icon: 'none',
|
||||
});
|
||||
reject(error);
|
||||
},
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('❌ 登录加密处理失败:', error);
|
||||
console.error('❌ 错误详情:', error.message);
|
||||
console.error('❌ 错误堆栈:', error.stack);
|
||||
uni.showToast({
|
||||
title: '登录处理失败: ' + error.message,
|
||||
icon: 'none',
|
||||
});
|
||||
return Promise.reject(error);
|
||||
}
|
||||
},
|
||||
|
||||
// 获取用户信息
|
||||
getUserInfo: () => {
|
||||
const token = getToken();
|
||||
console.log(
|
||||
'🎫 获取用户信息,当前token:',
|
||||
token ? token.substring(0, 20) + '...' : '无token',
|
||||
);
|
||||
|
||||
return request({
|
||||
url: '/system/user/getInfo',
|
||||
method: 'GET',
|
||||
header: {
|
||||
authorization: token,
|
||||
clientid: 'e5cd7e4891bf95d1d19206ce24a7b32e',
|
||||
referer: 'http://localhost/login?redirect=/index',
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
// 获取租户列表
|
||||
getTenantList: (isToken = false) =>
|
||||
request({
|
||||
url: '/auth/tenant/list',
|
||||
method: 'GET',
|
||||
header: {
|
||||
isToken: isToken,
|
||||
},
|
||||
}),
|
||||
|
||||
// 注销登录
|
||||
logout: () =>
|
||||
request({
|
||||
url: '/auth/logout',
|
||||
method: 'POST',
|
||||
}),
|
||||
};
|
||||
|
||||
// 用户信息缓存工具
|
||||
export const userStorage = {
|
||||
// 保存登录信息
|
||||
saveLoginInfo: (loginResult, userInfo) => {
|
||||
try {
|
||||
// 保存token
|
||||
uni.setStorageSync('access_token', loginResult.access_token);
|
||||
|
||||
// 保存用户信息(增加登录时间)
|
||||
if (userInfo) {
|
||||
const enhancedUserInfo = {
|
||||
...userInfo,
|
||||
loginTime: new Date().toISOString(), // 记录登录时间
|
||||
tokenExpireTime: loginResult.expires_in
|
||||
? new Date(Date.now() + loginResult.expires_in * 1000).toISOString()
|
||||
: null,
|
||||
};
|
||||
uni.setStorageSync('userInfo', JSON.stringify(enhancedUserInfo));
|
||||
}
|
||||
|
||||
// 保存登录状态
|
||||
uni.setStorageSync('isLoggedIn', true);
|
||||
|
||||
// 保存其他token信息
|
||||
if (loginResult.refresh_token) {
|
||||
uni.setStorageSync('refresh_token', loginResult.refresh_token);
|
||||
}
|
||||
|
||||
console.log('用户登录信息已保存', {
|
||||
hasToken: !!loginResult.access_token,
|
||||
hasUserInfo: !!userInfo,
|
||||
loginTime: new Date().toLocaleString(),
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('保存登录信息失败:', e);
|
||||
}
|
||||
},
|
||||
|
||||
// 获取用户信息
|
||||
getUserInfo: () => {
|
||||
try {
|
||||
const userInfo = uni.getStorageSync('userInfo');
|
||||
return userInfo ? JSON.parse(userInfo) : null;
|
||||
} catch (e) {
|
||||
console.error('获取用户信息失败:', e);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
// 获取token
|
||||
getToken: () => {
|
||||
try {
|
||||
return uni.getStorageSync('access_token') || '';
|
||||
} catch (e) {
|
||||
return '';
|
||||
}
|
||||
},
|
||||
|
||||
// 检查登录状态
|
||||
isLoggedIn: () => {
|
||||
try {
|
||||
const isLoggedIn = uni.getStorageSync('isLoggedIn');
|
||||
const token = uni.getStorageSync('access_token');
|
||||
return !!(isLoggedIn && token);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
// 清除登录信息
|
||||
clearLoginInfo: () => {
|
||||
try {
|
||||
uni.removeStorageSync('access_token');
|
||||
uni.removeStorageSync('userInfo');
|
||||
uni.removeStorageSync('isLoggedIn');
|
||||
console.log('用户登录信息已清除');
|
||||
} catch (e) {
|
||||
console.error('清除登录信息失败:', e);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
// 数据缓存工具
|
||||
export const storage = {
|
||||
// 设置缓存
|
||||
set: (key, value, expire) => {
|
||||
try {
|
||||
const data = {
|
||||
value,
|
||||
timestamp: Date.now(),
|
||||
expire: expire ? Date.now() + expire * 1000 : null,
|
||||
}
|
||||
uni.setStorageSync(key, JSON.stringify(data))
|
||||
} catch (e) {
|
||||
console.error('缓存设置失败:', e)
|
||||
}
|
||||
},
|
||||
// 设置缓存
|
||||
set: (key, value, expire) => {
|
||||
try {
|
||||
const data = {
|
||||
value,
|
||||
timestamp: Date.now(),
|
||||
expire: expire ? Date.now() + expire * 1000 : null,
|
||||
};
|
||||
uni.setStorageSync(key, JSON.stringify(data));
|
||||
} catch (e) {
|
||||
console.error('缓存设置失败:', e);
|
||||
}
|
||||
},
|
||||
|
||||
// 获取缓存
|
||||
get: (key) => {
|
||||
try {
|
||||
const data = uni.getStorageSync(key)
|
||||
if (!data) return null
|
||||
// 获取缓存
|
||||
get: (key) => {
|
||||
try {
|
||||
const data = uni.getStorageSync(key);
|
||||
if (!data) return null;
|
||||
|
||||
const parsed = JSON.parse(data)
|
||||
const parsed = JSON.parse(data);
|
||||
|
||||
// 检查是否过期
|
||||
if (parsed.expire && Date.now() > parsed.expire) {
|
||||
uni.removeStorageSync(key)
|
||||
return null
|
||||
}
|
||||
// 检查是否过期
|
||||
if (parsed.expire && Date.now() > parsed.expire) {
|
||||
uni.removeStorageSync(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
return parsed.value
|
||||
} catch (e) {
|
||||
console.error('缓存获取失败:', e)
|
||||
return null
|
||||
}
|
||||
},
|
||||
return parsed.value;
|
||||
} catch (e) {
|
||||
console.error('缓存获取失败:', e);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
// 删除缓存
|
||||
remove: (key) => {
|
||||
try {
|
||||
uni.removeStorageSync(key)
|
||||
} catch (e) {
|
||||
console.error('缓存删除失败:', e)
|
||||
}
|
||||
},
|
||||
// 删除缓存
|
||||
remove: (key) => {
|
||||
try {
|
||||
uni.removeStorageSync(key);
|
||||
} catch (e) {
|
||||
console.error('缓存删除失败:', e);
|
||||
}
|
||||
},
|
||||
|
||||
// 清空缓存
|
||||
clear: () => {
|
||||
try {
|
||||
uni.clearStorageSync()
|
||||
} catch (e) {
|
||||
console.error('缓存清空失败:', e)
|
||||
}
|
||||
},
|
||||
}
|
||||
// 清空缓存
|
||||
clear: () => {
|
||||
try {
|
||||
uni.clearStorageSync();
|
||||
} catch (e) {
|
||||
console.error('缓存清空失败:', e);
|
||||
}
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user