From 3ceb8cdc93475f1b3ee8a81039ab8431b1222d8a Mon Sep 17 00:00:00 2001 From: Changeden Date: Wed, 14 Apr 2021 14:13:20 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E4=B8=B4=E6=97=B6=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sop-sdk/sdk-nodejs-axios/.gitignore | 16 -- sop-sdk/sdk-nodejs-axios/common/OpenClient.js | 213 ------------------ .../sdk-nodejs-axios/common/RequestType.js | 6 - sop-sdk/sdk-nodejs-axios/common/SignUtil.js | 88 -------- sop-sdk/sdk-nodejs-axios/main.js | 44 ---- sop-sdk/sdk-nodejs-axios/package.json | 21 -- sop-sdk/sdk-nodejs-axios/readme.md | 10 - .../request/StoryGetRequest.js | 19 -- .../common/BaseRequest.js | 0 9 files changed, 417 deletions(-) delete mode 100644 sop-sdk/sdk-nodejs-axios/.gitignore delete mode 100644 sop-sdk/sdk-nodejs-axios/common/OpenClient.js delete mode 100644 sop-sdk/sdk-nodejs-axios/common/RequestType.js delete mode 100644 sop-sdk/sdk-nodejs-axios/common/SignUtil.js delete mode 100644 sop-sdk/sdk-nodejs-axios/main.js delete mode 100644 sop-sdk/sdk-nodejs-axios/package.json delete mode 100644 sop-sdk/sdk-nodejs-axios/readme.md delete mode 100644 sop-sdk/sdk-nodejs-axios/request/StoryGetRequest.js rename sop-sdk/{sdk-nodejs-axios => sdk-nodejs}/common/BaseRequest.js (100%) diff --git a/sop-sdk/sdk-nodejs-axios/.gitignore b/sop-sdk/sdk-nodejs-axios/.gitignore deleted file mode 100644 index 9ad28d23..00000000 --- a/sop-sdk/sdk-nodejs-axios/.gitignore +++ /dev/null @@ -1,16 +0,0 @@ -.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 diff --git a/sop-sdk/sdk-nodejs-axios/common/OpenClient.js b/sop-sdk/sdk-nodejs-axios/common/OpenClient.js deleted file mode 100644 index fe0d1820..00000000 --- a/sop-sdk/sdk-nodejs-axios/common/OpenClient.js +++ /dev/null @@ -1,213 +0,0 @@ -const axios = require('axios'); -const moment = require('moment'); -const qs = require('qs'); - -const RequestType = require('./RequestType'); -const SignUtil = require('./SignUtil'); -const BaseRequest = require('./BaseRequest'); - -const IS_RUN_IN_BROWSER = typeof window !== 'undefined' && this === window; - -const HEADERS = {'Accept-Encoding': 'identity'}; - -const getHeaders = (headers = {}) => { - return Object.assign({}, headers, HEADERS); -}; - -const parseResponse = (error, response, request) => { - if (!error && response.status === 200) { - return request.parseResponse(response.data); - } else { - return { // 重新封装请求异常回调,以防中断 - msg: '请求异常', - code: '502', - sub_msg: `${error}`, - sub_code: 'isv.invalid-server' - }; - } -}; - -const buildParams = (instance, request, token) => { - const {appId, privateKey} = instance; - const allParams = { - 'app_id': appId, - 'method': request.getMethod(), - 'charset': 'UTF-8', - 'sign_type': 'RSA2', - 'timestamp': moment().format('YYYY-MM-DD HH:mm:ss'), - 'version': request.getVersion(), - 'biz_content': JSON.stringify(request.bizModel) - }; - - if (token) { - allParams['app_auth_token'] = token; - } - // 创建签名 - allParams.sign = SignUtil.createSign(allParams, privateKey, 'RSA2'); - return allParams; -}; - -const executeRequest = async (instance = {}, request, token, callback, customOptions = {}) => { - const params = buildParams(instance, request, token); - const {url} = instance; - let {headers} = customOptions; - const config = { - url, - method: 'POST', - params: undefined, - data: undefined - }; - headers = getHeaders(headers); - const requestType = request.getRealRequestType(); - switch (requestType) { - case RequestType.GET: { - config.method = 'GET'; - config.params = params; - } - break; - case RequestType.POST_FORM: { - headers = Object.assign(headers, { - 'Content-Type': 'application/x-www-form-urlencoded' - }); - config.data = qs.stringify(params); - } - break; - case RequestType.POST_JSON: { - config.data = params; - } - break; - case RequestType.POST_FILE: { - 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()); - } - Object.keys(params).forEach(key => { - const value = params[key]; - if (!(typeof key === 'undefined' || typeof value === 'undefined')) { - formData.append(key, params[key]); - } - }); - config.data = formData; - } - break; - default: { - callback(parseResponse(new Error('request.getRequestType()类型不正确'), undefined, request)); - return; - } - } - try { - config['headers'] = headers; - const response = await axios.request(config); - callback(parseResponse(undefined, response, request)); - } catch (error) { - callback(parseResponse(error, undefined, request)); - } -}; - -module.exports = class OpenClient { - /** - * 初始化客户端 - * @param appId 应用ID - * @param privateKey 应用私钥,2048位,PKCS8 - * @param url 请求url - */ - constructor(appId, privateKey, url) { - this.appId = appId || ''; - this.privateKey = privateKey || ''; - this.url = url || ''; - } - - setAppId(appId) { - this.appId = appId || ''; - return this; - } - - setPrivateKey(privateKey) { - this.privateKey = privateKey || ''; - return this; - } - - setUrl(url) { - this.url = url || ''; - return this; - } - - /** - * 发送请求 - * @param request 请求类 - * @param callback 回调函数,参数json(undefined则使用executeSync) - * @param options 自定义参数,如headers - */ - execute(request, callback, options) { - if (typeof callback == 'function') { - return this.executeToken(request, null, callback, options); - } else { - return this.executeSync(request, options); - } - } - - /** - * 发送同步请求 - * @param request 请求类 - * @param options 自定义参数,如headers - * */ - executeSync(request, options) { - return new Promise((resolve) => { - const _ = this.execute(request, res => { - resolve(res); - }, options); - }); - } - - /** - * 发送请求 - * @param request 请求类 - * @param token token - * @param callback 回调函数,参数json(undefined则使用executeTokenSync) - * @param options 自定义参数,如headers - */ - async executeToken(request, token, callback, options) { - if (!(request instanceof BaseRequest)) { - throw 'request类未继承BaseRequest'; - } - if (typeof callback == 'function') { - const files = request.files; - if (files && files.length > 0) { - request.setForceRequestType(RequestType.POST_FILE); - } - return await executeRequest(this, request, token, callback, options); - } else { - return this.executeTokenSync(request, token); - } - } - - /** - * 发送同步请求 - * @param request 请求类 - * @param token token - * @param options 自定义参数,如headers - */ - executeTokenSync(request, token, options) { - return new Promise((resolve) => { - const _ = this.executeToken(request, token, res => { - resolve(res); - }, options); - }); - } - -}; diff --git a/sop-sdk/sdk-nodejs-axios/common/RequestType.js b/sop-sdk/sdk-nodejs-axios/common/RequestType.js deleted file mode 100644 index f745548d..00000000 --- a/sop-sdk/sdk-nodejs-axios/common/RequestType.js +++ /dev/null @@ -1,6 +0,0 @@ -module.exports = { - GET: 'GET', - POST_FORM: 'POST_FORM', - POST_JSON: 'POST_JSON', - POST_FILE: 'POST_FILE' -}; diff --git a/sop-sdk/sdk-nodejs-axios/common/SignUtil.js b/sop-sdk/sdk-nodejs-axios/common/SignUtil.js deleted file mode 100644 index 98e2ceb6..00000000 --- a/sop-sdk/sdk-nodejs-axios/common/SignUtil.js +++ /dev/null @@ -1,88 +0,0 @@ -const {KJUR, hextob64} = require('jsrsasign'); - -const HashMap = { - SHA256withRSA: 'SHA256withRSA', - SHA1withRSA: 'SHA1withRSA' -}; - -const PEM_BEGIN = '-----BEGIN PRIVATE KEY-----\n'; -const PEM_END = '\n-----END PRIVATE KEY-----'; - -/** - * rsa签名参考:https://www.jianshu.com/p/145eab95322c - */ -module.exports = { - /** - * 创建签名 - * @param params 请求参数 - * @param privateKey 私钥,PKCS8 - * @param signType 签名类型,RSA,RSA2 - * @returns 返回签名内容 - */ - createSign(params, privateKey, signType) { - const content = this.getSignContent(params); - return this.sign(content, privateKey, signType); - }, - sign: function (content, privateKey, signType) { - if (signType.toUpperCase() === 'RSA') { - return this.rsaSign(content, privateKey, HashMap.SHA1withRSA); - } else if (signType.toUpperCase() === 'RSA2') { - return this.rsaSign(content, privateKey, HashMap.SHA256withRSA); - } else { - throw 'signType错误'; - } - }, - /** - * rsa签名 - * @param content 签名内容 - * @param privateKey 私钥 - * @param hash hash算法,SHA256withRSA,SHA1withRSA - * @returns 返回签名字符串,base64 - */ - rsaSign: function (content, privateKey, hash) { - privateKey = this._formatKey(privateKey); - // 创建 Signature 对象 - const signature = new KJUR.crypto.Signature({ - alg: hash, - //!这里指定 私钥 pem! - prvkeypem: privateKey - }); - signature.updateString(content); - const signData = signature.sign(); - // 将内容转成base64 - return hextob64(signData); - }, - _formatKey: function (key) { - if (!key.startsWith(PEM_BEGIN)) { - key = PEM_BEGIN + key; - } - if (!key.endsWith(PEM_END)) { - key = key + PEM_END; - } - return key; - }, - /** - * 获取签名内容 - * @param params 请求参数 - * @returns {string} - */ - getSignContent: function (params) { - const paramNames = []; - for (const key in params) { - paramNames.push(key); - } - - paramNames.sort(); - - const paramNameValue = []; - - for (let i = 0, len = paramNames.length; i < len; i++) { - const paramName = paramNames[i]; - const val = params[paramName]; - if (paramName && val) { - paramNameValue.push(`${paramName}=${val}`); - } - } - return paramNameValue.join('&'); - } -}; diff --git a/sop-sdk/sdk-nodejs-axios/main.js b/sop-sdk/sdk-nodejs-axios/main.js deleted file mode 100644 index 766285c4..00000000 --- a/sop-sdk/sdk-nodejs-axios/main.js +++ /dev/null @@ -1,44 +0,0 @@ -const OpenClient = require('./common/OpenClient'); - -const StoryGetRequest = require('./request/StoryGetRequest'); - -// 应用ID -const appId = '2019032617262200001'; -// 应用私钥,2048位,PKCS8 -const privateKey = 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZkxH/tb4xfLkH3bA8ZAQnQsoXA0SguykMRZntF0TndUfvDrLqwhlR8r5iRdZLB6F8o8qXH6UPDfNEnf/K8wX5T4EB1b8x8QJ7Ua4GcIUqeUxGHdQpzNbJdaQvoi06lg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+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}/main.js`}, - // {name: 'file2', path: `${__dirname}/readme.md`} - // ]; - // 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); - } -})(); diff --git a/sop-sdk/sdk-nodejs-axios/package.json b/sop-sdk/sdk-nodejs-axios/package.json deleted file mode 100644 index 88232a74..00000000 --- a/sop-sdk/sdk-nodejs-axios/package.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": "sdk-nodejs", - "version": "1.0.0", - "description": "", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "keywords": [], - "author": "", - "license": "ISC", - "dependencies": { - "axios": "^0.21.1", - "form-data": "^4.0.0", - "isarray": "^2.0.5", - "isobject": "^4.0.0", - "jsrsasign": "^8.0.19", - "moment": "^2.27.0", - "qs": "^6.10.1" - } -} diff --git a/sop-sdk/sdk-nodejs-axios/readme.md b/sop-sdk/sdk-nodejs-axios/readme.md deleted file mode 100644 index 79b852e5..00000000 --- a/sop-sdk/sdk-nodejs-axios/readme.md +++ /dev/null @@ -1,10 +0,0 @@ -- 执行`npm install --registry=https://registry.npm.taobao.org` - -- 执行`node main.js`进行测试 - -## 封装步骤 - -1. 新建request类,继承BaseRequest,参考`StoryGetRequest.js` - -2. 调用接口,参考`main.js` - diff --git a/sop-sdk/sdk-nodejs-axios/request/StoryGetRequest.js b/sop-sdk/sdk-nodejs-axios/request/StoryGetRequest.js deleted file mode 100644 index 243084a7..00000000 --- a/sop-sdk/sdk-nodejs-axios/request/StoryGetRequest.js +++ /dev/null @@ -1,19 +0,0 @@ -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; - } -}; diff --git a/sop-sdk/sdk-nodejs-axios/common/BaseRequest.js b/sop-sdk/sdk-nodejs/common/BaseRequest.js similarity index 100% rename from sop-sdk/sdk-nodejs-axios/common/BaseRequest.js rename to sop-sdk/sdk-nodejs/common/BaseRequest.js