Browse Source

调通axios

yellowtaotao 3 years ago
parent
commit
207c228c72

+ 5 - 2
.env.development

@@ -1,2 +1,5 @@
-API_LIST_NORMAL_URL = "http://8.141.68.219:8080"
-BASE_URL = "http://8.141.68.219:8080"
+module.exports = {
+  NODE_ENV = 'development'
+  VUE_APP_BASE_API = 'http://8.141.68.219:8080'
+  VUE_APP_VERSION = '0.0.1'
+}

+ 5 - 2
.env.production

@@ -1,2 +1,5 @@
-API_LIST_NORMAL_URL = "http://8.141.68.219:8080"
-BASE_URL = "http://8.141.68.219:8080"
+module.exports = {
+  NODE_ENV = 'production'
+  VUE_APP_BASE_API = 'http://8.141.68.219:8080'
+  VUE_APP_VERSION = '0.0.1'
+}

+ 5 - 2
.env.test

@@ -1,2 +1,5 @@
-API_LIST_NORMAL_URL = "http://8.141.68.219:8080"
-BASE_URL = "http://8.141.68.219:8080"
+module.exports = {
+  NODE_ENV = 'test'
+  VUE_APP_BASE_API = 'http://8.141.68.219:8080'
+  VUE_APP_VERSION = '0.0.1'
+}

+ 5 - 0
package-lock.json

@@ -9630,6 +9630,11 @@
         "unicode-match-property-value-ecmascript": "^1.2.0"
       }
     },
