themeStore.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import type { SystemThemeState, ThemeMode } from '@/composables/types/theme'
  2. import { defineStore } from 'pinia'
  3. import { themeColorOptions } from '@/composables/types/theme'
  4. /**
  5. * 简化版系统主题状态管理
  6. * 仅支持跟随系统主题,不提供手动切换功能
  7. * 导航栏颜色通过 theme.json 自动处理
  8. */
  9. export const useThemeStore = defineStore('theme', {
  10. state: (): SystemThemeState => ({
  11. theme: 'light',
  12. themeVars: {
  13. darkBackground: '#0f0f0f',
  14. darkBackground2: '#1a1a1a',
  15. darkBackground3: '#242424',
  16. darkBackground4: '#2f2f2f',
  17. darkBackground5: '#3d3d3d',
  18. darkBackground6: '#4a4a4a',
  19. darkBackground7: '#606060',
  20. darkColor: '#ffffff',
  21. darkColor2: '#e0e0e0',
  22. darkColor3: '#a0a0a0',
  23. colorTheme: themeColorOptions[0].primary,
  24. },
  25. }),
  26. getters: {
  27. isDark: state => state.theme === 'dark',
  28. },
  29. actions: {
  30. /**
  31. * 获取系统主题
  32. * @returns 系统主题模式
  33. */
  34. getSystemTheme(): ThemeMode {
  35. // #ifdef MP-WEIXIN
  36. // 微信小程序使用 getAppBaseInfo
  37. const appBaseInfo = uni.getAppBaseInfo()
  38. if (appBaseInfo && appBaseInfo.theme) {
  39. return appBaseInfo.theme as ThemeMode
  40. }
  41. // #endif
  42. // #ifndef MP-WEIXIN
  43. // 其他平台使用 getSystemInfoSync
  44. const systemInfo = uni.getSystemInfoSync()
  45. if (systemInfo && systemInfo.theme) {
  46. return systemInfo.theme as ThemeMode
  47. }
  48. // #endif
  49. return 'light' // 默认返回 light
  50. },
  51. /**
  52. * 设置主题(仅内部使用)
  53. * @param theme 主题模式
  54. */
  55. setTheme(theme: ThemeMode) {
  56. this.theme = theme
  57. },
  58. /**
  59. * 初始化系统主题
  60. */
  61. initSystemTheme() {
  62. const systemTheme = this.getSystemTheme()
  63. this.theme = systemTheme
  64. console.log('初始化系统主题:', this.theme)
  65. },
  66. },
  67. })