|
@@ -1,25 +1,62 @@
|
|
|
-import axios from 'axios'
|
|
|
-import mock from '@/api/mock'
|
|
|
+import axios from './axios'
|
|
|
+import qs from 'qs'
|
|
|
|
|
|
-console.log('[debug]当前环境:', process.env)
|
|
|
-const service = axios.create({
|
|
|
- baseURL: process.env.VUE_APP_BASE_API
|
|
|
-})
|
|
|
+// jquery请求
|
|
|
+const ajax = config => new Promise((resolve, reject) => {
|
|
|
+ // 获取url参数
|
|
|
+ const url = config.baseURL ? config.baseURL + config.url : process.env.VUE_APP_BASE_API + config.url
|
|
|
|
|
|
-service.interceptors.request.use(config => {
|
|
|
- // 在请求发送之前做一些事
|
|
|
- return config
|
|
|
-}, function (error) {
|
|
|
- // 当出现请求错误是做一些事
|
|
|
- console.log('--请求超时--', error)
|
|
|
- return Promise.reject(error)
|
|
|
-})
|
|
|
+ const ajaxConf = {
|
|
|
+ url: url,
|
|
|
+ method: config.method
|
|
|
+ }
|
|
|
|
|
|
-// 添加一个返回拦截器
|
|
|
-service.interceptors.response.use(response => {
|
|
|
- return response.data
|
|
|
-})
|
|
|
+ // 获取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
|
|
|
+ }
|
|
|
|
|
|
-const useMock = process.env.NODE_ENV === 'development' && process.env.VUE_APP_MOCK === 'true'
|
|
|
+ // 此处数据预处理
|
|
|
+
|
|
|
+ // 此处添加请求拦截(请求发送前处理参数)
|
|
|
+ // $.ajaxPrefilter((options, originalOptions, jqXHR) => {
|
|
|
+ // console.log('options', options)
|
|
|
+ // console.log('originalOptions', originalOptions)
|
|
|
+ // console.log('jqXHR', jqXHR)
|
|
|
+ // })
|
|
|
+
|
|
|
+ $.ajax({
|
|
|
+ ...ajaxConf,
|
|
|
+ beforeSend: xhr => {},
|
|
|
+ success: res => {
|
|
|
+ // 此处添加响应拦截
|
|
|
+ resolve(res)
|
|
|
+ },
|
|
|
+ error: err => {
|
|
|
+ reject(err)
|
|
|
+ }
|
|
|
+ })
|
|
|
+})
|
|
|
|
|
|
-export default useMock ? mock : service
|
|
|
+const useJQueryAjax = !!window.antiAdd
|
|
|
+export default useJQueryAjax ? ajax : axios
|