baid222u vor 5 Jahren
Ursprung
Commit
7d185033f2
5 geänderte Dateien mit 88 neuen und 73 gelöschten Zeilen
  1. 5 1
      nuxt.config.js
  2. 9 6
      package.json
  3. 21 2
      pages/index.vue
  4. 16 8
      pages/me/chooseArea.vue
  5. 37 56
      plugins/axios.js

+ 5 - 1
nuxt.config.js

@@ -41,7 +41,7 @@ export default {
     '@nuxtjs/axios',
     // Doc: https://bootstrap-vue.js.org/docs/
     'bootstrap-vue/nuxt',
-    '@nuxtjs/pwa'
+    '@nuxtjs/pwa',
   ],
   /*
    ** Axios module configuration
@@ -49,7 +49,11 @@ export default {
   axios: {
     // See https://github.com/nuxt-community/axios-module#options
   },
+  proxy: {
 
+
+
+  },
   /*
    ** Build configuration
    */

+ 9 - 6
package.json

@@ -13,12 +13,12 @@
     "precommit": "npm run lint"
   },
   "dependencies": {
-    "cross-env": "^5.2.0",
-    "nuxt": "^2.4.0",
-    "bootstrap-vue": "^2.0.0-rc.11",
+    "@nuxtjs/pwa": "^2.6.0",
     "bootstrap": "^4.1.3",
-    "@nuxtjs/axios": "^5.3.6",
-    "@nuxtjs/pwa": "^2.6.0"
+    "bootstrap-vue": "^2.0.0-rc.11",
+    "cross-env": "^5.2.0",
+    "extract-text-webpack-plugin": "^4.0.0-beta.0",
+    "nuxt": "^2.4.0"
   },
   "config": {
     "nuxt": {
@@ -27,7 +27,10 @@
     }
   },
   "devDependencies": {
+    "@gauseen/nuxt-proxy": "^1.0.1",
+    "@nuxtjs/axios": "^5.5.2",
     "@nuxtjs/eslint-config": "^0.0.1",
+    "@nuxtjs/proxy": "^1.3.3",
     "babel-eslint": "^10.0.1",
     "eslint": "^5.15.1",
     "eslint-config-prettier": "^4.1.0",
@@ -46,4 +49,4 @@
     "prettier": "^1.16.4",
     "sass-loader": "^7.1.0"
   }
-}
+}

+ 21 - 2
pages/index.vue

@@ -88,9 +88,28 @@ export default {
   },
   data() {
     return {
-      author: '微信公众号 jinkey-love'
+      author: '微信公众号 jinkey-love',
+      http:{}
     }
   },
