common.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /** @format */
  2. import { defineStore } from 'pinia'
  3. import { useUserStore } from './user'
  4. export const useCommonStore = defineStore(
  5. 'commonStore',
  6. () => {
  7. const navigateTextColor = ref('#ffffff')
  8. const navigateBgColor = ref('#0F0820')
  9. const loginType = ref<string>('choice')
  10. const downloadCatalog = ref<any>(null)
  11. const accountList = ref<any>([])
  12. // const userStore = useUserStore()
  13. const loginedAccountList = computed(() => {
  14. // 获取已经登陆的账号列表的前两位
  15. return accountList.value.filter((item: any) => item.email).slice(0, 2)
  16. })
  17. /**
  18. * 设置头部导航背景色
  19. */
  20. const setNavigateBgColor = (color: string) => {
  21. if (!color)
  22. return
  23. color = color.toUpperCase()
  24. if (color === '#0F0820') {
  25. navigateTextColor.value = '#ffffff'
  26. navigateBgColor.value = 'rgba(15,8,32, 0.7)'
  27. }
  28. else if (color === '#F3F4FB') {
  29. navigateTextColor.value = '#0F0820'
  30. navigateBgColor.value = 'rgba(243,244,251, 0.7)'
  31. }
  32. else if (color === '#FFFFFF') {
  33. navigateTextColor.value = '#0F0820'
  34. navigateBgColor.value = 'rgba(255, 255, 255, 0.7)'
  35. }
  36. }
  37. /**
  38. * 设置已经登陆的账号列表 (在登陆成功后调用)
  39. */
  40. const pushAccount = (user: any) => {
  41. if (!user.id)
  42. return
  43. // 查找相同的id,并更新email和name信息
  44. const existingAccount = accountList.value.find((item: any) => item.id === user.id)
  45. if (existingAccount) {
  46. existingAccount.email = user.email
  47. existingAccount.name = `${user.firstName} ${user.lastName}`
  48. }
  49. else {
  50. accountList.value.push(user)
  51. }
  52. }
  53. /**
  54. * type 登录方式
  55. *
  56. */
  57. const setLoginType = (type: string) => {
  58. if (!type)
  59. return
  60. loginType.value = type
  61. }
  62. /**
  63. * 设置当前的下载的目录数据
  64. */
  65. const setDownloadCatalog = (catalog: any) => {
  66. if (!catalog)
  67. return
  68. downloadCatalog.value = catalog
  69. }
  70. return {
  71. navigateTextColor,
  72. navigateBgColor,
  73. downloadCatalog,
  74. setNavigateBgColor,
  75. setLoginType,
  76. accountList,
  77. loginType,
  78. pushAccount,
  79. loginedAccountList,
  80. setDownloadCatalog,
  81. }
  82. },
  83. {
  84. persist: true,
  85. },
  86. )