a0f03d4c532b3e32f19be2a4f9052337fc214d94.svn-base 4.9 KB

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