Browse Source

解决方案 Id 跳转到hashId

xinfeng 5 years ago
parent
commit
639aeec0cf
2 changed files with 49 additions and 10 deletions
  1. 17 10
      pages/kaifain/detail/_tid/index.vue
  2. 32 0
      plugins/genHashId.js

+ 17 - 10
pages/kaifain/detail/_tid/index.vue

@@ -113,7 +113,7 @@
   import DealSeoFooter from "@/components/kaifain/dealSeoFooter"
   import ChangeBgImage from "@/components/kaifain/ChangeBgImage"
   import KaifainFooter from "@/components/SeoFooter"
-
+  import {HashIDUtil, GenType} from "../../../../plugins/genHashId"
 
   export default {
     layout: "opacity_header",
@@ -138,17 +138,24 @@
       }
     },
     async asyncData({ ...params }) {
-      let { tid } = params.app.context.route.params || {}
-      tid = tid.replace(".html", "")
+      let { tid: newTid } = params.app.context.route.params || {}
+      let tid = newTid.replace(".html", "")
 
       if (process.server) {
-        // const {path, fullPath} = params.app.context && params.app.context.route || {}
-        // if (path && path.indexOf('/kaifain/s/14.html') !== -1) {
-        //   let reditUrl = fullPath
-        //   let path1 = path.replace("/user/", "/u/")
-        //   reditUrl = path.replace(path, path1)
-        //   redirect(301, reditUrl)
-        // }
+        const {path, fullPath} = params.app.context && params.app.context.route || {}
+        //将id都转到hashId
+        console.log("********* tid", tid, tid.length !== 12)
+        if (tid && tid.length !== 12 && Number(tid) === Number(tid)) {
+          console.log("********* innnnnn", 123)
+
+          let genHashId = new HashIDUtil()
+          let nowId = genHashId.getHashID(GenType.TYPE_SERVICE_PROVIDER, tid)
+          console.log("********* nowId", nowId)
+          let path1 = path.replace(newTid, nowId)
+          let reditUrl = path.replace(path, path1)
+          params.redirect(301, reditUrl)
+          return
+        }
       }
 
       let errInfo = ""

+ 32 - 0
plugins/genHashId.js

@@ -0,0 +1,32 @@
+const GenType = {
+  // 三位类型编码 一定不能大于280
+  TYPE_RECRUIT: '100',
+  TYPE_SERVICE_CASE: '110',
+  TYPE_SERVICE_PROVIDER: '120',
+  TYPE_TOPIC: '130',
+  TYPE_VIDEO: '140',
+}
+
+class HashIDUtil {
+  
+  getHashID(type, id) {
+    let idNew = `000000000000${id}`.slice(-12)
+    return this.dec2hex(`${type}${idNew}`).toLowerCase();
+  }
+  
+  dec2hex(number) {
+    number = Number(number)
+    let hexValues = [ '0', '1', '2', '3', '4', '5', '6', '7',
+      '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' ];
+    let hexval = '';
+    while (number !== 0) {
+      hexval = hexValues[ number % 16 ] + hexval;
+      number = Math.floor(number / 16)
+    }
+    return hexval;
+  }
+}
+
+export {
+  GenType, HashIDUtil
+}