5e1d39033ba6043329955a305fa744d5e0006b3f.svn-base 5.4 KB

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