61b54615a384f27c5c72a40a14766932b3699479.svn-base 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. response.data.list[0].ispastureuse = 0
  121. commit('SET_PASTUREID', response.data.list[0].pastureId)
  122. commit('SET_PASTURENAME', response.data.list[0].pastureName)
  123. commit('SET_PASTURENUMBER', response.data.list[0].pastureNumber)
  124. commit('SET_EMPLOYEID', response.data.list[0].employeId)
  125. commit('SET_EMPLOYENAME', response.data.list[0].employeName)
  126. commit('SET_ISPASTUREUSE', response.data.list[0].ispastureuse)
  127. GetDataByName({ 'name': 'getUserPCButtons', 'parammaps': { 'username': username, 'pastureid': response.data.list[0].pastureId } }).then(response => {
  128. commit('SET_BUTTONS', response.data.list)
  129. })
  130. }
  131. })
  132. commit('SET_ROLES', role)
  133. commit('SET_NAME', username)
  134. commit('SET_AVATAR', avatar)
  135. resolve(data)
  136. }).catch(error => {
  137. reject(error)
  138. })
  139. })
  140. },
  141. // user logout
  142. logout({ commit, state }) {
  143. return new Promise((resolve, reject) => {
  144. logout(state.token).then(() => {
  145. commit('SET_BUTTONS', [])
  146. // console.log('-------------', state)
  147. commit('SET_TOKEN', '')
  148. commit('SET_ROLES', [])
  149. Cookies.remove('employename')
  150. Cookies.remove('employeid')
  151. Cookies.remove('pastureid')
  152. Cookies.remove('ispastureuse')
  153. removeToken()
  154. resetRouter()
  155. resolve()
  156. location.reload()
  157. }).catch(error => {
  158. reject(error)
  159. })
  160. })
  161. },
  162. // remove token
  163. resetToken({ commit }) {
  164. return new Promise(resolve => {
  165. commit('SET_TOKEN', '')
  166. commit('SET_ROLES', [])
  167. removeToken()
  168. resolve()
  169. })
  170. }
  171. }
  172. export default {
  173. namespaced: true,
  174. state,
  175. mutations,
  176. actions
  177. }