/** * Bricks 页面入口 */ const { BricksParser } = require('../../utils/parser') const { BricksHttp } = require('../../utils/http') const { BricksRenderer } = require('../../utils/renderer') Page({ data: { tree: null }, onLoad(options) { this.parser = new BricksParser() this.http = new BricksHttp() this.renderer = new BricksRenderer(this) // 从 URL 参数加载 JSON if (options.url) { this.loadFromUrl(decodeURIComponent(options.url)) } else if (options.json) { this.loadFromJson(decodeURIComponent(options.json)) } else { // 加载默认示例 this.loadDefault() } }, async loadFromUrl(url) { try { const json = await this.http.get(url) const widgetTree = this.parser.parse(JSON.stringify(json)) this.renderer.render(widgetTree) } catch (e) { console.error('[Bricks] Load failed:', e) } }, loadFromJson(jsonString) { const widgetTree = this.parser.parse(jsonString) if (widgetTree) this.renderer.render(widgetTree) }, loadDefault() { const defaultJson = JSON.stringify({ widgettype: 'VBox', subwidgets: [ { widgettype: 'Title1', options: { text: '欢迎使用 Bricks' } }, { widgettype: 'Text', options: { text: 'JSON 驱动的跨平台 UI 框架' } }, { widgettype: 'KeyinText', options: { placeholder: '请输入...' } }, { widgettype: 'HBox', subwidgets: [ { widgettype: 'Text', options: { text: '左侧' } }, { widgettype: 'Filler' }, { widgettype: 'Text', options: { text: '右侧' } } ] } ] }) this.loadFromJson(defaultJson) }, onBricksAction(e) { const { actiontype, url, methodname } = e.detail if (actiontype === 'urlwidget' && url) { wx.navigateTo({ url: '/pages/bricks/bricks?url=' + encodeURIComponent(url) }) } }, onBricksTap(e) { if (this.renderer) this.renderer.onEvent(e) }, onInput(e) { if (this.renderer) this.renderer.onInput(e) this.onInputChange(e) }, onInputChange(e) { console.log('[Bricks] Input:', e.detail.value) } })