提交 ff9a1c46 作者: 王进

更新用户授权接口

1. 增加getUserProfile兼容
2. 优化getUserInfo代码
上级 ade6972a
......@@ -311,22 +311,28 @@ class WechatSDK {
});
}
// 获取用户信息
public getUserInfo = async () => {
let status = await this._getSetting();
public getUserInfo = (): Promise<any> => {
return new Promise(async (resolve, reject) => {
if ((wx as any).getUserProfile) {
const userInfo = await this._getUserProfile();
resolve(userInfo);
return;
}
const status = await this._getSetting();
const w = (wx as any).getSystemInfoSync().windowWidth;
const h = (wx as any).getSystemInfoSync().windowHeight;
if (status == 1) {
// 用户已授权,可以直接调用相关 API
let userInfo = await this._getUserInfo();
const userInfo = await this._getUserInfo();
if (userInfo.nickname) {
// 上报用户授权
this.ReportData({
userInfo: { ...userInfo, nickName: encodeURI(userInfo.nickName) },
action: 'authorize',
})
return userInfo;
}
else if (status == 0) {
resolve(userInfo);
} else if (status == 0) {
// 用户已拒绝授权,再调用相关 API 或者 wx.authorize 会失败,需要引导用户到设置页面打开授权开关
const w = (wx as any).getSystemInfoSync().windowWidth;
const h = (wx as any).getSystemInfoSync().windowHeight;
let OpenSettingButton = (wx as any).createOpenSettingButton({
type: "text",
text: "",
......@@ -339,11 +345,10 @@ class WechatSDK {
});
OpenSettingButton.onTap(async (res) => {
let t_status = await this._getSetting();
console.log("--SDK点击设置按钮返回t_status=", t_status);
if (t_status == 1) {
OpenSettingButton.offTap(this);
OpenSettingButton.destroy();
OpenSettingButton = null;
if (t_status == 1) {
let userInfo = await this._getUserInfo();
// 上报用户授权
this.ReportData({
......@@ -351,18 +356,27 @@ class WechatSDK {
action: 'authorize',
nickName: encodeURI(userInfo.nickName)
})
return userInfo;
resolve(userInfo);
}
});
}
else if (status == -1) {
let userBtn = this.createUserInfoButton();
let userBtn = (wx as any).createUserInfoButton({
type: "text",
text: "",
withCredentials: false,
style: {
left: 0,
top: 0,
width: w,
height: h,
},
});
userBtn.onTap((res) => {
if (res.userInfo) {
//上报授权
userBtn.offTap(this);
userBtn.destroy();
userBtn = null;
if (res.userInfo) {
let userInfo = res["userInfo"];
// 上报用户授权
this.ReportData({
......@@ -370,27 +384,26 @@ class WechatSDK {
action: 'authorize',
nickName: encodeURI(userInfo.nickName)
})
return userInfo;
resolve(userInfo);
}
});
}
}
private createUserInfoButton() {
// 未询问过用户授权,调用相关 API 或者 wx.authorize 会弹窗询问用户
let w = (wx as any).getSystemInfoSync().windowWidth;
let h = (wx as any).getSystemInfoSync().windowHeight;
let userBtn = (wx as any).createUserInfoButton({
type: "text",
text: "",
withCredentials: false,
style: {
left: 0,
top: 0,
width: w,
height: h
});
};
private async _getUserProfile(): Promise<any> {
return new Promise(async function (resolve, reject) {
(wx as any).getUserProfile({
desc: '完善用户资料',
success: (res) => {
let userInfo = res["userInfo"];
resolve(userInfo);
},
fail: (res: any) => {
console.log("--SDK:玩家头像等数据失败,用户未授权");
resolve();
}
});
});
return userBtn
}
private async _getUserInfo(): Promise<any> {
return new Promise(async function (resolve, reject) {
......@@ -402,7 +415,7 @@ class WechatSDK {
},
fail: (res: any) => {
console.log("--SDK:玩家头像等数据失败,用户未授权");
reject();
resolve();
}
});
});
......@@ -412,19 +425,11 @@ class WechatSDK {
(wx as any).getSetting({
success: function (res) {
let authSetting = res.authSetting;
if (authSetting['scope.userInfo'] === true) {
resolve(1);
}
else if (authSetting['scope.userInfo'] === false) {
resolve(0);
}
else {
resolve(-1);
}
},
fail: function () {
reject();
if (authSetting['scope.userInfo'] === true) resolve(1);
else if (authSetting['scope.userInfo'] === false) resolve(0);
else resolve(-1);
},
fail: reject,
});
});
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论