feat: 新增微信头像授权获取与个人中心展示功能
- 新增 `wechat-profile` 工具库,实现微信用户信息的本地存储、格式校验与授权获取逻辑 - 编写对应单元测试覆盖核心功能流程 - 重构个人中心成员组件,新增头像、昵称展示与授权触发按钮 - 更新个人中心页面,集成微信头像授权流程与状态管理
This commit is contained in:
1 parent
6296392e80
commit
e7b8c5bf43
4 files changed
+213
-8
No files matched your search
@@ -0,0 +1,49 @@
|
||||
export const WECHAT_PROFILE_KEY = "miniapp:wechat-profile";
|
||||
|
||||
export type WechatProfile = {
|
||||
avatarUrl: string;
|
||||
nickName?: string;
|
||||
};
|
||||
|
||||
function isWechatProfile(value: unknown): value is WechatProfile {
|
||||
if (!value || typeof value !== "object") return false;
|
||||
const profile = value as Partial<WechatProfile>;
|
||||
return typeof profile.avatarUrl === "string" && profile.avatarUrl.trim().length > 0;
|
||||
}
|
||||
|
||||
export function getStoredWechatProfile(): WechatProfile | null {
|
||||
try {
|
||||
const value = uni.getStorageSync(WECHAT_PROFILE_KEY);
|
||||
return isWechatProfile(value) ? value : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function saveWechatProfile(profile: WechatProfile) {
|
||||
if (!isWechatProfile(profile)) return;
|
||||
uni.setStorageSync(WECHAT_PROFILE_KEY, profile);
|
||||
}
|
||||
|
||||
export function requestWechatProfile(): Promise<WechatProfile | null> {
|
||||
return new Promise((resolve) => {
|
||||
if (typeof uni.getUserProfile !== "function") {
|
||||
resolve(null);
|
||||
return;
|
||||
}
|
||||
|
||||
uni.getUserProfile({
|
||||
provider: "weixin",
|
||||
desc: "用于展示您的微信头像",
|
||||
lang: "zh_CN",
|
||||
success: (result) => {
|
||||
const profile = {
|
||||
avatarUrl: result.userInfo?.avatarUrl?.trim() ?? "",
|
||||
nickName: result.userInfo?.nickName?.trim() || undefined,
|
||||
};
|
||||
resolve(isWechatProfile(profile) ? profile : null);
|
||||
},
|
||||
fail: () => resolve(null),
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user