import Vue from "vue"; import Vuex from "vuex"; import tool from "@/assets/js/toolClass"; import { tokenKey, userInfoKey } from "@/assets/js/config"; Vue.use(Vuex); export default new Vuex.Store({ state: { token: tool.getCookie(tokenKey), userInfo: tool.getCookie(userInfoKey) ? JSON.parse(tool.getCookie(userInfoKey)) : null, //书城浏览历史 historyBook: [], //电子样书列表 electronicBookList: [], //纸质样书申请列表 paperCopiesList: [], keepAliveList: [], }, getters: {}, mutations: { changeLogin(state, val) { if (val) { state.token = val; tool.setCookie(tokenKey, val); } else { state.token = null; tool.delCookie(tokenKey); } }, changeUserInfo(state, val) { if (val) { // 默认头像 // if (!val.icon) // val.icon = require("@/assets/images/common/pageHeader/userIcon.png"); state.userInfo = JSON.parse(JSON.stringify(val)); tool.setCookie(userInfoKey, JSON.stringify(val)); } else { state.userInfo = null; tool.delCookie(userInfoKey); } }, //设置浏览记录 setHistoryBook(state, val) { let currentIndex = ""; if (state.historyBook.length > 0) { var res = state.historyBook.some((item, index) => { if (item.id == val.id) { currentIndex = index; return true; } }); if (!res) { state.historyBook.unshift(val); } else { state.historyBook.splice(currentIndex, 1); state.historyBook.unshift(val); } } else { state.historyBook.unshift(val); } }, //删除浏览记录 deleteHistoryBook(state, val) { state.historyBook.splice(val, 1); }, //增加电子样书 addEbookList(state, val) { state.electronicBookList.push(val); }, //增加纸质样书 addPbookList(state, val) { state.paperCopiesList.push(val); }, //删除电子样书 deleteEbookList(state, val) { if (val == "all") { state.electronicBookList = []; } else { state.electronicBookList.splice(val, 1); } }, //删除纸质样书 deletePbookList(state, val) { if (val == "all") { state.paperCopiesList = []; } else { state.paperCopiesList.splice(val, 1); } }, emptyBookList(state, val) { state.electronicBookList = []; state.paperCopiesList = []; }, deleteAllHistory(state) { state.historyBook = []; }, addKeepAlive(state, val) { state.keepAliveList.push(val); console.log("state", state.keepAliveList); }, delKeepAlive(state, val) { if (state.keepAliveList.findIndex((item) => item == val) != -1) { state.keepAliveList.splice( state.keepAliveList.findIndex((item) => item == val), 1 ); } }, }, actions: { setUserInfo(context, val) { context.commit("changeUserInfo", val); }, setToken(context, val) { context.commit("changeLogin", val); }, }, modules: {}, });