index.js 2.72 KB
import rules from './rules';
import router from './router'
const urlParse = require('url');
const queryString = require('querystring');
import Taro from '@tarojs/taro'

const ACTION_TYPE = {
  h5: 'go.h5',
  ufo: 'go.ufo'
}

const OPEN_BY_TYPE = {
  GO_LIST: 'productList',
  GO_ORDER_LIST: 'orderList',
  GO_DETAIL: 'productDetail',
  GO_ORDER_DETAIL: 'BuyerOrderDetail',
  GO_HOME: 'home',
  GO_YOLUCK: 'yoluck',
};

export default {
  go(name, query, jumpType) {
    const jump = jumpType ? jumpType : 'navigateTo';
    const rule = rules[name];
    console.log(name);
    if (!rule) {
      return Promise.reject(`路由规则未匹配到: ${name}`);
    }
    query = query || {};

    const routerFn = router[jumpType] || router.navigateTo;
    return routerFn({
      url: rule.path,
      query
    });
  },
  goUrl(url) {
    const urlObj = urlParse.parse(url); // 使用 node 的 url 库
    const queryObj = queryString.parse(urlObj.query); // 使用 node 的 querystring
    let yohobuy = {};
    let yoho = {};
    if (queryObj['openby:yohobuy']) {
      yoho = JSON.parse(queryObj['openby:yohobuy']);
    }
    switch(yoho.action) {
      case ACTION_TYPE.h5: {
        yohobuy = {
          ...yoho.params.param,
          url: yoho.params.url,
          action: yoho.action
        }
        this.go('webview', yohobuy);
        break;
      }
      case ACTION_TYPE.ufo: {
        this.handleUFOJumpPage(yoho);
        break;
      }
    }
  },
  handleUFOJumpPage(yoho) {
    const params = yoho.params
    switch(params.pagename) {
      case OPEN_BY_TYPE.GO_LIST: {
        console.log(yoho);
        this.go(OPEN_BY_TYPE.GO_LIST, params);
        break;
      }
      case OPEN_BY_TYPE.GO_HOME: {
        this.go(OPEN_BY_TYPE.GO_HOME);
        break;
      }
      case OPEN_BY_TYPE.GO_DETAIL: {
        this.go(OPEN_BY_TYPE.GO_DETAIL, params);
        break;
      }
      case OPEN_BY_TYPE.GO_ORDER_DETAIL: {
        let userInfo = Taro.getStorageSync('userInfo');
        if (userInfo && userInfo.uid && userInfo.session_key) {
          this.go('orderDetail', params);
        } else {
          this.go('nativeLogin');
        }
        break;
      }
      case OPEN_BY_TYPE.GO_ORDER_LIST: {
        let userInfo = Taro.getStorageSync('userInfo');
        if (userInfo && userInfo.uid && userInfo.session_key) {
          this.go(OPEN_BY_TYPE.GO_ORDER_LIST, params);
        } else {
          this.go('nativeLogin');
        }
        break;
      }
      case OPEN_BY_TYPE.GO_YOLUCK: {
        let userInfo = Taro.getStorageSync('userInfo');
        if (userInfo && userInfo.uid && userInfo.session_key) {
          this.go('yoLuck', params);
        } else {
          this.go('nativeLogin');
        }
      }
      break;
      default: break;
    }
  }
}