From 27908379545df5378e5d6bd3b518c7e30fdae767 Mon Sep 17 00:00:00 2001 From: Changeden Date: Thu, 20 Jan 2022 11:16:34 +0800 Subject: [PATCH] =?UTF-8?q?v0.3.0=20=E7=A7=BB=E9=99=A4=E6=97=A7=E7=89=88?= =?UTF-8?q?=E5=85=BC=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 90 ++++++++++++++++++++++++++++---------------------- index.js | 18 +++++----- package.json | 8 ++--- src/client.js | 13 +++----- src/request.js | 19 ++++------- src/sign.js | 17 +++------- 6 files changed, 80 insertions(+), 85 deletions(-) diff --git a/README.md b/README.md index 1f89cda..1f0a665 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ ## jd-union > 京东联盟Nodejs SDK +> +> [全部API](https://union.jd.com/openplatform/api) ### NPM [ @@ -32,27 +34,34 @@ const {create, sign, request} = require('jd-union'); const {create} = require('jd-union'); const api = create( - { - appKey: '<联盟分配给应用的appKey>如:eefc33bDRea044cb8ctre5hycf0ac1934', - appSecret: '<联盟分配给应用的appSecret>如:6d34r0d0kild46460654b42f5e350982', - serverUrl: '默认为 https://api.jd.com/routerjson' - } + { + appKey: '<联盟分配给应用的appKey>如:eefc33bDRea044cb8ctre5hycf0ac1934', + appSecret: '<联盟分配给应用的appSecret>如:6d34r0d0kild46460654b42f5e350982' + } // 0.3.x版本后,不再支持自定义服务地址 ); // 调用Api // api.request(API接口名称, 业务参数); 返回promise const res = await api.request( - 'jd.union.open.goods.jingfen.query', - { - goodsReq: 110 - } + 'jd.union.open.goods.jingfen.query', + { + goodsReq: { + eliteId: 22 + } + }, + '1.0', + '' ); // 验签 const signature = api.sign( { - method: 'jd.union.open.goods.query', - param_json: {'goodsReqDTO': {'keyword': '男装', 'pageSize': 10, 'pageIndex': 1}}, // 0.2.x版本后,key为360buy_param_json或param_json皆可 + method: 'jd.union.open.goods.jingfen.query', + '360buy_param_json': { + goodsReq: { + eliteId: 22 + } + }, // 0.3.x版本后,key仅支持360buy_param_json v: '1.0', access_token: '', timestamp: '2018-10-18 11:13:12', @@ -69,18 +78,21 @@ console.log(signature); const {sign} = require('jd-union'); 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', - ccess_token: '', - timestamp: '2018-10-18 11:13:12', - sign_method: 'md5', - format: 'json' - }, - 'eefc33bDRea044cb8ctre5hycf0ac1934', - '6d34r0d0kild46460654b42f5e350982', - 'https://api.jd.com/routerjson' + { + method: 'jd.union.open.goods.jingfen.query', + '360buy_param_json': { + goodsReq: { + eliteId: 22 + } + }, // 0.3.x版本后,key仅支持360buy_param_json + v: '1.0', + access_token: '', + timestamp: '2018-10-18 11:13:12', + sign_method: 'md5', + format: 'json' + }, + 'eefc33bDRea044cb8ctre5hycf0ac1934', + '6d34r0d0kild46460654b42f5e350982' ); console.log(signature); @@ -91,23 +103,23 @@ console.log(signature); const {request} = require('jd-union'); /* -const res = await request('https://router.jd.com/api', - API接口名称, - 业务参数, - Api版本号, - access_token = '', - '<联盟分配给应用的appKey>如:eefc33bDRea044cb8ctre5hycf0ac1934', - '<联盟分配给应用的appSecret>如:6d34r0d0kild46460654b42f5e350982'); +const res = await request( + API接口名称, + 业务参数, + Api版本号, + access_token = '', + '<联盟分配给应用的appKey>如:eefc33bDRea044cb8ctre5hycf0ac1934', + '<联盟分配给应用的appSecret>如:6d34r0d0kild46460654b42f5e350982' +); */ const res = await request( - 'https://api.jd.com/routerjson', - 'jd.union.open.goods.jingfen.query', - { - goodsReq: 110 - }, - '1.0', - '', - 'eefc33bDRea044cb8ctre5hycf0ac1934', - '6d34r0d0kild46460654b42f5e350982' + 'jd.union.open.goods.jingfen.query', + { + goodsReq: 110 + }, + '1.0', + '', + 'eefc33bDRea044cb8ctre5hycf0ac1934', + '6d34r0d0kild46460654b42f5e350982' ); ``` diff --git a/index.js b/index.js index f26d1c6..3212362 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,13 @@ -const sign = require('./src/sign') -const request = require('./src/request') -const client = require('./src/client') +const sign = require('./src/sign'); +const request = require('./src/request'); +const client = require('./src/client'); const create = (obj = {}) => { - return new client(obj) -} + return new client(obj); +}; module.exports = { - sign, - request, - create -} + sign, + request, + create +}; diff --git a/package.json b/package.json index 1bdba3e..21f61ec 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jd-union", - "version": "0.2.1", + "version": "0.3.0", "description": "京东联盟SDK", "main": "index.js", "scripts": { @@ -22,11 +22,11 @@ "request-promise": "^4.2.5" }, "bugs": { - "url": "https://gitee.com/Chans-Open-Source/jd-union-nodejs-sdk/issues" + "url": "https://gitee.com/changeden/jd-union-nodejs-sdk/issues" }, - "homepage": "https://gitee.com/Chans-Open-Source/jd-union-nodejs-sdk", + "homepage": "https://gitee.com/changeden/jd-union-nodejs-sdk", "repository": { "type": "git", - "url": "https://gitee.com/Chans-Open-Source/jd-union-nodejs-sdk.git" + "url": "https://gitee.com/changeden/jd-union-nodejs-sdk.git" } } diff --git a/src/client.js b/src/client.js index 06248f9..cfe9917 100644 --- a/src/client.js +++ b/src/client.js @@ -1,6 +1,5 @@ const sign = require('./sign'); const request = require('./request'); -const {DEFAULT_SERVER} = require('./constants'); module.exports = class Client { constructor(obj = {}) { @@ -8,7 +7,7 @@ module.exports = class Client { } setConfig(obj = {}) { - const {appKey, appSecret, serverUrl} = obj; + const {appKey, appSecret} = obj; if (typeof appKey !== 'string' || appKey.length < 1) { throw new Error('请正确填写app_key,类型为字符串'); } @@ -17,17 +16,15 @@ module.exports = class Client { } this.appKey = appKey; this.appSecret = appSecret; - this.serverUrl = serverUrl || DEFAULT_SERVER; } 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); + const {appKey, appSecret} = this; + return await request(method, param_json, version, access_token, appKey, appSecret); } sign(obj) { - const {appKey, appSecret, serverUrl} = this; - return sign(obj, appKey, appSecret, serverUrl); + const {appKey, appSecret} = this; + return sign(obj, appKey, appSecret); } - }; diff --git a/src/request.js b/src/request.js index ed42345..3d3250d 100644 --- a/src/request.js +++ b/src/request.js @@ -3,8 +3,7 @@ const format = require('dateformat'); const sign = require('./sign'); const {DEFAULT_SERVER} = require('./constants'); -module.exports = async (url = DEFAULT_SERVER, method, param_json, version = '1.0', access_token = '', app_key = '', app_secret = '') => { - const isNewVersion = url === DEFAULT_SERVER; +module.exports = async (method, param_json, version = '1.0', access_token = '', app_key = '', app_secret = '') => { const qs = { method, v: version, @@ -12,18 +11,12 @@ module.exports = async (url = DEFAULT_SERVER, method, param_json, version = '1.0 app_key, timestamp: format(new Date(), 'yyyy-mm-dd HH:MM:ss'), sign_method: 'md5', - format: 'json' + format: 'json', + '360buy_param_json': param_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); + qs.sign = sign(qs, app_key, app_secret); + console.log(qs); return await req({ - uri: url, - qs, - json: true + uri: DEFAULT_SERVER, qs, json: true }); }; diff --git a/src/sign.js b/src/sign.js index 07c7d5f..778ec07 100644 --- a/src/sign.js +++ b/src/sign.js @@ -1,23 +1,16 @@ const md5 = require('md5'); -const {DEFAULT_SERVER} = require('./constants'); -module.exports = (obj = {}, app_key = '', app_secret = '', url = DEFAULT_SERVER) => { - const isNewVersion = url === DEFAULT_SERVER; +module.exports = (obj = {}, app_key = '', app_secret = '') => { const list = []; if (typeof obj['app_key'] === 'undefined') { obj['app_key'] = app_key; } - 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; - } + const param_json = obj['360buy_param_json'] || obj['param_json']; + obj['360buy_param_json'] = typeof param_json === 'object' ? JSON.stringify(param_json) : param_json; + obj ['access_token'] = obj ['access_token'] || 'undefined'; Object.keys(obj).sort().forEach(key => { const value = obj[key]; - if (key !== 'access_token' || (typeof value === 'string' && value.length > 0)) { + if (typeof value === 'string' && value.length > 0) { list.push(`${key}${typeof value === 'object' ? JSON.stringify(value) : value}`); } }); -- Gitee