index.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { Modal, message } from 'ant-design-vue'
  2. import { defineStore } from 'pinia'
  3. import { reactive } from 'vue'
  4. import { UserController } from '@/controller'
  5. import { useRouter, useRoute } from 'vue-router'
  6. import { routes } from '@/router'
  7. const initState: USER.Tenant.Detail = {
  8. id: '',
  9. tenantId: '',
  10. token: '',
  11. email: '',
  12. account: '',
  13. label: '蛟龙云'
  14. }
  15. export const useUserStore = defineStore('userStore', () => {
  16. const router = useRouter()
  17. const route = useRoute()
  18. let userInfo = reactive<USER.Tenant.Detail>(JSON.parse(window.localStorage.getItem('userInfo') as string) || initState)
  19. const clearUserInfo = () => {
  20. return new Promise((resolve) => {
  21. // 做些什么
  22. message.loading('清除用户信息中......', 0.5)
  23. window.localStorage.removeItem('userInfo')
  24. setTimeout(() => {
  25. resolve(true)
  26. }, 500)
  27. })
  28. }
  29. const login = async (data: USER.Params.Login) => {
  30. // 存储用户信息 跳转页面
  31. const { data: tenant, code } = await UserController.login({
  32. ...data,
  33. password: encryptPassWord(data.password)
  34. })
  35. if (code === 200) {
  36. window.localStorage.setItem('userInfo', JSON.stringify(tenant))
  37. const userInfoLocal = JSON.parse(window.localStorage.getItem('userInfo') as string)
  38. userInfo = reactive(userInfoLocal) || initState
  39. message.success('登录成功')
  40. if (route.query.redirectUrl) {
  41. router.push({ path: route.query.redirectUrl as string })
  42. } else {
  43. router.push({ path: routes[0].path })
  44. }
  45. }
  46. }
  47. const openUserInfoModal = () => {
  48. Modal.success({
  49. title: userInfo.label + '的信息',
  50. content: '需要想到优雅的方式来打开个人信息弹窗',
  51. cancelText: '取消',
  52. okText: '确定',
  53. onCancel: () => {},
  54. onOk: () => {}
  55. })
  56. }
  57. const logout = () => {
  58. Modal.confirm({
  59. title: '您确定要退出吗?',
  60. cancelText: '取消',
  61. okText: '确定',
  62. onCancel: () => {},
  63. onOk: async () => {
  64. await clearUserInfo()
  65. message.success('登出成功')
  66. // 退出
  67. router.push({ path: '/login' })
  68. }
  69. })
  70. }
  71. const encryptPassWord = (password: string) => encodeURI(btoa(password))
  72. return {
  73. userInfo,
  74. login,
  75. logout,
  76. clearUserInfo,
  77. openUserInfoModal,
  78. encryptPassWord
  79. }
  80. })