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
  • appletsRouter
  • util.js
  • 肖康's avatar
    first init · ddbafbda
    肖康 committed 2 years ago
    ddbafbda
util.js 6.1 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
import { Global, route as mergeRoute } from '../helpers/config';
import { copyObject, parseQuery } from '../helpers/util';
import { err } from '../helpers/warn';
import { baiduApple, touTiao } from '../helpers/compile';
/**
 * 触发指定生命钩子
 * @param {Array} funList	//需要执行的方法列表
 * @param {Object} args //触发生命钩子传递的参数
 */
export const callAppHook = function (funList, args) {
    for (let i = 0; i < funList.length; i += 1) {
        funList[i].call(this, args);
    }
};
/**
 * @param {Object} page //当前顶级页面对象
 * @param {Object} vim:? //是否获取 $vm 对象还是 $mp 对象
 */
export const getPageVmOrMp = function (page, vim = true) {
    if (vim) {
        return page.$vm;
    }
    const { $mp } = page.$vm;
    baiduApple(() => {	// 百度小程序新增一个route属性
        $mp.page.route = $mp.page.is;
    });
    touTiao(() => {	// 头条小程序新增一个route属性
        $mp.page.route = $mp.page.is;
    });
    return $mp;
};
/**
 * 统一格式话 路由传递的参数 看看是编码还是非编码 做相应的对策
 *
 * @param {Object} query 当前的路由参数
 * @param {Boolean} getter 是从页面获取 route 对象下的参数 还是编码后传输
 */
export const getFormatQuery = function (query = {}, getter = false) {
    if (Global.Router.CONFIG.encodeURI) {
        if (getter) {
            try {		// 除去微信小程序都不需要 decodeURIComponent
                query = JSON.parse(decodeURIComponent(query.query) || '{}');
            } catch (e) {	// 其他小程序
                query = JSON.parse(query.query || '{}');
            }
        } else {
            try {
                query = JSON.parse(decodeURIComponent(query.query || encodeURIComponent('{}')));
            } catch (e) {
                query = JSON.parse(query.query);
            }
        }
    }
    return query;
};
/**
 * @param {Number} index //需要获取的页面下标 -2:表示获取最后一个即当前页面 -1:表示全部 -3:当前页面的前一个页面
 * @param {Boolean} all //是否获取全部的页面
 */
export const getPages = function (index = -1, all) {
    const pages = getCurrentPages(all);
    if (index === -1) {
        return pages;
    }
    if (index === -2) {
        return pages[pages.length - 1];
    }
    if (index === -3) {
        return pages[pages.length - 2];
    }
    return pages[index];
};
/**
 * 通过一个未知的路径或者名称 在路由表中查找指定路由表 并返回
 * @param {string} type   //path 或者 name
 * @param {Object} routes //当前对象的所有路由表
 */
export const pathOrNameToRoute = function (type, routes = Global.Router.CONFIG.routes) {
    const routesKeys = Object.keys(routes);
    for (let i = 0; i < routesKeys.length; i += 1) {
        const key = routesKeys[i];
        const item = routes[key];
        if (item.path === `/${type}`) {
            return mergeRoute(item); // 合并一下对象,主要是合并 query:{} 及 params:{}
        }
        if (item.path === type) {
            return mergeRoute(item); // 合并一下对象,主要是合并 query:{} 及 params:{}
        }
        if (item.name == type) {
            return mergeRoute(item); // 合并一下对象,主要是合并 query:{} 及 params:{}
        }
    }
    err(`当前 '${type}' 在路由表中没有找到匹配的 name 或者 path`);
};

/**
 * 获取 to 的配置参数
 * @param {Object} rule 当前跳转的规则
 */
export const formatTo = function (finalRoute) {
    const route = copyObject(finalRoute.route);
    const { rule } = finalRoute;
    route.query = rule.query || rule.params || {};
    return route;
};

/**
 * 获取 from 的配置参数 from 页面永远都是站在当前页面忘其它地方走 所以都是最后一个页面
 *
 * @param {Object} routes //当前对象的所有路由表
 */
export const formatFrom = function (routes) {
    const topPage = getPages(-2);
    const { page, query } = getPageVmOrMp(topPage, false);
    const route = pathOrNameToRoute(page.route, routes);	// 获取到当前路由表下的 route
    route.query = getFormatQuery(query);	// 不管是编码传输还是非编码 最后都得在 to/from 中换成json对象
    return route;
};

/**
 *
 * 把用户的跳转路由规则格式化成uni-app可用的路由跳转规则
 *
 * @param {Object} rule  //当前用户跳转的路由规则
 * @param {Object} routes //当前simple-router 下的路由表
 */
export const ruleToUniNavInfo = function (rule, routes) {
    if (rule == null) {
        return err('当前跳转规则为空,请检查跳转代码');
    }
    // eslint-disable-next-line
    let [navType, route, query] = ['path', null, {}];
    if (rule.constructor === String) {		// 是字符串类型 那当前就是路径啦
        route = pathOrNameToRoute(rule, routes);	// 直接把 rule 当 path 传递 完事
    } else if (rule.constructor === Object) {		// 对象类型 可以是 path 或者 name
        route = pathOrNameToRoute(rule.path || (navType = 'name', rule.name), routes);	// 两则必有其一 报错自己处理
        query = rule.query || rule.params || {};
    } else {
        return err('传的什么乱七八糟的类型?路由跳转规则只认字符串 \'path\' , 对象 \'path\' , 对象 \'name\' ');
    }
    // 路径处理完后   开始格式化参数
    const uniRoute = parseQuery(route.path, query);	// uni-app 需要的跳转规则
    return {
        rule,
        route,
        uniRoute,
    };
};
/**
 * 获取当前页面下的 Route 信息
 *
 * @param {Object} pages 获取页面对象集合
 * @param {Object} Vim 用户传递的当前页面对象
 */
export const AppletsPageRoute = function (pages, Vim) {
    let [query, path] = [{}, ''];
    const page = pages[pages.length - 1];	// 获取到当前页面
    if (pages.length > 0) {
        const uniQuery = getPageVmOrMp(page, false).query;
        query = getFormatQuery(uniQuery, true);
        path = page.route;
    } else if (Vim != null) {
        query = getFormatQuery(Vim.$mp.page.options, true);
        path = page.route;
    }
    const route = pathOrNameToRoute(path);
    route.query = query;
    return route;
};