evaluate_script
Execute JavaScript in a WeChat mini-program's AppService context to access wx APIs, getApp(), and getCurrentPages(), returning JSON-serializable results.
Instructions
在小程序 AppService 上下文中执行 JavaScript 代码并返回结果。 支持访问 wx API、getApp()、getCurrentPages() 等小程序全局对象。 返回值必须是 JSON 可序列化的类型。
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| args | No | 可选的参数数组,传递给函数执行。 参数必须是 JSON 可序列化的类型(字符串、数字、布尔值、对象、数组等)。 示例: - 单个参数: ["testKey"] - 多个参数: ["key", 123, { foo: "bar" }] - 复杂对象: [{ name: "test", data: [1, 2, 3] }] | |
| function | Yes | JavaScript 函数声明,将在小程序 AppService 上下文中执行。 支持同步和异步函数,可访问 wx API 和 getApp()。 注意:函数会被序列化传递,无法使用闭包引用外部变量。 无参数示例: `() => { return wx.getSystemInfoSync(); }` 或使用字符串形式: `"() => wx.getSystemInfoSync()"` 异步示例: `async () => { return new Promise(resolve => { wx.getSystemInfo({ success: result => resolve(result) }); }); }` 带参数示例: `(key, value) => { wx.setStorageSync(key, value); return { success: true }; }` 访问全局数据示例: `() => { const app = getApp(); return app.globalData; }` 访问当前页面示例: `() => { const pages = getCurrentPages(); const currentPage = pages[pages.length - 1]; return currentPage.data; }` |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| ok | Yes | ||
| code | Yes | ||
| data | Yes | ||
| meta | Yes | ||
| error | Yes | ||
| warnings | Yes | ||
| nextActions | Yes | ||
| observation | Yes | ||
| partialData | Yes | ||
| schemaVersion | Yes |