scorpio 2 years ago
parent
commit
f090bf6f15
5 changed files with 102 additions and 78 deletions
  1. 2 0
      .prettierrc.cjs
  2. 49 41
      src/api/axios.js
  3. 46 33
      src/api/fetch.js
  4. 3 2
      src/api/system/index.js
  5. 2 2
      src/config/website.js

+ 2 - 0
.prettierrc.cjs

@@ -16,4 +16,6 @@ module.exports = {
   arrowParens: 'avoid',
   // 开启 eslint 支持
   eslintIntegration: true,
+  //
+  endOfLine: 'cr'
 }

+ 49 - 41
src/api/axios.js

@@ -26,49 +26,57 @@ NProgress.configure({
   showSpinner: false
 })
 // http request拦截
-axios.interceptors.request.use(config => {
-  // 开启 progress bar
-  NProgress.start()
-  const meta = (config.meta || {})
-  const isToken = meta.isToken === false
-  config.headers.Authorization = `Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}`
-  // 让每个请求携带token
-  if (getToken() && !isToken) {
-    config.headers['Blade-Auth'] = 'bearer ' + getToken()
+axios.interceptors.request.use(
+  config => {
+    // 开启 progress bar
+    NProgress.start()
+    const meta = config.meta || {}
+    const isToken = meta.isToken === false
+    config.headers.Authorization = `Basic ${Base64.encode(
+      `${website.clientId}:${website.clientSecret}`
+    )}`
+    // 让每个请求携带token
+    if (getToken() && !isToken) {
+      config.headers['Blade-Auth'] = 'bearer ' + getToken()
+    }
+    // headers中配置text请求
+    if (config.text === true) {
+      config.headers['Content-Type'] = 'text/plain'
+    }
+    // headers中配置serialize为true开启序列化
+    // if (config.method === 'post' && meta.isSerialize === true) {
+    //   config.data = serialize(config.data)
+    // }
+    return config
+  },
+  error => {
+    return Promise.reject(error)
   }
-  // headers中配置text请求
-  if (config.text === true) {
-    config.headers['Content-Type'] = 'text/plain'
-  }
-  // headers中配置serialize为true开启序列化
-  // if (config.method === 'post' && meta.isSerialize === true) {
-  //   config.data = serialize(config.data)
-  // }
-  return config
-}, error => {
-  return Promise.reject(error)
-})
+)
 // http response 拦截
-axios.interceptors.response.use(res => {
-  // 关闭 progress bar
-  NProgress.done()
-  // 获取状态码
-  const status = res.data.code || res.status
-  const statusWhiteList = website.statusWhiteList || []
-  // 如果在白名单里则自行catch逻辑处理
-  if (statusWhiteList.includes(status)) return Promise.reject(res)
-  // 如果是401则跳转到登录页面
-  if (status === 401) {
-    router.push('/login')
+axios.interceptors.response.use(
+  res => {
+    // 关闭 progress bar
+    NProgress.done()
+    // 获取状态码
+    const status = res.data.code || res.status
+    const statusWhiteList = website.statusWhiteList || []
+    // 如果在白名单里则自行catch逻辑处理
+    if (statusWhiteList.includes(status)) return Promise.reject(res)
+    // 如果是401则跳转到登录页面
+    if (status === 401) {
+      router.push('/login')
+    }
+    // 如果请求为非200否者默认统一处理
+    if (status !== 200) {
+      return Promise.reject(res)
+    }
+    return res
+  },
+  error => {
+    NProgress.done()
+    return Promise.reject(error)
   }
-  // 如果请求为非200否者默认统一处理
-  if (status !== 200) {
-    return Promise.reject(res)
-  }
-  return res
-}, error => {
-  NProgress.done()
-  return Promise.reject(error)
-})
+)
 
 export default axios

+ 46 - 33
src/api/fetch.js

@@ -8,20 +8,31 @@ import { ElMessage } from 'element-plus'
 
 axios.defaults.baseURL = ''
 
-axios.interceptors.request.use(config => {
-  config.headers.token = getToken()
-  // 小程序里用m的页面
-  config.headers.Platform = 'pc'
-  if (getToken() === null || getToken() === undefined) {
-    delete config.headers.token
+axios.interceptors.request.use(
+  config => {
+    config.headers.token = getToken()
+    // 小程序里用m的页面
+    config.headers.Platform = 'pc'
+    if (getToken() === null || getToken() === undefined) {
+      delete config.headers.token
+    }
+    return config
+  },
+  err => {
+    return Promise.reject(err)
   }
-  return config
-}, err => {
-  return Promise.reject(err)
-})
+)
 
 // insurance 保险 502 503 504时兜底的
-function fetch (url = '', params = {}, method = 'get', contentType = 'form', header = {}, insurance, timeout = 15000) {
+function fetch(
+  url = '',
+  params = {},
+  method = 'get',
+  contentType = 'form',
+  header = {},
+  insurance,
+  timeout = 15000
+) {
   contentType === 'form' && (contentType = 'application/x-www-form-urlencoded')
   contentType === 'json' && (contentType = 'application/json')
   contentType === 'file' && (contentType = 'multipart/form-data')
@@ -47,14 +58,11 @@ function fetch (url = '', params = {}, method = 'get', contentType = 'form', hea
         ...header
       }
     }
-    const success = (response) => {
-      const {
-        status,
-        data = {},
-        statusText
-      } = response
+    const success = response => {
+      const { status, data = {}, statusText } = response
       if (status >= 200 && status <= 401) {
-        if (data.code === 401) { // 未登录c
+        if (data.code === 401) {
+          // 未登录c
           removeToken()
           router.push(`/?redirect=${encodeURIComponent(window.location.href)}`)
           reject(new Error('需要登录'))
@@ -70,22 +78,27 @@ function fetch (url = '', params = {}, method = 'get', contentType = 'form', hea
         ElMessage.success(status + '-' + statusText)
       }
     }
-    axios(requestParams).then(success).catch((err) => {
-      if (/502|503|504/.test(err.message) || (err + '').indexOf('timeout') > -1) {
-        if (insurance) {
-          resolve(insurance)
-          return
-        }
-        let msg = '系统繁忙,正在为您排队中,请稍后再试'
-        if (window.location.href.indexOf('/zu') > -1) {
-          msg += ` ${err.message}`
+    axios(requestParams)
+      .then(success)
+      .catch(err => {
+        if (
+          /502|503|504/.test(err.message) ||
+          (err + '').indexOf('timeout') > -1
+        ) {
+          if (insurance) {
+            resolve(insurance)
+            return
+          }
+          let msg = '系统繁忙,正在为您排队中,请稍后再试'
+          if (window.location.href.indexOf('/zu') > -1) {
+            msg += ` ${err.message}`
+          }
+          ElMessage.error(msg)
+        } else {
+          resolve(err.data)
+          // ElMessage.error(err.data.error_description ? err.data.error_description : '网络异常,请点击重试')
         }
-        ElMessage.error(msg)
-      } else {
-        resolve(err.data)
-        // ElMessage.error(err.data.error_description ? err.data.error_description : '网络异常,请点击重试')
-      }
-    })
+      })
   })
 }
 

+ 3 - 2
src/api/system/index.js

@@ -1,11 +1,12 @@
 import fetch from '../fetch.js'
+import website from '@/config/website'
 
 export default {
   /**
    * 获取用户菜单
    * @returns {Promise<unknown>}
    */
-  getMenus () {
-    return fetch('/blade-system/menu/routes')
+  getMenus() {
+    return fetch('/blade-system/menu/routes', { clientId: website.clientId })
   }
 }

+ 2 - 2
src/config/website.js

@@ -4,7 +4,7 @@
 export default {
   title: '梧桐树云平台',
   tenant: '000000',
-  clientId: 'saber', // 客户端id
-  clientSecret: 'saber_secret', // 客户端密钥
+  clientId: 'HSXK7WE5OFD1', // 客户端id
+  clientSecret: 'HOHSQR6YJ9ESUHL5S09MM', // 客户端密钥
   statusWhiteList: []
 }