浏览代码

feat: [bigmemer-pc] 调整 axios

zhangyuhan 1 年之前
父节点
当前提交
20bd854f32

+ 3 - 81
apps/bigmember_pc/src/api/index.js

@@ -1,82 +1,4 @@
-import axios from './axios'
-import $ from 'jquery'
-import qs from 'qs'
+import service from './interceptors-anti'
+import './interceptors-data-models'
 
-// 此处添加全局请求拦截
-// $.ajaxPrefilter((options, originalOptions, jqXHR) => {
-//   console.log('options', options)
-//   console.log('originalOptions', originalOptions)
-//   console.log('jqXHR', jqXHR)
-// })
-// jquery请求
-const ajax = (config) =>
-  new Promise((resolve, reject) => {
-    // 获取url参数
-    const url = config.baseURL
-      ? config.baseURL + config.url
-      : import.meta.env.VITE_APP_BASE_API + config.url
-
-    const ajaxConf = {
-      url: url,
-      method: config.method
-    }
-
-    // 获取data数据
-    const data = config.data
-    const params = config.params
-    const headers = config.headers
-
-    if (ajaxConf.method.toLowerCase() === 'post') {
-      if (data) {
-        if (typeof data === 'string') {
-          // formData
-          ajaxConf.data = qs.parse(data)
-        } else {
-          // json
-          ajaxConf.contentType = 'application/json;charset=UTF-8'
-          ajaxConf.data = JSON.stringify(data)
-        }
-      }
-    } else if (ajaxConf.method.toLowerCase() === 'get') {
-      if (params) {
-        ajaxConf.data = params
-      }
-    }
-    if (headers) {
-      ajaxConf.headers = headers
-    }
-
-    // 此处数据预处理
-    // 此处添加请求拦截(请求发送前处理参数)
-
-    $.ajax({
-      ...ajaxConf,
-      beforeSend: (xhr) => {
-        // 此处请求预处理
-        // 此处添加请求拦截(请求发送前处理参数)
-      },
-      success: (res) => {
-        // 此处添加响应拦截
-        if (
-          import.meta.env.NODE_ENV === 'production' &&
-          config &&
-          !config.noIntercept &&
-          !window.$noIntercept
-        ) {
-          const noPermissionText = ['未登录', '需要登录', '需要登录!']
-          const noPermission = noPermissionText.includes(res.error_msg)
-          if (noPermission) {
-            location.href = '/notin/page'
-            return
-          }
-        }
-        resolve(res)
-      },
-      error: (err) => {
-        reject(err)
-      }
-    })
-  })
-
-const useJQueryAjax = !!window.antiAdd
-export default useJQueryAjax ? ajax : axios
+export default service

+ 82 - 0
apps/bigmember_pc/src/api/interceptors-anti.js

@@ -0,0 +1,82 @@
+import axios from './axios'
+import $ from 'jquery'
+import qs from 'qs'
+
+// 此处添加全局请求拦截
+// $.ajaxPrefilter((options, originalOptions, jqXHR) => {
+//   console.log('options', options)
+//   console.log('originalOptions', originalOptions)
+//   console.log('jqXHR', jqXHR)
+// })
+// jquery请求
+const ajax = (config) =>
+  new Promise((resolve, reject) => {
+    // 获取url参数
+    const url = config.baseURL
+      ? config.baseURL + config.url
+      : import.meta.env.VITE_APP_BASE_API + config.url
+
+    const ajaxConf = {
+      url: url,
+      method: config.method
+    }
+
+    // 获取data数据
+    const data = config.data
+    const params = config.params
+    const headers = config.headers
+
+    if (ajaxConf.method.toLowerCase() === 'post') {
+      if (data) {
+        if (typeof data === 'string') {
+          // formData
+          ajaxConf.data = qs.parse(data)
+        } else {
+          // json
+          ajaxConf.contentType = 'application/json;charset=UTF-8'
+          ajaxConf.data = JSON.stringify(data)
+        }
+      }
+    } else if (ajaxConf.method.toLowerCase() === 'get') {
+      if (params) {
+        ajaxConf.data = params
+      }
+    }
+    if (headers) {
+      ajaxConf.headers = headers
+    }
+
+    // 此处数据预处理
+    // 此处添加请求拦截(请求发送前处理参数)
+
+    $.ajax({
+      ...ajaxConf,
+      beforeSend: (xhr) => {
+        // 此处请求预处理
+        // 此处添加请求拦截(请求发送前处理参数)
+      },
+      success: (res) => {
+        // 此处添加响应拦截
+        if (
+          import.meta.env.NODE_ENV === 'production' &&
+          config &&
+          !config.noIntercept &&
+          !window.$noIntercept
+        ) {
+          const noPermissionText = ['未登录', '需要登录', '需要登录!']
+          const noPermission = noPermissionText.includes(res.error_msg)
+          if (noPermission) {
+            location.href = '/notin/page'
+            return
+          }
+        }
+        resolve(res)
+      },
+      error: (err) => {
+        reject(err)
+      }
+    })
+  })
+
+const useJQueryAjax = !!window.antiAdd
+export default useJQueryAjax ? ajax : axios

+ 4 - 0
apps/bigmember_pc/src/api/interceptors-data-models.js

@@ -0,0 +1,4 @@
+import service from './interceptors-anti'
+import { injectRequest } from '@jy/data-models'
+
+injectRequest(service)