Sfoglia il codice sorgente

路径错误需要报错404

Jing 1 settimana fa
parent
commit
278df698c2

+ 1 - 5
.nuxt/manifest/latest.json

@@ -1,5 +1 @@
-<<<<<<< HEAD
-{"id":"fac450b1-8b0c-4c27-bd09-d51837baf20e","timestamp":1746781732724}
-=======
-{"id":"13d44bbe-63ae-49d4-9ebf-b7dcd94aa110","timestamp":1745828563145}
->>>>>>> 6f7f78215e97bb9cd6b05dcdc651d07789b7b45f
+{"id":"13508e8a-e677-40b3-a2ab-3197eee808cc","timestamp":1747043176299}

+ 1 - 0
.nuxt/manifest/meta/13508e8a-e677-40b3-a2ab-3197eee808cc.json

@@ -0,0 +1 @@
+{"id":"13508e8a-e677-40b3-a2ab-3197eee808cc","timestamp":1747043176299,"matcher":{"static":{},"wildcard":{},"dynamic":{}},"prerendered":[]}

+ 0 - 1
.nuxt/manifest/meta/13d44bbe-63ae-49d4-9ebf-b7dcd94aa110.json

@@ -1 +0,0 @@
-{"id":"13d44bbe-63ae-49d4-9ebf-b7dcd94aa110","timestamp":1745828563145,"matcher":{"static":{},"wildcard":{},"dynamic":{}},"prerendered":[]}

+ 0 - 1
.nuxt/manifest/meta/fac450b1-8b0c-4c27-bd09-d51837baf20e.json

@@ -1 +0,0 @@
-{"id":"fac450b1-8b0c-4c27-bd09-d51837baf20e","timestamp":1746781732724,"matcher":{"static":{},"wildcard":{},"dynamic":{}},"prerendered":[]}

+ 1 - 5
.nuxt/tsconfig.json

