增加了refreshToken和getUserInfo接口

This commit is contained in:
Xu Chang 2023-02-08 20:45:24 +08:00
parent 27b31d4e13
commit a270afad1b
3 changed files with 102 additions and 12 deletions

View File

@ -15,15 +15,31 @@ declare type ServeMessageOption = TextServeMessageOption | NewsServeMessageOptio
export declare class WechatPublicInstance { export declare class WechatPublicInstance {
appId: string; appId: string;
appSecret: string; appSecret: string;
accessToken?: string; private accessToken?;
refreshAccessTokenHandler?: any; private refreshAccessTokenHandler?;
constructor(appId: string, appSecret: string); constructor(appId: string, appSecret: string);
private getAccessToken; private getAccessToken;
private access; private access;
code2Session(code: string): Promise<{ code2Session(code: string): Promise<{
sessionKey: string; accessToken: string;
openId: string; openId: string;
unionId: string; unionId: string;
scope: string;
refreshToken: string;
isSnapshotUser: boolean;
atExpiredAt: number;
rtExpiredAt: number;
}>;
refreshUserAccessToken(refreshToken: string): Promise<{
accessToken: string;
refreshToken: string;
atExpiredAt: number;
scope: string;
}>;
getUserInfo(accessToken: string, openId: string): Promise<{
nickname: string;
gender: string | undefined;
avatar: string;
}>; }>;
private refreshAccessToken; private refreshAccessToken;
decryptData(sessionKey: string, encryptedData: string, iv: string, signature: string): any; decryptData(sessionKey: string, encryptedData: string, iv: string, signature: string): any;

View File

@ -73,17 +73,59 @@ var WechatPublicInstance = /** @class */ (function () {
}; };
WechatPublicInstance.prototype.code2Session = function (code) { WechatPublicInstance.prototype.code2Session = function (code) {
return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__awaiter(this, void 0, void 0, function () {
var result, _a, session_key, openid, unionid; var result, _a, access_token, openid, unionid, scope, refresh_token, is_snapshotuser, expires_in;
return tslib_1.__generator(this, function (_b) { return tslib_1.__generator(this, function (_b) {
switch (_b.label) { switch (_b.label) {
case 0: return [4 /*yield*/, this.access("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".concat(this.appId, "&secret=").concat(this.appSecret, "&code=").concat(code, "&grant_type=authorization_code"), { session_key: 'aaa', openid: code, unionid: code })]; case 0: return [4 /*yield*/, this.access("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".concat(this.appId, "&secret=").concat(this.appSecret, "&code=").concat(code, "&grant_type=authorization_code"), { access_token: 'aaa', openid: code, unionid: code, refresh_token: 'aaa', is_snapshotuser: false, expires_in: 30, scope: 'userinfo' })];
case 1: case 1:
result = _b.sent(); result = _b.sent();
_a = typeof result === 'string' ? JSON.parse(result) : result, session_key = _a.session_key, openid = _a.openid, unionid = _a.unionid; _a = typeof result === 'string' ? JSON.parse(result) : result, access_token = _a.access_token, openid = _a.openid, unionid = _a.unionid, scope = _a.scope, refresh_token = _a.refresh_token, is_snapshotuser = _a.is_snapshotuser, expires_in = _a.expires_in;
return [2 /*return*/, { return [2 /*return*/, {
sessionKey: session_key, accessToken: access_token,
openId: openid, openId: openid,
unionId: unionid, unionId: unionid,
scope: scope,
refreshToken: refresh_token,
isSnapshotUser: !!is_snapshotuser,
atExpiredAt: Date.now() + expires_in * 1000,
rtExpiredAt: Date.now() + 30 * 86400 * 1000,
}];
}
});
});
};
WechatPublicInstance.prototype.refreshUserAccessToken = function (refreshToken) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var result, access_token, refresh_token, expires_in, scope;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.access("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=".concat(this.appId, "&grant_type=refresh_token&refresh_token=").concat(refreshToken), { access_token: 'aaa', refresh_token: 'aaa', expires_in: 30, scope: 'userinfo' })];
case 1:
result = _a.sent();
access_token = result.access_token, refresh_token = result.refresh_token, expires_in = result.expires_in, scope = result.scope;
return [2 /*return*/, {
accessToken: access_token,
refreshToken: refresh_token,
atExpiredAt: Date.now() + expires_in * 1000,
scope: scope,
}];
}
});
});
};
WechatPublicInstance.prototype.getUserInfo = function (accessToken, openId) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var result, nickname, sex, headimgurl;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.access("https://api.weixin.qq.com/sns/userinfo?access_token=".concat(accessToken, "&openid=").concat(openId, "&lang=zh_CN"), { nickname: '张三丰', sex: 1, headimgurl: 'hhttps://www.ertongzy.com/uploads/allimg/161005/2021233Y7-0.jpg' })];
case 1:
result = _a.sent();
nickname = result.nickname, sex = result.sex, headimgurl = result.headimgurl;
return [2 /*return*/, {
nickname: nickname,
gender: sex === 1 ? 'male' : sex === 2 ? 'female' : undefined,
avatar: headimgurl,
}]; }];
} }
}); });

