提交 ff9a1c46 作者: 王进

更新用户授权接口

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