global.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { reactive, toRefs } from 'vue'
  2. import { AppTheme } from '@/constants/theme'
  3. import { defineStore } from '../store'
  4. import { localData } from '../storage'
  5. import axios from 'axios'
  6. import plus from '@/utils/h5plus'
  7. export interface SystemInfo {
  8. appName: string; // 应用名称
  9. version: string; // 应用版本
  10. versionCode: string; // 应用版本号
  11. apiUrl: string; // 接口地址
  12. tradeChannel: 'http' | 'ws'; // 交易通道
  13. modules: ('register' | 'delivery')[]; // 应用包含的模块
  14. quotationPropertys: (keyof Model.QuoteDayRsp)[]; // 盘面可显示的属性
  15. forcedPasswordChange: boolean; // 首次登录是否强制修改密码
  16. slogan: string; // 标语
  17. registrationCodeRule: -1 | 0 | 1; // 注册编码规则,-1隐藏,0非必填,1必填
  18. riskType: 0 | 1 | 2; // 风控类型,1按单风控,2按账户风控
  19. }
  20. export const useGlobalStore = defineStore(() => {
  21. const appTheme = localData.getRef('appTheme')
  22. const androidPermissions = localData.getRef('androidPermissions')
  23. const state = reactive({
  24. dateDiff: 0, // 服务器和本地的时间差(毫秒数)
  25. showGoodsListing: false, // 是否显示订单交易挂牌
  26. showPricingListing: false, // 是否显示挂牌点价交易
  27. showTransferListing: false, // 是否显示定金转让挂牌
  28. clientWidth: 0, // 客户端宽度
  29. isMobile: false, // 是否移动设备
  30. systemInfo: <SystemInfo>{
  31. appName: document.title,
  32. version: '1.0.0',
  33. versionCode: '100000',
  34. apiUrl: 'http://localhost',
  35. tradeChannel: 'ws',
  36. modules: [],
  37. quotationPropertys: [],
  38. forcedPasswordChange: false,
  39. slogan: '数字化交易平台\r\n现代化综合服务',
  40. registrationCodeRule: 1,
  41. riskType: 0
  42. }
  43. })
  44. // 获取当前服务器时间戳
  45. const getTimestamp = () => {
  46. const ts = new Date().getTime()
  47. return ts - state.dateDiff
  48. }
  49. // 获取系统配置项
  50. const getSystemConfig = async () => {
  51. const filePath = './config/appconfig.json'
  52. if (plus.hasPlus()) {
  53. const res = await plus.getLocalFileContent(filePath)
  54. state.systemInfo = {
  55. ...state.systemInfo,
  56. ...JSON.parse(res)
  57. }
  58. } else {
  59. const res = await axios(filePath)
  60. state.systemInfo = {
  61. ...state.systemInfo,
  62. ...res.data
  63. }
  64. }
  65. return state.systemInfo
  66. }
  67. // 获取系统信息
  68. const getSystemInfo = <K extends keyof SystemInfo>(key: K) => {
  69. return state.systemInfo[key]
  70. }
  71. // 判断应用是否包含某个模块
  72. const hasSystemModule = (module: SystemInfo['modules'][number]) => {
  73. return state.systemInfo.modules.includes(module)
  74. }
  75. // 判断盘面是否包含某个属性
  76. const hasQuotationProperty = (prop: keyof Model.QuoteDayRsp) => {
  77. return state.systemInfo.quotationPropertys.includes(prop)
  78. }
  79. // 获取APP权限状态
  80. const getAndroidPermissions = <K extends keyof typeof androidPermissions.value>(key: K) => {
  81. if (plus.hasPlus() && plus.getSystemInfo('os') === 'Android') {
  82. return androidPermissions.value[key]
  83. }
  84. return 1
  85. }
  86. // 更新APP权限状态
  87. const setAndroidPermissions = <K extends keyof typeof androidPermissions.value>(key: K, value: number) => {
  88. androidPermissions.value[key] = value
  89. localData.setValue('androidPermissions', androidPermissions.value)
  90. }
  91. // 设置状态栏主题色
  92. const setStatusBarTheme = (theme: AppTheme) => {
  93. switch (theme) {
  94. case AppTheme.Default:
  95. case AppTheme.Dark: {
  96. plus.setStatusBarStyle('light')
  97. break
  98. }
  99. default: {
  100. plus.setStatusBarStyle('dark')
  101. }
  102. }
  103. }
  104. // 设置主题
  105. const setTheme = (key: keyof typeof AppTheme) => {
  106. const theme = AppTheme[key]
  107. setStatusBarTheme(theme)
  108. document.documentElement.setAttribute('theme', theme)
  109. appTheme.value = theme
  110. }
  111. // 适配客户端屏幕
  112. const screenAdapter = (remsize = false) => {
  113. const { isPc } = getClientAgent()
  114. const el = document.documentElement
  115. const body = document.body
  116. let screenWidth = el.clientWidth
  117. if (remsize) {
  118. const designSize = 750
  119. state.isMobile = true
  120. if (isPc) {
  121. screenWidth = designSize / 1.8
  122. body.style.setProperty('width', '540px')
  123. } else {
  124. body.style.removeProperty('width')
  125. }
  126. if (screenWidth > 0) {
  127. const fontSize = (screenWidth / designSize) * 100 + 'px'
  128. el.style.setProperty('font-size', fontSize)
  129. }
  130. } else {
  131. // if (screenWidth > 768) {
  132. // state.isMobile = false
  133. // } else {
  134. // state.isMobile = true
  135. // }
  136. el.setAttribute('screen', state.isMobile ? 'small' : 'normal')
  137. }
  138. state.clientWidth = body.clientWidth
  139. }
  140. // 获取客户端平台
  141. const getClientAgent = () => {
  142. const ua = navigator.userAgent,
  143. isWindowsPhone = /(?:Windows Phone)/.test(ua),
  144. isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone,
  145. isAndroid = /(?:Android)/.test(ua),
  146. isFireFox = /(?:Firefox)/.test(ua),
  147. isChrome = /(?:Chrome|CriOS)/.test(ua),
  148. isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua)),
  149. isiPhone = /(?:iPhone)/.test(ua) && !isTablet,
  150. isPc = !isiPhone && !isAndroid && !isSymbian && !isTablet
  151. return {
  152. isTablet,
  153. isiPhone,
  154. isAndroid,
  155. isChrome,
  156. isPc,
  157. }
  158. }
  159. // 加载主题
  160. const loadTheme = () => {
  161. setStatusBarTheme(appTheme.value)
  162. document.documentElement.setAttribute('theme', appTheme.value)
  163. document.removeEventListener('DOMContentLoaded', loadTheme)
  164. }
  165. // 等待 dom 加载完毕
  166. document.addEventListener('DOMContentLoaded', loadTheme, false)
  167. return {
  168. ...toRefs(state),
  169. appTheme,
  170. getTimestamp,
  171. getSystemConfig,
  172. getSystemInfo,
  173. hasSystemModule,
  174. hasQuotationProperty,
  175. getAndroidPermissions,
  176. setAndroidPermissions,
  177. setStatusBarTheme,
  178. setTheme,
  179. screenAdapter,
  180. getClientAgent
  181. }
  182. })