Rap 原分销系统代码Web
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

8051c51d80d9cfbd94a5b395986e54d01a3f5ea4.svn-base 2.2KB

pirms 5 mēnešiem
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { login, getInfo, logout } from '@/api/login'
  2. import { getToken, setToken, removeToken } from '@/utils/auth'
  3. const user = {
  4. state: {
  5. token: getToken(),
  6. user: {},
  7. roles: [],
  8. // 第一次加载菜单时用到
  9. loadMenus: false
  10. },
  11. mutations: {
  12. SET_TOKEN: (state, token) => {
  13. state.token = token
  14. },
  15. SET_USER: (state, user) => {
  16. state.user = user
  17. },
  18. SET_ROLES: (state, roles) => {
  19. state.roles = roles
  20. },
  21. SET_LOAD_MENUS: (state, loadMenus) => {
  22. state.loadMenus = loadMenus
  23. }
  24. },
  25. actions: {
  26. // 登录
  27. Login({ commit }, userInfo) {
  28. const rememberMe = userInfo.rememberMe
  29. return new Promise((resolve, reject) => {
  30. login(userInfo.username, userInfo.password, userInfo.code, userInfo.uuid).then(res => {
  31. setToken(res.token, rememberMe)
  32. commit('SET_TOKEN', res.token)
  33. setUserInfo(res.user, commit)
  34. // 第一次加载菜单时用到, 具体见 src 目录下的 permission.js
  35. commit('SET_LOAD_MENUS', true)
  36. resolve()
  37. }).catch(error => {
  38. reject(error)
  39. })
  40. })
  41. },
  42. // 获取用户信息
  43. GetInfo({ commit }) {
  44. return new Promise((resolve, reject) => {
  45. getInfo().then(res => {
  46. setUserInfo(res, commit)
  47. resolve(res)
  48. }).catch(error => {
  49. reject(error)
  50. })
  51. })
  52. },
  53. // 登出
  54. LogOut({ commit }) {
  55. return new Promise((resolve, reject) => {
  56. logout().then(res => {
  57. logOut(commit)
  58. resolve()
  59. }).catch(error => {
  60. logOut(commit)
  61. reject(error)
  62. })
  63. })
  64. },
  65. updateLoadMenus({ commit }) {
  66. return new Promise((resolve, reject) => {
  67. commit('SET_LOAD_MENUS', false)
  68. })
  69. }
  70. }
  71. }
  72. export const logOut = (commit) => {
  73. commit('SET_TOKEN', '')
  74. commit('SET_ROLES', [])
  75. removeToken()
  76. }
  77. export const setUserInfo = (res, commit) => {
  78. // 如果没有任何权限,则赋予一个默认的权限,避免请求死循环
  79. if (res.roles.length === 0) {
  80. commit('SET_ROLES', ['ROLE_SYSTEM_DEFAULT'])
  81. } else {
  82. commit('SET_ROLES', res.roles)
  83. }
  84. commit('SET_USER', res.user)
  85. }
  86. export default user