mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
完善sdk-nodejs-axios分支
This commit is contained in:
16
sop-sdk/sdk-nodejs-axios/.gitignore
vendored
Normal file
16
sop-sdk/sdk-nodejs-axios/.gitignore
vendored
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
.DS_Store
|
||||||
|
node_modules/
|
||||||
|
dist/
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
package-lock.json
|
||||||
|
tests/**/coverage/
|
||||||
|
|
||||||
|
# Editor directories and files
|
||||||
|
.idea
|
||||||
|
.vscode
|
||||||
|
*.suo
|
||||||
|
*.ntvs*
|
||||||
|
*.njsproj
|
||||||
|
*.sln
|
@@ -1,11 +1,12 @@
|
|||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const formData = require('form-data');
|
|
||||||
const moment = require('moment');
|
const moment = require('moment');
|
||||||
const qs = require('qs');
|
const qs = require('qs');
|
||||||
|
|
||||||
const {RequestType} = require('./RequestType');
|
const RequestType = require('./RequestType');
|
||||||
const {SignUtil} = require('./SignUtil');
|
const SignUtil = require('./SignUtil');
|
||||||
const {BaseRequest} = require('../request/BaseRequest');
|
const BaseRequest = require('./BaseRequest');
|
||||||
|
|
||||||
|
const IS_RUN_IN_BROWSER = this === window;
|
||||||
|
|
||||||
const HEADERS = {'Accept-Encoding': 'identity'};
|
const HEADERS = {'Accept-Encoding': 'identity'};
|
||||||
|
|
||||||
@@ -56,7 +57,7 @@ const executeRequest = async (instance = {}, request, token, callback, {headers}
|
|||||||
data: undefined
|
data: undefined
|
||||||
};
|
};
|
||||||
headers = getHeaders(headers);
|
headers = getHeaders(headers);
|
||||||
const requestType = request.getRequestType();
|
const requestType = request.getRealRequestType();
|
||||||
switch (requestType) {
|
switch (requestType) {
|
||||||
case RequestType.GET: {
|
case RequestType.GET: {
|
||||||
options.method = 'GET';
|
options.method = 'GET';
|
||||||
@@ -75,18 +76,31 @@ const executeRequest = async (instance = {}, request, token, callback, {headers}
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case RequestType.POST_FILE: {
|
case RequestType.POST_FILE: {
|
||||||
const formData = new formData();
|
|
||||||
(request.files || []).forEach(({name, path}) => {
|
|
||||||
formData.append(name, path, {
|
|
||||||
contentType: 'application/octet-stream'
|
|
||||||
});
|
|
||||||
});
|
|
||||||
Object.keys(params).forEach(key => {
|
Object.keys(params).forEach(key => {
|
||||||
const value = params[key];
|
const value = params[key];
|
||||||
if (!(typeof key === 'undefined' || typeof value === 'undefined')) {
|
if (!(typeof key === 'undefined' || typeof value === 'undefined')) {
|
||||||
formData.append(key, params[key]);
|
formData.append(key, params[key]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
let formData;
|
||||||
|
if (IS_RUN_IN_BROWSER) {
|
||||||
|
formData = new window.FormData()
|
||||||
|
(request.files || []).forEach(({name, path}) => {
|
||||||
|
formData.append(name, path, {
|
||||||
|
contentType: 'application/octet-stream'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const fs = require('fs');
|
||||||
|
const fd = require('form-data');
|
||||||
|
formData = new fd();
|
||||||
|
(request.files || []).forEach(({name, path}) => {
|
||||||
|
formData.append(name, fs.createReadStream(path), {
|
||||||
|
contentType: 'application/octet-stream'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
headers = Object.assign(headers, formData.getHeaders());
|
||||||
|
}
|
||||||
options.data = formData;
|
options.data = formData;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -112,9 +126,24 @@ module.exports = class OpenClient {
|
|||||||
* @param url 请求url
|
* @param url 请求url
|
||||||
*/
|
*/
|
||||||
constructor(appId, privateKey, url) {
|
constructor(appId, privateKey, url) {
|
||||||
this.appId = appId;
|
this.appId = appId || '';
|
||||||
this.privateKey = privateKey;
|
this.privateKey = privateKey || '';
|
||||||
this.url = url;
|
this.url = url || '';
|
||||||
|
}
|
||||||
|
|
||||||
|
setAppId(appId) {
|
||||||
|
this.appId = appId || '';
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
setPrivateKey(privateKey) {
|
||||||
|
this.privateKey = privateKey || '';
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
setUrl(url) {
|
||||||
|
this.url = url || '';
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
exports.RequestType = {
|
module.exports = {
|
||||||
GET: 'GET',
|
GET: 'GET',
|
||||||
POST_FORM: 'POST_FORM',
|
POST_FORM: 'POST_FORM',
|
||||||
POST_JSON: 'POST_JSON',
|
POST_JSON: 'POST_JSON',
|
||||||
|
@@ -11,7 +11,7 @@ const PEM_END = '\n-----END PRIVATE KEY-----';
|
|||||||
/**
|
/**
|
||||||
* rsa签名参考:https://www.jianshu.com/p/145eab95322c
|
* rsa签名参考:https://www.jianshu.com/p/145eab95322c
|
||||||
*/
|
*/
|
||||||
exports.SignUtil = {
|
module.exports = {
|
||||||
/**
|
/**
|
||||||
* 创建签名
|
* 创建签名
|
||||||
* @param params 请求参数
|
* @param params 请求参数
|
||||||
|
44
sop-sdk/sdk-nodejs-axios/main.js
Normal file
44
sop-sdk/sdk-nodejs-axios/main.js
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
const OpenClient = require('./common/OpenClient');
|
||||||
|
|
||||||
|
const StoryGetRequest = require('./request/StoryGetRequest');
|
||||||
|
|
||||||
|
// 应用ID
|
||||||
|
const appId = '2019032617262200001';
|
||||||
|
// 应用私钥,2048位,PKCS8
|
||||||
|
const privateKey = 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fP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vfVQgiNGQKBgGxU6P2aDAt6VNwWosHSe+d2WWXt8IZBhO9d6dn0f7ORvcjmCqNKTNGgrkewMZEuVcliueJquR47IROdY8qmwqcBAN7Vg2K7r7CPlTKAWTRYMJxCT1Hi5gwJb+CZF3+IeYqsJk2NF2s0w5WJTE70k1BSvQsfIzAIDz2yE1oPHvwVAoGAA6e+xQkVH4fMEph55RJIZ5goI4Y76BSvt2N5OKZKd4HtaV+eIhM3SDsVYRLIm9ZquJHMiZQGyUGnsvrKL6AAVNK7eQZCRDk9KQz+0GKOGqku0nOZjUbAu6A2/vtXAaAuFSFx1rUQVVjFulLexkXR3KcztL1Qu2k5pB6Si0K/uwQ=';
|
||||||
|
// 接口url
|
||||||
|
const url = 'http://localhost:8081';
|
||||||
|
|
||||||
|
// 创建客户端
|
||||||
|
const openClient = new OpenClient().setUrl(url).setAppId(appId).setPrivateKey(privateKey);
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
// 创建请求
|
||||||
|
const request = new StoryGetRequest();
|
||||||
|
|
||||||
|
// 设置业务参数
|
||||||
|
const bizModel = {
|
||||||
|
id: 111,
|
||||||
|
name: 'jim'
|
||||||
|
};
|
||||||
|
request.setBizModel(bizModel);
|
||||||
|
|
||||||
|
// 添加上传文件
|
||||||
|
// 批量添加
|
||||||
|
const files = [
|
||||||
|
// name: 表单名称,path:文件全路径
|
||||||
|
{name: 'file1', path: `${__dirname}/aa.txt`},
|
||||||
|
{name: 'file2', path: `${__dirname}/bb.txt`}
|
||||||
|
];
|
||||||
|
request.setFiles(files);
|
||||||
|
// 单个添加
|
||||||
|
request.addFile('file3', `${__dirname}/package.json`);
|
||||||
|
|
||||||
|
const data = await openClient.executeSync(request);
|
||||||
|
// 成功
|
||||||
|
if (!data.sub_code) {
|
||||||
|
console.log('成功', data);
|
||||||
|
} else {
|
||||||
|
console.error('失败', data);
|
||||||
|
}
|
||||||
|
})();
|
10
sop-sdk/sdk-nodejs-axios/readme.md
Normal file
10
sop-sdk/sdk-nodejs-axios/readme.md
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
- 执行`npm install --registry=https://registry.npm.taobao.org`
|
||||||
|
|
||||||
|
- 执行`node main.js`进行测试
|
||||||
|
|
||||||
|
## 封装步骤
|
||||||
|
|
||||||
|
1. 新建request类,继承BaseRequest,参考`StoryGetRequest.js`
|
||||||
|
|
||||||
|
2. 调用接口,参考`main.js`
|
||||||
|
|
19
sop-sdk/sdk-nodejs-axios/request/StoryGetRequest.js
Normal file
19
sop-sdk/sdk-nodejs-axios/request/StoryGetRequest.js
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
const BaseRequest = require('../common/BaseRequest');
|
||||||
|
const RequestType = require('../common/RequestType');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个请求类,继承BaseRequest,重写三个函数
|
||||||
|
*/
|
||||||
|
module.exports = class StoryGetRequest extends BaseRequest {
|
||||||
|
getMethod() {
|
||||||
|
return 'story.get';
|
||||||
|
}
|
||||||
|
|
||||||
|
getVersion() {
|
||||||
|
return '1.0';
|
||||||
|
}
|
||||||
|
|
||||||
|
getRequestType() {
|
||||||
|
return RequestType.GET;
|
||||||
|
}
|
||||||
|
};
|
Reference in New Issue
Block a user