t-comm
Version:
专业、稳定、纯粹的工具库
111 lines (110 loc) • 4.12 kB
TypeScript
import type { IRequestInterceptor, IRawRequest, ICocosLoginInfoStorage } from '../../types';
/** 加密能力接口,业务方注入(通常基于 crypto-js 实现) */
export interface ICocosFangshuaCrypto {
/** 计算 MD5,返回 hex 字符串 */
md5: (data: string) => string;
/**
* AES-CBC 解密
* @param key 密钥(MD5 hex 字符串)
* @param encryptedHex 密文(hex 字符串)
* @param iv 初始化向量(明文字符串)
* @returns 解密后的明文字符串
*/
aesDecrypt: (key: string, encryptedHex: string, iv: string) => string;
}
export interface ICocosFangshuaOptions {
/** 登录态存储(与 CocosLoginInfoParamInterceptor 共用同一个 storage) */
storage: ICocosLoginInfoStorage;
/** 存储登录态的 key,默认 cocos_login_info */
storageKey?: string;
/** 获取防刷时间戳(后台 GetTs 接口返回的 ts) */
getTs: () => string | undefined;
/** 获取防刷加密数据(后台 GetTs 接口返回的 encryptData) */
getEncryptData: () => string | undefined;
/**
* 加密能力注入(必填)
*
* 业务方基于 crypto-js 实现示例:
* ```ts
* import 'crypto-js/md5';
* import 'crypto-js/aes';
* import 'crypto-js/enc-utf8';
* import 'crypto-js/enc-hex';
* import 'crypto-js/enc-base64';
* import 'crypto-js/pad-pkcs7';
* import 'crypto-js/cipher-core';
* import * as CryptoJS from 'crypto-js/core';
*
* const crypto: ICocosFangshuaCrypto = {
* md5: (data) => CryptoJS.MD5(data).toString(),
* aesDecrypt: (key, encryptedHex, iv) => {
* const hexStr = CryptoJS.enc.Hex.parse(encryptedHex);
* const srcs = CryptoJS.enc.Base64.stringify(hexStr);
* const decrypt = CryptoJS.AES.decrypt(srcs, CryptoJS.enc.Utf8.parse(key), {
* iv: CryptoJS.enc.Utf8.parse(iv),
* mode: CryptoJS.mode.CBC,
* padding: CryptoJS.pad.Pkcs7,
* });
* return decrypt.toString(CryptoJS.enc.Utf8);
* },
* };
* ```
*/
crypto: ICocosFangshuaCrypto;
/** 判断当前是否为测试环境(可选,用于 mock 检测) */
isTestEnv?: () => boolean;
/** 是否打印调试日志,默认 false */
debug?: boolean;
}
/**
* Cocos 端防刷拦截器
*
* 加到请求拦截器的最后,会根据加密协议在请求 header 里加上防刷签名参数。
* 依赖业务提前请求 GetTs 接口获取加密参数(ts + encryptData)。
*
* 加密协议见文档:
* https://doc.weixin.qq.com/flowchart/f4_APsAyQbdAFwbbhTs281TFe1ncmazz?scode=AJEAIQdfAAor0oxY1IAPsAyQbdAFw
*
* 参考 pmd-npm/packages/business/src/network/interceptor/request/common/fangshua/fangshuaInterceptor.ts
*
* @example
* ```ts
* import { CocosFangshuaInterceptor } from 't-comm/cocos/network';
*
* const fangshuaInterceptor = new CocosFangshuaInterceptor({
* storage,
* getTs: () => appInfo.ts,
* getEncryptData: () => appInfo.encryptData,
* crypto: {
* md5: (data) => CryptoJS.MD5(data).toString(),
* aesDecrypt: (key, encryptedHex, iv) => { ... },
* },
* debug: true, // 开启调试日志(默认 false)
* });
* ```
*/
export declare class CocosFangshuaInterceptor implements IRequestInterceptor {
private options;
constructor(options: ICocosFangshuaOptions);
interceptor(param: IRawRequest): [boolean, IRawRequest];
/** 仅 debug=true 时打印日志 */
private log;
}
/**
* 判断请求是否需要带上防刷签名信息
* @param param 请求参数
* @returns 是否需要签名
*/
declare function needSign(param: IRawRequest): boolean;
/**
* 增加需要签名的 CGI 接口
* @param cgiList 需要签名的接口地址列表
*/
declare function appendSignCGI(cgiList?: Array<string>): void;
/**
* 判断是否是活跃上报
* @param body post 请求的 body 内容
* @returns 本次上报内容是否是活跃上报
*/
declare function isActiveReport(body: Record<string, any>): boolean;
export { needSign, isActiveReport, appendSignCGI, };