Fixiaobai
2023-10-13 e8308ddac0ba4a1f406e8f63d7e6b6f2541cb770
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import { getStore, setStore } from '@/util/store'
import { isURL, validatenull } from '@/util/validate'
import {
  loginByMobile,
  loginBySocial,
  loginByUsername,
  LoginBySSO,
  logout,
  refreshToken,
  LoginByMSL
} from '@/api/login'
import { deepClone, encryption } from '@/util/util'
import webiste from '@/const/website'
import { resetRouter } from '@/router/router'
import { getMenu, getTopMenu } from '@/api/admin/menu'
 
function addPath(ele, first) {
  const menu = webiste.menu
  const propsConfig = menu.props
  const propsDefault = {
    label: propsConfig.label || 'name',
    path: propsConfig.path || 'path',
    icon: propsConfig.icon || 'icon',
    children: propsConfig.children || 'children'
  }
  const icon = ele[propsDefault.icon]
  ele[propsDefault.icon] = validatenull(icon) ? menu.iconDefault : icon
  const isChild =
    ele[propsDefault.children] && ele[propsDefault.children].length !== 0
  if (!isChild) ele[propsDefault.children] = []
  if (!isChild && first && !isURL(ele[propsDefault.path])) {
    ele[propsDefault.path] = ele[propsDefault.path] + '/index'
  } else {
    ele[propsDefault.children].forEach((child) => {
      addPath(child)
    })
  }
}
 
// 优化设置session的Auth参数 By Luxn
function setAuth(data, commit) {
  commit('SET_ACCESS_TOKEN', data.access_token)
  commit('SET_REFRESH_TOKEN', data.refresh_token)
  commit('SET_EXPIRES_IN', data.expires_in)
  commit('SET_USER_INFO', data.user_info)
  commit('SET_PERMISSIONS', data.user_info.authorities || [])
  // 将相关Auth参数存入localStrorage By Luxn
  setAuthLocalStorage(data)
}
function setAuthLocalStorage(data) {
  const auth = {
    access_token: data.access_token,
    refresh_token: data.refresh_token,
    expires_in: data.expires_in,
    user_info: data.user_info,
    date_time: new Date().getTime()
  }
  localStorage.setItem('authorization', JSON.stringify(auth))
}
 
function getAuthLocalStorage(commit) {
  return JSON.stringify(localStorage.getItem('authorization'))
}
 