View File

@ -24,8 +24,8 @@ export class WechatPublicInstance {
appId: string; appId: string;
appSecret: string; appSecret: string;
accessToken?: string; private accessToken?: string;
refreshAccessTokenHandler?: any; private refreshAccessTokenHandler?: any;
constructor(appId: string, appSecret: string) { constructor(appId: string, appSecret: string) {
this.appId = appId; this.appId = appId;
@ -83,15 +83,47 @@ export class WechatPublicInstance {
async code2Session(code: string) { async code2Session(code: string) {
const result = await this.access( const result = await this.access(
`https://api.weixin.qq.com/sns/oauth2/access_token?appid=${this.appId}&secret=${this.appSecret}&code=${code}&grant_type=authorization_code`, `https://api.weixin.qq.com/sns/oauth2/access_token?appid=${this.appId}&secret=${this.appSecret}&code=${code}&grant_type=authorization_code`,
{ session_key: 'aaa', openid: code, unionid: code } { access_token: 'aaa', openid: code, unionid: code, refresh_token: 'aaa', is_snapshotuser: false, expires_in: 30, scope: 'userinfo' }
); );
const { session_key, openid, unionid } = const { access_token, openid, unionid, scope, refresh_token, is_snapshotuser, expires_in } =
typeof result === 'string' ? JSON.parse(result) : result; // 这里微信返回的数据有时候竟然是text/plain typeof result === 'string' ? JSON.parse(result) : result; // 这里微信返回的数据有时候竟然是text/plain
return { return {
sessionKey: session_key as string, accessToken: access_token as string,
openId: openid as string, openId: openid as string,
unionId: unionid as string, unionId: unionid as string,
scope: scope as string,
refreshToken: refresh_token as string,
isSnapshotUser: !!is_snapshotuser,
atExpiredAt: Date.now() + expires_in * 1000,
rtExpiredAt: Date.now() + 30 * 86400 * 1000,
};
}
async refreshUserAccessToken(refreshToken: string) {
const result = await this.access(
`https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=${this.appId}&grant_type=refresh_token&refresh_token=${refreshToken}`,
{ access_token: 'aaa', refresh_token: 'aaa', expires_in: 30, scope: 'userinfo' }
);
const { access_token, refresh_token, expires_in, scope } = result;
return {
accessToken: access_token as string,
refreshToken: refresh_token as string,
atExpiredAt: Date.now() + expires_in * 1000,
scope: scope as string,
};
}
async getUserInfo(accessToken: string, openId: string) {
const result = await this.access(
`https://api.weixin.qq.com/sns/userinfo?access_token=${accessToken}&openid=${openId}&lang=zh_CN`,
{ nickname: '张三丰', sex: 1, headimgurl: 'hhttps://www.ertongzy.com/uploads/allimg/161005/2021233Y7-0.jpg' }
);
const { nickname, sex, headimgurl } = result;
return {
nickname: nickname as string,
gender: sex === 1 ? 'male' : sex === 2 ?'female' : undefined,
avatar: headimgurl as string,
}; };
} }