@@ -102,11 +102,7 @@
         "./imports"
       ],
       "#app-manifest": [
-<<<<<<< HEAD
-        "./manifest/meta/fac450b1-8b0c-4c27-bd09-d51837baf20e.json"
-=======
-        "./manifest/meta/13d44bbe-63ae-49d4-9ebf-b7dcd94aa110.json"
->>>>>>> 6f7f78215e97bb9cd6b05dcdc651d07789b7b45f
+        "./manifest/meta/13508e8a-e677-40b3-a2ab-3197eee808cc.json"
       ],
       "#components": [
         "./components"

+ 79 - 0
middleware/setup.global.js

@@ -0,0 +1,79 @@
+export default async function (to, from) {
+    //获得全局url
+    const { $webUrl, $CwebUrl } = useNuxtApp();
+    if(to.path == '/' || to.path == '/404'){
+        //如果目标路由是/,或者/404 无需验证
+    }else{
+        // to是目标路由对象,from是来源路由对象
+        console.log('进入路由中间件,目标路由:', to.path,"当前路由:",from.path);
+        //拆分目标路由
+        let parts = parseRoute(to.path)
+        console.log(parts)
+
+        const responseRoute = await $fetch($webUrl + '/web/checkWebsiteRoute', {
+            headers: {
+                'Content-Type': 'application/json',
+                'Userurl': $CwebUrl,
+                'Origin': $CwebUrl
+            },
+            query: parts
+        })
+        console.log(responseRoute)
+        if(responseRoute.code == 200){
+            //如果路由存在,不做任何操作
+        }else{
+            return navigateTo('/404')
+        }
+    }
+}
+
+//提取路由
+function parseRoute(url) {
+    const parts = url.split('/').filter(Boolean); // 分割并过滤空字符串
+    const lastPart = parts[parts.length - 1];
+    
+    // 提取 id(数字.html 的数字部分)
+    const idMatch = lastPart.match(/^(\d+)\.html$/);
+    const id = idMatch ? idMatch[1] : undefined;
+    
+    // 如果最后是数字.html、index.html 或 list-数字.html,则去掉它
+    const isSpecialRoute = idMatch || lastPart === "index.html" || lastPart.startsWith("list-");
+    if (isSpecialRoute) {
+        parts.pop();
+    }
+    
+    let all_route, last_route;
+    
+    // 如果路径以 xiangcunshangcheng 开头
+    if (parts[0] === "xiangcunshangcheng") {
+        if( parts.length > 1){
+            if(parts[parts.length - 1] != 'gongying' && parts[parts.length - 1] != 'qiugou'){
+                // 否则按原逻辑处理
+                all_route = parts.join('/');
+                last_route = parts.length > 0 ? parts[parts.length - 1] : null;
+            }else{
+                // all_route = 去掉最后一层
+                all_route = parts.slice(0, -1).join('/');
+                // last_route = 新的最后一层
+                last_route = parts.length > 1 ? parts[parts.length - 2] : null;
+            }
+        }else{
+            // 否则按原逻辑处理
+            all_route = parts.join('/');
+            last_route = parts.length > 0 ? parts[parts.length - 1] : null;
+        }
+    } else {
+        // 否则按原逻辑处理
+        all_route = parts.join('/');
+        last_route = parts.length > 0 ? parts[parts.length - 1] : null;
+    }
+    // 返回结果(只有数字.html 时才包含 id)
+    const result = {
+        all_route,
+        last_route
+    };
+    if (id !== undefined) {
+        result.id = id;
+    }
+    return result;
+}

+ 10 - 10
plugins/globals.ts

@@ -1,15 +1,15 @@
 export default defineNuxtPlugin((nuxtApp) => {
   // 三农市场网pre环境
-  nuxtApp.provide('webUrl', 'http://apipre1.bjzxtw.org.cn:29501')
-  nuxtApp.provide('CwebUrl', 'http://pre.snscw.org.cn')
-  nuxtApp.provide('BwebUrl', 'http://adminpre.bjzxtw.org.cn')
-  nuxtApp.provide('LoginWebUrl', 'http://adminpre.bjzxtw.org.cn/adminapi')
-  nuxtApp.provide('userUrl', 'http://localhost:3000')
+  // nuxtApp.provide('webUrl', 'http://apipre1.bjzxtw.org.cn:29501')
+  // nuxtApp.provide('CwebUrl', 'http://pre.snscw.org.cn')
+  // nuxtApp.provide('BwebUrl', 'http://adminpre.bjzxtw.org.cn')
+  // nuxtApp.provide('LoginWebUrl', 'http://adminpre.bjzxtw.org.cn/adminapi')
+  // nuxtApp.provide('userUrl', 'http://localhost:3000')
 
   //正式环境
-//   nuxtApp.provide('webUrl', 'https://flzxw.bjzxtw.org.cn')
-//   nuxtApp.provide('CwebUrl', 'http://nw.snscw.org.cn')
-//   nuxtApp.provide('BwebUrl', 'https://admin.bjzxtw.org.cn')
-//   nuxtApp.provide('LoginWebUrl', 'http://admin.bjzxtw.org.cn/adminapi')
-//   nuxtApp.provide('userUrl', 'http://localhost:3000')
+  nuxtApp.provide('webUrl', 'https://flzxw.bjzxtw.org.cn')
+  nuxtApp.provide('CwebUrl', 'http://nw.snscw.org.cn')
+  nuxtApp.provide('BwebUrl', 'https://admin.bjzxtw.org.cn')
+  nuxtApp.provide('LoginWebUrl', 'http://admin.bjzxtw.org.cn/adminapi')
+  nuxtApp.provide('userUrl', 'http://localhost:3000')
 })

+ 4 - 4
plugins/request.ts

@@ -6,11 +6,11 @@ export default defineNuxtPlugin(() => {
 
     let Url = {
         //正式环境
-        // webUrl: 'https://flzxw.bjzxtw.org.cn', //接口地址
-        // CwebUrl: 'nw.snscw.org.cn' //页面地址
+        webUrl: 'https://flzxw.bjzxtw.org.cn', //接口地址
+        CwebUrl: 'nw.snscw.org.cn' //页面地址
         //pre环境
-        webUrl: 'http://apipre1.bjzxtw.org.cn:29501', //pre接口地址
-        CwebUrl: 'pre.snscw.org.cn' //页面地址
+        // webUrl: 'http://apipre1.bjzxtw.org.cn:29501', //pre接口地址
+        // CwebUrl: 'pre.snscw.org.cn' //页面地址
     }
 
     const service = axios.create({