gaozhan 5 жил өмнө
parent
commit
47b9f7c442

+ 19 - 12
.umirc.js

@@ -1,4 +1,6 @@
 // ref: https://umijs.org/config/
+import proxyMap from "./src/conf/proxyMap";
+// const proxyMap = require('./proxyMap')
 export default {
   treeShaking: true,
   // chainWebpack(config) {
@@ -42,17 +44,22 @@ export default {
             /services\//,
             /model\.(t|j)sx?$/,
             /service\.(t|j)sx?$/,
-            /components\//
-          ]
-        }
-      }
-    ]
+            /components\//,
+          ],
+        },
+      },
+    ],
   ],
-  proxy: {
-    "/api": {
-      target: process.env.TARGET,
-      pathRewrite: { "^/api": "" },
-      changeOrigin: true
-    }
-  }
+  proxy: (function () {
+    const proxyArr = Object.keys(proxyMap).map((apiPrefix) => ({
+      [apiPrefix]: {
+        target: proxyMap[apiPrefix],
+        changeOrigin: true,
+        pathRewrite: { [`^${apiPrefix}`]: "" },
+        cookieDomainRewrite: "",
+      },
+    }));
+    const proxy = proxyArr.reduce((acc, item) => ({ ...acc, ...item }), {});
+    return proxy;
+  })(),
 };

+ 19 - 7
src/apis/base.js

@@ -1,7 +1,8 @@
 import axios from 'axios'
 import { message } from 'antd'
 import { beforeRequest, handleRequestError, handleResponseSuccess, handleResponseError } from '@/apis/hook'
