From e478abcd56e0852d8fd03dbbd287661e18b1a033 Mon Sep 17 00:00:00 2001 From: Changeden Date: Wed, 30 Jun 2021 11:01:14 +0800 Subject: [PATCH] =?UTF-8?q?v0.2.1=EF=BC=8C=E4=BF=AE=E5=A4=8DSDK=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E6=9C=8D=E5=8A=A1=E5=99=A8=E5=9C=B0=E5=9D=80=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E9=97=AE=E9=A2=98=EF=BC=8C=E5=85=BC=E5=AE=B9=E6=96=B0?= =?UTF-8?q?=E6=97=A7=E7=89=88=E8=AF=B7=E6=B1=82=E4=B8=8E=E9=AA=8C=E7=AD=BE?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 37 +++++++++++++++++++----------- package.json | 2 +- src/client.js | 59 +++++++++++++++++++++--------------------------- src/constants.js | 3 +++ src/request.js | 51 ++++++++++++++++++++++------------------- src/sign.js | 19 +++++++++------- 6 files changed, 93 insertions(+), 78 deletions(-) create mode 100644 src/constants.js diff --git a/README.md b/README.md index 90a2af6..1f89cda 100644 --- a/README.md +++ b/README.md @@ -31,38 +31,45 @@ const {create, sign, request} = require('jd-union'); ```js const {create} = require('jd-union'); -const api = create({ +const api = create( + { appKey: '<联盟分配给应用的appKey>如:eefc33bDRea044cb8ctre5hycf0ac1934', appSecret: '<联盟分配给应用的appSecret>如:6d34r0d0kild46460654b42f5e350982', serverUrl: '默认为 https://api.jd.com/routerjson' -}); + } +); // 调用Api // api.request(API接口名称, 业务参数); 返回promise -const res = await api.request('jd.union.open.goods.jingfen.query', +const res = await api.request( + 'jd.union.open.goods.jingfen.query', { goodsReq: 110 - }); + } +); // 验签 -const signature = api.sign({ +const signature = api.sign( + { method: 'jd.union.open.goods.query', - param_json: {'goodsReqDTO': {'keyword': '男装', 'pageSize': 10, 'pageIndex': 1}}, + param_json: {'goodsReqDTO': {'keyword': '男装', 'pageSize': 10, 'pageIndex': 1}}, // 0.2.x版本后,key为360buy_param_json或param_json皆可 v: '1.0', access_token: '', timestamp: '2018-10-18 11:13:12', sign_method: 'md5', format: 'json' - }); + } +); -console.log(signature, signature === 'A5B6ED17EE5B5B82976AF620BABF425E'); +console.log(signature); ``` 单独验签 ```js const {sign} = require('jd-union'); -const signature = sign({ +const signature = sign( + { method: 'jd.union.open.goods.query', param_json: {"goodsReqDTO":{"keyword":"男装","pageSize":10,"pageIndex":1}}, // 0.2.x版本后,key为360buy_param_json或param_json皆可 v: '1.0', @@ -72,9 +79,11 @@ const signature = sign({ format: 'json' }, 'eefc33bDRea044cb8ctre5hycf0ac1934', - '6d34r0d0kild46460654b42f5e350982'); + '6d34r0d0kild46460654b42f5e350982', + 'https://api.jd.com/routerjson' +); -console.log(signature === 'A5B6ED17EE5B5B82976AF620BABF425E'); +console.log(signature); ``` 单独调用Api @@ -90,7 +99,8 @@ const res = await request('https://router.jd.com/api', '<联盟分配给应用的appKey>如:eefc33bDRea044cb8ctre5hycf0ac1934', '<联盟分配给应用的appSecret>如:6d34r0d0kild46460654b42f5e350982'); */ -const res = await request(url = 'https://router.jd.com/api', +const res = await request( + 'https://api.jd.com/routerjson', 'jd.union.open.goods.jingfen.query', { goodsReq: 110 @@ -98,5 +108,6 @@ const res = await request(url = 'https://router.jd.com/api', '1.0', '', 'eefc33bDRea044cb8ctre5hycf0ac1934', - '6d34r0d0kild46460654b42f5e350982'); + '6d34r0d0kild46460654b42f5e350982' +); ``` diff --git a/package.json b/package.json index 6452158..1bdba3e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jd-union", - "version": "0.2.0", + "version": "0.2.1", "description": "京东联盟SDK", "main": "index.js", "scripts": { diff --git a/src/client.js b/src/client.js index 263709e..06248f9 100644 --- a/src/client.js +++ b/src/client.js @@ -1,40 +1,33 @@ -const sign = require('./sign') -const request = require('./request') +const sign = require('./sign'); +const request = require('./request'); +const {DEFAULT_SERVER} = require('./constants'); module.exports = class Client { - constructor(obj = {}) { - this.setConfig(obj) - } - - setConfig(obj = {}) { - const {appKey, appSecret, serverUrl} = obj - if (typeof appKey !== 'string' || appKey.length < 1) { - throw new Error('请正确填写app_key,类型为字符串') + constructor(obj = {}) { + this.setConfig(obj); } - if (typeof appSecret !== 'string' || appSecret.length < 1) { - throw new Error('请正确填写appSecret,类型为字符串') + + setConfig(obj = {}) { + const {appKey, appSecret, serverUrl} = obj; + if (typeof appKey !== 'string' || appKey.length < 1) { + throw new Error('请正确填写app_key,类型为字符串'); + } + if (typeof appSecret !== 'string' || appSecret.length < 1) { + throw new Error('请正确填写appSecret,类型为字符串'); + } + this.appKey = appKey; + this.appSecret = appSecret; + this.serverUrl = serverUrl || DEFAULT_SERVER; } - this.appKey = appKey - this.appSecret = appSecret - this.serverUrl = serverUrl || 'https://router.jd.com/api' - } - async request(method, param_json, version = '1.0', access_token = '') { - const {appKey, appSecret, serverUrl} = this - return await request(serverUrl, - method, - param_json, - version, - access_token, - appKey, - appSecret) - } + async request(method, param_json, version = '1.0', access_token = '') { + const {appKey, appSecret, serverUrl} = this; + return await request(serverUrl, method, param_json, version, access_token, appKey, appSecret); + } - sign(obj) { - const {appKey, appSecret} = this - return sign(obj, - appKey, - appSecret) - } + sign(obj) { + const {appKey, appSecret, serverUrl} = this; + return sign(obj, appKey, appSecret, serverUrl); + } -} +}; diff --git a/src/constants.js b/src/constants.js new file mode 100644 index 0000000..b507bdd --- /dev/null +++ b/src/constants.js @@ -0,0 +1,3 @@ +module.exports = { + DEFAULT_SERVER: 'https://api.jd.com/routerjson' +}; diff --git a/src/request.js b/src/request.js index df74dce..ed42345 100644 --- a/src/request.js +++ b/src/request.js @@ -1,24 +1,29 @@ -const req = require('request-promise') -const format = require('dateformat') -const sign = require('./sign') +const req = require('request-promise'); +const format = require('dateformat'); +const sign = require('./sign'); +const {DEFAULT_SERVER} = require('./constants'); -module.exports = async (url = 'https://api.jd.com/routerjson', method, param_json, version = '1.0', access_token = '', app_key = '', app_secret = '') => { - const qs = { - method, - '360buy_param_json': typeof param_json === 'object' ? JSON.stringify(param_json) : param_json, - v: version, - access_token, - app_key, - timestamp: format(new Date(), 'yyyy-mm-dd HH:MM:ss'), - sign_method: 'md5', - format: 'json' - } - qs.sign = sign(qs, - app_key, - app_secret) - return await req({ - uri: url, - qs, - json: true - }) -} +module.exports = async (url = DEFAULT_SERVER, method, param_json, version = '1.0', access_token = '', app_key = '', app_secret = '') => { + const isNewVersion = url === DEFAULT_SERVER; + const qs = { + method, + v: version, + access_token, + app_key, + timestamp: format(new Date(), 'yyyy-mm-dd HH:MM:ss'), + sign_method: 'md5', + format: 'json' + }; + const params = typeof param_json === 'object' ? JSON.stringify(param_json) : param_json; + if (isNewVersion) { + qs['360buy_param_json'] = params; + } else { + qs['param_json'] = params; + } + qs.sign = sign(qs, app_key, app_secret, url); + return await req({ + uri: url, + qs, + json: true + }); +}; diff --git a/src/sign.js b/src/sign.js index 4b86a18..07c7d5f 100644 --- a/src/sign.js +++ b/src/sign.js @@ -1,23 +1,26 @@ const md5 = require('md5'); +const {DEFAULT_SERVER} = require('./constants'); -module.exports = (obj = {}, app_key = '', app_secret = '') => { +module.exports = (obj = {}, app_key = '', app_secret = '', url = DEFAULT_SERVER) => { + const isNewVersion = url === DEFAULT_SERVER; const list = []; if (typeof obj['app_key'] === 'undefined') { obj['app_key'] = app_key; } - if (typeof obj['param_json'] !== 'undefined') { // 转为新版 - obj['360buy_param_json'] = obj['param_json']; + const params = obj['360buy_param_json'] || obj['param_json']; + if (isNewVersion) { delete obj['param_json']; + obj['360buy_param_json'] = params; + } else { + delete obj['360buy_param_json']; + obj['param_json'] = params; } - Object.keys(obj) - .sort() - .forEach(key => { + Object.keys(obj).sort().forEach(key => { const value = obj[key]; if (key !== 'access_token' || (typeof value === 'string' && value.length > 0)) { list.push(`${key}${typeof value === 'object' ? JSON.stringify(value) : value}`); } }); const signStr = `${app_secret}${list.join('')}${app_secret}`; - return md5(signStr) - .toUpperCase(); + return md5(signStr).toUpperCase(); }; -- Gitee