Explorar o código

挂接封装请求

xiaohaizhao %!s(int64=2) %!d(string=hai) anos
pai
achega
1fbdb10a7b
Modificáronse 3 ficheiros con 127 adicións e 0 borrados
  1. 6 0
      main.js
  2. 58 0
      utils/Http.js
  3. 63 0
      utils/api.js

+ 6 - 0
main.js

@@ -7,6 +7,12 @@ Vue.config.productionTip = false;
 import uView from '@/uni_modules/uview-ui'
 Vue.use(uView)
 
+//挂载接口
+import {
+  ApiModel
+} from './utils/api'
+Vue.prototype.$Http = new ApiModel();
+
 App.mpType = 'app'
 const app = new Vue({
   ...App

+ 58 - 0
utils/Http.js

@@ -0,0 +1,58 @@
+class HTTP {
+    constructor() {
+        this.env = 'dev';
+    }
+    request({
+        url,
+        data = {},
+        method = "POST",
+        header = {
+            'content-type': 'application/json'
+        },
+        showLoading = ''
+    }) {
+        return new Promise((resolve, reject) => {
+            this._request(url, resolve, reject, data, method, header, showLoading);
+        })
+    }
+    _request(url, resolve, reject, data, method, header, showLoading) {
+        if (showLoading) uni.showLoading({
+            title: showLoading,
+            mask: true
+        })
+        uni.request({
+            // #ifdef H5
+            url: (this.env == 'dev' ? "/apis1" : "/apis") + '/waserver/rest/index' + url,
+            // #endif
+            // #ifndef H5
+            url: (this.env == 'dev' ? "http://60.204.153.188" : "https://oms.idcgroup.com.cn:8079") + '/waserver/rest/index' + url,
+            // #endif 
+            data: data,
+            method: method,
+            header: header,
+            timeout: 60000,
+            success: res => resolve(res.data),
+            fial: err => reject(err),
+            complete: (res) => {
+                if (showLoading) uni.hideLoading()
+                if (res.errMsg != 'request:ok') {
+                    uni.showToast({
+                        title: '网络异常,请重新进入',
+                        icon: "none"
+                    })
+                } else if (res.data.msg == '登陆状态已过期,请重新登陆!') {
+                    uni.redirectTo({
+                        url: '/pages/login/login',
+                    });
+                    uni.showToast({
+                        title: res.msg,
+                        icon: "none"
+                    })
+                }
+            }
+        })
+    }
+}
+export {
+    HTTP
+}

+ 63 - 0
utils/api.js

@@ -0,0 +1,63 @@
+import {
+    HTTP
+} from './Http.js'
+class ApiModel extends HTTP {
+    loginbywechat(data) {
+        return this.request({
+            url: "/loginbywechat",
+            data
+        })
+    }
+    /* 登录 */
+    login(data) {
+        return this.request({
+            url: "/loginbyaccount",
+            data
+        })
+    }
+    /* 验证码登录 */
+    plogin(data) {
+        return this.request({
+            url: "/login",
+            data
+        })
+    }
+    /* 获取验证码 */
+    getpassword(data) {
+        return this.request({
+            url: "/getpassword",
+            data
+        })
+    }
+    /* 有状态通用 */
+    basic(data, loading = true) {
+        data.accesstoken = wx.getStorageSync('userMsg').token;
+        return this.request({
+            url: "",
+            data,
+            loading
+        })
+    }
+    /* 无状态 */
+    base(data, loading = true) {
+        return this.request({
+            url: "",
+            data,
+            loading
+        })
+    }
+    /* 退出登录 */
+    logout() {
+        let data = {
+            accesstoken: wx.getStorageSync('userMsg').token
+        }
+        return this.request({
+            url: "/logout",
+            data
+        })
+    }
+
+}
+export {
+    ApiModel
+}