123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- import { uniAppHook, Global } from '../helpers/config';
- import {
- callAppHook, getPageVmOrMp, ruleToUniNavInfo, formatTo, formatFrom, getPages,
- } from './util';
- import appletsUniPushTo from './appletsNav';
- import { noop } from '../helpers/util';
- import { warn } from '../helpers/warn';
- const toutiaoIndexHookCall = function (key, hook) {
- const { indexVue } = uniAppHook;
- const indeHooks = indexVue[key];
- indeHooks.splice(indeHooks.length - 1, 1, hook);
- };
- const callwaitHooks = function (callHome) {
- return new Promise(async (resolve) => {
- const variation = [];
- const {
- appVue, onLaunch, onShow, waitHooks, variationFuns, indexCallHooks,
- } = uniAppHook;
- const app = appVue.$options;
- await onLaunch.fun[onLaunch.fun.length - 1].call(appVue, onLaunch.args);
- onShow.fun[onShow.fun.length - 1].call(appVue, onShow.args);
- if (callHome) {
-
- for (const key in waitHooks) {
- if (indexCallHooks.includes(key)) {
- callAppHook.call(this, waitHooks[key].fun);
- }
- }
- }
- if (onLaunch.isHijack) {
- app.onLaunch.splice(app.onLaunch.length - 1, 1, onLaunch.fun[0]);
- }
- if (onShow.isHijack) {
- app.onShow.splice(app.onShow.length - 1, 1, onShow.fun[0]);
- }
-
- for (const key in waitHooks) {
- const item = waitHooks[key];
- if (item.isHijack) {
- if (variationFuns.includes(key)) {
- variation.push({ key, fun: item.fun[0] });
- } else {
- toutiaoIndexHookCall(key, item.fun[0]);
- }
- }
- }
- resolve(variation);
- });
- };
- const callVariationHooks = function (variation) {
- for (let i = 0; i < variation.length; i += 1) {
- const { key, fun } = variation[i];
- toutiaoIndexHookCall(key, fun);
- }
- };
- export const proxyLaunchHook = function () {
- const {
- onLaunch,
- onShow,
- } = this.$options;
- uniAppHook.appVue = this;
- if (onLaunch.length > 1) {
- uniAppHook.onLaunch.isHijack = true;
- uniAppHook.onLaunch.fun = onLaunch.splice(onLaunch.length - 1, 1, (arg) => {
- uniAppHook.onLaunch.args = arg;
- });
- }
- if (onShow.length > 0) {
- uniAppHook.onShow.isHijack = true;
- uniAppHook.onShow.fun = onShow.splice(onShow.length - 1, 1, (arg) => {
- uniAppHook.onShow.args = arg;
- if (uniAppHook.pageReady) {
- callAppHook.call(this, uniAppHook.onShow.fun, arg);
- }
- });
- }
- };
- const beforeHooks = function (_from, _to) {
- return new Promise(async (resolve) => {
- const beforeHooksFun = this.lifeCycle.beforeHooks[0];
- if (beforeHooksFun == null) {
- return resolve();
- }
- await beforeHooksFun.call(this, _to, _from, resolve);
- });
- };
- const afterEachHooks = function (_from, _to) {
- const afterHooks = this.lifeCycle.afterHooks[0];
- if (afterHooks != null && afterHooks.constructor === Function) {
- afterHooks.call(this, _to, _from);
- }
- };
- const beforeEnterHooks = function (finalRoute, _from, _to) {
- return new Promise(async (resolve) => {
- const { beforeEnter } = finalRoute.route;
- if (beforeEnter == null || beforeEnter.constructor !== Function) {
- return resolve();
- }
- await beforeEnter.call(this, _to, _from, resolve);
- });
- };
- const beforeRouteLeaveHooks = function (from, to, leaveHook) {
- return new Promise(async (resolve) => {
- if (leaveHook) {
- warn('beforeRouteLeave next到其他地方,无须再执行!');
- return resolve();
- }
- if (from.path == to.path) {
- return resolve();
- }
- const currentPage = getPages(-2);
- const callThis = getPageVmOrMp(currentPage);
- const { beforeRouteLeave } = callThis.$options;
- if (beforeRouteLeave == null) {
- warn('当前页面下无 beforeRouteLeave 钩子声明,无须执行!');
- return resolve();
- }
- if (beforeRouteLeave != null && beforeRouteLeave.constructor !== Function) {
- warn('beforeRouteLeave 生命钩子声明错误,必须是一个函数!');
- return resolve();
- }
- await beforeRouteLeave.call(callThis, to, from, resolve);
- });
- };
- export const appletsTransitionTo = async function (rule, fnType, navCB, leaveHook = false) {
- await this.lifeCycle.routerbeforeHooks[0].call(this);
- const finalRoute = ruleToUniNavInfo(rule, this.CONFIG.routes);
- const _from = formatFrom(this.CONFIG.routes);
- const _to = formatTo(finalRoute);
- try {
- const leaveResult = await beforeRouteLeaveHooks.call(this, _from, _to, leaveHook);
-
- await isNext.call(this, leaveResult, fnType, navCB,true);
- const beforeResult = await beforeHooks.call(this, _from, _to);
-
- await isNext.call(this, beforeResult, fnType, navCB);
- const enterResult = await beforeEnterHooks.call(this, finalRoute, _from, _to);
-
- await isNext.call(this, enterResult, fnType, navCB);
- } catch (e) {
- warn(e);
- return false;
- }
- if (navCB) {
- navCB.call(this, finalRoute, fnType);
- }
- afterEachHooks.call(this, _from, _to);
- await this.lifeCycle.routerAfterHooks[0].call(this);
- };
- export const backCallHook = function (backLayer, next) {
- const pages = getPages();
- const toPage = pages.reverse()[backLayer];
- if (toPage == null) {
- return warn('亲爱的开发者,你确定页面栈中有这么多历史记录给你返回?');
- }
- const { query, page } = getPageVmOrMp(toPage, false);
- const beforeFntype = 'RouterBack';
- appletsTransitionTo.call(this, { path: page.route, query }, beforeFntype, (finalRoute, fnType) => {
- const toPath = finalRoute.uniRoute.url;
- if (`/${page.route}` == toPath || page.route == toPath) {
- next();
- } else {
- if (fnType == beforeFntype) {
- return warn('调用返回api被拦截到其他页面需要指定合理的 ‘NAVTYPE’ ');
- }
- appletsUniPushTo(finalRoute, fnType);
- }
- });
- };
- export const triggerLifeCycle = function (Router) {
- const topPage = getCurrentPages()[0];
- if (topPage == null) {
- return warn('打扰了,当前一个页面也没有 这不是官方的bug是什么??');
- }
- const { query, page } = getPageVmOrMp(topPage, false);
- appletsTransitionTo.call(Router, { path: page.route, query }, 'push', async (finalRoute, fnType) => {
- let variation = [];
- if (`/${page.route}` == finalRoute.route.path || page.route == finalRoute.route.path) {
- uniAppHook.pageReady = true;
- await callwaitHooks.call(this, true);
- } else {
- variation = await callwaitHooks.call(this, false);
- await appletsUniPushTo(finalRoute, fnType);
- }
- uniAppHook.pageReady = true;
- callVariationHooks(variation);
- });
- };
- export const appletsProxyIndexHook = function (Router) {
- if (process.env.VUE_APP_PLATFORM == 'mp-toutiao') {
- uniAppHook.toutiaoIndexThis = this;
- }
- const { needHooks, waitHooks } = uniAppHook;
- const options = this.$options;
- uniAppHook.indexVue = options;
- for (let i = 0; i < needHooks.length; i += 1) {
- const key = needHooks[i];
- if (options[key] != null) {
- const { length } = options[key];
-
- const whObject= waitHooks[key]={};
- whObject.fun = options[key].splice(length - 1, 1, noop);
- whObject.isHijack = true;
- }
- }
- triggerLifeCycle.call(this, Router);
- };
- const isNext = function (Intercept, fnType, navCB, leaveHookCall = false) {
- return new Promise((resolve, reject) => {
- if (Intercept == null) {
- return resolve();
- }
- if (Intercept === false) {
- Global.LockStatus = false;
- return reject('路由终止');
- }
- if (Intercept.constructor === String) {
- reject('next到其他页面');
- return appletsTransitionTo.call(this, Intercept, fnType, navCB, leaveHookCall);
- }
- if (Intercept.constructor === Object) {
- reject('next到其他页面');
- return appletsTransitionTo.call(this, Intercept, Intercept.NAVTYPE || fnType, navCB, leaveHookCall);
- }
- });
- };
|