11 lines
1.0 KiB
TypeScript
11 lines
1.0 KiB
TypeScript
export type MainTab='gallery'|'make'|'store'|'mine'
|
|
export function mainRoute(tab:MainTab){return '/pages/'+tab+'/index'}
|
|
export function route(path:string,query:Record<string,string|number|undefined>={}){
|
|
let parts=Object.entries(query).filter(([,v])=>v!==undefined).map(([k,v])=>encodeURIComponent(k)+'='+encodeURIComponent(String(v)))
|
|
return '/'+path.replace(/^\//,'')+(parts.length?'?'+parts.join('&'):'')
|
|
}
|
|
export function returnAction(depth:number,tab:MainTab):{type:'back'}|{type:'tab';url:string}{return depth>1?{type:'back'}:{type:'tab',url:mainRoute(tab)}}
|
|
export function goMain(tab:MainTab){uni.switchTab({url:mainRoute(tab)})}
|
|
export function goPage(path:string,query:Record<string,string|number|undefined>={}){uni.navigateTo({url:route(path,query),fail:()=>uni.showToast({title:'页面暂时无法打开,请重试',icon:'none'})})}
|
|
export function backOrMain(tab:MainTab){let action=returnAction(getCurrentPages().length,tab);if(action.type==='back')uni.navigateBack({fail:()=>goMain(tab)});else goMain(tab)}
|