You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.2 KiB

import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
const debug = process.env.NODE_ENV !== "production";
import cookie from "@/utils/store/cookie";
5 years ago
import {
getUserInfo,
getUser
5 years ago
} from "@/api/user";
import dialog from "@/utils/dialog";
const LOGIN_KEY = "login_status";
const vuexStore = new Vuex.Store({
5 years ago
state: {
// 是否已经在授权页面
isAuthorizationPage: false,
// 是否授权
isAuthorization: false,
// 不建议从这里取 token,但是删除掉会影响其他的页面
token: cookie.get(LOGIN_KEY) || null,
userInfo: cookie.get('userInfo'),
5 years ago
$deviceType: null,
},
mutations: {
SHOW_FOOTER(state) {
state.footer = true;
},
HIDE_FOOTER(state) {
state.footer = false;
},
SHOW_HOME(state) {
state.home = true;
},
HIDE_HOME(state) {
state.home = false;
},
OPEN_HOME(state) {
state.homeActive = true;
},
CLOSE_HOME(state) {
state.homeActive = false;
},
CHANGE_TABTAR(state, index) {
state.tabtarIndex = index;
},
LOGIN(state, token, expires_time) {
state.token = token;
cookie.set(LOGIN_KEY, token, expires_time);
5 years ago
},
LOGOUT(state) {
state.token = null;
state.userInfo = null
cookie.clearAll()
5 years ago
},
BACKGROUND_COLOR(state, color) {
state.color = color;
// document.body.style.backgroundColor = color;
},
UPDATE_USERINFO(state, userInfo) {
5 years ago
state.userInfo = userInfo;
if (userInfo) {
cookie.set('userInfo', userInfo)
} else {
cookie.set('userInfo', null)
}
5 years ago
},
UPDATE_AUTHORIZATIONPAGE(state, isAuthorizationPage) {
state.isAuthorizationPage = isAuthorizationPage;
},
UPDATE_AUTHORIZATION(state, isAuthorization) {
state.isAuthorization = isAuthorization;
},
UPDATE_DEVICETYPE(state, $deviceType) {
state.$deviceType = $deviceType;
},
},
actions: {
USERINFO({ state, commit }, force) {
if (state.userInfo !== null && !force) {
5 years ago
return Promise.resolve(state.userInfo);
} else {
5 years ago
return new Promise(reslove => {
getUserInfo().then(res => {
commit("UPDATE_USERINFO", res.data);
reslove(res.data);
});
}).catch(() => {
dialog.error("获取信息失败!");
});
}
},
getUser({ state, commit }) {
return new Promise(reslove => {
getUser().then(res => {
console.log(res)
commit("UPDATE_USERINFO", res.data);
reslove(res.data);
});
}).catch((error) => {
console.log(error)
dialog.error("获取信息失败!");
});
5 years ago
},
changeLogin({
state,
commit
}, data, date) {
commit("LOGIN", data, date);
},
setUserInfo({
state,
commit
}, user) {
commit("UPDATE_USERINFO", user);
},
changeAuthorizationPage({
state,
commit
}, index) {
commit("UPDATE_AUTHORIZATIONPAGE", index);
},
changeAuthorization({
state,
commit
}, index) {
commit("UPDATE_AUTHORIZATION", index);
},
},
getters: {
isAuthorizationPage: state => state.isAuthorizationPage,
isAuthorization: state => state.isAuthorization,
token: state => state.token,
isLogin: state => !!state.token,
userInfo: state => state.userInfo || {},
},
strict: debug
});
export default vuexStore