duhuizhe
2023-10-16 3aa55dd3f62cee2c1c4c0aa74e1570acf83f8927
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import {getInfo, login, logout,getCurInfo} from '@/api/user';
import {getToken, removeToken,setUserInfo, setToken,getUser,getUserDetail,setUserDetail,setCPass} from '@/utils/auth';
import router, {resetRouter} from '@/router';
import {isLoginApi} from '@/api/system/sms';
import Cookies from 'js-cookie';
import {getQueryString} from '@/libs/wechat';
import {Loading} from 'element-ui';
import {encrypt} from '@/utils/jsencrypt'
import store from "@/store";
 
const state = {
  info: getUser(),
  userInfo:getUserDetail(),//用户详细信息
  token: getToken(),
  userId:'',
  name: '',
  avatar: '',
  introduction: '',
  roles: [],
  isLogin: Cookies.get('isLogin'),
  permissions: [],
  myButtonPermission:[],
  captcha: {
    captchaVerification: '',
    secretKey: '',
    token: '',
  }, //滑块验证token
};
 
const mutations = {
  SET_USER_INFO:(state, info)=>{
    // 设置详细信息
    state.userInfo = info
    setUserDetail(info)
  },
  SET_MYBUTTONPERMISSION: (state, myButtonPermission) => {
    state.myButtonPermission = myButtonPermission
  },
  SET_TOKEN: (state, token) => {
    state.token = token;
    setToken(token)
  },
  SET_USERINFO: (state, user) =>{
    // 保存用户信息
    state.info = user
    setUserInfo(user)
  },
  SET_ISLOGIN: (state, isLogin) => {
    state.isLogin = isLogin;
    Cookies.set(isLogin);
  },
  SET_INTRODUCTION: (state, introduction) => {
    state.introduction = introduction;
  },
  SET_USERID: (state, id) => {
    state.userId = id;
  },
  SET_NAME: (state, name) => {
    state.name = name;
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar;
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles;
  },
  SET_PERMISSIONS: (state, permissions) => {
    state.permissions = permissions;
  },
  SET_CAPTCHA: (state, captcha) => {
    state.captcha = captcha;
  }
};
 
const actions = {
  // user login
  login({ commit }, userInfo) {
    // const { account, pwd, key, code, wxCode } = userInfo;
    const username = userInfo.account.trim()
    // const password = userInfo.password
    const password = encrypt(userInfo.password)
    const code = userInfo.code
    const uuid = userInfo.uuid
    const loginType = userInfo.loginType
    const verifyType = userInfo.verifyType
    Loading.service();
    return new Promise((resolve, reject) => {
      // login(userInfo)
      login(username, password, code, uuid, loginType, verifyType)
        .then((data) => {
          let loadingInstance = Loading.service();
          loadingInstance.close();
          commit('SET_TOKEN', data.token);
          commit('SET_USERINFO', data.userInfo);
          getCurInfo().then(res=>{
            commit('SET_USER_INFO',res)
          })
          resolve();
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
 
  // 短信是否登录
  isLogin({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      isLoginApi()
        .then(async (res) => {
          commit('SET_ISLOGIN', res.isLogin);
          resolve(res);
        })
        .catch((res) => {
          commit('SET_ISLOGIN', false);
          reject(res);
        });
    });
  },
 
  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token)
        .then(data => {
          if (!data) {
            reject('Verification failed, please Login again.');
          }
          let roles = data.roleIds;
          if (!roles || roles.length <= 0) {
            reject('getInfo: roles must be a non-null array!');
          }
          // 获取并设置修改密码的状态
          setCPass(data.force_change_pass)
          if( data.force_change_pass ){
            router.push({ path: '/resetPwd' });
            reject(data);
          }
          commit('SET_USERID', data.id);
          commit('SET_ROLES', roles);
          commit('SET_NAME', data.account);
          commit('SET_AVATAR', 'http://kaifa.crmeb.net/system/images/admin_logo.png');
          commit('SET_INTRODUCTION', data.realName);
          commit('SET_PERMISSIONS', data.permissions); //权限标识
          resolve(data);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
 
  // user logout
  logout({ commit, state, dispatch }) {
    Loading.service();
    return new Promise((resolve, reject) => {
      logout(state.token)
        .then(() => {
          let loadingInstance = Loading.service();
          loadingInstance.close();
          commit('SET_TOKEN', '');
          commit('SET_ROLES', []);
          commit('SET_PERMISSIONS', []);
          commit('SET_CONNECTION_CLEAR', null) // 断开链接
          removeToken();
          resetRouter();
          // localStorage.clear();
          Cookies.remove('storeStaffList');
          Cookies.remove('JavaInfo');
          localStorage.clear('sidebarRouters');
          sessionStorage.removeItem('token');
          // reset visited views and cached views
          // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
          dispatch('tagsView/delAllViews', null, { root: true });
 
          resolve();
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
 
  // remove token
  resetToken({ commit }) {
    return new Promise((resolve) => {
      commit('SET_TOKEN', '');
      commit('SET_ROLES', []);
      removeToken();
      resolve();
    });
  },
  // // 设置token
  // setToken({ commit }, state) {
  //   return new Promise((resolve) => {
  //     commit('SET_TOKEN', state.token);
  //     // Cookies.set('JavaInfo', JSON.stringify(state));
  //     setToken(data.token);
  //     resolve();
  //   });
  // },
 
  // dynamically modify permissions
  changeRoles({ commit, dispatch }, role) {
    return new Promise(async (resolve) => {
      // const token = role + '-token';
      //
      // commit('SET_TOKEN', token);
      // // setToken(token);
      //
      const { roles } = await dispatch('getInfo');
      // resetRouter();
      // generate accessible routes map based on roles
      const accessRoutes = await dispatch('permission/generateRoutes', roles, { root: true });
      // dynamically add accessible routes
      router.addRoutes(accessRoutes);
 
      // reset visited views and cached views
      dispatch('tagsView/delAllViews', null, { root: true });
 
      resolve();
    });
  },
};
 
export default {
  namespaced: true,
  state,
  mutations,
  actions,
};