Explorar el Código

feat:请求

wqb2017 hace 5 años
padre
commit
856c3af24f
Se han modificado 4 ficheros con 197 adiciones y 3 borrados
  1. 21 0
      src/assets/js/api.request.js
  2. 137 0
      src/assets/js/axios.js
  3. 36 0
      src/config/index.js
  4. 3 3
      src/route/white-list.js

+ 21 - 0
src/assets/js/api.request.js

@@ -0,0 +1,21 @@
+/*
+ * @Author: WangQiBiao
+ * @LastEditors: WangQiBiao
+ * @Description:
+ * @Date: 2019-03-02 14:21:27
+ * @LastEditTime: 2019-10-08 16:52:58
+ */
+import HttpRequest from './axios'
+import config from '@/config'
+
+let baseUrl = '/api/'
+if (process.env.NODE_ENV === 'production') {
+  if (process.env.VUE_APP_SECRET === 'test') {
+    baseUrl = config.baseUrl.test
+  } else {
+    baseUrl = config.baseUrl.pro
+  }
+}
+
+const axios = new HttpRequest(baseUrl)
+export default axios

+ 137 - 0
src/assets/js/axios.js

@@ -0,0 +1,137 @@
+/*
+ * @Author: WangQiBiao
+ * @LastEditors: WangQiBiao
+ * @Description:
+ * @Date: 2019-03-12 09:40:46
+ * @LastEditTime: 2019-10-08 16:54:04
+ */
+import axios from 'axios'
+import { Loading, Message } from 'element-ui'
+import router from '../../route/white-list'
+// import store from './../store'
+class HttpRequest {
+  constructor (baseUrl) {
+    this.baseUrl = baseUrl
+    this.queue = {}
+    this.needLoadingRequestCount = 0
+    this.fullscreenLoading = null
+  }
+
+  getInsideConfig () {
+    const config = {
+      baseURL: this.baseUrl,
+      // timeout: 1000 * 60 * 5, // 5分钟
+      withCredentials: true, // 跨域
+      headers: {
+        Accept: 'application/json;charset=utf-8'
+      }
+    }
+    return config
+  }
+
+  destroy (url) {
+    delete this.queue[url]
+    if (!Object.keys(this.queue).length) {
+      // Spin.hide()
+    }
+  }
+  /**
+   * 错误处理
+   */
+  handleError (err) {
+    if (err.response && err.response.status) {
+      switch (err.response.status) {
+        // 没有权限
+        case 401:
+          router.replace({
+            path: '/login'
+          })
+          break
+      }
+    }
+  }
+
+  interceptors (instance, url) {
+    instance.defaults.transformResponse = [
+      function (res) {
+        res = JSON.parse(res)
+        // 出错处理
+        if (!res.data && res.responseJSON) {
+          Message.error(res.responseJSON ? res.responseJSON.message : '服务器出小差了!')
+        }
+        return res
+      }
+    ]
+    // 请求拦截
+    instance.interceptors.request.use(config => {
+      // 添加全局的loading...
+      if (!Object.keys(this.queue).length) {
+        // Spin.show() // 不建议开启,因为界面不友好
+      }
+      // let needLoading = false
+      if (config.params && config.params.needLoading) {
+        // 需要loading的时候才增加一次
+        this.needLoadingRequestCount += 1
+        this.fullscreenLoading = Loading.service({
+          fullscreen: true,
+          lock: true,
+          text: 'Loading',
+          background: 'rgba(0, 0, 0, 0.3)'
+        })
+        // if (config.method.toLowerCase === 'get') {
+        //   needLoading = config.params.needLoading
+        //   delete config.params.needLoading
+        // } else {
+        //   needLoading = config.params.needLoading
+        //   delete config.params.needLoading
+        // }
+        // needLoading && store.dispatch('actionIsLoading', true)
+      }
+      this.queue[url] = true
+      return config
+    }, error => {
+      this.handleError(error)
+      return Promise.reject(error)
+    })
+    // 响应拦截
+    instance.interceptors.response.use(res => {
+      // 响应成功后设置loading状态
+      if (this.needLoadingRequestCount > 0) {
+        this.needLoadingRequestCount--
+      } else if (this.needLoadingRequestCount === 0 && this.fullscreenLoading) {
+        this.fullscreenLoading.close()
+        // store.dispatch('actionIsLoading', false)
+      }
+      this.destroy(url)
+      const { data, status } = res
+      return { data, status }
+    }, (error) => {
+      this.handleError(error)
+      // this.destroy(url)
+      return Promise.reject(error.response.data)
+    })
+  }
+
+  request (options) {
+    const instance = axios.create()
+    options = Object.assign(this.getInsideConfig(), options)
+    this.interceptors(instance, options.url)
+    return instance(options)
+  }
+
+  // request stream data method
+  createdFormDataAxios (URL, formData) {
+    return axios.post(this.baseUrl + URL, formData, {
+      transformResponse: [
+        function (response) {
+          return new Blob([response], { type: 'application/json' })
+        }
+      ],
+      headers: {
+        'responseType': 'blob',
+        'content-type': 'application/x-www-form-urlencoded'
+      }
+    })
+  }
+}
+export default HttpRequest

+ 36 - 0
src/config/index.js

@@ -0,0 +1,36 @@
+/*
+ * @Author: WangQiBiao
+ * @LastEditors: WuWei
+ * @Description: 全局参数设置
+ * @Date: 2019-03-12 09:40:46
+ * @LastEditTime: 2019-06-14 11:24:44
+ */
+const REQUEST_MATCHING_HASH = `/9ad134a361f8d778` // 【9ad134a361f8d778 = 前端服务,用于后端接口匹配】
+
+export default {
+  /**
+     * @description token在Cookie中存储的天数,默认1天
+     */
+  cookieExpires: 1,
+  /**
+     * @description 是否使用国际化,默认为false
+     *              如果不使用,则需要在路由中给需要在菜单中展示的路由设置meta: {title: 'xxx'}
+     *              用来在菜单中显示文字
+     */
+  useI18n: false,
+  /**
+     * @description api请求基础路径
+     */
+  baseUrl: {
+    pro: `${REQUEST_MATCHING_HASH}`, // 生产环境
+    test: `${REQUEST_MATCHING_HASH}` // 测试环境
+  },
+  // 图片路径
+  fileUrl: process.env.NODE_ENV === 'production' ? `${REQUEST_MATCHING_HASH}` : 'http://139.159.229.250:8161',
+  // 导出文件路径
+  exportUrl: process.env.NODE_ENV === 'production' ? `${REQUEST_MATCHING_HASH}` : '/api',
+  /**
+     * 输入框最长值
+     */
+  maxTextNum: 20
+}

+ 3 - 3
src/route/white-list.js

@@ -2,7 +2,7 @@
  * @Author: WangQiBiao
  * @Date: 2019-09-18 09:40:26
  * @LastEditors: WangQiBiao
- * @LastEditTime: 2019-10-08 16:14:50
+ * @LastEditTime: 2019-10-08 16:55:23
  * @Description: 白名单菜单
  */
 import _import from './_import'
@@ -15,11 +15,11 @@ import _import from './_import'
 export default [
   {
     path: '',
-    redirect: 'index'
+    redirect: 'login'
   },
   {
     path: '/',
-    redirect: 'index'
+    redirect: 'login'
   },
   {
     path: '/index',