getaccesstoken
This commit is contained in:
parent
3ec4bd961d
commit
ae7eff3a25
|
|
@ -18,6 +18,7 @@ export declare class WechatPublicInstance {
|
|||
accessToken?: string;
|
||||
refreshAccessTokenHandler?: any;
|
||||
constructor(appId: string, appSecret: string);
|
||||
private getAccessToken;
|
||||
private access;
|
||||
code2Session(code: string): Promise<{
|
||||
sessionKey: string;
|
||||
|
|
|
|||
|
|
@ -11,6 +11,24 @@ var WechatPublicInstance = /** @class */ (function () {
|
|||
this.appSecret = appSecret;
|
||||
this.refreshAccessToken();
|
||||
}
|
||||
WechatPublicInstance.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*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
WechatPublicInstance.prototype.access = function (url, mockData, init) {
|
||||
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
||||
var response, headers, status, contentType, json, data;
|
||||
|
|
@ -108,7 +126,7 @@ var WechatPublicInstance = /** @class */ (function () {
|
|||
};
|
||||
WechatPublicInstance.prototype.getQrCode = function (options) {
|
||||
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
||||
var sceneId, sceneStr, expireSeconds, isPermanent, scene, actionName, myInit, result;
|
||||
var sceneId, sceneStr, expireSeconds, isPermanent, scene, actionName, myInit, token, result;
|
||||
return tslib_1.__generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
|
|
@ -152,12 +170,15 @@ var WechatPublicInstance = /** @class */ (function () {
|
|||
}),
|
||||
};
|
||||
}
|
||||
return [4 /*yield*/, this.access("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".concat(this.accessToken), {
|
||||
return [4 /*yield*/, this.getAccessToken()];
|
||||
case 1:
|
||||
token = _a.sent();
|
||||
return [4 /*yield*/, this.access("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".concat(token), {
|
||||
ticket: "ticket".concat(Date.now()),
|
||||
url: "http://mock/q/".concat(sceneId ? sceneId : sceneStr),
|
||||
expireSeconds: expireSeconds,
|
||||
}, myInit)];
|
||||
case 1:
|
||||
case 2:
|
||||
result = _a.sent();
|
||||
return [2 /*return*/, {
|
||||
ticket: result.ticket,
|
||||
|
|
@ -170,7 +191,7 @@ var WechatPublicInstance = /** @class */ (function () {
|
|||
};
|
||||
WechatPublicInstance.prototype.sendTemplateMessage = function (options) {
|
||||
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
||||
var openId, templateId, url, data, miniProgram, clientMsgId, myInit, result, errcode;
|
||||
var openId, templateId, url, data, miniProgram, clientMsgId, myInit, token, result, errcode;
|
||||
return tslib_1.__generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
|
|
@ -189,12 +210,15 @@ var WechatPublicInstance = /** @class */ (function () {
|
|||
data: data,
|
||||
}),
|
||||
};
|
||||
return [4 /*yield*/, this.access("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".concat(this.accessToken), {
|
||||
return [4 /*yield*/, this.getAccessToken()];
|
||||
case 1:
|
||||
token = _a.sent();
|
||||
return [4 /*yield*/, this.access("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".concat(token), {
|
||||
errcode: 0,
|
||||
errmsg: 'ok',
|
||||
msgid: Date.now(),
|
||||
}, myInit)];
|
||||
case 1:
|
||||
case 2:
|
||||
result = _a.sent();
|
||||
errcode = result.errcode;
|
||||
if (errcode === 0) {
|
||||
|
|
@ -207,7 +231,7 @@ var WechatPublicInstance = /** @class */ (function () {
|
|||
};
|
||||
WechatPublicInstance.prototype.sendServeMessage = function (options) {
|
||||
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
||||
var openId, type, myInit, result, errcode;
|
||||
var openId, type, myInit, token, result, errcode;
|
||||
return tslib_1.__generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
|
|
@ -254,11 +278,14 @@ var WechatPublicInstance = /** @class */ (function () {
|
|||
throw new Error('当前消息类型暂不支持');
|
||||
}
|
||||
}
|
||||
return [4 /*yield*/, this.access("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".concat(this.accessToken), {
|
||||
return [4 /*yield*/, this.getAccessToken()];
|
||||
case 1:
|
||||
token = _a.sent();
|
||||
return [4 /*yield*/, this.access("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".concat(token), {
|
||||
errcode: 0,
|
||||
errmsg: 'ok',
|
||||
}, myInit)];
|
||||
case 1:
|
||||
case 2:
|
||||
result = _a.sent();
|
||||
errcode = result.errcode;
|
||||
if (errcode === 0) {
|
||||
|
|
@ -271,7 +298,7 @@ var WechatPublicInstance = /** @class */ (function () {
|
|||
};
|
||||
WechatPublicInstance.prototype.batchGetArticle = function (options) {
|
||||
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
||||
var offset, count, noContent, myInit, result, errcode;
|
||||
var offset, count, noContent, myInit, token, result, errcode;
|
||||
return tslib_1.__generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0:
|
||||
|
|
@ -287,7 +314,10 @@ var WechatPublicInstance = /** @class */ (function () {
|
|||
no_content: noContent,
|
||||
}),
|
||||
};
|
||||
return [4 /*yield*/, this.access("https://api.weixin.qq.com/cgi-bin/freepublish/batchget?access_token=".concat(this.accessToken), {
|
||||
return [4 /*yield*/, this.getAccessToken()];
|
||||
case 1:
|
||||
token = _a.sent();
|
||||
return [4 /*yield*/, this.access("https://api.weixin.qq.com/cgi-bin/freepublish/batchget?access_token=".concat(token), {
|
||||
total_count: 1,
|
||||
item_count: 1,
|
||||
item: [
|
||||
|
|
@ -314,7 +344,7 @@ var WechatPublicInstance = /** @class */ (function () {
|
|||
},
|
||||
],
|
||||
}, myInit)];
|
||||
case 1:
|
||||
case 2:
|
||||
result = _a.sent();
|
||||
errcode = result.errcode;
|
||||
if (!errcode) {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ export declare class WechatWebInstance {
|
|||
accessToken?: string;
|
||||
refreshAccessTokenHandler?: any;
|
||||
constructor(appId: string, appSecret: string);
|
||||
private getAccessToken;
|
||||
private access;
|
||||
code2Session(code: string): Promise<{
|
||||
sessionKey: string;
|
||||
|
|
|
|||
|
|
@ -11,6 +11,24 @@ var WechatWebInstance = /** @class */ (function () {
|
|||
this.appSecret = appSecret;
|
||||
this.refreshAccessToken();
|
||||
}
|
||||
WechatWebInstance.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*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
WechatWebInstance.prototype.access = function (url, mockData, init) {
|
||||
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
||||
var response, headers, status, contentType, json, data;
|
||||
|
|
|
|||
|
|
@ -154,4 +154,5 @@ export class WechatMpInstance {
|
|||
);
|
||||
return (await result.arrayBuffer()) as ArrayBuffer;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,6 +34,16 @@ export class WechatPublicInstance {
|
|||
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, mockData: any, init?: RequestInit) {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
return mockData;
|
||||
|
|
@ -169,9 +179,9 @@ export class WechatPublicInstance {
|
|||
}),
|
||||
};
|
||||
}
|
||||
|
||||
const token = await this.getAccessToken();
|
||||
const result = await this.access(
|
||||
`https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=${this.accessToken}`,
|
||||
`https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=${token}`,
|
||||
{
|
||||
ticket: `ticket${Date.now()}`,
|
||||
url: `http://mock/q/${sceneId ? sceneId : sceneStr}`,
|
||||
|
|
@ -213,8 +223,9 @@ export class WechatPublicInstance {
|
|||
data,
|
||||
}),
|
||||
};
|
||||
const token = await this.getAccessToken();
|
||||
const result = await this.access(
|
||||
`https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=${this.accessToken}`,
|
||||
`https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=${token}`,
|
||||
{
|
||||
errcode: 0,
|
||||
errmsg: 'ok',
|
||||
|
|
@ -276,8 +287,9 @@ export class WechatPublicInstance {
|
|||
throw new Error('当前消息类型暂不支持')
|
||||
}
|
||||
}
|
||||
const token = await this.getAccessToken();
|
||||
const result = await this.access(
|
||||
`https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=${this.accessToken}`,
|
||||
`https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=${token}`,
|
||||
{
|
||||
errcode: 0,
|
||||
errmsg: 'ok',
|
||||
|
|
@ -307,8 +319,9 @@ export class WechatPublicInstance {
|
|||
no_content: noContent,
|
||||
}),
|
||||
};
|
||||
const token = await this.getAccessToken();
|
||||
const result = await this.access(
|
||||
`https://api.weixin.qq.com/cgi-bin/freepublish/batchget?access_token=${this.accessToken}`,
|
||||
`https://api.weixin.qq.com/cgi-bin/freepublish/batchget?access_token=${token}`,
|
||||
{
|
||||
total_count: 1,
|
||||
item_count: 1,
|
||||
|
|
|
|||
|
|
@ -17,6 +17,16 @@ export class WechatWebInstance {
|
|||
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, mockData: any, init?: RequestInit) {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
return mockData;
|
||||
|
|
|
|||
Loading…
Reference in New Issue