-import { auth, hostDev } from '@/conf/config'
+import { auth } from '@/conf/config'
+import getRealUrl from "@/utils/getRealUrl";
 
 function getPrefix () {
   if (process.env.NODE_ENV === 'development') {
@@ -12,7 +13,7 @@ function getPrefix () {
 }
 
 export const request = axios.create({
-  baseURL: `${getPrefix()}/api`,
+  baseURL: `${getPrefix()}`,
   timeout: 5000,
   withCredentials: true,
   headers: {
@@ -52,11 +53,22 @@ request.interceptors.response.use(
 )
 
 async function authRequest ({method, url, params, data}) {
-  const {code, msg, data: { token, time, ding_id: dd_id, user_name: dd_name, ding_mobile: telephone, power }} = await request({
-    method: 'get',
-    url: `${auth}/common/token?url=${encodeURIComponent(url)}`,
-    params: {}
-  })
+  const {
+    code,
+    msg,
+    data: {
+      token,
+      time,
+      ding_id: dd_id,
+      user_name: dd_name,
+      ding_mobile: telephone,
+      power,
+    },
+  } = await request({
+    method: "get",
+    url: `${auth}/common/token?url=${encodeURIComponent(getRealUrl(url))}`,
+    params: {},
+  });
   if (code===0) {
     const newParams = (method && method.toLowerCase() === 'post')?{data: {...data,token, time, dd_id, dd_name, telephone, power}}:{params: {...params,token, time, dd_id, dd_name, telephone, power}}
     return request({

+ 14 - 45
src/conf/config.js

@@ -1,50 +1,19 @@
-let dc = 'http://back-apit.weipaitang.com'
-let tc = 'http://localhost:8080'
+let dc = '/back-apit/api'
+let tc = '/back-apit/api'
+let us = '/back-apit/api'
 const apiCdn = 'http://cdn01t.weipaitang.com/img/'
-let auth = 'https://back-autht.weipaitang.com'
-let hostDev = 'http://back-admint.weipaitang.com'
-let us = 'http://back-apit.weipaitang.com'
-let sTaskJobLog = 'http://10.3.7.11:16686' // 定时任务日志跳转
-let thanos = 'http://thanos-admint.wpt.la'
+let auth = '/back-autht/api'
+const hostDev = `${window.location.protocol}//${window.location.host}`
+let sTaskJobLog = '/ms-tracet/api' // 定时任务日志跳转
+let thanos = '/thanos-admint/api'
 
-if (document.domain === 'localhost') {
-  // dc = 'http://localhost:8000/api'
-  // dc = 'http://backend.wpt.local:8087'
-  dc = 'http://back-apit.weipaitang.com'
-  // thanos = 'http://thanos-admint.wpt.la'
-  thanos = 'http://localhost:7070'
-} else if (document.domain === 'back-admint.wpt.la') {
-  dc = 'http://back-apit.weipaitang.com'
-  us = 'http://back-apit.weipaitang.com'
-  tc = 'http://back-apit.weipaitang.com'
-  auth = 'https://back-autht.weipaitang.com'
-  hostDev = 'http://back-admint.weipaitang.com'
-  sTaskJobLog = 'http://10.3.7.11:16686' // 定时任务日志跳转
-  thanos = 'http://thanos-admint.wpt.la'
-} else if (document.domain === 'back-admint.weipaitang.com') {
-  dc = 'http://back-apit.weipaitang.com'
-  tc = 'http://back-apit.weipaitang.com'
-  auth = 'https://back-autht.weipaitang.com'
-  us = 'http://back-apit.weipaitang.com'
-  hostDev = 'http://back-admint.weipaitang.com'
-  sTaskJobLog = 'http://10.3.7.11:16686' // 定时任务日志跳转
-  thanos = 'http://thanos-admint.wpt.la'
-} else if (document.domain === 'back-admin.wpt.la') {
-  dc = 'http://back-admin.wpt.la/api'
-  tc = 'http://back-admin.wpt.la/api'
-  auth = 'https://back-auth.weipaitang.com'
-  us = 'http://back-api.weipaitang.com'
-  hostDev = 'http://back-admin.wpt.la'
-  sTaskJobLog = 'http://10.3.7.11:16686' // 定时任务日志跳转
-  thanos = 'http://thanos-admin.weipaitang.com'
-} else if (document.domain === 'back-admin.weipaitang.com') {
-  dc = 'http://back-api.weipaitang.com'
-  tc = 'http://back-api.weipaitang.com'
-  auth = 'https://back-auth.weipaitang.com'
-  us = 'http://back-api.weipaitang.com'
-  hostDev = 'http://back-admin.weipaitang.com'
-  sTaskJobLog = 'http://ms-trace.weipaitang.com' // 定时任务日志跳转
-  thanos = 'http://thanos-admin.weipaitang.com'
+if (document.domain === 'back-admin.weipaitang.com') {
+  dc = '/back-api/api'
+  tc = '/back-api/api'
+  auth = '/back-auth/api'
+  us = '/back-api/api'
+  sTaskJobLog = '/ms-trace/api' // 定时任务日志跳转
+  thanos = '/thanos-admin/api'
 }
 
 export {

+ 12 - 0
src/conf/proxyMap.js

@@ -0,0 +1,12 @@
+module.exports = {
+  // 开发测试环境
+  '/back-apit/api': 'http://back-apit.weipaitang.com',
+  '/back-autht/api': 'https://back-autht.weipaitang.com',
+  '/ms-tracet/api': 'http://10.3.7.11:16686',
+  '/thanos-admint/api': 'http://thanos-admint.wpt.la',
+  // 生产环境
+  '/back-auth/api': 'https://back-auth.weipaitang.com',
+  '/back-api/api': 'http://back-api.weipaitang.com',
+  '/ms-trace/api': 'http://ms-trace.weipaitang.com',
+  '/thanos-admin/api': 'http://thanos-admin.weipaitang.com'
+}

+ 0 - 1
src/models/user.js

@@ -2,7 +2,6 @@ import cloneDeep from 'lodash/cloneDeep';
 import { query, logout, delColumnCache } from '@/services/user';
 import { message } from 'antd';
 // import { menuIcon } from '@/conf/const';
-import { hostDev } from '@/conf/config';
 
 const generateAntdData = data => {
   data.unshift({ id: -1, name: '全部' })

+ 0 - 1
src/pages/login/index.js

@@ -1,7 +1,6 @@
 import React from 'react'
 import { login } from '@/apis'
 import router from 'umi/router'
-import { hostDev } from '@/conf/config'
 
 function getQueryString(name) {
   const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`);

+ 18 - 0
src/utils/getRealUrl.js

@@ -0,0 +1,18 @@
+import proxyMap from '@/conf/proxyMap'
+
+export default function (url) {
+  if (/^\/{1}[^/]+/.test(url)) {
+    const matchApiPrefix = Object.keys(proxyMap).find(apiPrefix => {
+      const reg = new RegExp(`^${apiPrefix}`)
+      return reg.test(url)
+    })
+    if (matchApiPrefix) {
+      return url.replace(matchApiPrefix, proxyMap[matchApiPrefix])
+    } else {
+      console.log('无匹配 url')
+      return url
+    }
+  } else {
+    return url
+  }
+}