免插件 · 云开发脚本(云开发调试)
入口:首页 → 快捷调试 → 云开发调试 → 脚本。脚本对所有云请求无条件执行 (无需配置匹配),运行在逻辑层,改 req.data / res.resp 即改写真实字节。
回调对象
onRequest(req)
js
{
id: 'C12', // 云记录 id
type: 'cloud_gateway', // cloud_gateway | cloud_container | cloud_function | cloud_other
type_label: 'V3 HTTP云网关',// V3 HTTP云网关 | Native云网关 | HTTP 云托管 | Native 云托管
// | 云函数 | 其他云开发
source: 'http', // http | native
gateway_config: { // 只读,仅 HTTP 云网关 / HTTP 云托管携带
domain: 'gw.example.com', token: '110_example', key: '…base64…',
rid: '1234567890', exp: 1795070943
},
data: { … } // ★ 改写点(对象,原地改字段),见下表
}type × source | data 的内层结构 |
|---|---|
cloud_gateway + http | { method, header:{…}, body, call_id }(路由在 header 的 x-wx-http-host / x-wx-http-path) |
cloud_gateway + native | { qbase_api_name:'tcbapi_call_gateway', qbase_req:{ method, headers:[…], data, call_id }, qbase_options:{…}, qbase_meta:{…} } |
cloud_container + http | 同 cloud_gateway + http(路由头为 x-wx-service / x-wx-container-path / x-wx-env) |
cloud_container + native | { qbase_api_name:'tcbapi_call_container', qbase_req:{ call_id, action, headers:[…], data }, … }(轮询 action:2 无业务体) |
cloud_function | { qbase_api_name:'tcbapi_slowcallfunction_v2', qbase_req:{ function_name, data, action, call_id }, … }(qbase_req.data = 函数入参) |
cloud_other | 同云函数形态(gettempfileurl / 数据库等) |
onResponse(res)
js
{
id, type, type_label, source, gateway_config, // 只读,同上
req: { …最终请求回调对象(只读)… },
resp: { … } // ★ 改写点
}source | resp 的内层结构 |
|---|---|
http | { statusCode, header:{…全部响应头…}, body } |
native | { baseresponse:{ errcode }, status, http_code, headers, data }(data = 业务响应明文) |
改写规则
- 原地改
data/resp即生效(引擎前后序列化对比,return可省略)。 id/type/type_label/source/gateway_config/req为只读,改了不采纳。- 无改动不重新加密,原字节放行;语法错误只废本条脚本。
- HTTP 通道的路由就在
data.header里(改x-wx-http-path即改真实目标), 宿主负责重新加密,不需要关心 AES / snappy。
纯协议重放怎么选通道(V3 HTTP vs Native jsop)
同一个云网关 / 云托管调用有两条可达通道,客户端 SDK 在 tcbapi_get_service_info 之后自动二选一:
- V3 加密 HTTP:先
get-cloud-gateway-v3-token-key/get-cloud-container-token-key取credential.{token,key},再用wxcloud-gateway-sdk(Python)自行加密发送; - Native 直发(jsop):整个调用包成
tcbapi_call_gateway/tcbapi_call_container/tcbapi_slowcallfunction_v2,走/v1/ilink/js-operate-wx-data(-by-api),全程明文。
选择原则(实测):小程序自己走哪条就优先跟哪条(看捕获记录的 link 字段: V3云网关/Native云网关/HTTP云托管/Native云托管/V3云函数/Native云函数); 一条跑不通(WAF 403 / token 过期 / 挑战失败)再换另一条。Native 直发的完整实测模板 (含 UA 必须与 get_service_info 设备对齐否则网关 WAF 403、operate_directly 鉴权差异、 -103006 参数自检)见 Skill 的 reference/pure-protocol.md 与 examples/cloud-dev-protocol.md。
六类改写示例
以下示例统一使用假环境 prod-example123、假服务 example-service、 假函数 exampleFunc、假域名 api.example.com。
1. V3 HTTP 云网关(cloud_gateway + http)
js
function onRequest(req) {
// 路由在 header 里:改 x-wx-http-path 即改真实目标
req.data.header['x-wx-http-path'] = 'https://api.example.com/mock/ok';
req.data.body = JSON.stringify({ mock: true });
}2. HTTP 云托管(cloud_container + http)
js
function onRequest(req) {
// 改容器路径头;注意不要塞入不属于自己的业务体(服务端会拒绝整条调用)
req.data.header['x-wx-container-path'] = '/api/other';
}3. Native 云托管(cloud_container + native)
js
function onRequest(req) {
var q = req.data.qbase_req;
// 轮询调用(action:2)没有业务体,直接跳过
if (q.action === 2) return;
q.headers.push({ k: 'x-mock', v: '1' });
}4. Native 云网关(cloud_gateway + native)
js
function onRequest(req) {
var q = req.data.qbase_req;
var o = JSON.parse(q.data || '{}');
o.city = '北京';
q.data = JSON.stringify(o);
}5. 云函数(cloud_function)
js
function onRequest(req) {
var q = req.data.qbase_req;
var o = JSON.parse(q.data || '{}');
o.extra = 1;
q.data = JSON.stringify(o);
}
function onResponse(res) {
// 云函数返回体在 resp.data(字符串),直接改
if (typeof res.resp.data === 'string') {
res.resp.data = res.resp.data.replace('"code":0', '"code":0,"touched":true');
}
}6. 其他云开发(cloud_other:gettempfileurl / 数据库等)
js
function onRequest(req) {
if (req.type === 'cloud_other') {
var q = req.data.qbase_req;
q.file_list = JSON.stringify(['cloud://prod-example123.example-file']);
}
}回调样例(真实形态,示例数据)
请求回调(HTTP 云托管):
json
{
"id": "C12",
"type": "cloud_container",
"type_label": "HTTP 云托管",
"source": "http",
"gateway_config": {
"domain": "gw.example.com",
"token": "110_exampleToken",
"key": "ExampleKeyBase64==",
"rid": "1234567890",
"exp": 1795070943
},
"data": {
"method": "GET",
"header": {
"x-wx-service": "example-service",
"x-wx-container-path": "/api/day/nearest",
"x-wx-env": "prod-example123",
"content-type": "application/json"
},
"body": "",
"call_id": "1617246_0.123456"
}
}响应回调(Native 云托管,resp 不含 req):
json
{
"id": "C12",
"type": "cloud_container",
"type_label": "Native 云托管",
"source": "native",
"resp": {
"baseresponse": { "errcode": 0 },
"status": 1,
"http_code": 200,
"headers": { "content-type": "application/json" },
"data": "{\"code\":0,\"msg\":\"success\",\"data\":null}"
}
}查看回调对象
每条云记录详情里都有回调视图:展示脚本/插件实收的 onRequest(req) / onResponse(res) 回调对象原文(响应对象不含 req,避免重复)。写脚本前先看 一眼回调视图,可以确认字段形态再动手。