index.js 2.99 KB
const routerPath = {
  index: '/pages/index/index', // 首页
  authorize: '/pages/authorize/authorize', // 授权
  register: '/pages/register/register', // 注册
  scanResult: '/pages/scan-result/scan-result', // 扫码结果 (含扫码成功和扫码失败)
  integralDetail: '/pages/integral-detail/integral-detail', // 积分明细
  giftShop: '/pages/gift-shop/gift-shop', // 礼品商场
  giftDetail: '/pages/gift-detail/gift-detail', // 礼品详情 (含购物卡和实物)
  addressManagement: '/pages/address-management/address-management', // 地址管理
  addressEdit: '/pages/address-edit/address-edit', // 地址编辑
  myQrcode: '/pages/my-qrcode/my-qrcode', // 我的二维码
  myOrder: '/pages/my-order/my-order', // 我的订单
  myMessage: '/pages/my-message/my-message', // 我的消息
  contact: '/pages/contact/contact', // 联系我们
  contactTable: '/pages/contact-table/contact-table', // 联系我们-提交表单
  vipLogin: '/pages/vip-login/vip-login', // 会员信息验证(填表单)
  vipVerify: '/pages/vip-verify/vip-verify', // 会员信息审核
  userCenter: '/pages/user-center/user-center', // 个人中心 (合并签到页,很提示页)
  signInRecord: '/pages/sign-in-record/sign-in-record', // 签到记录
  rank: '/pages/rank/rank', // 排行榜
  rankGift: '/pages/rank-gift/rank-gift', // 排行榜奖励

  // 拍卖
  auctionList: '/pages/auction-list/auction-list', // 拍卖活动列表
  auctionMine: '/pages/auction-mine/auction-mine', // 我的拍卖订单
  auctionDetail: '/pages/auction-detail/auction-detail', // 拍卖活动详情
  auctionBidDetail: '/pages/auction-bid-detail/auction-bid-detail', // 拍卖出价详情

  webview: '/pages/webview/webview',
  example: '/pages/example/example',
  more: '/pages/more/more',
}

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
}