This commit is contained in:
“xHuPo” 2025-06-17 14:44:48 +08:00
parent 17a02ea47e
commit 70e7a113e6
22 changed files with 967 additions and 285 deletions

View file

@ -92,22 +92,31 @@ const refreshToken = async () => {
/**
* 用户登录
* @param {string} username - 用户名
* @param {string} password - 密码
* @returns {Promise<Object>} 登录结果包含success和message字段
*/
const login = async (username, password) => {
const login = async () => {
try {
// 获取微信登录凭证
const loginResult = await wx.login();
if (!loginResult.code) {
return {
success: false,
message: '获取微信登录凭证失败'
};
}
// 发送code到后端
const response = await wx.request({
url: `${config.API_BASE_URL}${config.API_ENDPOINTS.AUTH.LOGIN}`,
method: 'POST',
data: { username, password }
data: { code: loginResult.code }
});
if (response.statusCode === 200 && response.data.access_token) {
const { access_token, refresh_token } = response.data;
const { access_token, refresh_token, openid } = response.data;
wx.setStorageSync(config.JWT_CONFIG.storage.access, access_token);
wx.setStorageSync(config.JWT_CONFIG.storage.refresh, refresh_token);
wx.setStorageSync('openid', openid);
// 触发登录成功事件
eventManager.emit('auth:login', parseToken(access_token));
@ -120,7 +129,7 @@ const login = async (username, password) => {
return {
success: false,
message: response.data?.message || '登录失败'
message: response.data?.errmsg || response.data?.message || '登录失败'
};
} catch (error) {
console.error('登录失败:', error);
@ -138,6 +147,7 @@ const logout = () => {
// 清除所有认证信息
wx.removeStorageSync(config.JWT_CONFIG.storage.access);
wx.removeStorageSync(config.JWT_CONFIG.storage.refresh);
wx.removeStorageSync('openid');
// 触发登出事件
eventManager.emit('auth:logout');
@ -149,7 +159,8 @@ const logout = () => {
*/
const isLoggedIn = () => {
const token = wx.getStorageSync(config.JWT_CONFIG.storage.access);
if (!token) return false;
const openid = wx.getStorageSync('openid');
if (!token || !openid) return false;
try {
// 解析JWT token不验证签名
@ -170,7 +181,8 @@ const isLoggedIn = () => {
*/
const getCurrentUser = () => {
const token = wx.getStorageSync(config.JWT_CONFIG.storage.access);
if (!token) return null;
const openid = wx.getStorageSync('openid');
if (!token || !openid) return null;
try {
// 解析JWT token不验证签名
@ -179,7 +191,7 @@ const getCurrentUser = () => {
return {
id: decoded.sub,
username: decoded.username,
openid: openid,
// 其他用户信息...
};
} catch (error) {