43c32c0efb696705f75bea12fdda9482622c2ce0.svn-base 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { login, logout, getInfo } from '@/api/user'
  2. import { GetDataByName } from '@/api/common'
  3. import { getToken, setToken, removeToken } from '@/utils/auth'
  4. import { resetRouter } from '@/router'
  5. import { Message } from 'element-ui'
  6. import Cookies from 'js-cookie'
  7. const state = {
  8. token: getToken(),
  9. name: '',
  10. avatar: '',
  11. pastureid: '',
  12. pasturename: '',
  13. pasturenumber: '',
  14. employename: '',
  15. employeid: '',
  16. roles: [],
  17. buttons: []
  18. }
  19. const mutations = {
  20. SET_TOKEN: (state, token) => {
  21. state.token = token
  22. },
  23. SET_NAME: (state, name) => {
  24. state.name = name
  25. Cookies.set('name', name)
  26. },
  27. SET_AVATAR: (state, avatar) => {
  28. state.avatar = avatar
  29. },
  30. SET_ROLES: (state, roles) => {
  31. state.roles = roles
  32. },
  33. SET_PASTUREID: (state, pastureid) => {
  34. Cookies.set('pastureid', pastureid)
  35. state.pastureid = pastureid
  36. },
  37. SET_PASTURENAME: (state, pasturename) => {
  38. Cookies.set('pasturename', pasturename)
  39. state.pasturename = pasturename
  40. },
  41. SET_PASTURENUMBER: (state, pasturenumber) => {
  42. Cookies.set('pasturenumber', pasturenumber)
  43. state.pasturenumber = pasturenumber
  44. },
  45. SET_EMPLOYEID: (state, employeid) => {
  46. Cookies.set('employeid', employeid)
  47. // console.log('Cookies.set.employeid', employeid)
  48. state.employeid = employeid
  49. },
  50. SET_EMPLOYENAME: (state, employename) => {
  51. Cookies.set('employename', employename)
  52. state.employename = employename
  53. },
  54. SET_DEPARTMENTID: (state, departmentid) => {
  55. Cookies.set('departmentid', departmentid)
  56. state.departmentid = departmentid
  57. },
  58. SET_BUTTONS: (state, buttons) => {
  59. Cookies.set('buttons', buttons)
  60. sessionStorage.setItem('buttons', JSON.stringify(buttons))
  61. state.buttons = buttons
  62. }
  63. }
  64. const actions = {
  65. // user login
  66. login({ commit }, userInfo) {
  67. const { username, password } = userInfo
  68. return new Promise((resolve, reject) => {
  69. login({ username: username.trim(), password: password }).then(response => {
  70. const { data, msg } = response
  71. if (msg !== 'ok') {
  72. Message({
  73. message: data,
  74. type: 'error',
  75. duration: 5 * 1000
  76. })
  77. reject(data)
  78. }
  79. commit('SET_TOKEN', data.token)
  80. if (Cookies == null) {
  81. console.log(1)
  82. }
  83. setToken(data.token)
  84. resolve()
  85. }).catch(error => {
  86. reject(error)
  87. })
  88. })
  89. },
  90. // get user info
  91. getInfo({ commit, state }) {
  92. return new Promise((resolve, reject) => {
  93. getInfo(state.token).then(response => {
  94. const { data } = response
  95. if (!data) {
  96. reject('Verification failed, please Login again.')
  97. }
  98. const { role, username, avatar } = data
  99. // roles must be a non-empty array
  100. if (!role || role.length <= 0) {
  101. reject('getInfo: roles must be a non-null array!')
  102. }
  103. // if (roleid !== null || roleid !== undefined || roleid !== '') {
  104. // reject('getInfo: roles must be a non-null array!')
  105. // }
  106. GetDataByName({ 'name': 'findByUserPastureName', 'parammaps': { 'jwt_username': username }}).then(response => {
  107. if (response.data.list !== null) {
  108. commit('SET_PASTUREID', response.data.list[0].pastureId)
  109. commit('SET_PASTURENAME', response.data.list[0].pastureName)
  110. commit('SET_PASTURENUMBER', response.data.list[0].pastureNumber)
  111. commit('SET_EMPLOYEID', response.data.list[0].employeId)
  112. commit('SET_EMPLOYENAME', response.data.list[0].employeName)
  113. GetDataByName({ 'name': 'getUserPCButtons', 'parammaps': { 'username': username,'pastureid':response.data.list[0].pastureId }}).then(response => {
  114. commit('SET_BUTTONS', response.data.list)
  115. })
  116. }
  117. })
  118. commit('SET_ROLES', role)
  119. commit('SET_NAME', username)
  120. commit('SET_AVATAR', avatar)
  121. resolve(data)
  122. }).catch(error => {
  123. reject(error)
  124. })
  125. })
  126. },
  127. // user logout
  128. logout({ commit, state }) {
  129. return new Promise((resolve, reject) => {
  130. logout(state.token).then(() => {
  131. commit('SET_BUTTONS', [])
  132. // console.log('-------------', state)
  133. commit('SET_TOKEN', '')
  134. commit('SET_ROLES', [])
  135. Cookies.remove('employename')
  136. Cookies.remove('employeid')
  137. Cookies.remove('pastureid')
  138. removeToken()
  139. resetRouter()
  140. resolve()
  141. location.reload()
  142. }).catch(error => {
  143. reject(error)
  144. })
  145. })
  146. },
  147. // remove token
  148. resetToken({ commit }) {
  149. return new Promise(resolve => {
  150. commit('SET_TOKEN', '')
  151. commit('SET_ROLES', [])
  152. removeToken()
  153. resolve()
  154. })
  155. }
  156. }
  157. export default {
  158. namespaced: true,
  159. state,
  160. mutations,
  161. actions
  162. }