+   fetch ({ app }) {
+    //console.log(app.$axios)
+  },
+  asyncData ({ app }) {
+    //this.http=app
+    // app.$axios.get('/province.html').then((response)=>{
+    //     console.log(response)
+    //   }).catch((response)=>{
+    //     //console.log(response.response);
+    //   })
+  },
+  created () {
+   this.$axios.get('/province.html').then((response)=>{
+        console.log(response)
+      }).catch((response)=>{
+        //console.log(response.response);
+      })
+  },
   methods: {
     Subscribe(e) {
       this.$router.push({ path: '/bidding/subList' })
@@ -103,7 +122,7 @@ export default {
 .bid {
   height: 100%;
 }
-body  div {
+body div {
   height: 100%;
 }
 .p1 {

+ 16 - 8
pages/me/chooseArea.vue

@@ -14,22 +14,30 @@ import areaone from '~/components/areaSelect.vue'
 import tit from '~/components/titles.vue'
 
 export default {
-  data(){
-    return{
-      name:'选择区域'
+  data() {
+    return {
+      name: '选择区域'
     }
   },
- components:{
-   areaone,tit
- }
+  components: {
+    areaone,
+    tit
+  },
+  created() {
+    // this.$axios.get('/province.html').then((response)=>{
+    //     console.log(response)
+    //   }).catch((response)=>{
+    //     //console.log(response.response);
+    //   })
+  }
 }
 </script>
 
 <style>
 body {
-  background-color: #fff!important;
+  background-color: #fff !important;
 }
-.page_detail_list{
+.page_detail_list {
   padding: 0 1.5rem;
 }
 </style>

+ 37 - 56
plugins/axios.js

@@ -1,26 +1,25 @@
-import axios from 'axios'
-import qs from 'qs'
-// 在config.js文件中统一存放一些公共常量,方便之后维护
-import { baseURL } from './config.js'
-
-// 添加请求拦截器,在发送请求之前做些什么(**具体查看axios文档**)--------------------------------------------
-axios.interceptors.request.use(function (config) {
-  // 显示loading
-  return config
-}, function (error) {
-  // 请求错误时弹框提示,或做些其他事
-  return Promise.reject(error)
-})
-
-// 添加响应拦截器(**具体查看axios文档**)----------------------------------------------------------------
-axios.interceptors.response.use(function (response) {
-  // 对响应数据做点什么,允许在数据返回客户端前,修改响应的数据
-  // 如果只需要返回体中数据,则如下,如果需要全部,则 return response 即可
-  return response.data
-}, function (error) {
-  // 对响应错误做点什么
-  return Promise.reject(error)
-})
+export default function (app) {
+  let axios = app.$axios; 
+ // 基本配置
+  axios.defaults.timeout = 10000
+  axios.defaults.baseURL = 'https://gcb.china0001.com.cn/pubdata'
+  axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
+  axios.interceptors.request.use(function (config) {
+    console.log('请求之前做什么')
+    return config
+  }, function (error) {
+    // 请求错误时弹框提示,或做些其他事
+    return Promise.reject(error)
+  })
+  axios.interceptors.response.use(function (response) {
+    // 对响应数据做点什么,允许在数据返回客户端前,修改响应的数据
+    // 如果只需要返回体中数据,则如下,如果需要全部,则 return response 即可
+    console.log('数据返回客户端前')
+    return response.data
+  }, function (error) {
+    // 对响应错误做点什么
+    return Promise.reject(error)
+  })
 
 // 封装数据返回失败提示函数---------------------------------------------------------------------------
 function errorState (response) {
@@ -45,39 +44,21 @@ function successState (res) {
 }
 
 // 封装axios--------------------------------------------------------------------------------------
-function apiAxios (method, url, params) {
-  let httpDefault = {
-    method: method,
-    baseURL: baseURL,
-    url: url,
-    // `params` 是即将与请求一起发送的 URL 参数
-    // `data` 是作为请求主体被发送的数据
-    params: method === 'GET' || method === 'DELETE' ? params : null,
-    data: method === 'POST' || method === 'PUT' ? qs.stringify(params) : null,
-    timeout: 10000
-  }
+  // 请求回调
+  axios.onRequest(config => {
+    console.log('请求回调')
+    //console.log(config)
+  })
 
-  // 注意**Promise**使用(Promise首字母大写)
-  return new Promise((resolve, reject) => {
-    axios(httpDefault)
-      // 此处的.then属于axios
-      .then((res) => {
-        successState(res)
-        resolve(res)
-      }).catch((response) => {
-        errorState(response)
-        reject(response)
-      })
+  // 返回回调
+  axios.onResponse(res => {
+    console.log('返回回调')
+    //console.log(res)
   })
-}
 
-// 输出函数getAxios、postAxios、putAxios、delectAxios,供其他文件调用-----------------------------
-// Vue.js的插件应当有一个公开方法 install。这个方法的第一个参数是 Vue 构造器,第二个参数是一个可选的选项对象。
-export default {
-  install: function (Vue) {
-    Vue.prototype.getAxios = (url, params) => apiAxios('GET', url, params)
-    Vue.prototype.postAxios = (url, params) => apiAxios('POST', url, params)
-    Vue.prototype.putAxios = (url, params) => apiAxios('PUT', url, params)
-    Vue.prototype.delectAxios = (url, params) => apiAxios('DELECT', url, params)
-  }
-}
+  // 错误回调
+  axios.onError(error => {
+    console.log('错误回调')
+    //console.log(error)
+  })
+}