Authored by 肖亚东

跳转路由功能添加 — review by 黄敬囿

@@ -5,8 +5,8 @@ @@ -5,8 +5,8 @@
5 "pages/shopCart/shopCart", 5 "pages/shopCart/shopCart",
6 "pages/userCenter/userCenter", 6 "pages/userCenter/userCenter",
7 "pages/productList/index", 7 "pages/productList/index",
8 - "pages/goodsList/goodsList"  
9 - 8 + "pages/goodsList/goodsList",
  9 + "pages/webview/webview"
10 ], 10 ],
11 "window":{ 11 "window":{
12 "navigationBarBackgroundColor": "#3a3a3a", 12 "navigationBarBackgroundColor": "#3a3a3a",
@@ -3,6 +3,7 @@ import { getChannelCode, getGenderCode, getRecPosCode, getRecommandContentCode, @@ -3,6 +3,7 @@ import { getChannelCode, getGenderCode, getRecPosCode, getRecommandContentCode,
3 import { shouldDiscardTap, getGoodDetailParam, getImageUrl, formatImageUrl,getGuangType, getYHStorageSync} from '../../utils/util'; 3 import { shouldDiscardTap, getGoodDetailParam, getImageUrl, formatImageUrl,getGuangType, getYHStorageSync} from '../../utils/util';
4 import homeService from './indexService.js' 4 import homeService from './indexService.js'
5 import { parseProductListData } from '../../utils/productListUtil'; 5 import { parseProductListData } from '../../utils/productListUtil';
  6 +import router from '../../router/index.js'
6 7
7 //获取应用实例 8 //获取应用实例
8 let app = getApp() 9 let app = getApp()
@@ -352,6 +353,8 @@ Page({ @@ -352,6 +353,8 @@ Page({
352 353
353 jumpByRuleEvent(event) { 354 jumpByRuleEvent(event) {
354 console.log(event.detail); 355 console.log(event.detail);
  356 + let url = event.detail.currentTarget.dataset.jump_rule;
  357 + router.goUrl(url);
355 }, 358 },
356 359
357 // 清除storage 360 // 清除storage
  1 +import {
  2 + parse,
  3 + stringify
  4 +} from '../../vendors/query-string.js';
  5 +
  6 +// import event from '../../utils/event.js';
  7 +
  8 +Page({
  9 +
  10 + /**
  11 + * 页面的初始数据
  12 + */
  13 + data: {
  14 + title: '',
  15 + url: '',
  16 + shareUrl: ''
  17 + },
  18 +
  19 + /**
  20 + * 生命周期函数--监听页面加载
  21 + */
  22 + onLoad: function (options) {
  23 + console.log(options);
  24 + options.url = decodeURIComponent(options.url || '');
  25 +
  26 + if (options && options.scene && options.scene.length > 0) {
  27 + let code = options.scene;
  28 +
  29 + // getQRCodeSource(code).then(json => {
  30 + // console.log('json:', json);
  31 + // if (json) {
  32 + // this.generateUrl(json);
  33 + // }
  34 + // }).catch(error => { });
  35 + } else {
  36 + if (options.url) {
  37 + let uid = this.getUid();
  38 +
  39 + const search = options.url.split('?')[1] || '';
  40 + const qs = parse(search);
  41 +
  42 + if (Number(qs.needLogin) === 1 && uid === 0) {
  43 + // event.emit('user-is-login', () => {
  44 +
  45 + // }, () => {
  46 + // this.generateUrl(options);
  47 + // });
  48 + this.generateUrl(options);
  49 + } else {
  50 + this.generateUrl(options);
  51 + }
  52 + }
  53 + }
  54 + },
  55 +
  56 + /**
  57 + * 获取 UID
  58 + */
  59 + getUid: function () {
  60 + const app = getApp() || {};
  61 +
  62 + return app.globalData &&
  63 + app.globalData.userInfo &&
  64 + app.globalData.userInfo.uid ?
  65 + app.globalData.userInfo.uid : 0;
  66 + },
  67 +
  68 + /**
  69 + * 取得URL中的参数
  70 + */
  71 + getQueryObj(link) {
  72 + let loc = decodeURIComponent(link);
  73 + let variables = '';
  74 + let variableArr = [];
  75 + let finalArr = [];
  76 +
  77 + if (loc.indexOf('?') > 0) {
  78 + variables = loc.split('?')[1];
  79 + }
  80 +
  81 + if (variables.length > 0) {
  82 + variableArr = variables.split('#')[0].split('&');
  83 + }
  84 +
  85 + for (let i = 0; i < variableArr.length; i++) {
  86 + let obj = {};
  87 +
  88 + obj.name = variableArr[i].split('=')[0];
  89 + obj.value = variableArr[i].split('=')[1];
  90 + finalArr.push(obj);
  91 + }
  92 +
  93 + let query_obj = {};
  94 +
  95 + for (let i = 0; i < finalArr.length; i++) {
  96 + query_obj[finalArr[i].name] = finalArr[i].value;
  97 + }
  98 +
  99 + return query_obj;
  100 + },
  101 +
  102 +
  103 + /**
  104 + * 生成链接
  105 + */
  106 + generateUrl: function (options) {
  107 + console.log(options);
  108 + let isSign = true;
  109 +
  110 + if (options.url) {
  111 + let queryObj = this.getQueryObj(options.url);
  112 +
  113 + console.log('query=', queryObj);
  114 + if (queryObj.title) {
  115 + this.setData({
  116 + title: queryObj.title
  117 + });
  118 + }
  119 + if (queryObj.shareURL) {
  120 + this.setData({
  121 + shareUrl: queryObj.shareURL
  122 + });
  123 + }
  124 + if (queryObj.noSign) {
  125 + isSign = false;
  126 + }
  127 + }
  128 +
  129 + console.log('option.url=', decodeURIComponent(options.url));
  130 +
  131 + // const app = getApp() || {};
  132 + // let param = {
  133 + // app_version: config.apiParams.app_version,
  134 + // session_key: app.globalData && app.globalData.sessionKey || '',
  135 + // client_type: config.apiParams.client_type,
  136 + // udid: udid.get()
  137 + // };
  138 + // let uid = this.getUid();
  139 +
  140 + // if (uid) {
  141 + // param.uid = uid;
  142 + // }
  143 +
  144 + let url = options.url;
  145 + // let params = verify.computeSecret(param);
  146 +
  147 + const [uri, search] = url.split('?');
  148 + const qs = parse(search);
  149 +
  150 + url = uri + '?' + stringify(Object.assign({}, qs));
  151 + // console.log('webview-url:', url);
  152 +
  153 + this.setData({
  154 + url: url
  155 + });
  156 + },
  157 +
  158 + bindGetMsg: function (e) {
  159 + if (e.detail && e.detail.data && e.detail.data[0] && e.detail.data[0].title) {
  160 + this.setData({
  161 + title: e.detail.data[0].title,
  162 + });
  163 + }
  164 + },
  165 +});
  1 +<web-view src="{{url}}" bindmessage="bindGetMsg"></web-view>
  1 +/* src/pages/webview/webview.wxss */
  1 +import rules from './rules';
  2 +import router from './router'
  3 +// const urlParse = require('url');
  4 +// const queryString = require('querystring');
  5 +
  6 +const ACTION_TYPE = {
  7 + h5: 'go.h5',
  8 + goodsList: 'go.list'
  9 +}
  10 +
  11 +const OPEN_BY_TYPE = {
  12 + GO_LIST: 'productList',
  13 + GO_ORDER_LIST: 'orderList',
  14 + GO_DETAIL: 'productDetail',
  15 + GO_ORDER_DETAIL: 'BuyerOrderDetail',
  16 + GO_HOME: 'home',
  17 + GO_YOLUCK: 'yoluck',
  18 +};
  19 +
  20 +export default {
  21 + go(name, query, jumpType) {
  22 + const jump = jumpType ? jumpType : 'navigateTo';
  23 + const rule = rules[name];
  24 +
  25 + if (!rule) {
  26 + return Promise.reject(`路由规则未匹配到: ${name}`);
  27 + }
  28 + query = query || {};
  29 +
  30 + const routerFn = router[jumpType] || router.navigateTo;
  31 + return routerFn({
  32 + url: rule.path,
  33 + query
  34 + });
  35 + },
  36 + goUrl(jumpUrl) {
  37 + if (!jumpUrl) {
  38 + return;
  39 + }
  40 +
  41 + let url = "";
  42 + let startIndex = jumpUrl.indexOf('openby:yohobuy=');
  43 + if (startIndex < 0) {
  44 + return;
  45 + }
  46 +
  47 + let jsonRule = jumpUrl.substring(startIndex+'openby:yohobuy='.length);
  48 + let yoho = JSON.parse(jsonRule);
  49 + console.log('===============router===============');
  50 + console.log(jsonRule);
  51 + console.log(yoho);
  52 + console.log('====================================');
  53 +
  54 +
  55 + switch(yoho.action) {
  56 + case ACTION_TYPE.h5: {
  57 + let yohobuy = {
  58 + ...yoho.params.param,
  59 + url: yoho.params.url,
  60 + action: yoho.action
  61 + }
  62 + this.go('webview', yohobuy);
  63 + break;
  64 + }
  65 + case ACTION_TYPE.goodsList: {
  66 + let yohobuy = {
  67 + ...yoho.params,
  68 + url: yoho.params.url,
  69 + action: yoho.action
  70 + }
  71 + this.go('goodsList', yohobuy);
  72 + break;
  73 + }
  74 + case ACTION_TYPE.ufo: {
  75 + this.handleUFOJumpPage(yoho);
  76 + break;
  77 + }
  78 + default:
  79 + break;
  80 + }
  81 + },
  82 + handleUFOJumpPage(yoho) {
  83 + const params = yoho.params
  84 + switch(params.pagename) {
  85 + case OPEN_BY_TYPE.GO_LIST: {
  86 + console.log(yoho);
  87 + this.go(OPEN_BY_TYPE.GO_LIST, params);
  88 + break;
  89 + }
  90 + case OPEN_BY_TYPE.GO_HOME: {
  91 + this.go(OPEN_BY_TYPE.GO_HOME);
  92 + break;
  93 + }
  94 + case OPEN_BY_TYPE.GO_DETAIL: {
  95 + this.go(OPEN_BY_TYPE.GO_DETAIL, params);
  96 + break;
  97 + }
  98 + case OPEN_BY_TYPE.GO_ORDER_DETAIL: {
  99 + let userInfo = tt.getStorageSync('userInfo');
  100 + if (userInfo && userInfo.uid && userInfo.session_key) {
  101 + this.go('orderDetail', params);
  102 + } else {
  103 + this.go('nativeLogin');
  104 + }
  105 + break;
  106 + }
  107 + case OPEN_BY_TYPE.GO_ORDER_LIST: {
  108 + let userInfo = tt.getStorageSync('userInfo');
  109 + if (userInfo && userInfo.uid && userInfo.session_key) {
  110 + this.go(OPEN_BY_TYPE.GO_ORDER_LIST, params);
  111 + } else {
  112 + this.go('nativeLogin');
  113 + }
  114 + break;
  115 + }
  116 + case OPEN_BY_TYPE.GO_YOLUCK: {
  117 + let userInfo = tt.getStorageSync('userInfo');
  118 + if (userInfo && userInfo.uid && userInfo.session_key) {
  119 + this.go('yoLuck', params);
  120 + } else {
  121 + this.go('nativeLogin');
  122 + }
  123 + }
  124 + break;
  125 + default: break;
  126 + }
  127 + }
  128 +}
  1 +import tt from '../utils/tt.js'
  2 +import { stringify } from '../vendors/query-stringify.js';
  3 +
  4 +export default {
  5 + navigateTo(params) {
  6 + return tt.navigateTo({
  7 + url: `${params.url}?${stringify(params.query)}`,
  8 + });
  9 + },
  10 + reLaunch(params) {
  11 + return tt.reLaunch({
  12 + url: params.url
  13 + });
  14 + },
  15 + redirectTo(params) {
  16 + return tt.redirectTo({
  17 + url: `${params.url}?${stringify(params.query)}`,
  18 + })
  19 + },
  20 + switchTab(params) {
  21 + return tt.switchTab({
  22 + url: params.url,
  23 + })
  24 + }
  25 +}
  1 +export default {
  2 + home: {
  3 + path: 'pages/index/index'
  4 + },
  5 + goodsList: {
  6 + path: 'pages/goodsList/goodsList'
  7 + },
  8 + webview: {
  9 + path: 'pages/webview/webview'
  10 + }
  11 +}