引入
import {
IAegisReportOption,
IRawRequest,
IRawResponse,
IRequestInterceptor,
IResponseInterceptorParam,
ICocosLoginInfoStorage,
NEED_LOGIN_RET,
ACCESS_DENIED_RET,
memoryStore,
defaultStorage,
defaultGetLoginCode,
getDefaultLocalStorage,
initCocosNetwork,
IDecorator
} from 't-comm';
// 不支持 tree-shaking 的项目
import {
IAegisReportOption,
IRawRequest,
IRawResponse,
IRequestInterceptor,
IResponseInterceptorParam,
ICocosLoginInfoStorage,
NEED_LOGIN_RET,
ACCESS_DENIED_RET,
memoryStore,
defaultStorage,
defaultGetLoginCode,
getDefaultLocalStorage,
initCocosNetwork,
IDecorator
} from 't-comm/lib/cocos/network/index';
// 只支持 ESM 的项目
import {
IAegisReportOption,
IRawRequest,
IRawResponse,
IRequestInterceptor,
IResponseInterceptorParam,
ICocosLoginInfoStorage,
NEED_LOGIN_RET,
ACCESS_DENIED_RET,
memoryStore,
defaultStorage,
defaultGetLoginCode,
getDefaultLocalStorage,
initCocosNetwork,
IDecorator
} from 't-comm/es/cocos/network/index';IAegisReportOption
描述:aegisReport 选项:透传到 AegisReportInterceptor
参数:
IAegisReportOption.report:function.truncateSize:number
iAegisReportOption.report : function
业务注入的上报回调。 形如:
`aegisReport: {
report: (info) => aegis.infoAll({
msg: `${info.method} ${info.url} ret=${info.ret} dur=${info.duration}ms`,
ext1: info.requestBody,
ext2: info.responseData,
ext3: `${info.status} | ${info.responseHeader}`,
duration: info.duration,
}),
truncateSize: 1000,
}
`Kind: instance property of IAegisReportOption
iAegisReportOption.truncateSize : number
单项 JSON 序列化后最大字符数,默认 1000。<=0 不截断。 超过会在末尾追加 ...(truncated, total N) 标记。
Kind: instance property of IAegisReportOption
Optional:
IRawRequest
描述:Cocos Network 子模块 - 公共类型定义 不依赖 @tencent/pmd-network / pmd-network-v2,完全自持, 便于 pmd-api-cocos 独立发布、独立编译。
参数:
iRawRequest.url : string
最终请求 URL(已含 query)
Kind: instance property of IRawRequest
iRawRequest.method : 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS'
HTTP method,默认 POST
Kind: instance property of IRawRequest
Optional:
iRawRequest.header : Record.<string, string>
请求头
Kind: instance property of IRawRequest
Optional:
iRawRequest.reqData : any
请求体(通常是业务 reqData)
Kind: instance property of IRawRequest
Optional:
iRawRequest.extra : object
业务层传入的附加参数(会在拦截器链之间透传)
Kind: instance property of IRawRequest
Optional:
Properties
| Name | Type | Description |
|---|---|---|
| [withCredentials] | boolean | |
| [headers] | Record.<string, string> | |
| [___cocosRetried] | boolean | decorator 内部用,标记重试次数防死循环 |
| k: | any | string |
iRawRequest.___cocosOriginUrl : string
记录原始 url(拦截器可能多次改写,需要这个做幂等重试)
Kind: instance property of IRawRequest
Optional:
IRawResponse
描述:底层响应(doRequest 的返回)
参数:
IRawResponse.status:number.ok:boolean.data:any.statusText:string.getHeader:function
iRawResponse.status : number
HTTP 状态码
Kind: instance property of IRawResponse
iRawResponse.ok : boolean
2xx => true
Kind: instance property of IRawResponse
iRawResponse.data : any
响应 body(已做 JSON parse 尝试)
Kind: instance property of IRawResponse
iRawResponse.statusText : string
原始状态文本或 errMsg
Kind: instance property of IRawResponse
iRawResponse.getHeader : function
大小写不敏感取 header
Kind: instance property of IRawResponse
IRequestInterceptor
描述:请求拦截器 返回 [shouldAbort, param]
- shouldAbort=true:中止链路,由上层处理
- shouldAbort=false:继续下一个拦截器
参数:
IResponseInterceptorParam
描述:响应拦截器 入参包含 request + response,方便拦截器做路由判断
参数:
iResponseInterceptorParam.request : IRawRequest
原始请求
Kind: instance property of IResponseInterceptorParam
iResponseInterceptorParam.response : IRawResponse
底层响应(含 getHeader)
Kind: instance property of IResponseInterceptorParam
iResponseInterceptorParam.data : any
业务数据(= response.data,由 GetDataInterceptor 提取出来)
Kind: instance property of IResponseInterceptorParam
Optional:
ICocosLoginInfoStorage
描述:登录态存储接口(业务方注入,用 wx.getStorageSync 封装即可)
参数:
iCocosLoginInfoStorage.removeItem : function
删除指定 key(可选,业务方自定义 storage 未实现时回退到 set(key, null))
Kind: instance property of ICocosLoginInfoStorage
Optional:
NEED_LOGIN_RET
描述:后台约定:ret=100000 表示登录态失效,需要重新登录
参数:
ACCESS_DENIED_RET
描述:后台约定:ret=100006 表示「访问被拒」(常见于测试环境白名单 / 黑名单场景)。 触发时机:业务后台对当前小游戏 appid / uid / ip 等维度判定为「不可访问」时返回。 框架层本身不强制业务方必须做什么,只负责在 NormalizeResponseRetInterceptor 检测到 该 r 时调用业务方注入的 onAccessDenied 回调(由业务方决定是弹窗 / 跳 H5 申请页 / 重启小游戏等),并清空响应 msg 避免上层兜底 toast。
参数:
memoryStore
描述:内存兜底(cocos sys.localStorage / 浏览器 localStorage 都不可用时使用)
参数:
defaultStorage
描述:默认 storage:Cocos sys.localStorage → 浏览器 localStorage → 内存兜底。
get:自动尝试JSON.parse,失败则原样返回字符串set:非字符串值自动JSON.stringify;传null/undefined等价removeItemremoveItem:删除指定 key
参数:
defaultGetLoginCode
描述:默认 getLoginCode:优先用 wx.login,wx 不存在则返回空 code
参数:
getDefaultLocalStorage()
描述:运行时获取 Cocos sys.localStorage(优先)或浏览器 localStorage。 两者 API 一致:getItem / setItem / removeItem。 避免编译期硬依赖 cc 模块。
参数:
initCocosNetwork()
描述:一键初始化 Cocos 微信小游戏网络层 等价于 pmd-npm business/src/network-v2/application/cocos/index-mp.ts 的 initNetworkManager,但适配到本包自带的 NetworkManager 和 wx.request。 调用一次后,所有通过 pmd-api-cocos 的 NetworkManager 发出的请求 都会:
- 自动拼 baseUrl + host
- 无登录态时带 code → 触发后台换登录态
- 有登录态时 body.login_info + cookie 透传
- 响应 header logininfo 自动入库
- ret=100000 自动清态重试一次
使用示例(见 README):
`import { initCocosNetwork } from '@tencent/pmd-api-cocos/network';
initCocosNetwork({
network: {
svrdomain: {
test: 'atest.igame.qq.com',
prod: 'igame.qq.com',
},
},
loginPath: '/v2/login/code',
appid: 'wx14f5bb5ae9a067f8',
// storage / getLoginCode 可不传,默认走 sys.localStorage + wx.login
onLoginInfo: (info) => {
// 可选:同步到 AuthService / 刷新 UI 等
},
});
// 控制台可执行(微信小游戏 / 开发者工具):
wx.__pmdSetEnv__('prod') // → 切到正式环境并自动重启
wx.__pmdSetEnv__('test') // → 切回测试环境
wx.__pmdGetEnv__() // → 查看当前环境
// (非微信环境会回退挂到 globalThis 上)
`参数:
IDecorator
描述:装饰器:包装整个 request Promise,用于做重试、限流、调度等横切逻辑 需要自己决定调用 request() 的时机和次数
参数:
| 参数名 | 类型 |
|---|---|
| request | function |
| param | IRawRequest |