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