20261008
This commit is contained in:
@@ -0,0 +1,171 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const CryptoJS = require("crypto-js");
|
||||
const request = require("request");
|
||||
var AuthorizationType;
|
||||
(function (AuthorizationType) {
|
||||
AuthorizationType[AuthorizationType["OPEN-FORM-PARAM"] = 0] = "OPEN-FORM-PARAM";
|
||||
AuthorizationType[AuthorizationType["OPEN-BODY-SIG"] = 1] = "OPEN-BODY-SIG";
|
||||
AuthorizationType[AuthorizationType["OPEN-ACCESS-TOKEN"] = 2] = "OPEN-ACCESS-TOKEN";
|
||||
})(AuthorizationType || (AuthorizationType = {}));
|
||||
/**
|
||||
* Open SDK for Node.JS
|
||||
*/
|
||||
class OpenSdk {
|
||||
/**
|
||||
* @class
|
||||
* @param {OpenSdkConfig} config 初始化 SDK 配置
|
||||
*/
|
||||
constructor(config) {
|
||||
if (!config.appId) {
|
||||
throw Error('config.appId is required');
|
||||
}
|
||||
if (!config.appKey) {
|
||||
throw Error('config.appKey is required');
|
||||
}
|
||||
this.config = config;
|
||||
}
|
||||
/**
|
||||
* 生成请求字符串
|
||||
* @param {QueryParams} params 请求参数
|
||||
* @return {string} 请求字符串
|
||||
*/
|
||||
generateSignature(params) {
|
||||
if (!AuthorizationType[params.authorization]) {
|
||||
params.authorization = AuthorizationType['OPEN-BODY-SIG'];
|
||||
}
|
||||
let content = JSON.stringify(params.content);
|
||||
if (params.authorization === AuthorizationType['OPEN-FORM-PARAM']) {
|
||||
let contentHash = CryptoJS.SHA256(content).toString();
|
||||
let signStr = this.config.appId + params.timestamp + params.nonce + contentHash;
|
||||
let signature = CryptoJS.HmacSHA256(signStr, this.config.appKey);
|
||||
let signatureStr = CryptoJS.enc.Base64.stringify(signature);
|
||||
let signatureEncode = encodeURIComponent(signatureStr);
|
||||
let str = `?authorization=${params.authorization}&appId=${this.config.appId}×tamp=${params.timestamp}&nonce=${params.nonce}&content=${content}&signature=${signatureEncode}`;
|
||||
return str;
|
||||
}
|
||||
if (params.authorization === AuthorizationType['OPEN-BODY-SIG']) {
|
||||
let contentHash = CryptoJS.SHA256(content).toString();
|
||||
let signStr = this.config.appId + params.timestamp + params.nonce + contentHash;
|
||||
let signature = CryptoJS.HmacSHA256(signStr, this.config.appKey);
|
||||
let signatureStr = CryptoJS.enc.Base64.stringify(signature);
|
||||
return signatureStr;
|
||||
}
|
||||
if (params.authorization === AuthorizationType['OPEN-ACCESS-TOKEN']) {
|
||||
let signStr = this.config.appId + params.timestamp + params.nonce + this.config.appKey;
|
||||
let signatureStr = CryptoJS.SHA256(signStr).toString();
|
||||
return signatureStr;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 发送请求
|
||||
* @param {QueryParams} params 请求参数
|
||||
* @return {Promise<OpenSdkCommonResult | string>} 请求执行结果
|
||||
*/
|
||||
exec(params) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
if (!params.queryUrl) {
|
||||
throw Error('params.queryUrl is required');
|
||||
}
|
||||
if (!params.nonce) {
|
||||
throw Error('params.nonce is required');
|
||||
}
|
||||
if (!params.timestamp) {
|
||||
throw Error('params.timestamp is required');
|
||||
}
|
||||
const str = this.generateSignature(params);
|
||||
const config = this.config;
|
||||
if (params.authorization === AuthorizationType['OPEN-FORM-PARAM']) {
|
||||
const Url = params.queryUrl + encodeURIComponent(str);
|
||||
return new Promise((resolve, reject) => {
|
||||
request(Url, (error, _response, body) => {
|
||||
if (!error) {
|
||||
if (typeof (body) === 'object') {
|
||||
resolve(JSON.parse(body));
|
||||
}
|
||||
else {
|
||||
resolve(body);
|
||||
}
|
||||
}
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
if (params.authorization === AuthorizationType['OPEN-BODY-SIG']) {
|
||||
const Url = params.queryUrl;
|
||||
return new Promise((resolve, reject) => {
|
||||
request({
|
||||
url: Url,
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Authorization": `OPEN-BODY-SIG AppId="${config.appId}", Timestamp="${params.timestamp}", Nonce="${params.nonce}", Signature="${str}"`,
|
||||
},
|
||||
body: JSON.stringify(params.content),
|
||||
}, (error, _response, body) => {
|
||||
if (!error) {
|
||||
if (typeof (body) === 'object') {
|
||||
resolve(JSON.parse(body));
|
||||
}
|
||||
else {
|
||||
resolve(body);
|
||||
}
|
||||
}
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
if (params.authorization === AuthorizationType['OPEN-ACCESS-TOKEN']) {
|
||||
const Url = params.queryUrl;
|
||||
const token = yield new Promise((resolve, reject) => {
|
||||
request({
|
||||
url: 'https://test-api-open.chinaums.com/v1/token/access',
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
appId: config.appId,
|
||||
timestamp: params.timestamp,
|
||||
nonce: params.nonce,
|
||||
signMethod: 'SHA256',
|
||||
Signature: str
|
||||
})
|
||||
}, (error, _response, body) => {
|
||||
if (!error) {
|
||||
resolve(JSON.parse(body).accessToken);
|
||||
}
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
request({
|
||||
url: Url,
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Authorization": `OPEN-ACCESS-TOKEN AccessToken="${token}"`,
|
||||
},
|
||||
body: JSON.stringify(params.content),
|
||||
}, (error, _response, body) => {
|
||||
if (!error) {
|
||||
if (typeof (body) === 'object') {
|
||||
resolve(JSON.parse(body));
|
||||
}
|
||||
else {
|
||||
resolve(body);
|
||||
}
|
||||
}
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.default = OpenSdk;
|
||||
//# sourceMappingURL=base.js.map
|
||||
Reference in New Issue
Block a user