wangcz 6 éve
szülő
commit
171a889a96
3 módosított fájl, 36 hozzáadás és 20 törlés
  1. 3 3
      nuxt.config.js
  2. 12 11
      plugins/axios-nuxt.js
  3. 21 6
      store/index.js

+ 3 - 3
nuxt.config.js

@@ -91,9 +91,9 @@ module.exports = {
     baseUrl,
   },
   plugins: [
-    {
-      src: '~plugins/axios-nuxt.js'
-    },
+    // {
+    //   src: '~plugins/axios-nuxt.js'
+    // },
   //   {
   //   src: '~plugins/axios.js'
   // },

+ 12 - 11
plugins/axios-nuxt.js

@@ -1,22 +1,23 @@
 import Vue from 'vue'
 import Axios from 'axios'
 
-export function axiosHttp(store, req) {
+// export default function(store, req) {
   const service = Axios.create({
     withCredentials: true,
     baseUrl: '/'
   })
   service.interceptors.request.use(config => {
+    config.url = config.url || '/'
     if (process.server) {
-      const cookie = req.headers['cookie']
-      store.commit('option/SET_COOKIES', cookie)
+      // const cookie = config.req.headers['cookie']
+      // store.commit('option/SET_COOKIES', cookie)
       if (config.url.indexOf('.') === -1) {
         config.url = process.env.baseUrl + config.url
       }
-      config.headers.cookie = store.state.option.cookies + '; ' +  store.state.option.sessionId
-      config.headers['User-Agent'] = store.state.option.userAgent
+      // config.headers.cookie = store.state.option.cookies + '; ' +  store.state.option.sessionId
+      // config.headers['User-Agent'] = store.state.option.userAgent
     }
-    console.log(config.url)
+    // console.log(config.url)
     // if (typeof window === 'undefined') {
       // config.headers.cookie = store.state.option.cookies + '; ' + store.state.option.sessionId
       // // config.headers['User-Agent'] = Vue.$store.state.option.userAgent
@@ -48,11 +49,11 @@ export function axiosHttp(store, req) {
   })
 
   Vue.prototype.$http = service
-  return service
+export default service
 
-}
+// }
 
-export default function ({ isServer, store, req }) {
-  axiosHttp(store, req)
-}
+// export default function ({ isServer, store, req }) {
+//   axios(store, req)
+// }
 

+ 21 - 6
store/index.js

@@ -1,18 +1,17 @@
-import { axiosHttp } from '~plugins/axios-nuxt'
+import axios from '~plugins/axios-nuxt'
 import cityService from '~plugins/city-service'
-var axios = null
+// var axios = axiosHttp()
 export const actions = {
   // 全局服务初始化
   nuxtServerInit (store, context) {
-    // console.log(axios)
     // 检查设备类型
-    axios = axiosHttp(store, context.req)
+    // axios = axiosHttp(store, context.req)
     const route = context.route
     const isServer = context.isServer
     const isDev = context.isDev
-    const userAgent = isServer ? req.headers['user-agent'] :context.req.headers['user-agent']
+    const userAgent = isServer ? context.req.headers['user-agent'] :context.req.headers['user-agent']
     const isMobile = /(iPhone|iPad|Opera Mini|Android.*Mobile|NetFront|PSP|BlackBerry|Windows Phone)/ig.test(userAgent)
-    const cookie = isServer ? req.headers['cookie'] : null
+    const cookie = process.server ? context.req.headers['cookie'] : null
     store.commit('option/SET_MOBILE_LAYOUT', isMobile)
     store.commit('option/SET_USER_AGENT', userAgent)
     store.commit('option/SET_COOKIES', cookie)
@@ -34,6 +33,21 @@ export const actions = {
       store.commit('option/SET_INVITEUSERUU', inviteUserUU)
       store.commit('option/SET_INVITATIONTIME', invitationTime)
     }
+    if (cookie && cookie.length) {
+      // let cookies = cookie.split(';')
+      // for (let i = 0; i < cookies.length; i++) {
+      //   let cookieArr = cookies[i].split('=')
+      //   if (cookieArr.length === 2 && cookieArr[0] === 'JSESSIONID') {
+      //     store.commit('option/SET_SESSION_ID', cookieArr[1])
+      //     break
+      //   }
+      // }
+      axios.defaults.headers['cookie'] = store.state.option.cookies
+      axios.defaults.headers['User-Agent'] = store.state.option.userAgent
+    } else {
+      axios.defaults.headers['cookie'] = ''
+      axios.defaults.headers['User-Agent'] = ''
+    }
     // 设置跳转的URL
     if (!isDev) {
       store.commit('option/UPDATE_URL', 'http://192.168.253.66:8081/')
@@ -70,6 +84,7 @@ export const actions = {
   // 获取成员列表信息
   loadMemberList ({ commit }, params = {}) {
     commit('cloudCenter/REQUEST_MEMBER')
+    // console.log(axios)
     return axios.get('/sso/center/member/info', {params})
       .then(res => {
         commit('cloudCenter/REQUEST_MEMBER_SUCCESS', res.data)