0f3f91650c096154a508395de7080a5ae85e264e.svn-base 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. decimal:''
  21. }
  22. const mutations = {
  23. SET_DASHCHARTID: (state, dashchartid) => {
  24. state.dashchartid = dashchartid
  25. },
  26. SET_DASHCHARTIDISEDIT: (state, dashchartidisedit) => {
  27. state.dashchartidisedit = dashchartidisedit
  28. },
  29. SET_TOKEN: (state, token) => {
  30. state.token = token
  31. },
  32. SET_NAME: (state, name) => {
  33. state.name = name
  34. Cookies.set('name', name)
  35. },
  36. SET_AVATAR: (state, avatar) => {
  37. state.avatar = avatar
  38. },
  39. SET_ROLES: (state, roles) => {
  40. state.roles = roles
  41. },
  42. SET_PASTUREID: (state, pastureid) => {
  43. Cookies.set('pastureid', pastureid)
  44. Cookies.set('pageCount', 50)
  45. state.pastureid = pastureid
  46. },
  47. SET_PASTURENAME: (state, pasturename) => {
  48. Cookies.set('pasturename', pasturename)
  49. state.pasturename = pasturename
  50. },
  51. SET_PASTURENUMBER: (state, pasturenumber) => {
  52. Cookies.set('pasturenumber', pasturenumber)
  53. state.pasturenumber = pasturenumber
  54. },
  55. SET_EMPLOYEID: (state, employeid) => {
  56. Cookies.set('employeid', employeid)
  57. // console.log('Cookies.set.employeid', employeid)
  58. state.employeid = employeid
  59. },
  60. SET_EMPLOYENAME: (state, employename) => {
  61. Cookies.set('employename', employename)
  62. state.employename = employename
  63. },
  64. SET_ISPASTUREUSE: (state, ispastureuse) => {
  65. Cookies.set('ispastureuse', ispastureuse)
  66. state.ispastureuse = ispastureuse
  67. },
  68. SET_DECIMAL: (state, decimal) => {
  69. Cookies.set('decimal', decimal)
  70. state.decimal = decimal
  71. },
  72. SET_DEPARTMENTID: (state, departmentid) => {
  73. Cookies.set('departmentid', departmentid)
  74. state.departmentid = departmentid
  75. },
  76. SET_BUTTONS: (state, buttons) => {
  77. Cookies.set('buttons', buttons)
  78. sessionStorage.setItem('buttons', JSON.stringify(buttons))
  79. state.buttons = buttons
  80. }
  81. }
  82. const actions = {
  83. // user login
  84. login({ commit }, userInfo) {
  85. const { username, password } = userInfo
  86. return new Promise((resolve, reject) => {
  87. login({ username: username.trim(), password: password }).then(response => {
  88. const { data, msg } = response
  89. if (msg !== 'ok') {
  90. Message({
  91. message: data,
  92. type: 'error',
  93. duration: 5 * 1000
  94. })
  95. reject(data)
  96. }
  97. commit('SET_TOKEN', data.token)
  98. if (Cookies == null) {
  99. console.log(1)
  100. }
  101. setToken(data.token)
  102. resolve()
  103. }).catch(error => {
  104. reject(error)
  105. })
  106. })
  107. },
  108. // get user info
  109. getInfo({ commit, state }) {
  110. return new Promise((resolve, reject) => {
  111. getInfo(state.token).then(response => {
  112. const { data } = response
  113. if (!data) {
  114. reject('Verification failed, please Login again.')
  115. }
  116. const { role, username, avatar } = data
  117. // roles must be a non-empty array
  118. if (!role || role.length <= 0) {
  119. // reject('getInfo: roles must be a non-null array!')
  120. reject('该用户未授权,请联系管理员!')
  121. return false
  122. }
  123. // if (roleid !== null || roleid !== undefined || roleid !== '') {
  124. // reject('getInfo: roles must be a non-null array!')
  125. // }
  126. GetDataByName({ 'name': 'findByUserPastureName', 'parammaps': { 'jwt_username': username } }).then(response => {
  127. if (response.data.list !== null) {
  128. commit('SET_PASTUREID', response.data.list[0].pastureId)
  129. commit('SET_PASTURENAME', response.data.list[0].pastureName)
  130. commit('SET_PASTURENUMBER', response.data.list[0].pastureNumber)
  131. commit('SET_EMPLOYEID', response.data.list[0].employeId)
  132. commit('SET_EMPLOYENAME', response.data.list[0].employeName)
  133. commit('SET_ISPASTUREUSE', response.data.list[0].isgroups)
  134. commit('SET_DECIMAL', 2)
  135. GetDataByName({ 'name': 'getUserPCButtons', 'parammaps': { 'username': username, 'pastureid': response.data.list[0].pastureId } }).then(response => {
  136. if(response.data.list !== null){
  137. commit('SET_BUTTONS', response.data.list)
  138. }else{
  139. commit('SET_BUTTONS', [])
  140. }
  141. })
  142. }
  143. })
  144. commit('SET_ROLES', role)
  145. commit('SET_NAME', username)
  146. commit('SET_AVATAR', avatar)
  147. resolve(data)
  148. }).catch(error => {
  149. reject(error)
  150. })
  151. })
  152. },
  153. // user logout
  154. logout({ commit, state }) {
  155. return new Promise((resolve, reject) => {
  156. logout(state.token).then(() => {
  157. commit('SET_BUTTONS', [])
  158. // console.log('-------------', state)
  159. commit('SET_TOKEN', '')
  160. commit('SET_ROLES', [])
  161. Cookies.remove('employename')
  162. Cookies.remove('employeid')
  163. Cookies.remove('pastureid')
  164. Cookies.remove('ispastureuse')
  165. Cookies.remove('decimal')
  166. removeToken()
  167. resetRouter()
  168. resolve()
  169. location.reload()
  170. }).catch(error => {
  171. reject(error)
  172. })
  173. })
  174. },
  175. // remove token
  176. resetToken({ commit }) {
  177. return new Promise(resolve => {
  178. commit('SET_TOKEN', '')
  179. commit('SET_ROLES', [])
  180. removeToken()
  181. resolve()
  182. })
  183. }
  184. }
  185. export default {
  186. namespaced: true,
  187. state,
  188. mutations,
  189. actions
  190. }