38 lines
1.0 KiB
JavaScript

App({
globalData: {
baseUrl: '',
authToken: '',
bricks: null
},
onLaunch() {
// 加载 bricks 解析引擎
const { BricksParser } = require('./utils/parser')
this.globalData.bricks = new BricksParser()
},
// 工具函数: 对应 JS 版 bricks.extend
extend(target, source) {
for (let key in source) {
if (source.hasOwnProperty(key)) {
if (typeof source[key] === 'object' && source[key] !== null && !Array.isArray(source[key])) {
target[key] = this.extend(target[key] || {}, source[key])
} else {
target[key] = source[key]
}
}
}
return target
},
// 模板变量替换: 对应 bricks.obj_fmtstr
fmt(str, data) {
return str.replace(/\{\{([^}]+)\}\}/g, (match, key) => {
return data[key] !== undefined ? data[key] : match
})
},
// entire_url: 拼接 baseUrl
entireUrl(path) {
if (/^https?:\/\//.test(path)) return path
const base = this.globalData.baseUrl || ''
return base.replace(/\/$/, '') + '/' + path.replace(/^\//, '')
}
})