杨磊
4 天以前 ef37c59e055a990ce247b265b27d3fcef430a243
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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: {},
});