Procházet zdrojové kódy

供应商链接跳转权限

yuwenjun1997 před 2 roky
rodič
revize
c504fa5bbe
7 změnil soubory, kde provedl 150 přidání a 97 odebrání
  1. 4 5
      .env.development
  2. 2 2
      configs/mode-map.js
  3. 93 35
      middleware/auth.js
  4. 41 0
      middleware/auth1.js
  5. 7 7
      nuxt.config.js
  6. 0 48
      pages/_old/_.vue
  7. 3 0
      pages/_template/_.vue

+ 4 - 5
.env.development

@@ -2,13 +2,12 @@
 EVN = 'development'
 
 # 网站地址
-LOCALHOSE = 'https://zp-b.caimei365.com'
-# LOCALHOSE = 'http://192.168.2.81:8888'
-# LOCALHOSE = 'http://192.168.2.92:8888'
+# LOCALHOSE = 'https://zp-b.caimei365.com'
+LOCALHOSE = 'http://192.168.2.92:8888'
 
 # 接口api地址
-BASE_URL = 'https://zplma-b.caimei365.com'
-# BASE_URL = 'http://192.168.2.68:8012'
+# BASE_URL = 'https://zplma-b.caimei365.com'
+BASE_URL = 'http://192.168.2.68:8012'
 
 # 静态资源文件地址
 STATIC_URL = 'https://static.caimei365.com/www/authentic'

+ 2 - 2
configs/mode-map.js

@@ -2,10 +2,10 @@
 export default [
   {
     authUserId: 10,
-    mode: 'ldm',
+    prefix: 'ldm',
   },
   {
     authUserId: 12,
-    mode: 'ross',
+    prefix: 'ross',
   },
 ]

+ 93 - 35
middleware/auth.js

@@ -1,41 +1,99 @@
-import modeMap from '~/configs/mode-map'
 import axios from 'axios'
