zhengyiming
5 天以前 c004767a84f293ab98eb3948bbcd2af64b264c57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import cryptoJs from 'crypto-js';
import { type IRequestOptions } from 'senior-request';
 
const ApiSignPrivateKey = 'X07uIM2cHK5IJhDi4ppoxQglYE21gq3E';
 
export function getTimestampInSeconds() {
  var date = new Date();
  var timestampInSeconds = Math.floor(date.getTime() / 1000);
  return timestampInSeconds;
}
 
export function generateApiSign(
  token: string,
  userId: string,
  timestampInSeconds: number | string
) {
  return cryptoJs.MD5(`${token}-${userId}-${timestampInSeconds}-${ApiSignPrivateKey}`).toString();
}
 
export function setRequestheaders(config: IRequestOptions, accessToken: string, userId: string) {
  config.headers['Authorization'] = 'Bearer ' + accessToken;
  const timestampInSeconds = getTimestampInSeconds();
  config.headers['userId'] = userId;
  config.headers['time'] = timestampInSeconds;
  config.headers['sign'] = generateApiSign(
    config.headers['Authorization'],
    userId,
    timestampInSeconds
  );
  return config;
}