index.js 2.28 KB
const routerPath = {
  index: '/pages/index/index', // 首页
  wish: '/pages/wish/wish', // 我的心愿单
  createWish: '/pages/create-wish/create-wish', // 创建心愿单
  newWelfare: '/pages/new-welfare/new-welfare', // 新人福利
  userTable: '/pages/user-table/user-table', // 用户表单
  rank: '/pages/rank/rank', // 排行榜
  poster: '/pages/poster/poster', // 海报页
  coop: '/pages/coop/coop', // 协作页/好友查看
  register: '/pages/register/register', // 注册
  authorize: '/pages/authorize/authorize', // 授权
  example: '/pages/example/example',
  more: '/pages/more/more',

  // 组件 蒙层/提示
  // rule 规则
  // tips 会员登陆/注册提示
  // tips 重复登陆提示
  // tips 创建心愿单提示
  // tips new-question-tips 新人答题提示
  // tips shake 摇手机提示
  // tips award-old 老会员获得积分提示
  // tips award-new 老会员获得积分提示
  // tips award-coupon 领取代金券
  // tips new-award-coupon 新人答题领取代金券

  // comp nearby-store 附近门店查询
}

function parse(data) {
  let tempArr = [];
  for (let key in data) {
    tempArr.push(key + '=' + encodeURIComponent(data[key]));
  }
  return tempArr.join('&');
}

function push(path, option = {}) {
  if (typeof path == 'string') {
    option.path = path; //兼容无参数路径
  } else {
    option = path;
  }
  // console.log("option:", option);
  // 配置key值找到对应path
  let url = routerPath[option.path] || routerPath['index'];
  // console.log("url:", url);
  // 读取传入的配置参数
  let {
    query = {}, openType = 'navigate', duration = 0
  } = option;
  // json 转换为 字符串拼接参数
  let params = parse(query)
  // console.log("params:", params);
  if (params) {
    url = url + '?' + params;
  }
  // 是否需要延时跳转
  duration ? setTimeout(() => {
    to(openType, url);
  }, duration) : to(openType, url);
}

function to(openType, url) {
  let obj = {
    url
  };

  if (openType == 'redirect') {
    wx.redirectTo(obj);
  } else if (openType == 'reLaunch') {
    wx.reLaunch(obj);
  } else if (openType == 'switchTab') {
    wx.switchTab(obj);
  } else if (openType == 'back') {
    wx.navigateBack({
      delta: 1
    });
  } else {
    wx.navigateTo(obj);
  }
}

module.exports = {
  parse,
  push,
  to
}