From 52d463e03c1f074099ed8e8a6b7c3ddde52d2708 Mon Sep 17 00:00:00 2001
From: quanyawei <401863037@qq.com>
Date: Fri, 27 Oct 2023 14:50:28 +0800
Subject: [PATCH] fix:小程序初版

---
 utils/request.js |   76 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 76 insertions(+), 0 deletions(-)

diff --git a/utils/request.js b/utils/request.js
new file mode 100644
index 0000000..d863cdf
--- /dev/null
+++ b/utils/request.js
@@ -0,0 +1,76 @@
+import storage from './storage' // ������������
+import store from '../store/index.js'
+export default {
+    console(options) {
+        if (config.debug) {
+            console.log('<<===============================================>>')
+            // console.log("request start");
+            // console.log("header" + JSON.stringify(options.header));
+            // console.log("method: " + options.method + " URL: " + options.url);
+            // console.log(options.data);
+            // console.log("request end");
+            // console.log("<<===============================================>>");
+        }
+    },
+    domain() {
+        return config.uni_app_web_api_url.replace('api', '')
+    },
+    send(options = {}, isLogin = true) {
+        const baseUrl = process.uniEnv.baseUrl
+        storage.set('baseUrl', baseUrl)
+        // loading������
+        uni.showLoading({ title: '���������', })
+        // ���������������������������������������������
+        // uni.showLoading({ title: baseUrl, })
+        options.url = baseUrl + '' + options.url
+        // ������������
+        options.method = options.method || 'GET'
+        // ������������������
+        if (isLogin) {
+            let token = uni.getStorageSync('tonken')
+            console.log('token', token)
+            if (token !== null) {
+                // options.header["token"] = token;
+                options.header = {
+                    token: token,
+                    Authorization: token,
+                }
+            }
+        }
+        // this.console(options); // ���������������������������������������������������
+        // ������Promise������
+        return new Promise((resolve, reject) => {
+            uni.request(options).then(data => {
+                var [error, res] = data
+                if (error !== null) {
+                    reject(error)
+                } else {
+                    // ���������������������������������������������������������������������������
+                    if (res.data.code === 0) {
+                        uni.hideLoading()
+                        // uni.navigateTo({
+                        //   url: "/pages/Login/login/login",
+                        // });
+                        resolve(res.data)
+                    } else {
+                        uni.hideLoading()
+                        reject(res.data.message)
+                    }
+                }
+            })
+        })
+    },
+    get(url = '', data = {}, isLogin = true) {
+        return this.send({
+            url: url,
+            data: data,
+        }, isLogin)
+    },
+    post(url = '', data = {}, isLogin = true) {
+        return this.send({
+            url: url,
+            data: data,
+            method: 'POST',
+        }, isLogin)
+    },
+}
\ No newline at end of file

--
Gitblit v1.8.0