| 1 | // @ts-ignore |
| 2 | import kuaiShosignCore from './kuaiShoSignCore.js'; |
| 3 | import qs from 'qs'; |
| 4 | // @ts-ignore |
| 5 | import crypto from 'crypto-js'; |
| 6 | |
| 7 | interface ISignParams { |
| 8 | json: Record<string, any>; |
| 9 | type: 'form-data' | 'json'; |
| 10 | url: string; |
| 11 | } |
| 12 | |
| 13 | // 快手平台签名 |
| 14 | class KwaiSign { |
| 15 | exports: any = {}; |
| 16 | |
| 17 | constructor() { |
| 18 | const obj = { |
| 19 | exports: {}, |
| 20 | id: 75407, |
| 21 | loaded: true, |
| 22 | }; |
| 23 | kuaiShosignCore[75407](obj); |
| 24 | this.exports = obj.exports; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * 输入: |
| 29 | * sign({ |
| 30 | * url: '/rest/cp/creator/comment/report/menu', |
| 31 | * type: 'json', |
| 32 | * json: { |
| 33 | * 'kuaishou.web.cp.api_ph': '19af6d5b24cb170a03331ce9254b1204154c', |
| 34 | * }, |
| 35 | * }) |
| 36 | * 输出: |
| 37 | * /rest/cp/creator/comment/report/menu?__NS_sig3=4656112138efc7727e1b18193ee992f9c3278f63070705050a0b0812 |
| 38 | * @param params |
| 39 | */ |
| 40 | sign(params: ISignParams) { |
| 41 | return new Promise(async (resolve, reject) => { |
| 42 | const { url } = params; |
| 43 | const md5 = this.md5(params); |
| 44 | |
| 45 | this.exports.realm.global['$encode'](md5, { |
| 46 | suc(s: string) { |
| 47 | resolve(`${url}?__NS_sig3=${s}`); |
| 48 | }, |
| 49 | err(e: string) { |
| 50 | console.error('签名失败:', e); |
| 51 | reject(e); |
| 52 | }, |
| 53 | }); |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | private md5({ json, type }: ISignParams) { |
| 58 | let str = ''; |
| 59 | if (type === 'form-data') { |
| 60 | str = qs.stringify(json); |
| 61 | } else { |
| 62 | str = JSON.stringify(json); |
| 63 | } |
| 64 | return crypto.MD5(str).toString(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | const kwaiSign = new KwaiSign(); |
| 69 | export default kwaiSign; |
| 70 |