index.js 2.18 KB
import rules from './rules';
import router from './router';

// const urlParse = require('url');
// const queryString = require('querystring');

const ACTION_TYPE = {
  h5: 'go.h5',
  productList: 'go.list',
  brandStore: 'go.shop',
  brand: 'go.brand'
}

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];

    if (!rule) {
      return Promise.reject(`路由规则未匹配到: ${name}`);
    }
    query = query || {};

    const routerFn = router[jump] || router.navigateTo;
    return routerFn({
      url: rule.path,
      query
    });
  },
  goUrl(jumpUrl) {
    if (!jumpUrl) {
      return;
    }

    let url = "";
    let startIndex = jumpUrl.indexOf('openby:yohobuy=');
    if (startIndex < 0) {
      return;
    }
  
    let jsonRule = jumpUrl.substring(startIndex+'openby:yohobuy='.length);
    let yoho = JSON.parse(jsonRule);
    console.log('===============router===============');
    console.log(jsonRule);
    console.log(yoho);
    console.log('====================================');
    
    switch(yoho.action) {
      case ACTION_TYPE.h5: {
        let yohobuy = {
          ...yoho.params.param,
          url: yoho.params.url,
          action: yoho.action
        }
        this.go('webview', yohobuy);
        break;
      }
      case ACTION_TYPE.productList: {
        let yohobuy = {
          ...yoho.params,
          url: yoho.params.url,
          action: yoho.action
        }
        this.go('productList', yohobuy);
        break;
      }
      case ACTION_TYPE.brandStore: {
        let yohobuy = {
          ...yoho.params,
          url: yoho.params.url,
          action: yoho.action
        }
        this.go('brandStore', yohobuy);
        break;
      }
      case ACTION_TYPE.brand: {
        let yohobuy = {
          ...yoho.params,
          url: yoho.params.url,
          action: yoho.action
        }
        this.go('brand', yohobuy);
        break;
      }
      default: 
        break;
    }
  }
}