3afb59b6222d4c5c9f34a036d2fdd535141f2aca.svn-base 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. dashchartid: '',
  17. dashchartidisedit: '',
  18. roles: [],
  19. buttons: []
  20. }
  21. const mutations = {
  22. SET_DASHCHARTID: (state, dashchartid) => {
  23. state.dashchartid = dashchartid
  24. },
  25. SET_DASHCHARTIDISEDIT: (state, dashchartidisedit) => {
  26. state.dashchartidisedit = dashchartidisedit
  27. },
  28. SET_TOKEN: (state, token) => {
  29. state.token = token
  30. },
  31. SET_NAME: (state, name) => {
  32. state.name = name
  33. Cookies.set('name', name)
  34. },
  35. SET_AVATAR: (state, avatar) => {
  36. state.avatar = avatar
  37. },
  38. SET_ROLES: (state, roles) => {
  39. state.roles = roles
  40. },
  41. SET_PASTUREID: (state, pastureid) => {
  42. Cookies.set('pastureid', pastureid)
  43. state.pastureid = pastureid
  44. },
  45. SET_PASTURENAME: (state, pasturename) => {
  46. Cookies.set('pasturename', pasturename)
  47. state.pasturename = pasturename
  48. },
  49. SET_PASTURENUMBER: (state, pasturenumber) => {
  50. Cookies.set('pasturenumber', pasturenumber)
  51. state.pasturenumber = pasturenumber
  52. },
  53. SET_EMPLOYEID: (state, employeid) => {
  54. Cookies.set('employeid', employeid)
  55. // console.log('Cookies.set.employeid', employeid)
  56. state.employeid = employeid
  57. },
  58. SET_EMPLOYENAME: (state, employename) => {
  59. Cookies.set('employename', employename)
  60. state.employename = employename
  61. },
  62. SET_DEPARTMENTID: (state, departmentid) => {
  63. Cookies.set('departmentid', departmentid)
  64. state.departmentid = departmentid
  65. },
  66. SET_BUTTONS: (state, buttons) => {
  67. Cookies.set('buttons', buttons)
  68. sessionStorage.setItem('buttons', JSON.stringify(buttons))
  69. state.buttons = buttons
  70. }
  71. }
  72. const actions = {
  73. // user login
  74. login({ commit }, userInfo) {
  75. const { username, password } = userInfo
  76. return new Promise((resolve, reject) => {
  77. login({ username: username.trim(), password: password }).then(response => {
  78. const { data, msg } = response
  79. if (msg !== 'ok') {
  80. Message({
  81. message: data,
  82. type: 'error',
  83. duration: 5 * 1000
  84. })
  85. reject(data)
  86. }
  87. commit('SET_TOKEN', data.token)
  88. if (Cookies == null) {
  89. console.log(1)
  90. }
  91. setToken(data.token)
  92. resolve()
  93. }).catch(error => {
  94. reject(error)
  95. })
  96. })
  97. },
  98. // get user info
  99. getInfo({ commit, state }) {
  100. return new Promise((resolve, reject) => {
  101. getInfo(state.token).then(response => {
  102. const { data } = response
  103. if (!data) {
  104. reject('Verification failed, please Login again.')
  105. }
  106. const { role, username, avatar } = data
  107. // roles must be a non-empty array
  108. if (!role || role.length <= 0) {
  109. reject('getInfo: roles must be a non-null array!')
  110. }
  111. // if (roleid !== null || roleid !== undefined || roleid !== '') {
  112. // reject('getInfo: roles must be a non-null array!')
  113. // }
  114. GetDataByName({ 'name': 'findByUserPastureName', 'parammaps': { 'jwt_username': username } }).then(response => {
  115. if (response.data.list !== null) {
  116. commit('SET_PASTUREID', response.data.list[0].pastureId)
  117. commit('SET_PASTURENAME', response.data.list[0].pastureName)
  118. commit('SET_PASTURENUMBER', response.data.list[0].pastureNumber)
  119. commit('SET_EMPLOYEID', response.data.list[0].employeId)
  120. commit('SET_EMPLOYENAME', response.data.list[0].employeName)
  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. }