| | |
| | | /* |
| | | * @Author: shuishen 1109946754@qq.com |
| | | * @Date: 2022-12-27 09:33:01 |
| | | * @LastEditors: shuishen 1109946754@qq.com |
| | | * @LastEditTime: 2023-04-02 12:18:26 |
| | | * @FilePath: \srs-police-affairs\src\store\modules\user.js |
| | | * @Description: |
| | | * |
| | | * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. |
| | | */ |
| | | import { setToken, setRefreshToken, removeToken, removeRefreshToken } from '@/utils/auth' |
| | | import { Message } from 'element-ui' |
| | | import { setStore, getStore } from '@/utils/store' |
| | | import { isURL, validatenull } from '@/utils/validate' |
| | | import { deepClone } from '@/utils/tool' |
| | | import website from '@/config/website' |
| | | import { loginByUsername, loginBySocial, getUserInfo, logout, refreshToken, getButtons } from '@/api/user' |
| | | import md5 from 'js-md5' |
| | | |
| | | const user = { |
| | | state: { |
| | | examId: '', |
| | | isShowVideoDialog: '' |
| | | }, |
| | | actions: { |
| | | |
| | | }, |
| | | mutations: { |
| | | SET_EXAMID: (state, active) => { |
| | | state.examId = active |
| | | }, |
| | | |
| | | SET_ISSHOWVIDEODIALOG: (state, active) => { |
| | | state.isShowVideoDialog = active |
| | | }, |
| | | } |
| | | function addPath(ele, first) { |
| | | const menu = website.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); |
| | | }) |
| | | } |
| | | } |
| | | |
| | | export default user |
| | | const user = { |
| | | state: { |
| | | tenantId: getStore({ name: 'tenantId' }) || '', |
| | | userInfo: getStore({ name: 'userInfo' }) || [], |
| | | permission: getStore({ name: 'permission' }) || {}, |
| | | roles: [], |
| | | menuId: {}, |
| | | menu: getStore({ name: 'menu' }) || [], |
| | | menuAll: getStore({ name: 'menuAll' }) || [], |
| | | token: getStore({ name: 'token' }) || '', |
| | | refreshToken: getStore({ name: 'refreshToken' }) || '', |
| | | hideHome: false, |
| | | examId: '', |
| | | isShowVideoDialog: '' |
| | | }, |
| | | actions: { |
| | | LoginByUsername({ commit }, userInfo) { |
| | | return new Promise((resolve, reject) => { |
| | | loginByUsername(userInfo.tenantId, userInfo.username, md5(userInfo.password), userInfo.type, userInfo.key, userInfo.code).then(res => { |
| | | const data = res.data; |
| | | if (data.error_description) { |
| | | Message({ |
| | | message: data.error_description, |
| | | type: 'error' |
| | | }) |
| | | } else { |
| | | commit('SET_TOKEN', data.access_token); |
| | | commit('SET_REFRESH_TOKEN', data.refresh_token); |
| | | commit('SET_TENANT_ID', data.tenant_id); |
| | | commit('SET_USER_INFO', data); |
| | | commit('DEL_ALL_TAG'); |
| | | commit('CLEAR_LOCK'); |
| | | } |
| | | resolve(data); |
| | | }).catch(error => { |
| | | reject(error); |
| | | }) |
| | | }) |
| | | }, |
| | | LoginByPhone({ commit }, userInfo) { |
| | | return new Promise((resolve) => { |
| | | loginByUsername(userInfo.phone, userInfo.code).then(res => { |
| | | const data = res.data.data; |
| | | commit('SET_TOKEN', data); |
| | | commit('DEL_ALL_TAG'); |
| | | commit('CLEAR_LOCK'); |
| | | resolve(); |
| | | }) |
| | | }) |
| | | }, |
| | | LoginBySocial({ commit }, userInfo) { |
| | | return new Promise((resolve) => { |
| | | loginBySocial(userInfo.tenantId, userInfo.source, userInfo.code, userInfo.state).then(res => { |
| | | const data = res.data; |
| | | if (data.error_description) { |
| | | Message({ |
| | | message: data.error_description, |
| | | type: 'error' |
| | | }) |
| | | } else { |
| | | commit('SET_TOKEN', data.access_token); |
| | | commit('SET_REFRESH_TOKEN', data.refresh_token); |
| | | commit('SET_USER_INFO', data); |
| | | commit('DEL_ALL_TAG'); |
| | | commit('CLEAR_LOCK'); |
| | | } |
| | | resolve(); |
| | | }) |
| | | }) |
| | | }, |
| | | GetUserInfo({ commit }) { |
| | | return new Promise((resolve, reject) => { |
| | | getUserInfo().then((res) => { |
| | | const data = res.data.data; |
| | | commit('SET_ROLES', data.roles); |
| | | resolve(data); |
| | | }).catch(err => { |
| | | reject(err); |
| | | }) |
| | | }) |
| | | }, |
| | | refreshToken({ state, commit }) { |
| | | return new Promise((resolve, reject) => { |
| | | refreshToken(state.refreshToken, state.tenantId).then(res => { |
| | | const data = res.data; |
| | | commit('SET_TOKEN', data.access_token); |
| | | commit('SET_REFRESH_TOKEN', data.refresh_token); |
| | | resolve(); |
| | | }).catch(error => { |
| | | reject(error) |
| | | }) |
| | | }) |
| | | }, |
| | | LogOut({ commit }) { |
| | | return new Promise((resolve, reject) => { |
| | | logout().then(() => { |
| | | commit('SET_TOKEN', ''); |
| | | commit('SET_MENU', []); |
| | | commit('SET_MENU_ALL_NULL', []); |
| | | commit('SET_ROLES', []); |
| | | commit('SET_TAG_LIST', []); |
| | | commit('DEL_ALL_TAG'); |
| | | commit('CLEAR_LOCK'); |
| | | removeToken(); |
| | | removeRefreshToken(); |
| | | resolve(); |
| | | }).catch(error => { |
| | | reject(error) |
| | | }) |
| | | }) |
| | | }, |
| | | FedLogOut({ commit }) { |
| | | return new Promise(resolve => { |
| | | commit('SET_TOKEN', ''); |
| | | commit('SET_MENU_ALL_NULL', []); |
| | | commit('SET_MENU', []); |
| | | commit('SET_ROLES', []); |
| | | commit('SET_TAG_LIST', []); |
| | | commit('DEL_ALL_TAG'); |
| | | commit('CLEAR_LOCK'); |
| | | removeToken(); |
| | | removeRefreshToken(); |
| | | resolve(); |
| | | }) |
| | | }, |
| | | GetButtons({ commit }) { |
| | | return new Promise((resolve) => { |
| | | getButtons().then(res => { |
| | | const data = res.data.data; |
| | | commit('SET_PERMISSION', data); |
| | | resolve(); |
| | | }) |
| | | }) |
| | | }, |
| | | }, |
| | | mutations: { |
| | | set_hideHome: (state, val) => { |
| | | state.hideHome = val; |
| | | }, |
| | | SET_TOKEN: (state, token) => { |
| | | setToken(token); |
| | | state.token = token; |
| | | setStore({ name: 'token', content: state.token }) |
| | | }, |
| | | SET_MENU_ID(state, menuId) { |
| | | state.menuId = menuId; |
| | | }, |
| | | SET_MENU_ALL: (state, menuAll) => { |
| | | let menu = state.menuAll; |
| | | menuAll.forEach(ele => { |
| | | if (!menu.find(item => item.label === ele.label && item.path === ele.path)) { |
| | | menu.push(ele); |
| | | } |
| | | }) |
| | | state.menuAll = menu |
| | | setStore({ name: 'menuAll', content: state.menuAll }) |
| | | }, |
| | | SET_MENU_ALL_NULL: (state) => { |
| | | state.menuAll = [] |
| | | setStore({ name: 'menuAll', content: state.menuAll }) |
| | | }, |
| | | SET_MENU: (state, menu) => { |
| | | state.menu = menu |
| | | setStore({ name: 'menu', content: state.menu }) |
| | | }, |
| | | SET_REFRESH_TOKEN: (state, refreshToken) => { |
| | | setRefreshToken(refreshToken) |
| | | state.refreshToken = refreshToken; |
| | | setStore({ name: 'refreshToken', content: state.refreshToken }) |
| | | }, |
| | | SET_TENANT_ID: (state, tenantId) => { |
| | | state.tenantId = tenantId; |
| | | setStore({ name: 'tenantId', content: state.tenantId }) |
| | | }, |
| | | SET_USER_INFO: (state, userInfo) => { |
| | | if (validatenull(userInfo.avatar)) { |
| | | userInfo.avatar = "http://61.131.136.25:2081/zhba/upload/picture/mrtx.png"; |
| | | } |
| | | state.userInfo = userInfo; |
| | | setStore({ name: 'userInfo', content: state.userInfo }) |
| | | }, |
| | | SET_ROLES: (state, roles) => { |
| | | state.roles = roles; |
| | | }, |
| | | SET_PERMISSION: (state, permission) => { |
| | | let result = []; |
| | | |
| | | function getCode(list) { |
| | | list.forEach(ele => { |
| | | if (typeof (ele) === 'object') { |
| | | const chiildren = ele.children; |
| | | const code = ele.code; |
| | | if (chiildren) { |
| | | getCode(chiildren) |
| | | } else { |
| | | result.push(code); |
| | | } |
| | | } |
| | | }) |
| | | } |
| | | |
| | | getCode(permission); |
| | | state.permission = {}; |
| | | result.forEach(ele => { |
| | | state.permission[ele] = true; |
| | | }); |
| | | setStore({ name: 'permission', content: state.permission }) |
| | | }, |
| | | SET_EXAMID: (state, active) => { |
| | | state.examId = active |
| | | }, |
| | | SET_ISSHOWVIDEODIALOG: (state, active) => { |
| | | state.isShowVideoDialog = active |
| | | }, |
| | | } |
| | | } |
| | | |
| | | export default user |