138dee173439e2fbaf9ab3c3764ab2e5dc21f12b.svn-base 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. // if (roleid !== null || roleid !== undefined || roleid !== '') {
  110. // reject('getInfo: roles must be a non-null array!')
  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. // GetDataByName({ 'name': 'getUserPCButtons', 'parammaps': { 'username': username,'pastureid':response.data.list[0].pastureId }}).then(response => {
  122. // commit('SET_BUTTONS', response.data.list)
  123. // })
  124. }
  125. })
  126. commit('SET_ROLES', role)
  127. commit('SET_NAME', username)
  128. commit('SET_AVATAR', avatar)
  129. resolve(data)
  130. }).catch(error => {
  131. reject(error)
  132. })
  133. })
  134. },
  135. // user logout
  136. logout({ commit, state }) {
  137. return new Promise((resolve, reject) => {
  138. logout(state.token).then(() => {
  139. // commit('SET_BUTTONS', [])
  140. // console.log('-------------', state)
  141. commit('SET_TOKEN', '')
  142. commit('SET_ROLES', [])
  143. Cookies.remove('employename')
  144. Cookies.remove('employeid')
  145. Cookies.remove('pastureid')
  146. removeToken()
  147. resetRouter()
  148. resolve()
  149. location.reload()
  150. }).catch(error => {
  151. reject(error)
  152. })
  153. })
  154. },
  155. // remove token
  156. resetToken({ commit }) {
  157. return new Promise(resolve => {
  158. commit('SET_TOKEN', '')
  159. commit('SET_ROLES', [])
  160. removeToken()
  161. resolve()
  162. })
  163. }
  164. }
  165. export default {
  166. namespaced: true,
  167. state,
  168. mutations,
  169. actions
  170. }