jingshuiji/utils/inject-static.js
liluo293254 11c5647ae5 feat: 统一环境配置并移除本地测试登录入口
新增 config/env.js 管理 local/test/prod 网关,清理调试登录与页面联调相关改动。
2026-08-07 08:47:52 +08:00

42 lines
1.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 仅包装 Page向页面 data 注入 staticUrl供 wxml {{staticUrl}}
* 禁止包装 Component会破坏 Vant 等第三方组件注册,表现为 wx://not-found / 白屏
*/
const env = require('../config/env.js')
function mergeStaticData(options) {
if (!options) options = {}
const data = options.data || {}
if (typeof data === 'function') {
const orig = data
options.data = function () {
const d = orig.call(this) || {}
if (d.staticUrl == null) d.staticUrl = env.static_url
return d
}
} else {
if (data.staticUrl == null) data.staticUrl = env.static_url
options.data = data
}
return options
}
function install() {
if (typeof Page !== 'function' || Page.__staticUrlPatched) return
const rawPage = Page
function PatchedPage(options) {
return rawPage(mergeStaticData(options))
}
PatchedPage.__staticUrlPatched = true
// 保留原生静态属性
try {
Object.keys(rawPage).forEach(function (k) {
try { PatchedPage[k] = rawPage[k] } catch (e) { /* ignore */ }
})
} catch (e) { /* ignore */ }
Page = PatchedPage
}
module.exports = { install, static_url: env.static_url }
install()