190 lines
4.9 KiB
JavaScript
190 lines
4.9 KiB
JavaScript
'use strict';
|
|
|
|
const crypto = require('node:crypto');
|
|
|
|
/**
|
|
* 判断输入是否为普通对象。
|
|
* @param {*} value 任意输入值。
|
|
* @return {boolean} 是否为普通对象。
|
|
*/
|
|
function isPlainObject(value) {
|
|
return Object.prototype.toString.call(value) === '[object Object]';
|
|
}
|
|
|
|
/**
|
|
* 将对象按 key 排序后稳定序列化。
|
|
* @param {*} value 待序列化值。
|
|
* @return {string} 稳定序列化字符串。
|
|
*/
|
|
function stableStringify(value) {
|
|
if (value === undefined) {
|
|
return 'null';
|
|
}
|
|
|
|
if (value === null || typeof value !== 'object') {
|
|
return JSON.stringify(value);
|
|
}
|
|
|
|
if (Array.isArray(value)) {
|
|
return `[${value.map(item => stableStringify(item)).join(',')}]`;
|
|
}
|
|
|
|
if (!isPlainObject(value)) {
|
|
return JSON.stringify(value);
|
|
}
|
|
|
|
const keys = Object.keys(value)
|
|
.filter(key => value[key] !== undefined)
|
|
.sort();
|
|
return `{${keys.map(key => `${JSON.stringify(key)}:${stableStringify(value[key])}`).join(',')}}`;
|
|
}
|
|
|
|
/**
|
|
* 归一化参数值为可拼接字符串。
|
|
* @param {*} value 参数值。
|
|
* @return {string} 归一化后的字符串。
|
|
*/
|
|
function normalizeValue(value) {
|
|
if (value === undefined || value === null) {
|
|
return '';
|
|
}
|
|
|
|
if (typeof value === 'object') {
|
|
return stableStringify(value);
|
|
}
|
|
|
|
return String(value);
|
|
}
|
|
|
|
/**
|
|
* 归一化密钥值。
|
|
* @param {string} clientSecret 客户端密钥。
|
|
* @return {string} 归一化后的密钥。
|
|
*/
|
|
function normalizeSecret(clientSecret) {
|
|
return String(clientSecret || '').trim();
|
|
}
|
|
|
|
/**
|
|
* 归一化并排序请求参数。
|
|
* @param {Object} [params={}] 请求参数。
|
|
* @return {Array<[string, string]>} 排序后的键值对。
|
|
*/
|
|
function buildSortedPairs(params = {}) {
|
|
if (!isPlainObject(params)) {
|
|
throw new Error('params 必须是对象');
|
|
}
|
|
|
|
return Object.keys(params)
|
|
.filter(key => params[key] !== undefined)
|
|
.sort()
|
|
.map(key => [ key, normalizeValue(params[key]) ]);
|
|
}
|
|
|
|
/**
|
|
* 构建拼多多签名原文。
|
|
* @param {Object} [params={}] 所有参与签名的参数。
|
|
* @param {string} clientSecret 客户端密钥。
|
|
* @return {{ signText: string, sortedPairs: Array<[string, string]> }} 签名原文与排序结果。
|
|
*/
|
|
function buildSignText(params = {}, clientSecret) {
|
|
const secret = normalizeSecret(clientSecret);
|
|
if (!secret) {
|
|
throw new Error('clientSecret 不能为空');
|
|
}
|
|
|
|
const sortedPairs = buildSortedPairs(params);
|
|
const signText = secret + sortedPairs.map(([ key, value ]) => key + value).join('') + secret;
|
|
|
|
return {
|
|
signText,
|
|
sortedPairs,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 生成拼多多 API 签名。
|
|
* @param {Object} [params={}] 所有参与签名的参数。
|
|
* @param {string} clientSecret 客户端密钥。
|
|
* @return {{ sign: string, signText: string, sortedPairs: Array<[string, string]> }} 签名结果。
|
|
*/
|
|
function sign(params = {}, clientSecret) {
|
|
const { signText, sortedPairs } = buildSignText(params, clientSecret);
|
|
|
|
return {
|
|
signText,
|
|
sortedPairs,
|
|
sign: crypto.createHash('md5').update(signText, 'utf8').digest('hex')
|
|
.toUpperCase(),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 构建拼多多接口请求体,并自动补充 sign。
|
|
* @param {Object} [options={}] 请求选项。
|
|
* @param {string} options.type API 接口名。
|
|
* @param {string|number} options.clientId 应用 client_id。
|
|
* @param {string|number} options.timestamp UNIX 时间戳。
|
|
* @param {string} [options.dataType='JSON'] 返回数据格式。
|
|
* @param {string} [options.accessToken] 用户授权令牌。
|
|
* @param {string} [options.version] API 版本。
|
|
* @param {Object} [options.bizParams={}] 业务参数。
|
|
* @param {string} options.clientSecret 客户端密钥。
|
|
* @return {{ requestBody: Object, signText: string, sortedPairs: Array<[string, string]> }} 请求结果。
|
|
*/
|
|
function buildRequestBody(options = {}) {
|
|
const {
|
|
type,
|
|
clientId,
|
|
timestamp,
|
|
dataType = 'JSON',
|
|
accessToken,
|
|
version,
|
|
bizParams = {},
|
|
clientSecret,
|
|
} = options;
|
|
|
|
const requestBody = {
|
|
...(isPlainObject(bizParams) ? bizParams : {}),
|
|
type,
|
|
client_id: clientId,
|
|
timestamp,
|
|
data_type: dataType,
|
|
access_token: accessToken,
|
|
version,
|
|
};
|
|
|
|
Object.keys(requestBody).forEach(key => {
|
|
if (requestBody[key] === undefined || requestBody[key] === null || requestBody[key] === '') {
|
|
delete requestBody[key];
|
|
}
|
|
});
|
|
|
|
// PDD 路由参数验签以字符串值为准,复杂参数需与签名使用同一序列化结果。
|
|
Object.keys(requestBody).forEach(key => {
|
|
if (requestBody[key] && typeof requestBody[key] === 'object') {
|
|
requestBody[key] = normalizeValue(requestBody[key]);
|
|
}
|
|
});
|
|
|
|
const signed = sign(requestBody, clientSecret);
|
|
|
|
return {
|
|
requestBody: {
|
|
...requestBody,
|
|
sign: signed.sign,
|
|
},
|
|
signText: signed.signText,
|
|
sortedPairs: signed.sortedPairs,
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
buildRequestBody,
|
|
buildSignText,
|
|
normalizeSecret,
|
|
normalizeValue,
|
|
sign,
|
|
stableStringify,
|
|
};
|