From 3a378f658940a13cda336c0fa6d7ef9369e7464a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TOP=E7=B3=AF=E7=B1=B3?= <1130395124@qq.com> Date: Mon, 6 Mar 2023 23:04:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9request.js=E3=80=81config.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/config.js | 1 + src/core/request.js | 49 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/core/config.js b/src/core/config.js index 2f797aa..7cfcadc 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -1,4 +1,5 @@ export default { name: "熊熊安装", + appId: "wx239055764f21ba10", root: "http://xiongxiong.vipwjf.com/api.php/wxapp" } diff --git a/src/core/request.js b/src/core/request.js index 3762cbc..c94dab2 100644 --- a/src/core/request.js +++ b/src/core/request.js @@ -1,17 +1,52 @@ +import Vue from "vue" +import store from "@/store/index"; + +function findRule(name) { + let pos = name.indexOf('.'); + if (pos > 0) { + let temp, arr = name.split('.'); + for (let i = 0; i < arr.length; i++) { + if (i == 0) { + temp = Vue.prototype.$apis[arr[i]] || {}; + } else { + temp = temp[arr[i]] || {}; + } + } + return temp; + } + + return name; +} + const request = async (args) => { - const header = { - 'X-Requested-With': 'XMLHttpRequest', - 'content-type': 'application/x-www-form-urlencoded', + let rule = findRule(args.api || {}); + if (JSON.stringify(rule) === "{}") { + throw "找不到API:" + args.api; + } + + if (rule.auth && !store.state.user.token) { + Vue.prototype.$store.commit("user/showLoginModal", true); + return; + } + + const headers = { + "XX-Wxapp-AppId": Vue.prototype.$config.appId, + 'XX-Token': store.state.user.token, + 'XX-Device-Type': 'wxapp', }; const [error, response] = await uni.request({ - url: args.url, - method: args.method || 'get', + url: Vue.prototype.$config.root + rule.url, + method: rule.method || 'get', data: args.data, - header: header + header: { + 'X-Requested-With': 'XMLHttpRequest', + 'content-type': 'application/x-www-form-urlencoded', + ...headers + } }); - return Promise.resolve(response); + return Promise.resolve(response.data); }; export default request \ No newline at end of file