shenjunjie 6 years ago
parent
commit
b71ca5d6d3
3 changed files with 12 additions and 11 deletions
  1. 2 1
      middleware/authenticated.js
  2. 8 9
      plugins/axios-nuxt.js
  3. 2 1
      store/index.js

+ 2 - 1
middleware/authenticated.js

@@ -1,9 +1,10 @@
 
 export default function ({ isServer, store, req, redirect }) {
   if (isServer && !req) return
+  console.log(store.state.option.isLogin.data)
 
   // if (!store.state.option.isLogin.logged) {
-  if (!store.state.option.isLogin.data.content.isLogin) {
+  if (!store.state.option.isLogin.data.content || !store.state.option.isLogin.data.content.isLogin) {
     return redirect('/')
   }
 }

+ 8 - 9
plugins/axios-nuxt.js

@@ -1,24 +1,23 @@
 import Vue from 'vue'
 import Axios from 'axios'
 
-export function axiosHttp(store) {
+export function axiosHttp(store, req) {
   const service = Axios.create({
     withCredentials: true,
     baseUrl: '/'
   })
   service.interceptors.request.use(config => {
+    // console.log(config.url)
     if (process.server) {
+      const cookie = 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
     }
-    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
-    // } else {
-      // console.log(store)
-    // }
+    // console.log(config.headers)
     return config
   }, error => {
     return Promise.reject(error)
@@ -49,6 +48,6 @@ export function axiosHttp(store) {
 }
 
 export default function ({ isServer, store, req }) {
-  axiosHttp(store)
+  axiosHttp(store, req)
 }
 

+ 2 - 1
store/index.js

@@ -1,11 +1,12 @@
 import { axiosHttp } from '~plugins/axios-nuxt'
 import cityService from '~plugins/city-service'
-const axios = axiosHttp()
+var axios = null
 export const actions = {
   // 全局服务初始化
   nuxtServerInit (store, context) {
     // console.log(axios)
     // 检查设备类型
+    axios = axiosHttp(store, context.req)
     const route = context.route
     const isServer = context.isServer
     const isDev = context.isDev