136239f1b8a3a0e6aed45ed3227507531680d575.svn-base 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. if(response.data.list !== null){
  128. commit('SET_BUTTONS', response.data.list)
  129. }else{
  130. commit('SET_BUTTONS', [])
  131. }
  132. })
  133. }
  134. })
  135. commit('SET_ROLES', role)
  136. commit('SET_NAME', username)
  137. commit('SET_AVATAR', avatar)
  138. resolve(data)
  139. }).catch(error => {
  140. reject(error)
  141. })
  142. })
  143. },
  144. // user logout
  145. logout({ commit, state }) {
  146. return new Promise((resolve, reject) => {
  147. logout(state.token).then(() => {
  148. commit('SET_BUTTONS', [])
  149. // console.log('-------------', state)
  150. commit('SET_TOKEN', '')
  151. commit('SET_ROLES', [])
  152. Cookies.remove('employename')
  153. Cookies.remove('employeid')
  154. Cookies.remove('pastureid')
  155. Cookies.remove('ispastureuse')
  156. removeToken()
  157. resetRouter()
  158. resolve()
  159. location.reload()
  160. }).catch(error => {
  161. reject(error)
  162. })
  163. })
  164. },
  165. // remove token
  166. resetToken({ commit }) {
  167. return new Promise(resolve => {
  168. commit('SET_TOKEN', '')
  169. commit('SET_ROLES', [])
  170. removeToken()
  171. resolve()
  172. })
  173. }
  174. }
  175. export default {
  176. namespaced: true,
  177. state,
  178. mutations,
  179. actions
  180. }