index.uts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. export type SafeArea = {
  2. top : number,
  3. right : number,
  4. bottom : number,
  5. left : number,
  6. width : number,
  7. height : number,
  8. }
  9. export const AGREE_PRIVACY = 'UNI-PRIVACY-AGREE'
  10. type State = {
  11. lifeCycleNum : number,
  12. // 状态栏高度
  13. statusBarHeight : number,
  14. safeArea : SafeArea
  15. // 设备像素比
  16. devicePixelRatio : number
  17. // 事件判断回调
  18. eventCallbackNum: number
  19. // 是否匹配左侧窗口
  20. noMatchLeftWindow:boolean
  21. // 当前激活的tab页
  22. active: string
  23. leftWinActive:string
  24. // 是否同意隐私政策
  25. agreeToPrivacy: boolean | null
  26. // 是否同意截屏
  27. allowCapture: boolean,
  28. // 是否暗黑主题(适配web端)
  29. isDarkMode: boolean
  30. // 是否无网环境
  31. netless: boolean
  32. }
  33. const getAgreePrivacy = () => {
  34. const data = uni.getStorageSync(AGREE_PRIVACY)
  35. if (typeof data === 'boolean') {
  36. return data
  37. }
  38. if (data == null) {
  39. return null
  40. }
  41. return null
  42. }
  43. export const state = reactive({
  44. lifeCycleNum: 0,
  45. statusBarHeight: 0,
  46. devicePixelRatio: 1,
  47. eventCallbackNum: 0,
  48. noMatchLeftWindow: true,
  49. active: 'componentPage',
  50. leftWinActive: '/pages/component/view/view',
  51. safeArea: {
  52. top: 0,
  53. right: 0,
  54. bottom: 0,
  55. left: 0,
  56. width: 0,
  57. height: 0,
  58. },
  59. agreeToPrivacy: getAgreePrivacy(),
  60. allowCapture: true,
  61. isDarkMode:false,
  62. netless: false
  63. } as State)
  64. export const setLifeCycleNum = (num : number) => {
  65. state.lifeCycleNum = num
  66. }
  67. export const setEventCallbackNum = (num : number) => {
  68. state.eventCallbackNum = num
  69. }
  70. export const setStatusBarHeight = (height : number) => {
  71. state.statusBarHeight = height
  72. }
  73. export const setSafeArea = (value : SafeArea) => {
  74. state.safeArea = value
  75. }
  76. export const setDevicePixelRatio = (devicePixelRatio : number) => {
  77. state.devicePixelRatio = devicePixelRatio
  78. }
  79. export const setMatchLeftWindow = (matchLeftWindow:boolean) => {
  80. state.noMatchLeftWindow = !matchLeftWindow
  81. }
  82. export const setActive = (tabPage:string) => {
  83. state.active = tabPage
  84. }
  85. export const setLeftWinActive = (leftWinActive:string) => {
  86. state.leftWinActive = leftWinActive
  87. }
  88. export const setAgreeToPrivacy = (agree: boolean) => {
  89. state.agreeToPrivacy = agree
  90. uni.setStorageSync(AGREE_PRIVACY, agree);
  91. }
  92. export const setAllowCapture = (allow: boolean) => {
  93. state.allowCapture = allow
  94. }
  95. // 检查系统主题
  96. export const checkSystemTheme = () => {
  97. // #ifdef WEB || MP
  98. const info = uni.getAppBaseInfo();
  99. state.isDarkMode = info.hostTheme === 'dark';
  100. uni.onHostThemeChange((result) => {
  101. state.isDarkMode = result.hostTheme === 'dark';
  102. });
  103. // #endif
  104. // #ifdef APP
  105. uni.getSystemInfo({
  106. success: (res : GetSystemInfoResult) => {
  107. const appTheme = res.appTheme == "auto" ? res.osTheme! : res.appTheme!
  108. state.isDarkMode = appTheme.trim() == 'dark';
  109. }
  110. })
  111. uni.onAppThemeChange((res : AppThemeChangeResult) => {
  112. state.isDarkMode = res.appTheme.trim() == 'dark';
  113. })
  114. // #endif
  115. }
  116. export const setNetless = (netless: boolean) => {
  117. state.netless = netless
  118. }