From 6128711a4e7f0f1a2ea4780b05c8c6b3f53eb056 Mon Sep 17 00:00:00 2001 From: ‘chensiAb’ <‘chenchenco03@163.com’> Date: Sat, 12 Apr 2025 11:18:33 +0800 Subject: [PATCH] style:自定义导航条-宽度调宽 --- bases/Pilot.js | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 107 insertions(+), 10 deletions(-) diff --git a/bases/Pilot.js b/bases/Pilot.js index babc9de..80de29a 100644 --- a/bases/Pilot.js +++ b/bases/Pilot.js @@ -3,14 +3,12 @@ * @author Tevin */ -import { Fetcher } from './Fetcher'; -import { Tools } from '@components/common/Tools'; +import Taro, { getCurrentInstance, getCurrentPages } from '@tarojs/taro'; +import { $hostBoot } from '@components/bases/HostBoot'; import project from '@project'; export class Pilot { - - constructor() { - } + constructor() {} $data() { return {}; @@ -21,10 +19,15 @@ methods: {}, }; const names = []; + let parent = null; // 实例本身的字段 Object.getOwnPropertyNames(this).forEach(name => names.push(name)); - // 类的字段 - Object.getOwnPropertyNames(Object.getPrototypeOf(this)).forEach(name => names.push(name)); + // 基类的字段,直到Pilot为止(不含Pilot) + parent = Object.getPrototypeOf(this); + while (parent instanceof Pilot) { + Object.getOwnPropertyNames(parent).forEach(name => names.push(name)); + parent = Object.getPrototypeOf(parent); + } // 传递 names.forEach(name => { // 构造器忽略,如果存在属性 $methods 也忽略 @@ -52,7 +55,102 @@ options.methods[name] = this[name]; } }); + this._bindTaroPage(options); return options; + } + + _bindTaroPage(options) { + // 绑定页面实例到Page + const { created, beforeDestroy, onMessage, onBridge } = options; + const option2 = { + created() { + const instance = getCurrentInstance(); + instance.page.$component = this; + // 跨页消息发送 + this.$poster = Pilot.createPoster(this); + // 跨页接收消息 + this.$onMessage = onMessage || (() => null); + // 跨端接收消息 + this.$onBridge = onBridge || (() => null); + // 实际生命周期回调 + created && created.call(this); + }, + beforeDestroy() { + const instance = getCurrentInstance(); + delete instance.page.$component; + // 实际生命周期回调 + beforeDestroy && beforeDestroy.call(this); + // 取消绑定 + this.$onMessage = null; + this.$poster = null; + this.$onBridge = null; + }, + }; + options.created = option2.created; + options.beforeDestroy = option2.beforeDestroy; + delete options.onMessage; + delete options.poster; + delete options.onBridge; + } + + // 创建页面通讯器 + static createPoster($vm) { + return (pageType, action, data) => { + // 解除原引用关系、移除非数据内容 + const nextData = JSON.parse(JSON.stringify(data || {})); + // 传递消息到下一页 + if (pageType === 'nextPage') { + // 持续 3 秒,检测下一页是否存在,如果 3 秒内未创建下一页,忽略 + let count = 0; + const timer = setInterval(() => { + if (++count > 60) { + clearInterval(timer); + } + const currentPages = getCurrentPages(); + const pageIndex = currentPages.findIndex(page => page.$component === $vm); + const nextPage = currentPages[pageIndex + 1]; + if (nextPage) { + clearInterval(timer); + setTimeout(() => { + nextPage.$component?.$onMessage(action, nextData); + }, 0); + } + }, 50); + } + // 传递消息到上一页,上一页的上一页,依次直到首页 + else if (pageType === 'prevPage') { + const currentPages = getCurrentPages(); + let pageIndex = currentPages.findIndex(page => page.$component === $vm); + while (pageIndex - 1 >= 0) { + const prevPage = currentPages[pageIndex - 1]; + if (prevPage) { + prevPage.$component?.$onMessage(action, nextData); + } + pageIndex--; + } + } + // 传递消息到原地新页(redirectTo原地新开页面) + else if (pageType === 'sitePage') { + const currentPages = getCurrentPages(); + const pageIndex = currentPages.findIndex(page => page.$component === $vm); + const pagePath = currentPages[pageIndex].$taroPath + // 持续 3 秒,检查当前页是否新开,如果 3 秒内没有新开,忽略 + let count = 0; + const timer = setInterval(() => { + if (++count > 60) { + clearInterval(timer); + } + const currentPages = getCurrentPages(); + const sitePage = currentPages[pageIndex]; + if (sitePage.$component !== $vm || sitePage.$taroPath !== pagePath) { + clearInterval(timer); + setTimeout(() => { + sitePage.$component?.$onMessage(action, nextData); + }, 0); + } + }, 50); + } + }; } /** @@ -75,10 +173,9 @@ } // 小程序 else if (process.env.TARO_ENV === 'weapp') { - assets2[key] = Fetcher.host + project.host.assetsPath + asset; + assets2[key] = $hostBoot.getHost() + project.host.assetsPath + asset; } }); return assets2; } - -} \ No newline at end of file +} -- Gitblit v1.9.1