feat: 添加微信授权登录和修改昵称功能
This commit is contained in:
@@ -9,50 +9,125 @@ export default class UserManager {
|
||||
this.openid = null;
|
||||
this.nickname = '';
|
||||
this.avatarUrl = '';
|
||||
this.token = '';
|
||||
this.isLoggedIn = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化用户
|
||||
* 检查是否已登录(只检查本地缓存)
|
||||
* @returns {boolean} 是否已登录
|
||||
*/
|
||||
checkLogin() {
|
||||
const cached = wx.getStorageSync('userInfo');
|
||||
if (cached && cached.userId && cached.token) {
|
||||
this.userId = cached.userId;
|
||||
this.openid = cached.openid;
|
||||
this.nickname = cached.nickname || '游客';
|
||||
this.avatarUrl = cached.avatarUrl || '';
|
||||
this.token = cached.token;
|
||||
this.isLoggedIn = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化用户(只恢复缓存,不自动登录)
|
||||
*/
|
||||
async init() {
|
||||
try {
|
||||
// 尝试从本地存储恢复用户信息
|
||||
const cached = wx.getStorageSync('userInfo');
|
||||
if (cached) {
|
||||
this.userId = cached.userId;
|
||||
this.openid = cached.openid;
|
||||
this.nickname = cached.nickname;
|
||||
this.avatarUrl = cached.avatarUrl;
|
||||
this.isLoggedIn = true;
|
||||
return;
|
||||
}
|
||||
// 只检查本地缓存,不自动登录
|
||||
this.checkLogin();
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行登录(供登录按钮调用)
|
||||
* @param {Object} userInfo - 微信用户信息(头像、昵称等),可选
|
||||
*/
|
||||
async doLogin(userInfo = null) {
|
||||
try {
|
||||
// 获取登录code
|
||||
const { code } = await this.wxLogin();
|
||||
|
||||
// 调用后端登录接口
|
||||
const result = await post('/user/login', { code });
|
||||
// 调用后端登录接口,传入用户信息
|
||||
const result = await post('/user/login', {
|
||||
code,
|
||||
userInfo: userInfo ? {
|
||||
nickname: userInfo.nickName,
|
||||
avatarUrl: userInfo.avatarUrl,
|
||||
gender: userInfo.gender || 0
|
||||
} : null
|
||||
});
|
||||
|
||||
this.userId = result.userId;
|
||||
this.openid = result.openid;
|
||||
this.nickname = result.nickname || '游客';
|
||||
this.avatarUrl = result.avatarUrl || '';
|
||||
// 优先使用后端返回的,其次用授权获取的
|
||||
this.nickname = result.nickname || (userInfo?.nickName) || '游客';
|
||||
this.avatarUrl = result.avatarUrl || (userInfo?.avatarUrl) || '';
|
||||
this.token = result.token || '';
|
||||
this.isLoggedIn = true;
|
||||
|
||||
// 缓存用户信息
|
||||
// 缓存用户信息(包含 token)
|
||||
wx.setStorageSync('userInfo', {
|
||||
userId: this.userId,
|
||||
openid: this.openid,
|
||||
nickname: this.nickname,
|
||||
avatarUrl: this.avatarUrl
|
||||
avatarUrl: this.avatarUrl,
|
||||
token: this.token
|
||||
});
|
||||
|
||||
return {
|
||||
userId: this.userId,
|
||||
nickname: this.nickname,
|
||||
avatarUrl: this.avatarUrl
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('用户初始化失败:', error);
|
||||
// 使用临时身份
|
||||
this.userId = 0;
|
||||
this.nickname = '游客';
|
||||
this.isLoggedIn = false;
|
||||
console.error('登录失败:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
*/
|
||||
logout() {
|
||||
this.userId = null;
|
||||
this.openid = null;
|
||||
this.nickname = '';
|
||||
this.avatarUrl = '';
|
||||
this.token = '';
|
||||
this.isLoggedIn = false;
|
||||
wx.removeStorageSync('userInfo');
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户资料
|
||||
*/
|
||||
async updateProfile(nickname, avatarUrl) {
|
||||
if (!this.isLoggedIn) return false;
|
||||
try {
|
||||
await post('/user/profile', {
|
||||
nickname,
|
||||
avatarUrl,
|
||||
gender: 0
|
||||
}, { params: { userId: this.userId } });
|
||||
|
||||
// 更新本地数据
|
||||
this.nickname = nickname;
|
||||
this.avatarUrl = avatarUrl;
|
||||
|
||||
// 更新缓存(保留 token)
|
||||
wx.setStorageSync('userInfo', {
|
||||
userId: this.userId,
|
||||
openid: this.openid,
|
||||
nickname: this.nickname,
|
||||
avatarUrl: this.avatarUrl,
|
||||
token: this.token
|
||||
});
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
console.error('更新资料失败:', e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,12 +138,16 @@ export default class UserManager {
|
||||
return new Promise((resolve, reject) => {
|
||||
const timeout = setTimeout(() => {
|
||||
reject(new Error('登录超时'));
|
||||
}, 3000);
|
||||
}, 5000);
|
||||
|
||||
wx.login({
|
||||
success: (res) => {
|
||||
clearTimeout(timeout);
|
||||
resolve(res);
|
||||
if (res.code) {
|
||||
resolve(res);
|
||||
} else {
|
||||
reject(new Error('获取code失败'));
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
clearTimeout(timeout);
|
||||
|
||||
Reference in New Issue
Block a user