wechatMp获得token的方式
This commit is contained in:
parent
ada14e6e62
commit
f16b62ca18
|
|
@ -1,9 +1,10 @@
|
|||
export declare class WechatMpInstance {
|
||||
appId: string;
|
||||
appSecret: string;
|
||||
accessToken?: string;
|
||||
refreshAccessTokenHandler?: any;
|
||||
private accessToken?;
|
||||
private refreshAccessTokenHandler?;
|
||||
constructor(appId: string, appSecret: string);
|
||||
private getAccessToken;
|
||||
private access;
|
||||
code2Session(code: string): Promise<{
|
||||
sessionKey: string;
|
||||
|
|
|
|||
|
|
@ -10,6 +10,24 @@ var WechatMpInstance = /** @class */ (function () {
|
|||
this.appSecret = appSecret;
|
||||
this.refreshAccessToken();
|
||||
}
|
||||
WechatMpInstance.prototype.getAccessToken = function () {
|
||||
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
||||
return tslib_1.__generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
if (!true) return [3 /*break*/, 2];
|
||||
if (this.accessToken) {
|
||||
return [2 /*return*/, this.accessToken];
|
||||
}
|
||||
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(function () { return resolve(0); }, 500); })];
|
||||
case 1:
|
||||
_a.sent();
|
||||
return [3 /*break*/, 0];
|
||||
case 2: return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
WechatMpInstance.prototype.access = function (url, init) {
|
||||
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
||||
var response, headers, status, contentType, json, data;
|
||||
|
|
@ -104,30 +122,33 @@ var WechatMpInstance = /** @class */ (function () {
|
|||
WechatMpInstance.prototype.getMpUnlimitWxaCode = function (_a) {
|
||||
var scene = _a.scene, page = _a.page, envVersion = _a.envVersion, width = _a.width, autoColor = _a.autoColor, lineColor = _a.lineColor, isHyaline = _a.isHyaline;
|
||||
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
||||
var result;
|
||||
var token, result;
|
||||
return tslib_1.__generator(this, function (_b) {
|
||||
switch (_b.label) {
|
||||
case 0: return [4 /*yield*/, this.access("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".concat(this.accessToken), {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-type': "application/json",
|
||||
'Accept': 'image/jpg',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
// access_token: this.accessToken,
|
||||
scene: scene,
|
||||
page: page,
|
||||
env_version: envVersion,
|
||||
width: width,
|
||||
auto_color: autoColor,
|
||||
line_color: lineColor,
|
||||
is_hyaline: isHyaline,
|
||||
})
|
||||
})];
|
||||
case 0: return [4 /*yield*/, this.getAccessToken()];
|
||||
case 1:
|
||||
token = _b.sent();
|
||||
return [4 /*yield*/, this.access("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".concat(token), {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-type': "application/json",
|
||||
'Accept': 'image/jpg',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
// access_token: this.accessToken,
|
||||
scene: scene,
|
||||
page: page,
|
||||
env_version: envVersion,
|
||||
width: width,
|
||||
auto_color: autoColor,
|
||||
line_color: lineColor,
|
||||
is_hyaline: isHyaline,
|
||||
})
|
||||
})];
|
||||
case 2:
|
||||
result = _b.sent();
|
||||
return [4 /*yield*/, result.arrayBuffer()];
|
||||
case 2: return [2 /*return*/, (_b.sent())];
|
||||
case 3: return [2 /*return*/, (_b.sent())];
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ export class WechatMpInstance {
|
|||
appId: string;
|
||||
appSecret: string;
|
||||
|
||||
accessToken?: string;
|
||||
refreshAccessTokenHandler?: any;
|
||||
private accessToken?: string;
|
||||
private refreshAccessTokenHandler?: any;
|
||||
|
||||
constructor(appId: string, appSecret: string) {
|
||||
this.appId = appId;
|
||||
|
|
@ -15,6 +15,18 @@ export class WechatMpInstance {
|
|||
this.refreshAccessToken();
|
||||
}
|
||||
|
||||
private async getAccessToken() {
|
||||
while (true) {
|
||||
if (this.accessToken) {
|
||||
return this.accessToken;
|
||||
}
|
||||
|
||||
await new Promise(
|
||||
(resolve) => setTimeout(() => resolve(0), 500)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async access(url: string, init?: RequestInit) {
|
||||
const response = await global.fetch(url, init);
|
||||
|
||||
|
|
@ -101,7 +113,8 @@ export class WechatMpInstance {
|
|||
};
|
||||
isHyaline?: true;
|
||||
}) {
|
||||
const result = await this.access(`https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${this.accessToken}`, {
|
||||
const token = await this.getAccessToken();
|
||||
const result = await this.access(`https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${token}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-type': "application/json",
|
||||
|
|
|
|||
Loading…
Reference in New Issue