| | |
| | | * @author Tevin |
| | | */ |
| | | |
| | | import Taro, { getCurrentInstance } from '@tarojs/taro'; |
| | | import { Fetcher } from './Fetcher'; |
| | | import { Tools } from '@components/common/Tools'; |
| | | import project from '@project'; |
| | |
| | | if (name === '$data' && typeof dataAdd !== 'undefined') { |
| | | // 转换 dataAdd 中 assets 属性下的图片地址值 |
| | | if (typeof dataAdd.assets !== 'undefined') { |
| | | dataAdd.assets = this._transDataAdd(dataAdd.assets); |
| | | dataAdd.assets = Pilot.transAssets(dataAdd.assets); |
| | | } |
| | | // 当有传data值进来,初始值必须带data中的字段 |
| | | const dataOrig = this.$data(); |
| | | options.data = () => { |
| | | // 当有传data值进来,初始值必须带data中的字段 |
| | | const dataOrig = this.$data(); |
| | | return { |
| | | ...dataOrig, |
| | | ...dataAdd, |
| | |
| | | options.methods[name] = this[name]; |
| | | } |
| | | }); |
| | | this._bindTaroPage(options); |
| | | return options; |
| | | } |
| | | |
| | | _transDataAdd(assets = {}) { |
| | | _bindTaroPage(options) { |
| | | // 非App内嵌模式,不执行 |
| | | if (!project.appHybrid) { |
| | | return; |
| | | } |
| | | // 绑定页面实例到Page |
| | | const { onLoad, onUnload, onBridge } = options; |
| | | const option2 = { |
| | | onLoad() { |
| | | const instance = getCurrentInstance(); |
| | | instance.page.$component = this; |
| | | onLoad && onLoad.call(this); |
| | | // 绑定 onBridge 到页面实例 |
| | | this.$onBridge = onBridge || (() => { |
| | | }); |
| | | }, |
| | | onUnload() { |
| | | const instance = getCurrentInstance(); |
| | | delete instance.page.$component; |
| | | onUnload && onUnload.call(this); |
| | | }, |
| | | }; |
| | | options.onLoad = option2.onLoad; |
| | | options.onUnload = option2.onUnload; |
| | | delete options.onBridge; |
| | | } |
| | | |
| | | /** |
| | | * 转换静态图片引用 |
| | | * @param assets |
| | | * @return {{}} |
| | | */ |
| | | static transAssets(assets = {}) { |
| | | const assets2 = {}; |
| | | Object.keys(assets).forEach(key => { |
| | | let asset = ''; |
| | | if (assets[key].indexOf('assets') > 0) { |
| | | if (assets[key].indexOf('assets') >= 0) { |
| | | asset = assets[key].split('assets')[1]; |
| | | } else { |
| | | asset = assets[key].replace(/^[.\/\\]*/, '/'); |