小程序 根据code获取手机号

This commit is contained in:
Wang Kejun 2023-02-09 12:00:40 +08:00
parent 6ecf53dcf6
commit dca0397198
3 changed files with 62 additions and 0 deletions

View File

@ -26,4 +26,13 @@ export declare class WechatMpInstance {
};
isHyaline?: true;
}): Promise<ArrayBuffer>;
getUserPhoneNumber(code: string): Promise<{
phoneNumber: string;
purePhoneNumber: string;
countryCode: number;
watermark: {
timestamp: number;
appid: string;
};
}>;
}

View File

@ -157,6 +157,30 @@ var WechatMpInstance = /** @class */ (function () {
});
});
};
WechatMpInstance.prototype.getUserPhoneNumber = function (code) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var token, result;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAccessToken()];
case 1:
token = _a.sent();
return [4 /*yield*/, this.access("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=".concat(token), {
method: 'POST',
headers: {
'Content-type': 'application/json',
},
body: JSON.stringify({
code: code,
}),
})];
case 2:
result = (_a.sent());
return [2 /*return*/, result.phone_info];
}
});
});
};
return WechatMpInstance;
}());
exports.WechatMpInstance = WechatMpInstance;

View File

@ -156,4 +156,33 @@ export class WechatMpInstance {
);
return (await result.arrayBuffer()) as ArrayBuffer;
}
async getUserPhoneNumber(code: string) {
const token = await this.getAccessToken();
const result = (await this.access(
`https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=${token}`,
{
method: 'POST',
headers: {
'Content-type': 'application/json',
},
body: JSON.stringify({
code,
}),
}
)) as {
errcode: number;
errmsg: 'ok' | string;
phone_info: {
phoneNumber: string;
purePhoneNumber: string;
countryCode: number;
watermark: {
timestamp: number;
appid: string;
};
};
};
return result.phone_info;
}
}