| | |
| | | return `${date}_${r}` |
| | | } |
| | | |
| | | // 对象排序并且排序后使用&连接 |
| | | export function sortAndJoinObj(obj: Record<string, any> = {}): string { |
| | | if (!obj || typeof obj !== 'object' || Array.isArray(obj)) return ''; |
| | | return Object.keys(obj) |
| | | .sort() |
| | | .map(key => `${key}=${obj[key]}`) |
| | | .join('&'); |
| | | } |
| | | |
| | | /** |
| | | * 定义对象排序方法 |
| | | */ |
| | |
| | | export function makeSign(method, params, content, secret = _XP_SECRET) { |
| | | let sign = ''; |
| | | let parm = ''; |
| | | |
| | | params = objectSort(params); |
| | | for (const i in params) { |
| | | const j = params[i]; |
| | |
| | | if (content && String(content) !== '{}') { |
| | | content_MD5 = MD5.hex(JSON.stringify(objectSort(content, { keyUpperCase: false }))); // content 也要排序,避免中间平台的干扰,例如 小红书 iOS 端发送的请求顺序就和我们传入的不一致 |
| | | } |
| | | const ass = `${method}&${parm}&${content_MD5}`; |
| | | let ass = '' |
| | | if (method == 'POST') { |
| | | ass = `${method}&${parm}&${content_MD5}`; |
| | | } else { |
| | | ass = `${method}&${parm}&${content && Object.keys(content).length > 0 ? sortAndJoinObj(content) + '&' : ''}`; |
| | | } |
| | | const afterUtf8 = utf8Encode(ass); |
| | | // const third = crypto.createHmac('sha256', secret).update(afterUtf8).digest('hex'); |
| | | // console.log('crypto---------------------------'); |