-const whiteList = ['/auth']
-
-export default function ({ route, error, redirect }) {
-  if (whiteList.indexOf(route.path) > -1) return
+import oldRoutes from '@/utils/old-routes'
+import { getQueryObject } from '~/utils'
+import modeMap from '~/configs/mode-map'
+const templateList = ['app', 'ross', 'ldm']
+const baseURL = process.env.BASE_URL + '/wx/auth/shop/info'
+const dev = process.env.NODE_ENV
 
-  const authUserId = parseInt(route.params.template)
-  const auth = modeMap.find((item) => item.authUserId === authUserId)
-  let routePrefix = !auth ? `/${authUserId}/app` : `/${authUserId}/${auth.mode}`
+// 获取供应商信息1 通过authUserId获取
+function fetchSupplierInfo(params) {
+  return axios.get(baseURL, { params })
+}
 
-  const baseURL = process.env.BASE_URL + '/wx/auth/shop/info'
-  console.log(auth && auth.mode, authUserId)
+// 旧链接初始化
+function oldLinkInit({ route, error, redirect }) {
+  return new Promise((resolve, reject) => {
+    let hash = route.hash
+    const query = getQueryObject(hash)
+    const appId = query.appId
+    const index = hash.indexOf('?')
+    hash = hash.slice(1, index)
+    const oldRoute = oldRoutes.find((item) => hash === item.path)
+    if (!oldRoute) {
+      error({ statusCode: 404, message: '页面不存在' })
+      reject()
+    }
+    fetchSupplierInfo({ appId })
+      .then(({ data }) => {
+        const res = data
+        if (res.code === -1) {
+          error({ statusCode: 404, message: '页面不存在' })
+        }
+        const path = `/${data.authUserId}` + oldRoute.redirect
+        redirect(path)
+        resolve()
+      })
+      .catch(() => {
+        error({ statusCode: 404, message: '页面不存在' })
+        resolve()
+      })
+  })
+}
 
-  return axios
-    .get(baseURL, {
-      params: { authUserId: authUserId },
-    })
-    .then(({ data }) => {
-      if (data.code === -1) {
-        error({
-          statusCode: 404,
-          message: '页面不存在',
-        })
-      } else if (authUserId === 12 && auth.mode === 'ross' && route.path.indexOf(`${authUserId}/app`) > -1) {
-        // console.log(route.fullPath.replace(`${authUserId}/app`, `${authUserId}/ross`))
-        redirect(route.fullPath.replace(`${authUserId}/app`, `${authUserId}/ross`))
-      } else if (route.path.indexOf(routePrefix) !== 0) {
-        error({
-          statusCode: 404,
-          message: '页面不存在',
-        }) 
-      }
-    })
-    .catch(() => {
-      error({
-        statusCode: 404,
-        message: '页面不存在',
+// 新链接初始化
+function newLinkInit({ route, error, redirect }) {
+  return new Promise((resolve) => {
+    const authUserId = parseInt(route.params.template)
+    const mode = modeMap.find((mode) => mode.authUserId === authUserId)
+    const prefixPath_app = `/${authUserId}/app`
+    fetchSupplierInfo({ authUserId })
+      .then(({ data }) => {
+        if (dev === 'production') {
+          const res = data
+          const prefix = res.data.prefix
+          const prefixPath = `/${authUserId}/${prefix}`
+          // 获取供应商失败
+          if (res.code === -1) {
+            error({ statusCode: 404, message: '页面不存在' })
+          }
+          // 没有指定模板 && 模板不存在 && 不是app默认模板路由地址
+          else if (
+            !mode &&
+            !templateList.includes(prefix) &&
+            !route.fullPath.startsWith(prefixPath_app)
+          ) {
+            if (!route.fullPath.startsWith(prefixPath)) {
+              error({ statusCode: 404, message: '页面不存在' })
+            } else {
+              const redirectPath = route.fullPath.replace(
+                prefixPath,
+                prefixPath_app
+              )
+              redirect(redirectPath)
+            }
+          }
+          // 没有指定模板 && 是app默认模板路由地址
+          else if (!mode && route.fullPath.startsWith(prefixPath_app)) {
+          }
+          // 模板未定义
+          else if (!templateList.includes(prefix)) {
+            error({ statusCode: 404, message: '页面不存在' })
+          }
+          // 模板与供应商不匹配
+          else if (!route.fullPath.startsWith(prefixPath)) {
+            error({ statusCode: 404, message: '页面不存在' })
+          }
+        }
+        resolve()
+      })
+      .catch((err) => {
+        error({ statusCode: 404, message: '页面不存在' })
+        console.log(err)
       })
-    })
+  })
+}
+
+export default function (context) {
+  return context.route.hash ? oldLinkInit(context) : newLinkInit(context)
 }

+ 41 - 0
middleware/auth1.js

@@ -0,0 +1,41 @@
+import modeMap from '~/configs/mode-map'
+import axios from 'axios'
+
+export default async function ({ route, error, redirect }) {
+  const authUserId = parseInt(route.params.template)
+  const auth = modeMap.find((item) => item.authUserId === authUserId)
+  let routePrefix = !auth ? `/${authUserId}/app` : `/${authUserId}/${auth.mode}`
+  const baseURL = process.env.BASE_URL + '/wx/auth/shop/info'
+  return axios
+    .get(baseURL, {
+      params: { authUserId: authUserId },
+    })
+    .then(({ data }) => {
+      if (data.code === -1) {
+        error({
+          statusCode: 404,
+          message: '页面不存在',
+        })
+      } else if (
+        authUserId === 12 &&
+        auth.mode === 'ross' &&
+        route.path.indexOf(`${authUserId}/app`) > -1
+      ) {
+        // console.log(route.fullPath.replace(`${authUserId}/app`, `${authUserId}/ross`))
+        redirect(
+          route.fullPath.replace(`${authUserId}/app`, `${authUserId}/ross`)
+        )
+      } else if (route.path.indexOf(routePrefix) !== 0) {
+        error({
+          statusCode: 404,
+          message: '页面不存在',
+        })
+      }
+    })
+    .catch(() => {
+      error({
+        statusCode: 404,
+        message: '页面不存在',
+      })
+    })
+}

+ 7 - 7
nuxt.config.js

@@ -99,12 +99,12 @@ export default {
   server: {
     port: process.env.PORT,
     host: process.env.HOST,
-    https:
-      process.env.HTTPS === 'true'
-        ? {
-            key: fs.readFileSync(path.join(__dirname, 'cert.key')),
-            cert: fs.readFileSync(path.join(__dirname, 'cert.crt')),
-          }
-        : null,
+    // https:
+    //   process.env.HTTPS === 'true'
+    //     ? {
+    //         key: fs.readFileSync(path.join(__dirname, 'cert.key')),
+    //         cert: fs.readFileSync(path.join(__dirname, 'cert.crt')),
+    //       }
+    //     : null,
   },
 }

+ 0 - 48
pages/_old/_.vue

@@ -1,48 +0,0 @@
-<template>
-  <div></div>
-</template>
-
-<script>
-import oldRoutes from '@/utils/old-routes'
-import { getQueryObject } from '@/utils/index'
-export default {
-  data() {
-    return {
-      authUserId: '',
-      redirectInfo: null,
-    }
-  },
-  mounted() {
-    this.initData()
-  },
-  methods: {
-    initData() {
-      // 获取页面hash值 因为老页面基于vue-router hash模式进行路由跳转
-      let hash = this.$route.hash
-      const query = getQueryObject(hash)
-      this.appId = query.appId
-
-      const index = hash.indexOf('?')
-      hash = hash.slice(1, index)
-      this.redirectInfo = oldRoutes.find((route) => hash === route.path)
-      if (this.redirectInfo) {
-        this.fetchSupplierInfo()
-      }
-    },
-    // 获取供应商信息
-    async fetchSupplierInfo() {
-      try {
-        const { data } = await this.$http.api.fetchSupplierInfo({
-          appId: this.appId,
-        })
-        const path = `/${data.authUserId}` + this.redirectInfo.redirect
-        this.$router.push(path)
-      } catch (error) {
-        console.log(error)
-      }
-    },
-  },
-}
-</script>
-
-<style scoped></style>

+ 3 - 0
pages/_template/_.vue

@@ -0,0 +1,3 @@
+<template>
+  <div></div>
+</template>