Authored by bevishuang

添加路由管理 review bybaoss

... ... @@ -98,7 +98,8 @@
"wxed31f9e8705fb8d1",
"wx207f18be42db9028",
"wx7678b61d1e6702fe",
"wxc677c88385762287"
"wxc677c88385762287",
"wx8c854ca3917e9ba8"
],
"plugins": {
"yoho-utils": {
... ...
... ... @@ -30,6 +30,8 @@ function jumpByUrl(jumpUrl,pageName){
return;
}
router.goUrl(jumpUrl);
return;
// 跳小程序
if (jumpUrl.indexOf(MINI_APP_DOMAIN)>=0) {
router.goUrl(jumpUrl);
... ...
import jumpToMiniapp from './jump-to-miniapp';
import {parse, stringify} from '../vendors/query-string';
var plugin = requirePlugin("yoho-utils");
// import jumpToMiniapp from './jump-to-miniapp';
// import {parse, stringify} from '../vendors/query-string';
// import url2path from './url-to-path';
import jumpTo from './jump';
// import { parse, stringify } from '../vendors/query-stringify';
const MINI_APP_DOMAIN = 'miniapp.yohobuy.com';
// const MINI_APP_DOMAIN = 'miniapp.yohobuy.com';
global.router = {
goUrl(url) {
if (!url) {
return Promise.reject('error url');
}
// goUrl(url) {
// plugin.goUrl(url,'wx084ab813d88c594b')
// },
goUrl(url) {
console.log("url:" + url)
if (!url) {
return Promise.reject('error url');
}
let { appId,
pagePath,
query,
jumpType
} = plugin.urlToPath(url, 'wx084ab813d88c594b')
console.log(appId)
const [uri, search] = url.split('?');
const path = uri.split(MINI_APP_DOMAIN)[1];
const qs = parse(search);
this.go(pagePath, query, jumpType, appId)
},
if (qs.app && path) {
return jumpToMiniapp({
app: qs.app,
path: `${path}?${stringify(qs)}`
});
}
/**
* pathName:页面路径
* query:页面参数
* jumpType:跳转类型
* appId:miniApp 的唯一标识
*/
go(pathName, query, jumpType, appId) {
const jump = jumpType ? jumpType : 'navigateTo';
query = query || {};
//appId 存在,跳转到外部小程序,否则内部跳转
if (appId) {
jumpTo.navigateToMiniProgram(appId, pathName, query)
} else {
switch (jump) {
case 'navigateTo':
jumpTo.navigateTo(pathName, query)
break;
case 'redirectTo':
jumpTo.redireatTo(pathName, query)
break;
case 'switchTab':
jumpTo.switchTab(pathName, query)
break;
}
}
},
};
... ...
... ... @@ -2,19 +2,19 @@ import wx from '../utils/wx';
import {stringify} from '../vendors/query-string';
export default {
navigateTo(path, qs) {
return wx.navigateTo({
url: `${path}?${stringify(qs, {encode: false})}`,
});
},
redirectTo(path, qs) {
return wx.redirectTo({
url: `${path}?${stringify(qs, {encode: false})}`,
});
},
switchTab(path) {
return wx.switchTab({
url: path,
});
}
};
navigateTo(path, qs) {
return wx.navigateTo({ url: `${path}?${stringify(qs)}` });
},
redirectTo(path, qs) {
return wx.redirectTo({ url: `${path}?${stringify(qs)}` });
},
switchTab(path) {
return wx.switchTab({ url: path });
},
navigateToMiniProgram(appId, path, qs) {
return wx.navigateToMiniProgram({
appId,
path: `${path}?${stringify(qs)}`,
})
}
};
\ No newline at end of file
... ...
import api, { verify, computeSecret} from "./common/api.js"
import dataUtils from "./common/data-utils.js"
import formId from "./common/formId-collect.js"
import router from "./router/url-to-path.js"
module.exports = {
...dataUtils,
verify,
computeSecret,
...api,
...formId
...formId,
...router
}
\ No newline at end of file
... ...
import url2path from './url-to-path';
import jumpTo from './jumpTo';
import { parse, stringify } from '../vendors/query-stringify';
const MINI_APP_DOMAIN = 'miniapp.yohobuy.com';
export default {
goUrl(url, defauteAppId) {
console.log("url:"+url)
if (!url) {
return Promise.reject('error url');
}
let { appId,
pagePath,
query,
jumpType
} = url2path.urlToPath(url, defauteAppId)
this.go(pagePath, query, jumpType, appId)
},
/**
* pathName:页面路径
* query:页面参数
* jumpType:跳转类型
* appId:miniApp 的唯一标识
*/
go(pathName, query, jumpType, appId) {
const jump = jumpType ? jumpType : 'navigateTo';
// const rule = rules[name];
// if (!rule) {
// return Promise.reject(`路由规则未匹配到: ${name}`);
// }
query = query || {};
//appId 存在,跳转到外部小程序,否则内部跳转
if (appId) {
jumpTo.navigateToMiniProgram(appId, pathName, query)
} else {
switch (jump) {
case 'navigateTo':
jumpTo.navigateTo(pathName, query)
break;
case 'redirectTo':
jumpTo.redireatTo(pathName, query)
break;
case 'switchTab':
jumpTo.switchTab(pathName, query)
break;
}
}
},
};
... ...
import wx from '../utils/wx';
import { stringify } from '../vendors/query-stringify';
export default {
navigateTo(path, qs) {
return wx.navigateTo({ url: `${path}?${stringify(qs)}` });
},
redirectTo(path, qs) {
return wx.redirectTo({ url: `${path}?${stringify(qs)}` });
},
switchTab(path) {
return wx.switchTab({ url: path });
},
navigateToMiniProgram(appId,path,qs){
return wx.navigateToMiniProgram({
appId,
path:`${path}?${stringify(qs)}`,
})
}
};
\ No newline at end of file
... ...
// import appInfo from './wxIdConfig';
const appData = {
yohobuy: {
appId: 'wx084ab813d88c594b',
page: {
productDetail: '/pages/goodsDetail/goodsDetail',//商品详情
productList: '/pages/goodsList/goodsList',//商品列表
productShop: '/pages/goodsList/brandStore',//店铺
brandDetail: '/pages/goodsList/brand'//品牌
}
},
anotherApp1: {
appId: 'wx084ab813d88c594b',
page: {
home: '/pages/product/detail/detail'
}
},
};
export default appData
\ No newline at end of file
... ...
/**
* url解析到path
*/
import {
stringify,
parse
} from '../vendors/query-stringify';
import APPIDCONFIG from './wxIdConfig'
import PATHS from './routerConfig.js'
const LOCAL_HOST = 'https://miniapp.yohobuy.com/';
const BRIAGE_FLAG = 'openby:yohobuy';
const GO_MINIAPP = 'go.miniapp';
/**
* jumpUrl:跳转URL
* defaultAppId:默认appId,当前引用小程序ID
*/
function urlToPath(jumpUrl, defaultAppId) {
console.log('jumpUrl:' + jumpUrl);
if (!jumpUrl) {
return {};
}
let targetAppId = defaultAppId
let pageName = '';
let pagePath = '';
let query = {};
let appId = '';
let appName = '';
let jumpType = 'navigateTo'
let miniApp = '';
//MKT,STORE小程序使用的规则,后续尽量不使用,统一规则
// 跳转到小程序内
// router.go('https://miniapp.yohobuy.com/pages/index/index?xxxx=ghh&ytty=444');
// 跳转到小程序外
// router.go('https://miniapp.yohobuy.com/pages/index/index?app=yohobuy&xxxx=ghh&ytty=444');
//yohobuy={ "action": "go.miniapp", "params": { "appId": "gh_afb5ac7a8f9d", "path": "/pages/index/index", "debugmode": "1" } }
//appId:小程序标识(appId) , path:小程序落地页面路径,缺省到小程序首页 开发人员配置选项debugmode,1表示开发版 2表示体验版 0 or字段缺省表示正式版本
// url 包含 open:yohobuy
let index = jumpUrl.indexOf(BRIAGE_FLAG)
if (index > -1) {
let openBy = {}
if (typeof jumpUrl === 'string') { // 处理普通url
let [uri, search] = jumpUrl.split('?');
const qs = parse(search);
console.log('search:' + search);
// 排除search内from_page_param参数的url参数
search = search.replace(/"from_page_param":"[^,]+"/, '"from_page_param":""');
search = search.replace(/'from_page_param':'[^,]+'/, "'from_page_param':''");
console.log('qs:')
console.log(qs)
// console.log('tempArr:' + tempArr)
// tempArr.splice(0, 1);
// let search = tempArr.join('?');
let qsItem = qs['openby:yohobuy']
console.log(qsItem)
if (!qs[BRIAGE_FLAG]) return {};
openBy = JSON.parse(qs[BRIAGE_FLAG]);
} else if (typeof jumpUrl === 'object') { // 处理公共跳转页面path参数
openBy = JSON.parse(jumpUrl[BRIAGE_FLAG]);
}
let action = openBy.action;
console.log(action)
query = openBy.params;
jumpType = openBy.jumpType || 'navigateTo';
appId = openBy.appId || '';
if (query && query.appId) {
appId = query.appId || '';
}
//跳转到外部小程序
if (action === GO_MINIAPP) {
pagePath = query.path
} else {
if (!appId && !appName){
targetAppId = appId || defaultAppId
}
let { cAppId, pagePath } = parsePath(targetAppId, appName, action, pageName)
if (cAppId && cAppId !== defaultAppId) {
appId = cAppId
}
}
delete query.appId
delete query.path
delete query.jumpType
miniApp = openBy.miniApp;
//==========================
} else {
//是否以LOCAL_HOST开头
let index = jumpUrl.indexOf(LOCAL_HOST)
console.log('is-indexOf:' + index)
const [uri, search] = jumpUrl.split('?');
query = parse(search);
if (index > -1) {
pagePath = uri.split(LOCAL_HOST)[1];
appId = query.appId || '';
appName = query.app || '';
jumpType = query.jumpType || 'navigateTo';
delete query.appId
delete query.jumpType
}
}
return {
appId,
pagePath,
query,
jumpType
};
}
/**
* 获取页面路径
*/
function parsePath(targetAppId, appName, action, pageName) {
let toPath = ''
let appId = ''
APPIDCONFIG.some((info) => {
let cAppId = info.appId;
let cAppName = info.appName;
console.log('cAppId:' + cAppId + "//appName:" + appName)
//如果ID相同,查找path
if (cAppId === targetAppId || cAppName === appName) {
info.paths.some((pathInfo) => {
if (pathInfo.action === action || pathInfo.tag === pageName) {
toPath = pathInfo.pathName
return true;
}
})
appId = cAppId;
}
return (cAppId === targetAppId || cAppName === appName);
});
return {appId,toPath};
}
// export default urlToPath;
module.exports = {
urlToPath: urlToPath,
}
\ No newline at end of file
... ...
const appInfo = [
{
appId: 'wx084ab813d88c594b',
appName: 'yohobuy',
paths: [
{
pathName: '/pages/index/index',
tag: 'home',
action: 'go.home'
},
{
pathName: '/pages/detail/index',
tag: 'detail',
action: 'go.detail'
},
{
pathName: '/pages/mine/index',
tag: 'mine',
action: 'go.mine'
},
{
pathName: '/pages/webview/webview',
tag: 'webview',
action: 'go.h5'
}
]
},
{
appId: 'wx084ab813d88c594b',
appName: 'group'
}
];
export default appInfo;
\ No newline at end of file
... ...
/* global wx */
const api = {};
const apiNames = [
'request',
'navigateTo',
'navigateBack',
'showLoading',
'hideLoading',
'canvasToTempFilePath',
'saveImageToPhotosAlbum',
'navigateToMiniProgram',
'getImageInfo',
'getUserInfo',
'getSetting',
'openSetting',
'showModal',
'login',
'scanCode',
'checkSession',
'setStorage',
'showToast',
'getLocation',
'authorize',
'setClipboardData'
];
apiNames.forEach(key => {
if (typeof wx[key] === 'function') {
api[key] = (params = {}) => {
return new Promise((resolve, reject) => {
console.debug(`call api ${key}: `, Object.assign({}, params));
params.success = res => {
console.debug(`call api ${key} result: `, res);
return resolve(res);
};
params.fail = err => {
console.debug(`call api ${key} error: `, err);
return reject(err);
};
return wx[key](params);
});
};
}
});
export default Object.assign({}, wx, api);
... ...