This commit is contained in:
weiwei committed 2025-03-25 21:54:20 +08:00
1 parent 40a5288b0e
commit a41dad0a99
4 files changed
+105 -111

No files matched your search

+20 -20
View File
@@ -57,25 +57,25 @@
"root": "pages/order",
"pages": []
},
// {
// "root": "pages/shop",
// "pages": [
// {
// "path": "detail",
// "style": {
// "navigationBarTitleText": "",
// "disableScroll": true
// }
// },
// {
// "path": "product-detail",
// "style": {
// "navigationBarTitleText": "",
// "disableScroll": true
// }
// }
// ]
// },
{
"root": "pages/shop",
"pages": [
{
"path": "detail",
"style": {
"navigationBarTitleText": "",
"disableScroll": true
}
},
{
"path": "product-detail",
"style": {
"navigationBarTitleText": "",
"disableScroll": true
}
}
]
},
{
"root": "pages/apply",
"pages": [
@@ -371,4 +371,4 @@
// },
]
}
}
}
+1
View File
@@ -37,6 +37,7 @@ const toDetail = async (v: any) => {
</script>
<template>
1
<crud-layout bg="/static/page/blue-bg.png">
<z-paging
ref="pageRef"
+2 -1
View File
@@ -23,7 +23,7 @@ onLoad(() => {
app._initTabbar()
})
onShow(() => {
pageRef.value && pageRef.value.reload(false)
// pageRef.value && pageRef.value.reload(false)
})
const toDetail = async (v: any) => {
@@ -35,6 +35,7 @@ const toDetail = async (v: any) => {
</script>
<template>
1
<crud-layout bg="/static/page/blue-bg.png">
<z-paging
ref="pageRef"
+82 -90
View File
@@ -1,99 +1,91 @@
import fs from "fs";
import stripJsonComments from "./strip-json-comments";
import fs from "fs"
import stripJsonComments from "./strip-json-comments"
class TransformPages {
routes: any = [];
tabbar: any = [];
includes = [];
uniPagesJSON: any = "";
routes: any = []
tabbar: any = []
includes = []
uniPagesJSON: any = ""
constructor({ includes, pagesJsonDir }: any) {
this.includes = includes;
this.uniPagesJSON = JSON.parse(
stripJsonComments(fs.readFileSync(pagesJsonDir, "utf-8"))
);
this.routes = this.getPagesRoutes().concat(this.getSubPackagesRoutes());
this.tabbar = this.getTabbarRoutes();
}
constructor({ includes, pagesJsonDir }: any) {
this.includes = includes
this.uniPagesJSON = JSON.parse(stripJsonComments(fs.readFileSync(pagesJsonDir, "utf-8")))
this.routes = this.getPagesRoutes().concat(this.getSubPackagesRoutes())
this.tabbar = this.getTabbarRoutes()
}
getPagesRoutes(pages = this.uniPagesJSON.pages, rootPath = null) {
let routes = [];
for (let i = 0; i < pages.length; i++) {
const item = pages[i];
let route: any = {};
for (let j = 0; j < this.includes.length; j++) {
const key = this.includes[j];
let value = item[key];
if (key === "path") {
value = rootPath ? `/${rootPath}/${value}` : `/${value}`;
}
if (key === "aliasPath" && i == 0 && rootPath == null) {
route[key] = route[key] || "/";
} else if (value !== undefined) {
route[key] = value;
}
}
routes.push(route);
}
return routes;
}
getPagesRoutes(pages = this.uniPagesJSON.pages, rootPath = null) {
let routes = []
for (let i = 0; i < pages.length; i++) {
const item = pages[i]
let route: any = {}
for (let j = 0; j < this.includes.length; j++) {
const key = this.includes[j]
let value = item[key]
if (key === "path") {
value = rootPath ? `/${rootPath}/${value}` : `/${value}`
}
if (key === "aliasPath" && i == 0 && rootPath == null) {
route[key] = route[key] || "/"
} else if (value !== undefined) {
route[key] = value
}
}
routes.push(route)
}
return routes
}
getSubPackagesRoutes() {
if (!(this.uniPagesJSON && this.uniPagesJSON.subPackages)) {
return [];
}
const subPackages = this.uniPagesJSON.subPackages;
let routes: any = [];
for (let i = 0; i < subPackages.length; i++) {
const subPages = subPackages[i].pages;
const root = subPackages[i].root;
const subRoutes = this.getPagesRoutes(subPages, root);
routes = routes.concat(subRoutes);
}
return routes;
}
getSubPackagesRoutes() {
if (!(this.uniPagesJSON && this.uniPagesJSON.subPackages)) {
return []
}
const subPackages = this.uniPagesJSON.subPackages
let routes: any = []
for (let i = 0; i < subPackages.length; i++) {
const subPages = subPackages[i].pages
const root = subPackages[i].root
const subRoutes = this.getPagesRoutes(subPages, root)
routes = routes.concat(subRoutes)
}
return routes
}
getTabbarRoutes() {
if (
!(
this.uniPagesJSON &&
this.uniPagesJSON.tabBar &&
this.uniPagesJSON.tabBar.list
)
) {
return [];
}
const tabbar = this.uniPagesJSON.tabBar.list;
let tabbarMap: any = [];
tabbar.forEach((bar: { pagePath: string }) => {
tabbarMap.push("/" + bar.pagePath);
});
return tabbarMap;
}
getTabbarRoutes() {
if (!(this.uniPagesJSON && this.uniPagesJSON.tabBar && this.uniPagesJSON.tabBar.list)) {
return []
}
const tabbar = this.uniPagesJSON.tabBar.list
let tabbarMap: any = []
tabbar.forEach((bar: { pagePath: string }) => {
tabbarMap.push("/" + bar.pagePath)
})
return tabbarMap
}
}
export const useRouter = ({ pagesJsonDir, includes = [] }: any) => {
let defaultIncludes = ["path", "aliasPath", "name"];
includes = [...defaultIncludes, ...includes];
let pages = new TransformPages({
pagesJsonDir,
includes,
});
if (pages.tabbar && pages.tabbar.length) {
console.table(pages.tabbar);
}
if (pages.routes && pages.routes.length) {
console.table(pages.routes);
}
return {
name: "use-router",
config() {
return {
define: {
__ROUTES__: JSON.stringify(pages.routes),
__TABBAR__: JSON.stringify(pages.tabbar),
},
};
},
};
};
let defaultIncludes = ["path", "aliasPath", "name"]
includes = [...defaultIncludes, ...includes]
let pages = new TransformPages({
pagesJsonDir,
includes,
})
if (pages.tabbar && pages.tabbar.length) {
console.table(pages.tabbar)
}
if (pages.routes && pages.routes.length) {
console.table(pages.routes)
}
return {
name: "use-router",
config() {
return {
define: {
__ROUTES__: JSON.stringify(pages.routes),
__TABBAR__: JSON.stringify(pages.tabbar),
},
}
},
}
}