230549d9b7a73be2fab0512e107a5a4b952a8275.svn-base 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_ISPASTUREUSE: (state, ispastureuse) => {
  63. Cookies.set('ispastureuse', ispastureuse)
  64. state.ispastureuse = ispastureuse
  65. },
  66. SET_DEPARTMENTID: (state, departmentid) => {
  67. Cookies.set('departmentid', departmentid)
  68. state.departmentid = departmentid
  69. },
  70. SET_BUTTONS: (state, buttons) => {
  71. Cookies.set('buttons', buttons)
  72. sessionStorage.setItem('buttons', JSON.stringify(buttons))
  73. state.buttons = buttons
  74. }
  75. }
  76. const actions = {
  77. // user login
  78. login({ commit }, userInfo) {
  79. const { username, password } = userInfo
  80. return new Promise((resolve, reject) => {
  81. login({ username: username.trim(), password: password }).then(response => {
  82. const { data, msg } = response
  83. if (msg !== 'ok') {
  84. Message({
  85. message: data,
  86. type: 'error',
  87. duration: 5 * 1000
  88. })
  89. reject(data)
  90. }
  91. commit('SET_TOKEN', data.token)
  92. if (Cookies == null) {
  93. console.log(1)
  94. }
  95. setToken(data.token)
  96. resolve()
  97. }).catch(error => {
  98. reject(error)
  99. })
  100. })
  101. },
  102. // get user info
  103. getInfo({ commit, state }) {
  104. return new Promise((resolve, reject) => {
  105. getInfo(state.token).then(response => {
  106. const { data } = response
  107. if (!data) {
  108. reject('Verification failed, please Login again.')
  109. }
  110. const { role, username, avatar } = data
  111. // roles must be a non-empty array
  112. if (!role || role.length <= 0) {
  113. reject('getInfo: roles must be a non-null array!')
  114. }
  115. // if (roleid !== null || roleid !== undefined || roleid !== '') {
  116. // reject('getInfo: roles must be a non-null array!')
  117. // }
  118. GetDataByName({ 'name': 'findByUserPastureName', 'parammaps': { 'jwt_username': username } }).then(response => {
  119. if (response.data.list !== null) {
  120. commit('SET_PASTUREID', response.data.list[0].pastureId)
  121. commit('SET_PASTURENAME', response.data.list[0].pastureName)
  122. commit('SET_PASTURENUMBER', response.data.list[0].pastureNumber)
  123. commit('SET_EMPLOYEID', response.data.list[0].employeId)
  124. commit('SET_EMPLOYENAME', response.data.list[0].employeName)
  125. commit('SET_ISPASTUREUSE', response.data.list[0].isgroups)
  126. GetDataByName({ 'name': 'getUserPCButtons', 'parammaps': { 'username': username, 'pastureid': response.data.list[0].pastureId } }).then(response => {
  127. commit('SET_BUTTONS', response.data.list)
  128. })
  129. }
  130. })
  131. commit('SET_ROLES', role)
  132. commit('SET_NAME', username)
  133. commit('SET_AVATAR', avatar)
  134. resolve(data)
  135. }).catch(error => {
  136. reject(error)
  137. })
  138. })
  139. },
  140. // user logout
  141. logout({ commit, state }) {
  142. return new Promise((resolve, reject) => {
  143. logout(state.token).then(() => {
  144. commit('SET_BUTTONS', [])
  145. // console.log('-------------', state)
  146. commit('SET_TOKEN', '')
  147. commit('SET_ROLES', [])
  148. Cookies.remove('employename')
  149. Cookies.remove('employeid')
  150. Cookies.remove('pastureid')
  151. Cookies.remove('ispastureuse')
  152. removeToken()
  153. resetRouter()
  154. resolve()
  155. location.reload()
  156. }).catch(error => {
  157. reject(error)
  158. })
  159. })
  160. },
  161. // remove token
  162. resetToken({ commit }) {
  163. return new Promise(resolve => {
  164. commit('SET_TOKEN', '')
  165. commit('SET_ROLES', [])
  166. removeToken()
  167. resolve()
  168. })
  169. }
  170. }
  171. export default {
  172. namespaced: true,
  173. state,
  174. mutations,
  175. actions
  176. }