Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

肖康 / H5_2.0

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
BlameHistoryPermalink
Switch branch/tag
  • H5_2.0
  • node_modules
  • uni-simple-router
  • index.js
  • 肖康's avatar
    first init · ddbafbda
    肖康 committed 2 years ago
    ddbafbda
index.js 4.77 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
import { isH5, formatConfig, appPlatform } from './helpers/util';
import navjump from './helpers/navJump';
import { H5GetPageRoute } from './vueRouter/util';
import { APPGetPageRoute } from './appRouter/util';
import { AppletsPageRoute } from './appletsRouter/util';
import { lifeCycle, Global } from './helpers/config';
import { warn, err } from './helpers/warn';
import { registerRouterHooks, registerHook } from './lifeCycle/hooks';
import { vueMount } from './vueRouter/base';
import appletsMount from './patch/applets-patch';
import appMount from './patch/app-patch';
import initMixins from './helpers/mixins';
import ParseQuery from './helpers/urlQuery';
// #ifdef H5
import H5 from './patch/h5-patch';
// #endif

let H5PATCH = null;
// #ifdef H5
H5PATCH = new H5(isH5());
// #endif

const parseQuery = new ParseQuery();

Global.H5RouterReady = new Promise((resolve) => Global.RouterReadyPromise = resolve);

class Router {
    constructor(arg) {
        Router.$root = this;
        Global.Router = this; // 全局缓存一个对象,不必使用时都传递
        Global.$parseQuery = parseQuery;
        this.CONFIG = formatConfig(arg);
        this.lifeCycle = lifeCycle;
        registerRouterHooks.call(this);	// 注册全局Router生命钩子
        if (appPlatform() === 'H5') {
            H5PATCH.setLoadingStatus(this.CONFIG.h5);
        }
    }

    get $Route() {
        return this.getPageRoute();
    }

    /**
     * 获取 url 参数帮助类实例
     */
    get $parseQuery() {
        return Global.$parseQuery;
    }

    /**
     * 获取当前是否处于正在跳转的状态
     * H5 状态下始终为false
     */
    get $lockStatus() {
        return Global.LockStatus;
    }

    /**
     * 动态设置拦截状态
     */
    set $lockStatus(status) {
        warn('你确定要这么做?你知道后果?', true);
        Global.LockStatus = status;
    }

    /** 动态的导航到一个新 URL 保留浏览历史
	 * navigateTo
	 * @param {Object} rule
	 */
    push(rule) {
        navjump.call(this, rule, 'push');
    }

    /** 动态的导航到一个新 URL 关闭当前页面,跳转到的某个页面。
	 * redirectTo
	 * @param {Object} rule
	 */
    replace(rule) {
        navjump.call(this, rule, 'replace');
    }

    /** 动态的导航到一个新 URL 关闭所有页面,打开到应用内的某个页面
	 * 	reLaunch
	 * @param {Object} rule
	 */
    replaceAll(rule) {
        navjump.call(this, rule, 'replaceAll');
    }

    /** 动态的导航到一个新 url 关闭所有页面,打开到应用内的某个tab
	 * @param {Object} rule
	 */
    pushTab(rule) {
        navjump.call(this, rule, 'pushTab');
    }

    /**
	 * 返回到指定层级页面上
     * @param {Number} backLayer 需要返回的页面层级 默认 1
     * @param {Object} delta 暂时无用
     * @param {enforce} 是否强制越过跳转加锁检查 默认 false
	 */
    back(backLayer = 1, delta, enforce = false) {
        if (backLayer.constructor != Number) {
            return err(
                `返回层级参数必须是一个Number类型且必须大于1:${backLayer}`,
            );
        }
        navjump.call(this, {
            backLayer, delta, H5PATCH,
        }, 'back', true, enforce);
    }

    /**
	 * 获取当前页面下的 Route 信息
	 *
	 * @param {Object} Vim 当前开发者可以传递一个 vue 组件对象 来获取当前下的 Route 信息
	 */
    getPageRoute(Vim) {
        const pages = getCurrentPages();
        switch (appPlatform(true)) {
        case 'H5':
            return H5GetPageRoute.call(this, pages, Vim);
        case 'APP':
            return APPGetPageRoute(pages, Vim);
        case 'APPLETS':
            return AppletsPageRoute(pages, Vim);
        default:
            break;
        }
    }

    beforeEach(fn) {
        return registerHook(this.lifeCycle.beforeHooks, fn);
    }

    afterEach(fn) {
        return registerHook(this.lifeCycle.afterHooks, fn);
    }
}

Router.install = function (Vue) {
    initMixins(Vue, Router);
    Object.defineProperty(Vue.prototype, '$Router', {
        get() {
            return Router.$root;
        },
    });
    Object.defineProperty(Vue.prototype, '$Route', {
        get() {
            return Router.$root.getPageRoute(this);
        },
    });
};
export default Router;
/**
 *
 * @param {VueComponent } Vim vue实例对象
 * @param {dom} el	dom节点选择器
 */
export const RouterMount = function (Vim, el) {
    switch (appPlatform(true)) {
    case 'APP':
        appMount(Vim, el);
        break;
    case 'APPLETS':
        appletsMount(Vim, el);
        break;
    case 'H5':
        vueMount.push({ Vim, el });
        break;
    default:
        warn('糟糕!!!还有其他的执行环境???没听说过啊。一脸懵逼???加QQ群问问:769241495');
        break;
    }
};