Authored by 肖亚东

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

... ... @@ -5,8 +5,8 @@
"pages/shopCart/shopCart",
"pages/userCenter/userCenter",
"pages/productList/index",
"pages/goodsList/goodsList"
"pages/goodsList/goodsList",
"pages/webview/webview"
],
"window":{
"navigationBarBackgroundColor": "#3a3a3a",
... ...
... ... @@ -3,6 +3,7 @@ import { getChannelCode, getGenderCode, getRecPosCode, getRecommandContentCode,
import { shouldDiscardTap, getGoodDetailParam, getImageUrl, formatImageUrl,getGuangType, getYHStorageSync} from '../../utils/util';
import homeService from './indexService.js'
import { parseProductListData } from '../../utils/productListUtil';
import router from '../../router/index.js'
//获取应用实例
let app = getApp()
... ... @@ -352,6 +353,8 @@ Page({
jumpByRuleEvent(event) {
console.log(event.detail);
let url = event.detail.currentTarget.dataset.jump_rule;
router.goUrl(url);
},
// 清除storage
... ...
import {
parse,
stringify
} from '../../vendors/query-string.js';
// import event from '../../utils/event.js';
Page({
/**
* 页面的初始数据
*/
data: {
title: '',
url: '',
shareUrl: ''
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
console.log(options);
options.url = decodeURIComponent(options.url || '');
if (options && options.scene && options.scene.length > 0) {
let code = options.scene;
// getQRCodeSource(code).then(json => {
// console.log('json:', json);
// if (json) {
// this.generateUrl(json);
// }
// }).catch(error => { });
} else {
if (options.url) {
let uid = this.getUid();
const search = options.url.split('?')[1] || '';
const qs = parse(search);
if (Number(qs.needLogin) === 1 && uid === 0) {
// event.emit('user-is-login', () => {
// }, () => {
// this.generateUrl(options);
// });
this.generateUrl(options);
} else {
this.generateUrl(options);
}
}
}
},
/**
* 获取 UID
*/
getUid: function () {
const app = getApp() || {};
return app.globalData &&
app.globalData.userInfo &&
app.globalData.userInfo.uid ?
app.globalData.userInfo.uid : 0;
},
/**
* 取得URL中的参数
*/
getQueryObj(link) {
let loc = decodeURIComponent(link);
let variables = '';
let variableArr = [];
let finalArr = [];
if (loc.indexOf('?') > 0) {
variables = loc.split('?')[1];
}
if (variables.length > 0) {
variableArr = variables.split('#')[0].split('&');
}
for (let i = 0; i < variableArr.length; i++) {
let obj = {};
obj.name = variableArr[i].split('=')[0];
obj.value = variableArr[i].split('=')[1];
finalArr.push(obj);
}
let query_obj = {};
for (let i = 0; i < finalArr.length; i++) {
query_obj[finalArr[i].name] = finalArr[i].value;
}
return query_obj;
},
/**
* 生成链接
*/
generateUrl: function (options) {
console.log(options);
let isSign = true;
if (options.url) {
let queryObj = this.getQueryObj(options.url);
console.log('query=', queryObj);
if (queryObj.title) {
this.setData({
title: queryObj.title
});
}
if (queryObj.shareURL) {
this.setData({
shareUrl: queryObj.shareURL
});
}
if (queryObj.noSign) {
isSign = false;
}
}
console.log('option.url=', decodeURIComponent(options.url));
// const app = getApp() || {};
// let param = {
// app_version: config.apiParams.app_version,
// session_key: app.globalData && app.globalData.sessionKey || '',
// client_type: config.apiParams.client_type,
// udid: udid.get()
// };
// let uid = this.getUid();
// if (uid) {
// param.uid = uid;
// }
let url = options.url;
// let params = verify.computeSecret(param);
const [uri, search] = url.split('?');
const qs = parse(search);
url = uri + '?' + stringify(Object.assign({}, qs));
// console.log('webview-url:', url);
this.setData({
url: url
});
},
bindGetMsg: function (e) {
if (e.detail && e.detail.data && e.detail.data[0] && e.detail.data[0].title) {
this.setData({
title: e.detail.data[0].title,
});
}
},
});
... ...
{}
\ No newline at end of file
... ...
<web-view src="{{url}}" bindmessage="bindGetMsg"></web-view>
\ No newline at end of file
... ...
/* src/pages/webview/webview.wxss */
\ No newline at end of file
... ...
import rules from './rules';
import router from './router'
// const urlParse = require('url');
// const queryString = require('querystring');
const ACTION_TYPE = {
h5: 'go.h5',
goodsList: 'go.list'
}
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[jumpType] || 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.goodsList: {
let yohobuy = {
...yoho.params,
url: yoho.params.url,
action: yoho.action
}
this.go('goodsList', yohobuy);
break;
}
case ACTION_TYPE.ufo: {
this.handleUFOJumpPage(yoho);
break;
}
default:
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 = tt.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 = tt.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 = tt.getStorageSync('userInfo');
if (userInfo && userInfo.uid && userInfo.session_key) {
this.go('yoLuck', params);
} else {
this.go('nativeLogin');
}
}
break;
default: break;
}
}
}
... ...
import tt from '../utils/tt.js'
import { stringify } from '../vendors/query-stringify.js';
export default {
navigateTo(params) {
return tt.navigateTo({
url: `${params.url}?${stringify(params.query)}`,
});
},
reLaunch(params) {
return tt.reLaunch({
url: params.url
});
},
redirectTo(params) {
return tt.redirectTo({
url: `${params.url}?${stringify(params.query)}`,
})
},
switchTab(params) {
return tt.switchTab({
url: params.url,
})
}
}
\ No newline at end of file
... ...
export default {
home: {
path: 'pages/index/index'
},
goodsList: {
path: 'pages/goodsList/goodsList'
},
webview: {
path: 'pages/webview/webview'
}
}
... ...