lightlimx-website/store/index.js

64 lines
1.7 KiB
JavaScript

import Cookies from 'js-cookie'
export const state = () => ({
locales: ['en-US', 'zh-CN'],
locale: 'en-US',
userInfo: {},
authorization: '',
loading:true
})
export const getters = {
isLoading(){
return this.loading;
},
myAuthorization (state) {
return state.authorization
},
userInfo (state) {
return state.userInfo
}
}
export const mutations = {
SET_LOADING(state,loading){
state.loading=loading
},
SET_LANG (state, locale) {
Cookies.set('lang', JSON.stringify(locale), { expires: 7 })
if (state.locales.indexOf(locale) !== -1) {
state.locale = locale
}
},
SET_USER_INFO (state, userInfo) {
Cookies.set('userInfo', JSON.stringify(userInfo), { expires: 7 })
state.userInfo = userInfo
},
SET_AUTHORIZATION (state, authorization) {
// 继续 时间 继续 有 cookie
Cookies.set('authorization', JSON.stringify(authorization), { expires: 7 })
state.authorization = authorization
}
}
export const actions = {
// nuxtClientInit ({ commit }, { req }) {
// const autho = localStorage.getItem('auth._token.local') // or whatever yours is called
// commit('SET_AUTHO', autho)
// console.log('From nuxtClientInit - ' + autho)
// },
// // 仅 在 当前 index 文件中生效
nuxtServerInit ({ commit }, data) {
const { req } = data
const authorization = req.headers.authorization || req.headers.Authorization || ''
const configCookie = Cookies.get()
if (authorization) {
commit('SET_AUTHORIZATION', authorization)
}
if (configCookie) {
if (configCookie.userInfo) {
commit('SET_USER_INFO', req.cookie.userInfo)
}
if (configCookie.lang) {
commit('SET_LANG', req.cookie.lang)
}
}
}
}