管理系统PC端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

204 行
6.1 KiB

  1. import router from '@/router'
  2. import { MessageBox, } from 'element-ui'
  3. import { login, logout, getInfo, systemConfig } from '@/api/login'
  4. import { getToken, setToken, removeToken } from '@/utils/auth'
  5. import { isHttp, isEmpty } from "@/utils/validate"
  6. import defAva from '@/assets/images/profile.jpg'
  7. import Cookies from "js-cookie";
  8. const user = {
  9. state: {
  10. token: getToken(),
  11. id: '',
  12. name: '',
  13. nickName: '',
  14. avatar: '',
  15. user: {},
  16. userDept:null,
  17. roles: [],
  18. permissions: [],
  19. //获取登录页相关标题
  20. systemConfigList: [],
  21. //系统名称
  22. loginSystemName: '',
  23. //浏览器页签名称
  24. browserName: '',
  25. //技术支持
  26. technicalSupport: '',
  27. //界面logo
  28. globalLogo:'',
  29. },
  30. mutations: {
  31. SET_TOKEN: (state, token) => {
  32. state.token = token
  33. },
  34. SET_ID: (state, id) => {
  35. state.id = id
  36. },
  37. SET_NAME: (state, name) => {
  38. state.name = name
  39. },
  40. SET_NICK_NAME: (state, nickName) => {
  41. state.nickName = nickName
  42. },
  43. SET_AVATAR: (state, avatar) => {
  44. state.avatar = avatar
  45. },
  46. SET_USER: (state, user) => {
  47. state.user = user
  48. },
  49. SET_USERDEPT: (state, dept) => {
  50. state.userDept = dept
  51. },
  52. SET_ROLES: (state, roles) => {
  53. state.roles = roles
  54. },
  55. SET_PERMISSIONS: (state, permissions) => {
  56. state.permissions = permissions
  57. },
  58. SET_STSTEMCONFIGlIST: (state, systemConfigList) => {
  59. state.systemConfigList = systemConfigList
  60. },
  61. SET_LOGINSYSTEMNAME: (state, loginSystemName) => {
  62. state.loginSystemName = loginSystemName
  63. },
  64. SET_BORWSERNAME: (state, browserName) => {
  65. state.browserName = browserName
  66. },
  67. SET_TECHNICALSUPPORT: (state, technicalSupport) => {
  68. state.technicalSupport = technicalSupport
  69. },
  70. SET_GLOBALLOGO: (state, globalLogo) => {
  71. state.globalLogo = globalLogo
  72. },
  73. },
  74. actions: {
  75. // 登录
  76. Login({ commit }, userInfo) {
  77. const username = userInfo.username.trim()
  78. const password = userInfo.password
  79. const code = userInfo.code
  80. const uuid = userInfo.uuid
  81. return new Promise((resolve, reject) => {
  82. login(username, password, code, uuid).then(res => {
  83. setToken(res.token)
  84. commit('SET_TOKEN', res.token)
  85. resolve()
  86. }).catch(error => {
  87. reject(error)
  88. })
  89. })
  90. },
  91. // 获取用户信息
  92. GetInfo({ commit, state }) {
  93. return new Promise((resolve, reject) => {
  94. getInfo().then(res => {
  95. const user = res.user
  96. let avatar = user.avatar || ""
  97. if (!isHttp(avatar)) {
  98. avatar = (isEmpty(avatar)) ? defAva : process.env.VUE_APP_BASE_API + avatar
  99. }
  100. if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
  101. commit('SET_ROLES', res.roles)
  102. commit('SET_PERMISSIONS', res.permissions)
  103. } else {
  104. commit('SET_ROLES', ['ROLE_DEFAULT'])
  105. }
  106. commit('SET_ID', user.userId)
  107. commit('SET_NAME', user.userName)
  108. commit('SET_NICK_NAME', user.nickName)
  109. commit('SET_AVATAR', avatar)
  110. commit('SET_USER', user)
  111. commit('SET_USERDEPT', user.dept.deptId)
  112. Cookies.set('userDept',user.dept.deptId)
  113. Cookies.set('importCode',user.dept.importCode)
  114. /* 初始密码提示 */
  115. if(res.isDefaultModifyPwd) {
  116. MessageBox.confirm('您的密码还是初始密码,请修改密码!', '安全提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
  117. router.push({ name: 'Profile', params: { activeTab: 'resetPwd' } })
  118. }).catch(() => {})
  119. }
  120. /* 过期密码提示 */
  121. if(!res.isDefaultModifyPwd && res.isPasswordExpired) {
  122. MessageBox.confirm('您的密码已过期,请尽快修改密码!', '安全提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
  123. router.push({ name: 'Profile', params: { activeTab: 'resetPwd' } })
  124. }).catch(() => {})
  125. }
  126. resolve(res)
  127. }).catch(error => {
  128. reject(error)
  129. })
  130. })
  131. },
  132. // 退出系统
  133. LogOut({ commit, state }) {
  134. return new Promise((resolve, reject) => {
  135. logout(state.token).then(() => {
  136. commit('SET_TOKEN', '')
  137. commit('SET_ROLES', [])
  138. commit('SET_PERMISSIONS', [])
  139. removeToken()
  140. resolve()
  141. }).catch(error => {
  142. reject(error)
  143. })
  144. })
  145. },
  146. // 前端 登出
  147. FedLogOut({ commit }) {
  148. return new Promise(resolve => {
  149. commit('SET_TOKEN', '')
  150. removeToken()
  151. resolve()
  152. })
  153. },
  154. //获取登录页相关标题
  155. systemTitle({ commit, state }) {
  156. return new Promise((resolve, reject) => {
  157. systemConfig({ configKey: 'system' }).then((data) => {
  158. let content = data.rows;
  159. commit('SET_STSTEMCONFIGlIST', content)
  160. //系统名称
  161. let loginSystemName = '';
  162. //浏览器页签名称
  163. let browserName = '';
  164. //技术支持
  165. let technicalSupport = '';
  166. //界面logo
  167. let globalLogo = '';
  168. content.forEach((v, i) => {
  169. if (v.configKey == 'system.login.name') {
  170. loginSystemName = v.configValue
  171. } else if (v.configKey == 'system.browser.name') {
  172. browserName = v.configValue
  173. } else if (v.configKey == 'system.company.name') {
  174. technicalSupport = v.configValue
  175. }else if(v.configKey == 'system.logo.pic'){
  176. globalLogo = v.configValue
  177. }
  178. })
  179. commit('SET_LOGINSYSTEMNAME', loginSystemName)
  180. commit('SET_BORWSERNAME', browserName)
  181. commit('SET_TECHNICALSUPPORT', technicalSupport)
  182. commit('SET_GLOBALLOGO', globalLogo)
  183. resolve()
  184. }).catch(error => {
  185. reject(error)
  186. })
  187. })
  188. },
  189. }
  190. }
  191. export default user