通过插件 · 云开发脚本
插件通过 wad.cloud.intercept.* 改写云开发请求/响应(原地改 data / resp.resp), 或 wad.cloud.observe.* 只读观察。两者收到的是同一回调视图。
也可以在云开发调试页写云脚本完成改写(见 免插件 · 云开发脚本)。
事件形态
observe.request(fn) / observe.response(fn) 收到的对象与云脚本实收的 req / res 完全同形(同一构建器):
js
// observe.request 事件
{
id: 'C12',
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: { domain: 'gw.example.com', token: '110_example',
key: '…', rid: '…', exp: 1795070943 }, // 仅 HTTP 云网关/云托管
data: { // 内层原始 JSON 对象
method: 'GET',
header: { 'x-wx-http-path': 'https://api.example.com/…', … },
body: '',
call_id: '…'
},
appid: 'wx1234567890abcdef',
at: 1795070943000
}
// observe.response 事件额外携带
{
req: { …请求回调对象… }, // 只读
resp: { statusCode: 200, header: {…}, body: '{…}' } // native 为 { baseresponse, status, http_code, headers, data }
}订阅示例
js
// 逻辑层:观察所有云网关请求(只读)
wad.cloud.observe.request(function (ev) {
if (ev.type !== 'cloud_gateway') return;
wad.log('[cloud] ' + ev.type_label + ' ' + ev.data.method + ' ' +
(ev.data.header['x-wx-http-path'] || ''));
});
// 逻辑层:观察云函数响应
wad.cloud.observe.response(function (ev) {
if (ev.type !== 'cloud_function') return;
wad.log('[cloud fn] ' + ev.data.qbase_api_name + ' → ' + ev.resp.data);
});配合云脚本做联动
插件与云脚本都可以改写;两者可以用 wad.storage 联动:
js
// 插件侧:把要 mock 的路径写进共享存储
wad.cloud.observe.request(function (ev) {
if (ev.data.header && ev.data.header['x-wx-mock'] === '1') {
wad.storage.set('mockPath', ev.data.header['x-wx-container-path'] || '');
}
});js
// 云开发脚本侧:读共享存储决定是否改写
function onRequest(req) {
var p = wad.storage.get('mockPath');
if (p && req.data.header && req.data.header['x-wx-container-path'] === p) {
req.data.body = '{"mocked":true}';
}
}边界
| 通过插件 · 云开发回调 | 免插件 · 云开发脚本 | |
|---|---|---|
| 入口 | 插件 wad.cloud.observe | 云开发调试页 → 脚本 |
| 改写 | 只读(改了不生效) | ✅ 改 data / resp 即生效 |
| 分发 | 随插件安装/启用 | App 内脚本管理 |
| 适用 | 观察、联动、统计 | 临时调试、个人规则 |
旧版
wad.cloud.intercept(插件改写)已移除:调用不会报错,但不会触发任何改写。 请迁移到云开发脚本。