actions.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. import { queryWorkSpace } from '@/apis/envmonitor'
  16. type AugmentedActionContext = {
  17. commit<K extends keyof Mutations>(
  18. key: K,
  19. payload: Parameters<Mutations[K]>[1],
  20. ): ReturnType<Mutations[K]>
  21. } & Omit<ActionContext<UserState, RootState>, 'commit'>
  22. export interface Actions {
  23. [UserActionTypes.ACTION_LOGIN](
  24. { commit }: AugmentedActionContext,
  25. userInfo: { username: string, password: string }
  26. ): void
  27. [UserActionTypes.ACTION_RESET_TOKEN](
  28. { commit }: AugmentedActionContext
  29. ): void
  30. [UserActionTypes.ACTION_GET_USER_INFO](
  31. { commit }: AugmentedActionContext
  32. ): void
  33. [UserActionTypes.ACTION_GET_WORKSPACE](
  34. { commit }: AugmentedActionContext
  35. ): void
  36. [UserActionTypes.ACTION_CHANGE_ROLES](
  37. { commit }: AugmentedActionContext, role: string
  38. ): void
  39. [UserActionTypes.ACTION_LOGIN_OUT](
  40. { commit }: AugmentedActionContext,
  41. ): void
  42. }
  43. export const actions: ActionTree<UserState, RootState> & Actions = {
  44. async [UserActionTypes.ACTION_LOGIN](
  45. { commit }: AugmentedActionContext,
  46. userInfo: { username: string, password: string }
  47. ) {
  48. console.log(userInfo)
  49. // let { username, password } = userInfo
  50. // username = username.trim()
  51. // await loginRequest({ username, password }).then(async(res) => {
  52. // if (res?.code === 0 && res.data.accessToken) {
  53. // setToken(res.data.accessToken)
  54. commit(UserMutationTypes.SET_TOKEN, 'eyJhbGciOiJIUzI1NiJ9.eyJidWlsZGluZ19pZCI6ImZmZDc4OGIwOTQzNTQ4NTFiMDM0NTY2YzRkMTJkMTFjIiwidXNlcl9pZCI6IjYwMmVmNzA5YmViNjRhMWY5YTRiNmFmMjE5NmQwYWY3IiwidXNlcl9uYW1lIjpudWxsLCJvd25lcl9pZCI6IjkzNmE4ZWFlMDJmOTExZWNiNTQyMDJiNzIzZjVmYmQwIiwibXlfdGhpcmRfaWQiOiIyMGQ4Mzg0OTZjMGQ0ODk4YjFkN2MxNmRkZTc5ZGQwNiIsInRoaXJkX3BsYXRmb3JtX2lkIjoib3pUVGs1UFJSdUNIRUNqUXZKSXJyT3A1MkRuNCIsInNUaW1lIjoxNjQ2MDM1ODYzNTYwLCJleHAiOjE2NDYzOTU4NjM1NjAsInR5cGUiOiJ3ZWNoYXQiLCJhcHBfaWQiOiJ3eDNhZGQ2MzJmZDMxNDU3YzcifQ.C0khcRbjp_VuGF8gzr-cdybPhX1U1bSEk_48ypub6Y0')
  55. // }
  56. // }).catch((err) => {
  57. // console.log(err)
  58. // })
  59. },
  60. [UserActionTypes.ACTION_RESET_TOKEN](
  61. { commit }: AugmentedActionContext) {
  62. removeToken()
  63. commit(UserMutationTypes.SET_TOKEN, '')
  64. // commit(UserMutationTypes.SET_ROLES, [])
  65. },
  66. async [UserActionTypes.ACTION_GET_USER_INFO]({ commit }: AugmentedActionContext) {
  67. // let nowProjectId: any = 'Pj1101080259'
  68. const userInfo: any = {
  69. userName: '陈珍',
  70. userPhone: 15321277745,
  71. userId: '602ef709beb64a1f9a4b6af2196d0af7'
  72. // userId: '9a1ecfbacb6b4f249bf2dd3ec7793ead'
  73. // userId: '1302990695845462811'
  74. }
  75. setSessionUserInfo(JSON.stringify(userInfo))
  76. commit(UserMutationTypes.SET_USER_NAME, userInfo.userName)
  77. commit(UserMutationTypes.SET_USER_PHONE, userInfo.phone)
  78. commit(UserMutationTypes.SET_USER_ID, userInfo.userId)
  79. let macAdr: any = getCookieMac()
  80. commit(UserMutationTypes.SET_USER_MAC, macAdr)
  81. // store.commit(UserMutationTypes.SET_PROJECT_ID, nowProjectId)
  82. },
  83. async [UserActionTypes.ACTION_GET_WORKSPACE]({ commit }: AugmentedActionContext) {
  84. let params: any = {
  85. "criteria": {
  86. "macAddress": "DA:BF:E8:7A:61:97"
  87. },
  88. "orders": [
  89. {
  90. "column": "createTime",
  91. "asc": false
  92. }
  93. ],
  94. "page": 1,
  95. "size": 1
  96. }
  97. queryWorkSpace(params).then((res) => {
  98. console.log("===")
  99. console.log(res)
  100. })
  101. },
  102. async [UserActionTypes.ACTION_CHANGE_ROLES](
  103. { commit }: AugmentedActionContext,
  104. role: string
  105. ) {
  106. const token = role + '-token'
  107. const store = useStore()
  108. commit(UserMutationTypes.SET_TOKEN, token)
  109. setToken(token)
  110. await store.dispatch(UserActionTypes.ACTION_GET_USER_INFO, undefined)
  111. store.dispatch(PermissionActionType.ACTION_SET_ROUTES, [])
  112. store.state.permission.dynamicRoutes.forEach((item: RouteRecordRaw) => {
  113. router.addRoute(item)
  114. })
  115. },
  116. [UserActionTypes.ACTION_LOGIN_OUT](
  117. { commit }: AugmentedActionContext
  118. ) {
  119. console.log(commit)
  120. removeToken()
  121. commit(UserMutationTypes.SET_TOKEN, '')
  122. // commit(UserMutationTypes.SET_ROLES, [])
  123. resetRouter()
  124. }
  125. }