actions.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { ActionTree, ActionContext } from 'vuex'
  2. import { RootState, store, useStore } from '@/store'
  3. import { UserState } from './state'
  4. import { Mutations } from './mutations'
  5. import { UserMutationTypes } from './mutation-types'
  6. import { UserActionTypes } from './action-types'
  7. import { getCookieMac, removeToken, setSessionUserInfo, setToken } from '@/utils/cookies'
  8. import { PermissionActionType } from '../permission/action-types'
  9. import router, { resetRouter } from '@/router'
  10. import { useRoute, useRouter } from 'vue-router'
  11. import { RouteRecordRaw } from 'vue-router'
  12. import { getUsersInfo } from '@/apis/user'
  13. import { clearAllLocalData, getLocalProjectId, setLocalProjectId } from '@/utils'
  14. import { Toast } from 'vant'
  15. type AugmentedActionContext = {
  16. commit<K extends keyof Mutations>(
  17. key: K,
  18. payload: Parameters<Mutations[K]>[1],
  19. ): ReturnType<Mutations[K]>
  20. } & Omit<ActionContext<UserState, RootState>, 'commit'>
  21. export interface Actions {
  22. [UserActionTypes.ACTION_LOGIN](
  23. { commit }: AugmentedActionContext,
  24. userInfo: { username: string, password: string }
  25. ): void
  26. [UserActionTypes.ACTION_RESET_TOKEN](
  27. { commit }: AugmentedActionContext
  28. ): void
  29. [UserActionTypes.ACTION_GET_USER_INFO](
  30. { commit }: AugmentedActionContext
  31. ): void
  32. [UserActionTypes.ACTION_CHANGE_ROLES](
  33. { commit }: AugmentedActionContext, role: string
  34. ): void
  35. [UserActionTypes.ACTION_LOGIN_OUT](
  36. { commit }: AugmentedActionContext,
  37. ): void
  38. }
  39. export const actions: ActionTree<UserState, RootState> & Actions = {
  40. async [UserActionTypes.ACTION_LOGIN](
  41. { commit }: AugmentedActionContext,
  42. userInfo: { username: string, password: string }
  43. ) {
  44. console.log(userInfo)
  45. // let { username, password } = userInfo
  46. // username = username.trim()
  47. // await loginRequest({ username, password }).then(async(res) => {
  48. // if (res?.code === 0 && res.data.accessToken) {
  49. // setToken(res.data.accessToken)
  50. commit(UserMutationTypes.SET_TOKEN, 'eyJhbGciOiJIUzI1NiJ9.eyJidWlsZGluZ19pZCI6ImZmZDc4OGIwOTQzNTQ4NTFiMDM0NTY2YzRkMTJkMTFjIiwidXNlcl9pZCI6IjYwMmVmNzA5YmViNjRhMWY5YTRiNmFmMjE5NmQwYWY3IiwidXNlcl9uYW1lIjpudWxsLCJvd25lcl9pZCI6IjkzNmE4ZWFlMDJmOTExZWNiNTQyMDJiNzIzZjVmYmQwIiwibXlfdGhpcmRfaWQiOiIyMGQ4Mzg0OTZjMGQ0ODk4YjFkN2MxNmRkZTc5ZGQwNiIsInRoaXJkX3BsYXRmb3JtX2lkIjoib3pUVGs1UFJSdUNIRUNqUXZKSXJyT3A1MkRuNCIsInNUaW1lIjoxNjQ2MDM1ODYzNTYwLCJleHAiOjE2NDYzOTU4NjM1NjAsInR5cGUiOiJ3ZWNoYXQiLCJhcHBfaWQiOiJ3eDNhZGQ2MzJmZDMxNDU3YzcifQ.C0khcRbjp_VuGF8gzr-cdybPhX1U1bSEk_48ypub6Y0')
  51. // }
  52. // }).catch((err) => {
  53. // console.log(err)
  54. // })
  55. },
  56. [UserActionTypes.ACTION_RESET_TOKEN](
  57. { commit }: AugmentedActionContext) {
  58. removeToken()
  59. commit(UserMutationTypes.SET_TOKEN, '')
  60. // commit(UserMutationTypes.SET_ROLES, [])
  61. },
  62. async [UserActionTypes.ACTION_GET_USER_INFO]({ commit }: AugmentedActionContext) {
  63. // let nowProjectId: any = 'Pj1101080259'
  64. const userInfo: any = {
  65. userName: '陈珍',
  66. userPhone: 15321277745,
  67. userId: '602ef709beb64a1f9a4b6af2196d0af7'
  68. // userId: '9a1ecfbacb6b4f249bf2dd3ec7793ead'
  69. // userId: '1302990695845462811'
  70. }
  71. setSessionUserInfo(JSON.stringify(userInfo))
  72. commit(UserMutationTypes.SET_USER_NAME, userInfo.userName)
  73. commit(UserMutationTypes.SET_USER_PHONE, userInfo.phone)
  74. commit(UserMutationTypes.SET_USER_ID, userInfo.userId)
  75. let macAdr: any = getCookieMac()
  76. commit(UserMutationTypes.SET_USER_MAC, macAdr)
  77. // store.commit(UserMutationTypes.SET_PROJECT_ID, nowProjectId)
  78. },
  79. async [UserActionTypes.ACTION_CHANGE_ROLES](
  80. { commit }: AugmentedActionContext,
  81. role: string
  82. ) {
  83. const token = role + '-token'
  84. const store = useStore()
  85. commit(UserMutationTypes.SET_TOKEN, token)
  86. setToken(token)
  87. await store.dispatch(UserActionTypes.ACTION_GET_USER_INFO, undefined)
  88. store.dispatch(PermissionActionType.ACTION_SET_ROUTES, [])
  89. store.state.permission.dynamicRoutes.forEach((item: RouteRecordRaw) => {
  90. router.addRoute(item)
  91. })
  92. },
  93. [UserActionTypes.ACTION_LOGIN_OUT](
  94. { commit }: AugmentedActionContext
  95. ) {
  96. console.log(commit)
  97. removeToken()
  98. commit(UserMutationTypes.SET_TOKEN, '')
  99. // commit(UserMutationTypes.SET_ROLES, [])
  100. resetRouter()
  101. }
  102. }