Merge branch 'dev' of codeup.aliyun.com:61c14a7efa282c88e103c23f/oak-external-sdk into dev

This commit is contained in:
Xu Chang 2023-02-08 20:45:39 +08:00
commit bc4ec99614
37 changed files with 494 additions and 84 deletions

View File

@ -1,12 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AliSmsInstance = exports.TencentSmsInstance = void 0;
var tslib_1 = require("tslib");
var Sms_1 = require("./service/tencent/Sms");
Object.defineProperty(exports, "TencentSmsInstance", { enumerable: true, get: function () { return Sms_1.TencentSmsInstance; } });
var Sms_2 = require("./service/ali/Sms");
Object.defineProperty(exports, "AliSmsInstance", { enumerable: true, get: function () { return Sms_2.AliSmsInstance; } });
var assert_1 = tslib_1.__importDefault(require("assert"));
var SmsSDK = /** @class */ (function () {
function SmsSDK() {
this.tencentMap = {};
@ -26,7 +24,9 @@ var SmsSDK = /** @class */ (function () {
return instance;
}
else if (origin === 'ali') {
(0, assert_1.default)(apiVersion, '阿里云短信apiVersion必须传入');
if (!apiVersion) {
throw new Error('阿里云短信apiVersion必须传入');
}
if (this.aliMap[accessKey]) {
return this.aliMap[accessKey];
}

0
lib/fetch/index.d.ts vendored Normal file
View File

2
lib/fetch/index.js Normal file
View File

@ -0,0 +1,2 @@
"use strict";
require('isomorphic-fetch');

0
lib/fetch/index.mp.d.ts vendored Normal file
View File

2
lib/fetch/index.mp.js Normal file
View File

@ -0,0 +1,2 @@
"use strict";
global.fetch = global.fetch;

0
lib/fetch/index.web.d.ts vendored Normal file
View File

2
lib/fetch/index.web.js Normal file
View File

@ -0,0 +1,2 @@
"use strict";
global.fetch = fetch;

View File

@ -1,3 +1,4 @@
import Core from '@alicloud/pop-core/lib/rpc';
declare type SendSmsRequest = {
PhoneNumbers: string[];
TemplateCode: string;
@ -12,6 +13,7 @@ export declare class AliSmsInstance {
regionId: string;
endpoint: string;
apiVersion: string;
client: Core;
constructor(accessKeyId: string, accessKeySecret: string, regionId: string, endpoint: string, apiVersion: string);
sendSms(params: SendSmsRequest): Promise<void>;
}

View File

@ -1,23 +1,21 @@
"use strict";
// import Core from '@alicloud/pop-core/lib/rpc';
//todo 引入阿里云的库 在前端跑的时候会出现fs模块不存在
Object.defineProperty(exports, "__esModule", { value: true });
exports.AliSmsInstance = void 0;
var tslib_1 = require("tslib");
var rpc_1 = tslib_1.__importDefault(require("@alicloud/pop-core/lib/rpc"));
var AliSmsInstance = /** @class */ (function () {
// client: Core;
function AliSmsInstance(accessKeyId, accessKeySecret, regionId, endpoint, apiVersion) {
this.accessKeyId = accessKeyId;
this.accessKeySecret = accessKeySecret;
this.regionId = regionId;
this.endpoint = endpoint;
this.apiVersion = apiVersion;
// this.client = new Core({
// accessKeyId: this.accessKeyId,
// accessKeySecret: this.accessKeySecret,
// endpoint: this.endpoint || 'dysmsapi.aliyuncs.com',
// apiVersion: this.apiVersion,
// });
this.client = new rpc_1.default({
accessKeyId: this.accessKeyId,
accessKeySecret: this.accessKeySecret,
endpoint: this.endpoint || 'dysmsapi.aliyuncs.com',
apiVersion: this.apiVersion,
});
}
AliSmsInstance.prototype.sendSms = function (params) {
return tslib_1.__awaiter(this, void 0, void 0, function () {

24
lib/service/ali/Sms.mp.d.ts vendored Normal file
View File

@ -0,0 +1,24 @@
declare type SendSmsRequest = {
PhoneNumbers: string[];
TemplateCode: string;
SignName: string;
TemplateParam?: Record<string, string>;
SmsUpExtendCode?: string;
OutId?: string;
};
declare type SendSmsResponse = {
Code: 'OK' | string;
Message: string;
BizId: string;
RequestId: string;
};
export declare class AliSmsInstance {
secretId: string;
secretKey: string;
region: string;
endpoint: string;
client: any;
constructor(secretId: string, secretKey: string, region: string, endpoint: string);
sendSms(params: SendSmsRequest): Promise<SendSmsResponse>;
}
export {};

34
lib/service/ali/Sms.mp.js Normal file
View File

@ -0,0 +1,34 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AliSmsInstance = void 0;
var tslib_1 = require("tslib");
var AliSmsInstance = /** @class */ (function () {
function AliSmsInstance(secretId, secretKey, region, endpoint) {
this.secretId = secretId;
this.secretKey = secretKey;
this.region = region;
this.endpoint = endpoint;
var clientConfig = {
credential: {
secretId: this.secretId,
secretKey: this.secretKey,
},
region: this.region,
profile: {
httpProfile: {
endpoint: this.endpoint || 'dysmsapi.aliyuncs.com',
},
},
};
}
AliSmsInstance.prototype.sendSms = function (params) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
console.log('mp走不到这里');
return [2 /*return*/, {}];
});
});
};
return AliSmsInstance;
}());
exports.AliSmsInstance = AliSmsInstance;

24
lib/service/ali/Sms.web.d.ts vendored Normal file
View File

@ -0,0 +1,24 @@
declare type SendSmsRequest = {
PhoneNumbers: string[];
TemplateCode: string;
SignName: string;
TemplateParam?: Record<string, string>;
SmsUpExtendCode?: string;
OutId?: string;
};
declare type SendSmsResponse = {
Code: 'OK' | string;
Message: string;
BizId: string;
RequestId: string;
};
export declare class AliSmsInstance {
secretId: string;
secretKey: string;
region: string;
endpoint: string;
client: any;
constructor(secretId: string, secretKey: string, region: string, endpoint: string);
sendSms(params: SendSmsRequest): Promise<SendSmsResponse>;
}
export {};

View File

@ -0,0 +1,34 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AliSmsInstance = void 0;
var tslib_1 = require("tslib");
var AliSmsInstance = /** @class */ (function () {
function AliSmsInstance(secretId, secretKey, region, endpoint) {
this.secretId = secretId;
this.secretKey = secretKey;
this.region = region;
this.endpoint = endpoint;
var clientConfig = {
credential: {
secretId: this.secretId,
secretKey: this.secretKey,
},
region: this.region,
profile: {
httpProfile: {
endpoint: this.endpoint || 'dysmsapi.aliyuncs.com',
},
},
};
}
AliSmsInstance.prototype.sendSms = function (params) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
console.log('web走不到这里');
return [2 /*return*/, {}];
});
});
};
return AliSmsInstance;
}());
exports.AliSmsInstance = AliSmsInstance;

View File

@ -2,7 +2,7 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.AmapInstance = void 0;
var tslib_1 = require("tslib");
require('isomorphic-fetch');
require('../../fetch');
var AmapInstance = /** @class */ (function () {
function AmapInstance(key) {
this.key = key;
@ -15,7 +15,7 @@ var AmapInstance = /** @class */ (function () {
case 0:
from = data.from, to = data.to;
url = "http://restapi.amap.com/v3/direction/driving?origin=".concat(from[0].toFixed(6), ",").concat(from[1].toFixed(6), "&destination=").concat(to[0].toFixed(6), ",").concat(to[1].toFixed(6), "&strategy=10&key=").concat(this.key);
return [4 /*yield*/, fetch(url)];
return [4 /*yield*/, global.fetch(url)];
case 1:
result = _a.sent();
return [4 /*yield*/, result.json()];

View File

@ -2,7 +2,7 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.QiniuCloudInstance = void 0;
var tslib_1 = require("tslib");
require('isomorphic-fetch');
require('../../fetch');
var crypto_1 = tslib_1.__importDefault(require("crypto"));
var ts_md5_1 = require("ts-md5");
var buffer_1 = require("buffer");
@ -84,7 +84,7 @@ var QiniuCloudInstance = /** @class */ (function () {
contentType = 'application/json';
token = this.getLiveToken(method, path, host);
url = "https://pili.qiniuapi.com/v2/hubs/".concat(hub, "/streams");
return [4 /*yield*/, fetch(url, {
return [4 /*yield*/, global.fetch(url, {
method: 'POST',
headers: {
Authorization: token,
@ -152,7 +152,7 @@ var QiniuCloudInstance = /** @class */ (function () {
contentType = 'application/json';
token = this.getLiveToken(method, path, host, rawQuery, contentType, bodyStr);
url = "https://pili.qiniuapi.com".concat(path);
return [4 /*yield*/, fetch(url, {
return [4 /*yield*/, global.fetch(url, {
method: 'POST',
headers: {
Authorization: token,

10
lib/service/tencent/Sms.mp.d.ts vendored Normal file
View File

@ -0,0 +1,10 @@
import { SendSmsRequest, SendSmsResponse } from 'tencentcloud-sdk-nodejs/tencentcloud/services/sms/v20210111/sms_models';
export declare class TencentSmsInstance {
secretId: string;
secretKey: string;
region: string;
endpoint: string;
client: any;
constructor(secretId: string, secretKey: string, region: string, endpoint: string);
sendSms(params: SendSmsRequest): Promise<SendSmsResponse>;
}

View File

@ -0,0 +1,34 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TencentSmsInstance = void 0;
var tslib_1 = require("tslib");
var TencentSmsInstance = /** @class */ (function () {
function TencentSmsInstance(secretId, secretKey, region, endpoint) {
this.secretId = secretId;
this.secretKey = secretKey;
this.region = region;
this.endpoint = endpoint;
var clientConfig = {
credential: {
secretId: this.secretId,
secretKey: this.secretKey,
},
region: this.region,
profile: {
httpProfile: {
endpoint: this.endpoint || 'sms.tencentcloudapi.com',
},
},
};
}
TencentSmsInstance.prototype.sendSms = function (params) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
console.log('mp走不到这里');
return [2 /*return*/, {}];
});
});
};
return TencentSmsInstance;
}());
exports.TencentSmsInstance = TencentSmsInstance;

10
lib/service/tencent/Sms.web.d.ts vendored Normal file
View File

@ -0,0 +1,10 @@
import { SendSmsRequest, SendSmsResponse } from 'tencentcloud-sdk-nodejs/tencentcloud/services/sms/v20210111/sms_models';
export declare class TencentSmsInstance {
secretId: string;
secretKey: string;
region: string;
endpoint: string;
client: any;
constructor(secretId: string, secretKey: string, region: string, endpoint: string);
sendSms(params: SendSmsRequest): Promise<SendSmsResponse>;
}

View File

@ -0,0 +1,34 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TencentSmsInstance = void 0;
var tslib_1 = require("tslib");
var TencentSmsInstance = /** @class */ (function () {
function TencentSmsInstance(secretId, secretKey, region, endpoint) {
this.secretId = secretId;
this.secretKey = secretKey;
this.region = region;
this.endpoint = endpoint;
var clientConfig = {
credential: {
secretId: this.secretId,
secretKey: this.secretKey,
},
region: this.region,
profile: {
httpProfile: {
endpoint: this.endpoint || 'sms.tencentcloudapi.com',
},
},
};
}
TencentSmsInstance.prototype.sendSms = function (params) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
console.log('web走不到这里');
return [2 /*return*/, {}];
});
});
};
return TencentSmsInstance;
}());
exports.TencentSmsInstance = TencentSmsInstance;

View File

@ -2,7 +2,7 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.WechatMpInstance = void 0;
var tslib_1 = require("tslib");
require('isomorphic-fetch');
require('../../fetch');
var crypto_1 = tslib_1.__importDefault(require("crypto"));
var buffer_1 = require("buffer");
var WechatMpInstance = /** @class */ (function () {
@ -34,7 +34,7 @@ var WechatMpInstance = /** @class */ (function () {
var response, headers, status, contentType, json, data;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, fetch(url, init)];
case 0: return [4 /*yield*/, global.fetch(url, init)];
case 1:
response = _a.sent();
headers = response.headers, status = response.status;
@ -47,6 +47,9 @@ var WechatMpInstance = /** @class */ (function () {
case 2:
json = _a.sent();
if (typeof json.errcode === 'number' && json.errcode !== 0) {
if (json.errcode === 40001) {
this.refreshAccessToken();
}
throw new Error("\u8C03\u7528\u5FAE\u4FE1\u63A5\u53E3\u8FD4\u56DE\u51FA\u9519\uFF0Ccode\u662F".concat(json.errcode, "\uFF0C\u4FE1\u606F\u662F").concat(json.errmsg));
}
return [2 /*return*/, json];

View File

@ -2,7 +2,7 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.WechatPublicInstance = void 0;
var tslib_1 = require("tslib");
require('isomorphic-fetch');
require('../../fetch');
var crypto_1 = tslib_1.__importDefault(require("crypto"));
var buffer_1 = require("buffer");
var WechatPublicInstance = /** @class */ (function () {
@ -38,7 +38,7 @@ var WechatPublicInstance = /** @class */ (function () {
if (process.env.NODE_ENV === 'development') {
return [2 /*return*/, mockData];
}
return [4 /*yield*/, fetch(url, init)];
return [4 /*yield*/, global.fetch(url, init)];
case 1:
response = _a.sent();
headers = response.headers, status = response.status;
@ -51,6 +51,9 @@ var WechatPublicInstance = /** @class */ (function () {
case 2:
json = _a.sent();
if (typeof json.errcode === 'number' && json.errcode !== 0) {
if (json.errcode === 40001) {
this.refreshAccessToken();
}
throw new Error("\u8C03\u7528\u5FAE\u4FE1\u63A5\u53E3\u8FD4\u56DE\u51FA\u9519\uFF0Ccode\u662F".concat(json.errcode, "\uFF0C\u4FE1\u606F\u662F").concat(json.errmsg));
}
return [2 /*return*/, json];
@ -282,7 +285,7 @@ var WechatPublicInstance = /** @class */ (function () {
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
},
};
switch (type) {
case 'text': {
@ -308,9 +311,9 @@ var WechatPublicInstance = /** @class */ (function () {
title: options.title,
description: options.description,
url: options.url,
picurl: options.picurl
}
]
picurl: options.picurl,
},
],
},
}),
});

View File

@ -2,7 +2,7 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.WechatWebInstance = void 0;
var tslib_1 = require("tslib");
require('isomorphic-fetch');
require('../../fetch');
var crypto_1 = tslib_1.__importDefault(require("crypto"));
var buffer_1 = require("buffer");
var WechatWebInstance = /** @class */ (function () {
@ -38,7 +38,7 @@ var WechatWebInstance = /** @class */ (function () {
if (process.env.NODE_ENV === 'development') {
return [2 /*return*/, mockData];
}
return [4 /*yield*/, fetch(url, init)];
return [4 /*yield*/, global.fetch(url, init)];
case 1:
response = _a.sent();
headers = response.headers, status = response.status;
@ -51,6 +51,9 @@ var WechatWebInstance = /** @class */ (function () {
case 2:
json = _a.sent();
if (typeof json.errcode === 'number' && json.errcode !== 0) {
if (json.errcode === 40001) {
this.refreshAccessToken();
}
throw new Error("\u8C03\u7528\u5FAE\u4FE1\u63A5\u53E3\u8FD4\u56DE\u51FA\u9519\uFF0Ccode\u662F".concat(json.errcode, "\uFF0C\u4FE1\u606F\u662F").concat(json.errmsg));
}
return [2 /*return*/, json];

View File

@ -23,9 +23,7 @@
"dependencies": {
"@alicloud/pop-core": "^1.7.12",
"isomorphic-fetch": "^3.0.0",
"net": "^1.0.2",
"tencentcloud-sdk-nodejs": "^4.0.525",
"tls": "0.0.1",
"ts-md5": "^1.3.1"
}
}

View File

@ -1,6 +1,5 @@
import { TencentSmsInstance } from './service/tencent/Sms';
import { AliSmsInstance } from './service/ali/Sms';
import assert from 'assert';
class SmsSDK {
tencentMap: Record<string, TencentSmsInstance>;
@ -34,7 +33,9 @@ class SmsSDK {
});
return instance;
} else if (origin === 'ali') {
assert(apiVersion, '阿里云短信apiVersion必须传入');
if (!apiVersion) {
throw new Error('阿里云短信apiVersion必须传入');
}
if (this.aliMap[accessKey]) {
return this.aliMap[accessKey];
}

1
src/fetch/index.mp.ts Normal file
View File

@ -0,0 +1 @@
global.fetch = global.fetch;

1
src/fetch/index.ts Normal file
View File

@ -0,0 +1 @@
require('isomorphic-fetch');

1
src/fetch/index.web.ts Normal file
View File

@ -0,0 +1 @@
global.fetch = fetch;

53
src/service/ali/Sms.mp.ts Normal file
View File

@ -0,0 +1,53 @@
type SendSmsRequest = {
PhoneNumbers: string[];
TemplateCode: string;
SignName: string;
TemplateParam?: Record<string, string>;
SmsUpExtendCode?: string;
OutId?: string;
};
type SendSmsResponse = {
Code: 'OK' | string;
Message: string;
BizId: string;
RequestId: string;
};
export class AliSmsInstance {
secretId: string;
secretKey: string;
region: string;
endpoint: string;
client: any;
constructor(
secretId: string,
secretKey: string,
region: string,
endpoint: string
) {
this.secretId = secretId;
this.secretKey = secretKey;
this.region = region;
this.endpoint = endpoint;
const clientConfig = {
credential: {
secretId: this.secretId,
secretKey: this.secretKey,
},
region: this.region,
profile: {
httpProfile: {
endpoint: this.endpoint || 'dysmsapi.aliyuncs.com',
},
},
};
}
async sendSms(params: SendSmsRequest) {
console.log('mp走不到这里');
return {} as SendSmsResponse;
}
}

View File

@ -1,5 +1,4 @@
// import Core from '@alicloud/pop-core/lib/rpc';
//todo 引入阿里云的库 在前端跑的时候会出现fs模块不存在
import Core from '@alicloud/pop-core/lib/rpc';
type SendSmsRequest = {
PhoneNumbers: string[];
@ -23,7 +22,7 @@ export class AliSmsInstance {
regionId: string;
endpoint: string;
apiVersion: string;
// client: Core;
client: Core;
constructor(
accessKeyId: string,
@ -38,12 +37,12 @@ export class AliSmsInstance {
this.endpoint = endpoint;
this.apiVersion = apiVersion;
// this.client = new Core({
// accessKeyId: this.accessKeyId,
// accessKeySecret: this.accessKeySecret,
// endpoint: this.endpoint || 'dysmsapi.aliyuncs.com',
// apiVersion: this.apiVersion,
// });
this.client = new Core({
accessKeyId: this.accessKeyId,
accessKeySecret: this.accessKeySecret,
endpoint: this.endpoint || 'dysmsapi.aliyuncs.com',
apiVersion: this.apiVersion,
});
}
async sendSms(params: SendSmsRequest) {

View File

@ -0,0 +1,53 @@
type SendSmsRequest = {
PhoneNumbers: string[];
TemplateCode: string;
SignName: string;
TemplateParam?: Record<string, string>;
SmsUpExtendCode?: string;
OutId?: string;
};
type SendSmsResponse = {
Code: 'OK' | string;
Message: string;
BizId: string;
RequestId: string;
};
export class AliSmsInstance {
secretId: string;
secretKey: string;
region: string;
endpoint: string;
client: any;
constructor(
secretId: string,
secretKey: string,
region: string,
endpoint: string
) {
this.secretId = secretId;
this.secretKey = secretKey;
this.region = region;
this.endpoint = endpoint;
const clientConfig = {
credential: {
secretId: this.secretId,
secretKey: this.secretKey,
},
region: this.region,
profile: {
httpProfile: {
endpoint: this.endpoint || 'dysmsapi.aliyuncs.com',
},
},
};
}
async sendSms(params: SendSmsRequest) {
console.log('web走不到这里');
return {} as SendSmsResponse;
}
}

View File

@ -1,4 +1,4 @@
require('isomorphic-fetch');
require('../../fetch');
export class AmapInstance {
key: string;
@ -15,7 +15,7 @@ export class AmapInstance {
)},${from[1].toFixed(6)}&destination=${to[0].toFixed(
6
)},${to[1].toFixed(6)}&strategy=10&key=${this.key}`;
const result = await fetch(url);
const result = await global.fetch(url);
const jsonData = await result.json();
if (jsonData.status !== '1') {
throw new Error(JSON.stringify(jsonData));

View File

@ -1,5 +1,4 @@
require('isomorphic-fetch');
require('../../fetch');
import crypto from 'crypto';
import { Md5 } from 'ts-md5';
import { Buffer } from 'buffer';
@ -109,7 +108,7 @@ export class QiniuCloudInstance {
const token = this.getLiveToken(method, path, host);
const url = `https://pili.qiniuapi.com/v2/hubs/${hub}/streams`;
await fetch(url, {
await global.fetch(url, {
method: 'POST',
headers: {
Authorization: token,
@ -202,7 +201,7 @@ export class QiniuCloudInstance {
);
const url = `https://pili.qiniuapi.com${path}`;
await fetch(url, {
await global.fetch(url, {
method: 'POST',
headers: {
Authorization: token,

View File

@ -0,0 +1,42 @@
import {
SendSmsRequest,
SendSmsResponse,
} from 'tencentcloud-sdk-nodejs/tencentcloud/services/sms/v20210111/sms_models';
export class TencentSmsInstance {
secretId: string;
secretKey: string;
region: string;
endpoint: string;
client: any;
constructor(
secretId: string,
secretKey: string,
region: string,
endpoint: string
) {
this.secretId = secretId;
this.secretKey = secretKey;
this.region = region;
this.endpoint = endpoint;
const clientConfig = {
credential: {
secretId: this.secretId,
secretKey: this.secretKey,
},
region: this.region,
profile: {
httpProfile: {
endpoint: this.endpoint || 'sms.tencentcloudapi.com',
},
},
};
}
async sendSms(params: SendSmsRequest) {
console.log('mp走不到这里');
return {} as SendSmsResponse;
}
}

View File

@ -0,0 +1,42 @@
import {
SendSmsRequest,
SendSmsResponse,
} from 'tencentcloud-sdk-nodejs/tencentcloud/services/sms/v20210111/sms_models';
export class TencentSmsInstance {
secretId: string;
secretKey: string;
region: string;
endpoint: string;
client: any;
constructor(
secretId: string,
secretKey: string,
region: string,
endpoint: string
) {
this.secretId = secretId;
this.secretKey = secretKey;
this.region = region;
this.endpoint = endpoint;
const clientConfig = {
credential: {
secretId: this.secretId,
secretKey: this.secretKey,
},
region: this.region,
profile: {
httpProfile: {
endpoint: this.endpoint || 'sms.tencentcloudapi.com',
},
},
};
}
async sendSms(params: SendSmsRequest) {
console.log('web走不到这里');
return {} as SendSmsResponse;
}
}

View File

@ -1,5 +1,4 @@
require('isomorphic-fetch');
require('../../fetch');
import crypto from 'crypto';
import { Buffer } from 'buffer';
@ -28,7 +27,7 @@ export class WechatMpInstance {
}
private async access(url: string, init?: RequestInit) {
const response = await fetch(url, init);
const response = await global.fetch(url, init);
const { headers, status } = response;
if (![200, 201].includes(status)) {
@ -39,6 +38,9 @@ export class WechatMpInstance {
if (contentType.includes('application/json')) {
const json = await response.json();
if (typeof json.errcode === 'number' && json.errcode !== 0) {
if (json.errcode === 40001) {
this.refreshAccessToken();
}
throw new Error(
`调用微信接口返回出错code是${json.errcode},信息是${json.errmsg}`
);
@ -154,5 +156,4 @@ export class WechatMpInstance {
);
return (await result.arrayBuffer()) as ArrayBuffer;
}
}

View File

@ -1,22 +1,21 @@
require('isomorphic-fetch');
require('../../fetch');
import crypto from 'crypto';
import { Buffer } from 'buffer';
// 目前先支持text和news, 其他type文档https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Service_Center_messages.html
// type ServeMessageType = 'text' | 'news' | 'mpnews' | 'mpnewsarticle' | 'image' | 'voice' | 'video' | 'music' | 'msgmenu';/
type TextServeMessageOption = {
openId: string,
type: 'text',
content: string,
}
openId: string;
type: 'text';
content: string;
};
type NewsServeMessageOption = {
openId: string,
type: 'news',
title: string,
description?: string,
url: string,
picurl?: string,
}
openId: string;
type: 'news';
title: string;
description?: string;
url: string;
picurl?: string;
};
type ServeMessageOption = TextServeMessageOption | NewsServeMessageOption;
@ -48,7 +47,7 @@ export class WechatPublicInstance {
if (process.env.NODE_ENV === 'development') {
return mockData;
}
const response = await fetch(url, init);
const response = await global.fetch(url, init);
const { headers, status } = response;
if (![200, 201].includes(status)) {
@ -59,6 +58,9 @@ export class WechatPublicInstance {
if (contentType.includes('application/json')) {
const json = await response.json();
if (typeof json.errcode === 'number' && json.errcode !== 0) {
if (json.errcode === 40001) {
this.refreshAccessToken();
}
throw new Error(
`调用微信接口返回出错code是${json.errcode},信息是${json.errmsg}`
);
@ -177,11 +179,11 @@ export class WechatPublicInstance {
}
const scene = sceneId
? {
scene_id: sceneId,
}
scene_id: sceneId,
}
: {
scene_str: sceneStr,
};
scene_str: sceneStr,
};
let actionName = sceneId ? 'QR_SCENE' : 'QR_STR_SCENE';
let myInit = {
method: 'POST',
@ -277,12 +279,11 @@ export class WechatPublicInstance {
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
},
};
switch (type) {
case 'text': {
Object.assign(
myInit, {
Object.assign(myInit, {
body: JSON.stringify({
touser: openId,
msgtype: 'text',
@ -290,13 +291,11 @@ export class WechatPublicInstance {
content: options.content,
},
}),
}
);
});
break;
}
case 'news': {
Object.assign(
myInit, {
Object.assign(myInit, {
body: JSON.stringify({
touser: openId,
msgtype: 'news',
@ -306,17 +305,16 @@ export class WechatPublicInstance {
title: options.title,
description: options.description,
url: options.url,
picurl: options.picurl
}
]
picurl: options.picurl,
},
],
},
}),
}
);
});
break;
}
default: {
throw new Error('当前消息类型暂不支持')
throw new Error('当前消息类型暂不支持');
}
}
const token = await this.getAccessToken();

View File

@ -1,5 +1,4 @@
require('isomorphic-fetch');
require('../../fetch');
import crypto from 'crypto';
import { Buffer } from 'buffer';
@ -31,7 +30,7 @@ export class WechatWebInstance {
if (process.env.NODE_ENV === 'development') {
return mockData;
}
const response = await fetch(url, init);
const response = await global.fetch(url, init);
const { headers, status } = response;
if (![200, 201].includes(status)) {
@ -42,6 +41,9 @@ export class WechatWebInstance {
if (contentType.includes('application/json')) {
const json = await response.json();
if (typeof json.errcode === 'number' && json.errcode !== 0) {
if (json.errcode === 40001) {
this.refreshAccessToken();
}
throw new Error(
`调用微信接口返回出错code是${json.errcode},信息是${json.errmsg}`
);