Browse Source

接口添加环境判断

xiaohaizhao 2 years ago
parent
commit
1aead664af
1 changed files with 61 additions and 55 deletions
  1. 61 55
      utils/Http.js

+ 61 - 55
utils/Http.js

@@ -1,60 +1,66 @@
 class HTTP {
-  constructor() {
-    // this.baseUrl = "https://meida.cnyunl.com";
-    this.baseUrl = "http://192.168.3.111:8100";
-  }
-  request({
-    url,
-    data = {},
-    method = "POST",
-    header = {
-      'content-type': 'application/json'
-    },
-    loading = true
-  }) {
-    return new Promise((resolve, reject) => {
-      this._request(url, resolve, reject, data, method, header, loading);
-    })
-  }
-  _request(url, resolve, reject, data, method, header, loading) {
-    if (loading) wx.showLoading({
-      title: '加载中...',
-      mask: true
-    })
-    wx.request({
-      url: this.baseUrl + '/yos/rest/index' + url,
-      data: data,
-      method: method,
-      header: header,
-      timeout: 60000,
-      success: (res) => {
-        resolve(res.data);
-        if (loading) wx.hideLoading();
-      },
-      fial: (err) => {
-        reject(err);
-        if (loading) wx.hideLoading()
-      },
-      complete: (res) => {
-        if (res.errMsg != 'request:ok') {
-          wx.hideLoading()
-          wx.showToast({
-            title: '网络异常,请重新进入',
-            icon: "none"
-          })
-        } else if (res.data.msg == '登陆状态已过期,请重新登陆!') {
-          wx.redirectTo({
-            url: '/pages/login/phone',
-          });
-          wx.showToast({
-            title: '登陆状态已过期,请重新登陆!',
-            icon: "none"
-          })
+    constructor() {
+        let ENV = wx.getAccountInfoSync().miniProgram.envVersion;
+        // ENV = 'release';
+        if (ENV === 'release') { // 正式版
+            this.baseUrl = "https://meida.cnyunl.com";
+        } else {
+            this.baseUrl = "http://192.168.3.111:8100";
         }
-      }
-    })
-  }
+        console.log("接口地址", this.baseUrl)
+    }
+    request({
+        url,
+        data = {},
+        method = "POST",
+        header = {
+            'content-type': 'application/json'
+        },
+        loading = true
+    }) {
+        return new Promise((resolve, reject) => {
+            this._request(url, resolve, reject, data, method, header, loading);
+        })
+    }
+    _request(url, resolve, reject, data, method, header, loading) {
+        if (loading) wx.showLoading({
+            title: '加载中...',
+            mask: true
+        })
+        wx.request({
+            url: this.baseUrl + '/yos/rest/index' + url,
+            data: data,
+            method: method,
+            header: header,
+            timeout: 60000,
+            success: (res) => {
+                resolve(res.data);
+                if (loading) wx.hideLoading();
+            },
+            fial: (err) => {
+                reject(err);
+                if (loading) wx.hideLoading()
+            },
+            complete: (res) => {
+                if (res.errMsg != 'request:ok') {
+                    wx.hideLoading()
+                    wx.showToast({
+                        title: '网络异常,请重新进入',
+                        icon: "none"
+                    })
+                } else if (res.data.msg == '登陆状态已过期,请重新登陆!') {
+                    wx.redirectTo({
+                        url: '/pages/login/phone',
+                    });
+                    wx.showToast({
+                        title: '登陆状态已过期,请重新登陆!',
+                        icon: "none"
+                    })
+                }
+            }
+        })
+    }
 }
 export {
-  HTTP
+    HTTP
 }