2 Commits 988de245c5 ... 05c04c8544

Author SHA1 Message Date
  zjz 05c04c8544 Merge branch 'dev' of http://192.168.2.241:3000/tengmingxue/dcWaterService into dev 9 months ago
  zjz 02a2568ee6 1、request.js 9 months ago
1 changed files with 49 additions and 0 deletions
  1. 49 0
      src/utils/request.js

+ 49 - 0
src/utils/request.js

@@ -1,3 +1,11 @@
+/*
+ * @Author: zjz
+ * @Date: 2023-10-10 19:30:10
+ * @LastEditors: zjz
+ * @LastEditTime: 2024-07-02 14:15:52
+ * @FilePath: \dcWaterService\src\utils\request.js
+ * @Description:
+ */
 import axios from 'axios'
 import { MessageBox, Message } from 'element-ui'
 import store from '@/store'
@@ -8,6 +16,7 @@ import router from '@/router'
 // 创建axios实例
 // export const IP = 'http://192.168.2.15:10081'
 export const IP = 'http://36.138.232.124:10085'
+// export const IP = 'http://localhost:9527/api/'
 // export const IP = 'http://221.182.8.141:10085'
 export const NewIp = 'http://58.17.241.6:1212'
 const service = axios.create({
@@ -21,6 +30,8 @@ let requestTimes = 0
 // 请求拦截器
 service.interceptors.request.use(
   config => {
+    console.log('url', config.url)
+
     if (config.method === 'post' && config.url === '/auth/oauth/login') { // 请求为post    使用qs转换数据
       config.data = qs.stringify(config.data)
       config.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
@@ -223,4 +234,42 @@ export function getSXJList(data) {
   })
 }
 
+/**
+ * @description 封装Ajax请求
+ * @param {* 接口请求参数} json
+ * @param {* 返回JSON参数} callback
+ * @returns
+ */
+export function AjaxRequest(json, callback) {
+  var p = new Promise(function(resolve, reject) {
+    $.ajax({
+      url: json.url, // 请求的url地址
+      dataType: json.dataType, // 返回格式为json
+      async: json.isAsync, // 请求是否异步,默认为异步,这也是ajax重要特性
+      data: json.data, // 参数值
+      type: json.method, // 请求方式
+      xhrFields: {
+        withCredentials: false
+      },
+      crossDomain: true,
+      success: function(response) {
+        // 请求成功时处理
+        callback(response)
+        resolve()
+      },
+      error: function(XMLHttpRequest, textStatus, errorThrown) {
+        // 请求出错处理
+        if (XMLHttpRequest.status == '401') {
+          window.parent.location = '/enterprise/enterprise_login.html'
+          self.location = '/enterprise/enterprise_login.html'
+        } else {
+          alert(XMLHttpRequest.responseText)
+        }
+        reject()
+      }
+    })
+  })
+  return p
+}
+
 export default service