feat: 登录时同步微信头像和昵称
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -8,12 +8,34 @@ interface LoginResponse {
|
||||
|
||||
export async function wxLogin(): Promise<LoginResponse> {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Step 1:静默登录,获取 code
|
||||
uni.login({
|
||||
provider: 'weixin',
|
||||
success: async (loginRes) => {
|
||||
try {
|
||||
// Step 2: 获取用户微信头像和昵称
|
||||
let nickname: string | undefined
|
||||
let avatarUrl: string | undefined
|
||||
await new Promise<void>((res) => {
|
||||
uni.getUserProfile({
|
||||
desc: '用于完善个人资料',
|
||||
success: (profileRes) => {
|
||||
nickname = profileRes.userInfo.nickName
|
||||
avatarUrl = profileRes.userInfo.avatarUrl
|
||||
res()
|
||||
},
|
||||
fail: () => {
|
||||
// 用户拒绝授权,仍可继续登录
|
||||
res()
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
// Step 3: 发送登录请求
|
||||
const result = await post<LoginResponse>('/auth/login', {
|
||||
code: loginRes.code,
|
||||
nickname,
|
||||
avatarUrl,
|
||||
})
|
||||
uni.setStorageSync('token', result.token)
|
||||
resolve(result)
|
||||
|
||||
Reference in New Issue
Block a user