+    "register-service-worker": {
+      "version": "1.7.2",
+      "resolved": "https://registry.npm.taobao.org/register-service-worker/download/register-service-worker-1.7.2.tgz?cache=0&sync_timestamp=1607600654641&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fregister-service-worker%2Fdownload%2Fregister-service-worker-1.7.2.tgz",
+      "integrity": "sha1-ZRaYPh73kKmMQiWvEha8gJQaS9I="
+    },
     "regjsgen": {
       "version": "0.5.2",
       "resolved": "https://registry.npm.taobao.org/regjsgen/download/regjsgen-0.5.2.tgz",

+ 1 - 0
package.json

@@ -15,6 +15,7 @@
     "less": "^4.1.1",
     "less-loader": "^6.0.0",
     "nprogress": "^0.2.0",
+    "register-service-worker": "^1.7.2",
     "vue": "^2.6.11",
     "vue-router": "^3.5.1",
     "vuex": "^3.6.2",

+ 2 - 7
src/api/index.js

@@ -9,7 +9,7 @@ import Vue from "vue";
 const subList = [
   {
     name: "API_LIST_NORMAL",
-    url: "API_LIST_NORMAL_URL"
+    url: "VUE_APP_BASE_API"
   }
 ];
 
@@ -23,17 +23,12 @@ moduleFiles.keys().forEach(modulePath => {
 // 合成API地址集合
 let apiList = {};
 subList.forEach(it => {
-  console.log(API_LIST)
   let subApi = Object.entries(API_LIST[it.name]).reduce((target, [key, value]) => {
-    console.log(process.env)
-    // target[key] = process.env[it.url].replace(/\W*$/, "") + "/" + value.replace(/^\W*/, "");
-    target[key] = 'http://8.141.68.219:8080' + "/" + value.replace(/^\W*/, "");
+    target[key] = process.env[it.url].replace(/\W*$/, "") + "/" + value.replace(/^\W*/, "");
     return target;
   }, {});
-  // console.log(it)
   apiList = { ...apiList, ...subApi };
 });
-console.log(apiList)
 
 // 注册所有API地址
 Object.defineProperty(Vue.prototype, "$_API", {

+ 20 - 6
src/main.js

@@ -2,11 +2,20 @@ import Vue from 'vue'
 import App from './App.vue'
 // 路由
 import router from './router'
-Vue.use(router);
+// Vue.use(router);
 
 // Vuex
 import store from './store'
 
+// 是否启用PWA
+import './sw/registerServiceWorker'
+
+// 注册自定义插件
+import './plugins'
+
+// 调用共通js
+import './common'
+
 // 全局css
 import less from 'less'
 Vue.use(less);
@@ -17,16 +26,21 @@ import Antd from 'ant-design-vue';
 import 'ant-design-vue/dist/antd.css';
 Vue.use(Antd);
 
-// 注册自定义插件
-import './plugins'
-// 调用共通js
-import './common'
 
 
 Vue.config.productionTip = false;
 
+const $bus = new Vue()
+Object.defineProperties(Vue.prototype, {
+  $bus: {
+    get() {
+      return $bus
+    },
+  },
+})
+
 new Vue({
-  render: (h) => h(App),
   router: router,
   store: store,
+  render: (h) => h(App)
 }).$mount('#app')

+ 5 - 5
src/plugins/modules/axios.js

@@ -12,7 +12,7 @@ import { triggerEvent } from "@/utils/events";
 axios.defaults.headers.post["Content-Type"] = "application/json;charset=utf-8";
 
 let config = {
-  baseURL: process.env.BASE_URL || process.env.apiUrl || "",
+  baseURL: process.env.VUE_APP_BASE_API || process.env.apiUrl || "",
   // baseURL: "http://8.141.68.219:8080",
   timeout: 60 * 1000,
   withCredentials: false,
@@ -75,23 +75,23 @@ _axios.interceptors.request.use(
 _axios.interceptors.response.use(
   response => {
     // 用来判断是否请求成功
-    const success = response.status === 200 && response.data.code === 0;
+    const success = response.status === 200;
     let messages = "";
     if (!success) {
       if (typeof response.data === "string") {
         messages = "服务器错误,未获取到响应数据";
       } else {
-        if (response.status === 200 && response.data.code === 0) {
+        if (response.status === 200) {
           return Promise.reject(response);
         }
         // 请求成功,但在业务上为失败
-        messages = response.data.message || response.data.errorData || "操作执行失败";
+        messages = response.message || response.statusText || "操作执行失败";
       }
       message.error(messages);
       return Promise.reject(response);
     }
     return {
-      data: response.data.data,
+      data: response.data,
       success,
       messages
     };

+ 3 - 4
src/plugins/modules/loop_axios.js

@@ -9,8 +9,7 @@ import { getAllPromise } from "@/utils/tools";
 axios.defaults.headers.post["Content-Type"] = "application/json;charset=utf-8";
 
 let config = {
-  baseURL: process.env.BASE_URL || process.env.apiUrl || "",
-  // baseURL: "http://8.141.68.219:8080",
+  baseURL: process.env.VUE_APP_BASE_API || process.env.apiUrl || "",
   timeout: 60 * 1000,
   withCredentials: false,
   crossDomain: true,
@@ -71,10 +70,10 @@ _axios.interceptors.request.use(
 // 拦截响应
 _axios.interceptors.response.use(
   response => {
-    const success = response.status === 200 && response.data.code === 0;
+    const success = response.status === 200; //  && response.data.code === 0
     let message = "";
     return {
-      data: response.data.data,
+      data: response.data, // data
       success,
       message
     };

+ 1 - 2
src/router/index.js

@@ -23,7 +23,6 @@ commonFiles.keys().forEach(modulePath => {
 
 export default new Router({
   mode: "history",
-  base: process.env.BASE_URL,
-  // base: "http://8.141.68.219:8080",
+  base: process.env.VUE_APP_BASE_API,
   routes
 });

+ 32 - 0
src/sw/registerServiceWorker.js

@@ -0,0 +1,32 @@
+import { register } from "register-service-worker";
+
+if (process.env.NODE_ENV === "production") {
+  register(`${process.env.BASE_URL}service-worker.js`, {
+    ready() {
+      console.log(
+        "App is being served from cache by a service worker.\n" +
+          "For more details, visit https://goo.gl/AFskqB"
+      );
+    },
+    registered() {
+      console.log("Service worker has been registered.");
+    },
+    cached() {
+      console.log("Content has been cached for offline use.");
+    },
+    updatefound() {
+      console.log("New content is downloading.");
+    },
+    updated() {
+      console.log("New content is available; please refresh.");
+    },
+    offline() {
+      console.log(
+        "No internet connection found. App is running in offline mode."
+      );
+    },
+    error(error) {
+      console.error("Error during service worker registration:", error);
+    }
+  });
+}

+ 6 - 4
src/views/login/index.vue

@@ -28,14 +28,16 @@ export default {
     // 初始化数据
     initDataFun() {},
     clickFun() {
+      console.log(this.$_http);
+      console.log(this.$_API.INTERFACE_GET_USER_ADMIN_USERS);
       this.$_http
-        .get(this.$_API.JTXT_GET_USER_ADMIN_USERS)
+        .get(this.$_API.INTERFACE_GET_USER_ADMIN_USERS)
         .then((res) => {
           // 自己的操作
-          console.log(res.data[0]);
-        })
-        .catch((res) => {
           console.log(res);
+        })
+        .catch((err) => {
+          console.log(err);
         });
     },
   },