From 12afd457b34a2cbfde806d50bb489bc721f1f82a Mon Sep 17 00:00:00 2001 From: Changeden Date: Tue, 29 Jun 2021 10:34:53 +0800 Subject: [PATCH] =?UTF-8?q?v0.2.0=EF=BC=8C=E4=BB=85=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=96=B0=E7=89=88Api(https://api.jd.com/routerjson)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- package.json | 2 +- src/request.js | 4 ++-- src/sign.js | 34 +++++++++++++++++++--------------- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 22fe486..90a2af6 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ const {create} = require('jd-union'); const api = create({ appKey: '<联盟分配给应用的appKey>如:eefc33bDRea044cb8ctre5hycf0ac1934', appSecret: '<联盟分配给应用的appSecret>如:6d34r0d0kild46460654b42f5e350982', - serverUrl: '默认为 https://router.jd.com/api' + serverUrl: '默认为 https://api.jd.com/routerjson' }); // 调用Api @@ -64,7 +64,7 @@ const {sign} = require('jd-union'); const signature = 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', ccess_token: '', timestamp: '2018-10-18 11:13:12', diff --git a/package.json b/package.json index d503c8a..6452158 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jd-union", - "version": "0.1.1", + "version": "0.2.0", "description": "京东联盟SDK", "main": "index.js", "scripts": { diff --git a/src/request.js b/src/request.js index ef254aa..df74dce 100644 --- a/src/request.js +++ b/src/request.js @@ -2,10 +2,10 @@ const req = require('request-promise') const format = require('dateformat') const sign = require('./sign') -module.exports = async (url = 'https://router.jd.com/api', method, param_json, version = '1.0', access_token = '', app_key = '', app_secret = '') => { +module.exports = async (url = 'https://api.jd.com/routerjson', method, param_json, version = '1.0', access_token = '', app_key = '', app_secret = '') => { const qs = { method, - param_json: typeof param_json === 'object' ? JSON.stringify(param_json) : param_json, + '360buy_param_json': typeof param_json === 'object' ? JSON.stringify(param_json) : param_json, v: version, access_token, app_key, diff --git a/src/sign.js b/src/sign.js index 58e4c6a..4b86a18 100644 --- a/src/sign.js +++ b/src/sign.js @@ -1,19 +1,23 @@ -const md5 = require('md5') +const md5 = require('md5'); module.exports = (obj = {}, app_key = '', app_secret = '') => { - const list = [] - if (typeof obj['app_key'] === 'undefined') { - obj['app_key'] = app_key - } - Object.keys(obj) + 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']; + delete obj['param_json']; + } + 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() -} + 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(); +}; -- Gitee