user.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { loginReq, logoutReq, getInfoReq } from '@/api/user'
  2. import {
  3. getToken,
  4. setToken,
  5. removeToken,
  6. setUserId,
  7. TokenKey,
  8. } from '@/utils/auth'
  9. import { Query } from '@/api/dataIntegration'
  10. import { login, loginUp } from '@/api/newLogin'
  11. import { ObjTy } from '~/common'
  12. import router, { constantRoutes, asyncRoutes } from '@/router'
  13. import { defineStore } from 'pinia'
  14. import { usePermissionStore } from '@/store/permission'
  15. import { useTagsViewStore } from '@/store/tagsView'
  16. const resetRouter = () => {
  17. const asyncRouterNameArr: Array<any> = asyncRoutes.map((mItem) => mItem.name)
  18. asyncRouterNameArr.forEach((name) => {
  19. if (router.hasRoute(name)) {
  20. router.removeRoute(name)
  21. }
  22. })
  23. }
  24. export const useUserStore = defineStore('user', {
  25. state: () => {
  26. return {
  27. username: '',
  28. avatar: '',
  29. roles: [] as Array<any>,
  30. token: getToken(),
  31. UserType: sessionStorage.getItem('UserType')
  32. ? sessionStorage.getItem('UserType')
  33. : '',
  34. UserId: sessionStorage.getItem('User_Id')
  35. ? sessionStorage.getItem('User_Id')
  36. : '',
  37. userPoewrList: sessionStorage.getItem('userAuthList')
  38. ? JSON.parse(sessionStorage.getItem('userAuthList'))
  39. : [],
  40. }
  41. },
  42. actions: {
  43. M_username(username: string) {
  44. this.$patch((state) => {
  45. state.username = username
  46. })
  47. },
  48. M_AVATAR(avatar: string) {
  49. this.$patch((state) => {
  50. state.avatar = avatar
  51. })
  52. },
  53. M_roles(roles: Array<string>) {
  54. this.$patch((state) => {
  55. state.roles = roles
  56. })
  57. },
  58. SET_TOKEN(token: string) {
  59. this.$patch((state) => {
  60. state.token = token
  61. })
  62. },
  63. SET_UserId(UserId: string) {
  64. this.$patch((state) => {
  65. state.UserId = UserId
  66. })
  67. },
  68. SET_UserType(UserType: string) {
  69. this.$patch((state) => {
  70. state.UserType = UserType
  71. sessionStorage.setItem('UserType', UserType)
  72. })
  73. },
  74. SET_UserPoewrList(userPoewrList: any) {
  75. this.$patch((state) => {
  76. state.userPoewrList = userPoewrList
  77. })
  78. },
  79. login(data: ObjTy) {
  80. return new Promise((resolve, reject) => {
  81. login(data)
  82. .then((res: ObjTy) => {
  83. const { date, token, userid } = res.data
  84. this.SET_TOKEN(token)
  85. this.SET_UserId(userid)
  86. this.SET_UserType('1')
  87. setToken('userName', data.username)
  88. setToken(TokenKey, token)
  89. setUserId(userid)
  90. setToken('userid', userid)
  91. resolve(null)
  92. })
  93. .catch((error: any) => {
  94. reject(error)
  95. })
  96. })
  97. },
  98. // get user info
  99. getInfo() {
  100. return new Promise((resolve, reject) => {
  101. Query({
  102. id: DATACONTENT_ID.loginId,
  103. dataContent: [getToken('userid')],
  104. })
  105. .then((res: ObjTy) => {
  106. if (res.code == 0) {
  107. const { listValues } = res.returnData
  108. const datas = listValues
  109. if (datas && datas.length) {
  110. const arrs: any = []
  111. for (const item of datas) {
  112. if (item.ident) {
  113. arrs.push(item.ident)
  114. }
  115. }
  116. // arrs.push('transit');
  117. sessionStorage.setItem('userAuthList', JSON.stringify(arrs))
  118. setTimeout(() => {
  119. const data = {
  120. roles: arrs,
  121. introduction: '',
  122. avatar: '',
  123. name: getToken('userName'),
  124. token: this.token,
  125. }
  126. const { name, avatar, roles } = data
  127. this.M_username(name)
  128. this.M_AVATAR(avatar)
  129. this.M_roles(roles ? roles : ['admin'])
  130. this.SET_UserPoewrList(roles ? roles : ['admin'])
  131. resolve(data)
  132. }, 100)
  133. } else {
  134. const arr: any = []
  135. sessionStorage.setItem('userAuthList', arr)
  136. setTimeout(() => {
  137. const data = {
  138. roles: this.userPoewrList,
  139. introduction: '',
  140. avatar: '',
  141. name: getToken('userName'),
  142. token: this.token,
  143. }
  144. const { name, avatar, roles } = data
  145. this.M_username(name)
  146. this.M_AVATAR(avatar)
  147. this.M_roles(roles ? roles : ['admin'])
  148. this.SET_UserPoewrList(roles ? roles : ['admin'])
  149. resolve(data)
  150. }, 100)
  151. }
  152. } else {
  153. reject(res.returnData)
  154. }
  155. })
  156. .catch((error: any) => {
  157. reject(error)
  158. })
  159. })
  160. },
  161. // user logout
  162. logout() {
  163. return new Promise((resolve, reject) => {
  164. loginUp({})
  165. .then(() => {
  166. this.resetState()
  167. resolve(null)
  168. })
  169. .catch((error: any) => {
  170. reject(error)
  171. })
  172. })
  173. },
  174. resetState() {
  175. return new Promise((resolve) => {
  176. sessionStorage.clear()
  177. removeToken('codeToken')
  178. removeToken('systemSet')
  179. removeToken('userid')
  180. this.M_username('')
  181. this.M_roles([])
  182. removeToken() // must remove token first
  183. resetRouter() // reset the router
  184. const permissionStore = usePermissionStore()
  185. permissionStore.M_isGetUserInfo(false)
  186. const tagsViewStore = useTagsViewStore()
  187. tagsViewStore.delAllViews()
  188. resolve(null)
  189. })
  190. },
  191. },
  192. })