const user = {
  state: {
    userInfo:
      getStore({
        name: 'userInfo'
      }) || {},
    permissions:
      getStore({
        name: 'permissions'
      }) || [],
    roles: [],
    menu:
      getStore({
        name: 'menu'
      }) || [],
    menuAll: [],
    expires_in:
      getStore({
        name: 'expires_in'
      }) || '',
    access_token:
      getStore({
        name: 'access_token'
      }) || '',
    refresh_token:
      getStore({
        name: 'refresh_token'
      }) || ''
  },
  actions: {
    // 根据用户名登录
    LoginByUsername({ commit }, userInfo) {
      const user = encryption({
        data: userInfo,
        key: 'pigxpigxpigxpigx',
        param: ['password']
      })
      return new Promise((resolve, reject) => {
        loginByUsername(user.username, user.password, user.code, user.randomStr)
          .then((response) => {
            setAuth(response.data, commit)
            commit('CLEAR_LOCK')
            resolve()
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    // 单点登录
    LoginBySSO({ commit }, accessToken) {
      return new Promise((resolve, reject) => {
        LoginBySSO(accessToken)
          .then((response) => {
            const data = response.data
            commit('SET_ACCESS_TOKEN', data.access_token)
            commit('SET_REFRESH_TOKEN', data.refresh_token)
            commit('SET_EXPIRES_IN', data.expires_in)
            commit('SET_USER_INFO', data.user_info)
            commit('SET_PERMISSIONS', data.user_info.authorities || [])
            commit('CLEAR_LOCK')
            resolve()
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    // 根据手机号登录
    LoginByPhone({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        loginByMobile(userInfo.mobile, userInfo.code)
          .then((response) => {
            setAuth(response.data, commit)
            commit('CLEAR_LOCK')
            resolve()
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    // 根据OpenId登录
    LoginBySocial({ commit }, param) {
      return new Promise((resolve, reject) => {
        loginBySocial(param.state, param.code)
          .then((response) => {
            setAuth(response.data, commit)
            commit('CLEAR_LOCK')
            resolve()
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    // 刷新token
    RefreshToken({ commit, state }) {
      return new Promise((resolve, reject) => {
        refreshToken(state.refresh_token)
          .then((response) => {
            const data = response.data
            commit('SET_ACCESS_TOKEN', data.access_token)
            commit('SET_REFRESH_TOKEN', data.refresh_token)
            commit('SET_EXPIRES_IN', data.expires_in)
            commit('CLEAR_LOCK')
            resolve()
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    // 登出
    LogOut({ commit }) {
      return new Promise((resolve, reject) => {
        logout()
          .then((res) => {
            if (res.data.data) {
              window.location.href = res.data.data
            } else {
              resetRouter()
              commit('SET_MENU', [])
              commit('SET_PERMISSIONS', [])
              commit('SET_USER_INFO', {})
              commit('SET_ACCESS_TOKEN', '')
              commit('SET_REFRESH_TOKEN', '')
              commit('SET_EXPIRES_IN', '')
              commit('SET_ROLES', [])
              commit('DEL_ALL_TAG')
              commit('CLEAR_LOCK')
              resolve()
            }
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    // 注销session
    FedLogOut({ commit }) {
      return new Promise((resolve) => {
        resetRouter()
        commit('SET_MENU', [])
        commit('SET_PERMISSIONS', [])
        commit('SET_USER_INFO', {})
        commit('SET_ACCESS_TOKEN', '')
        commit('SET_REFRESH_TOKEN', '')
        commit('SET_ROLES', [])
        commit('DEL_ALL_TAG')
        commit('CLEAR_LOCK')
        resolve()
      })
    },
    // 获取系统菜单
    GetMenu({ commit }, obj) {
      return new Promise((resolve) => {
        getMenu(obj.id).then((res) => {
          const data = res.data.data
          const menu = deepClone(data)
          menu.forEach((ele) => {
            addPath(ele)
          })
          const type = obj.type
          commit('SET_MENU', { type, menu })
          resolve(menu)
        })
      })
    },
    // 顶部菜单
    GetTopMenu() {
      return new Promise((resolve) => {
        getTopMenu().then((res) => {
          const data = res.data.data || []
          resolve(data)
        })
      })
    },
    // 根据马上聊登录
    LoginByMSL({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        LoginByMSL(userInfo.username, userInfo.password)
          .then((response) => {
            setAuth(response.data, commit)
            commit('CLEAR_LOCK')
            resolve()
          })
          .catch((error) => {
            reject(error)
          })
      })
    }
  },
  mutations: {
    SET_ACCESS_TOKEN: (state, access_token) => {
      state.access_token = access_token
      setStore({
        name: 'access_token',
        content: state.access_token,
        type: 'session'
      })
    },
    SET_EXPIRES_IN: (state, expires_in) => {
      state.expires_in = expires_in
      setStore({
        name: 'expires_in',
        content: state.expires_in,
        type: 'session'
      })
    },
    SET_REFRESH_TOKEN: (state, rfToken) => {
      state.refresh_token = rfToken
      setStore({
        name: 'refresh_token',
        content: state.refresh_token,
        type: 'session'
      })
    },
    SET_USER_INFO: (state, userInfo) => {
      state.userInfo = userInfo
      setStore({
        name: 'userInfo',
        content: userInfo,
        type: 'session'
      })
    },
    SET_MENU: (state, params = {}) => {
      const { menu, type } = params
      if (type !== false) state.menu = menu
      setStore({
        name: 'menu',
        content: menu,
        type: 'session'
      })
    },
    SET_MENU_ALL: (state, menuAll) => {
      state.menuAll = menuAll
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      const list = {}
      for (let i = 0; i < permissions.length; i++) {
        list[permissions[i].authority] = true
      }
 
      state.permissions = list
      setStore({
        name: 'permissions',
        content: list,
        type: 'session'
      })
    }
  }
}
export default user