yangan
2025-02-27 ea7457be6870e35cd385e163ca9bba26d7621d66
api/request.js
@@ -1,13 +1,35 @@
import {
   globalApi
} from '@/api/globalApi.js';
import {
   apiLoginPassword
} from './publicInterface';
// 成功
const responseOK = (response) => {
   const {
      data
   } = response;
   test(data);
   return data;
}
const test = (obj) => {
   if (obj instanceof Array) {
      obj.forEach(e => {
         test(e);
      })
   } else if (obj instanceof Object) {
      for (let key in obj) {
         if (obj[key] == null) {
            obj[key] = "";
         } else {
            if (obj[key] instanceof Array || obj[key] instanceof Object) {
               test(obj[key])
            }
         }
      }
   }
}
// 响应失败的依赖性
@@ -78,10 +100,22 @@
   500: responseError
}
// const BaseUrl = "http://192.168.0.120:9997"
// const webSocketUrl = "ws://192.168.0.120:9997/wrzs/ws/info"
const BaseUrl = "http://192.168.110.101:9997";
const webSocketUrl = "ws://192.168.110.101:9997/wrzs/ws/info"
// const BaseUrl = "http://192.168.0.110:9896"
// const webSocketUrl = "ws://192.168.0.110:9896/wrzs/ws/info"
const BaseUrl = "http://192.168.0.87:9896"
const webSocketUrl = "ws://192.168.0.87:9896/wrzs/ws/info"
export const onlineurl = "https://mr1.res.jzeg.cn:9096";
// const BaseUrl = "http://192.168.0.100:9997";
// const webSocketUrl = "ws://192.168.0.100:9997/wrzs/ws/info"
// const BaseUrl = "http://192.168.0.108:9997";
// const webSocketUrl = "ws://192.168.0.108:9997/wrzs/ws/info"
// 线上生产
// const BaseUrl = "https://mr1.res.jzeg.cn:9096";
// const webSocketUrl = "wss://mr1.res.jzeg.cn:9096/wrzs/ws/info"
// 请求拦截
const fetch = (url, opt) => {
@@ -89,7 +123,12 @@
   let urls = (globalApi[opt.url]?.url || globalApi[url]?.url);
   let params = opt.params ? ('?' + Object.keys(opt.params).map(key => key + '=' + opt.params[key]).join('&')) :
      '';
   opt.url = BaseUrl + urls + params
   // #ifdef H5
   opt.url = urls + params
   // #endif
   // #ifndef H5
   opt.url = BaseUrl +urls + params
   // #endif
   opt.method = opt.method || "GET";
   var header = {}
   if (uni.getStorageSync('token')) {
@@ -109,9 +148,17 @@
   return new Promise((resolve, reject) => {
      let options = {}
      if (opt.data) {
         Object.keys(opt.data).map(key => {
            if (opt.data[key] == '' || opt.data[key] == "") {
               opt.data[key] = null;
            }
         });
      }
      Object.keys(opt).map(key => {
         if (key !== "params") {
            return options[key] = opt[key]
         }
      })
@@ -121,6 +168,59 @@
         .catch(err => interceptorsErr(err, reject))
   })
}
const fetchId = (url, opt) => {
    // 查找对应地址
    let urls = (globalApi[opt.url]?.url || globalApi[url]?.url);
    let params = opt.data ? ('/' +opt.data ):
    '';
    // #ifdef H5
    opt.url = urls + params
    // #endif
    // #ifndef H5
    opt.url = BaseUrl +urls + params
    // #endif
    opt.method = opt.method || "GET";
    var header = {}
    if (uni.getStorageSync('token')) {
        header = {
            "Authorization": "Bearer" + ' ' + uni.getStorageSync('token'),
            "clientToc": "Y",
            'CLIENT_TOC': 'Y',
        }
    }
    opt.header = {
        ...opt.header,
        ...header
    }
    opt.data = opt.data || {};
    return new Promise((resolve, reject) => {
        let options = {}
        if (opt.data) {
            Object.keys(opt.data).map(key => {
                if (opt.data[key] == '' || opt.data[key] == "") {
                    opt.data[key] = null;
                }
            });
        }
        Object.keys(opt).map(key => {
            if (key !== "params") {
                return options[key] = opt[key]
            }
        })
        uni.request(options)
            .then(res => interceptorsRes(res, resolve, reject))
            .catch(err => interceptorsErr(err, reject))
    })
}
// 响应拦截
@@ -147,6 +247,7 @@
export {
   fetch,
   fetchId,
   BaseUrl,
   webSocketUrl
}