App.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <script setup>
  2. import { ref, onMounted, nextTick, watch, onBeforeUnmount, getCurrentInstance } from 'vue';
  3. import { useI18n } from "vue-i18n";
  4. const { locale } = useI18n();
  5. import {
  6. onLoad,
  7. onShow,
  8. onLaunch
  9. } from '@dcloudio/uni-app'
  10. import {
  11. updateRoute
  12. } from "@/hooks/useRoute";
  13. import useGlobalStore from "@/stores/use-global-store";
  14. // import { useAppUpdate } from '@/hooks/useAppUpdate'
  15. // const { checkUpdate } = useAppUpdate()
  16. const globalStore = useGlobalStore()
  17. onLoad((options) => {
  18. updateRoute();
  19. // checkUpdate()
  20. })
  21. onShow((options) => {
  22. updateRoute();
  23. // checkUpdate()
  24. handleSignupRoute(options)
  25. })
  26. // App.vue 或你的初始化文件中
  27. function initTheme() {
  28. // #ifdef H5
  29. // H5 端:使用 matchMedia 主动获取当前系统主题
  30. const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
  31. const theme = isDarkMode ? 'dark' : 'light'
  32. globalStore.setGlobalTheme(theme)
  33. // 监听变化(你的 onThemeChange 已经能工作,但可以再加一层保险)
  34. const darkModeQuery = window.matchMedia('(prefers-color-scheme: dark)')
  35. const handleChange = (e) => {
  36. const newTheme = e.matches ? 'dark' : 'light'
  37. globalStore.setGlobalTheme(newTheme)
  38. }
  39. // 兼容旧版浏览器
  40. if (darkModeQuery.addEventListener) {
  41. darkModeQuery.addEventListener('change', handleChange)
  42. } else {
  43. darkModeQuery.addListener(handleChange)
  44. }
  45. // 设置 data-bs-theme 属性到 html 标签
  46. document.documentElement.setAttribute('data-bs-theme', theme);
  47. document.documentElement.setAttribute('data-color-theme', 'blue');
  48. // 同时设置到 body 标签
  49. document.body.setAttribute('data-bs-theme', theme);
  50. document.body.setAttribute('data-color-theme', 'blue');
  51. // #endif
  52. // #ifdef APP-PLUS
  53. // App 端:使用原生 API
  54. uni.getSystemInfo({
  55. success: (res) => {
  56. let theme = res.osTheme || 'light'
  57. globalStore.setGlobalTheme(theme)
  58. }
  59. })
  60. uni.onThemeChange((res) => {
  61. globalStore.setGlobalTheme(res.theme)
  62. })
  63. // #endif
  64. }
  65. onLaunch((options) => {
  66. // updateRoute();
  67. // checkUpdate()
  68. // 调用初始化
  69. // initTheme()
  70. // #ifdef H5
  71. // 处理 signup 路径
  72. document.title = 'CWGMarkets'
  73. // 防止后面又被改掉,再加个定时器兜底
  74. setInterval(() => {
  75. if (!document.title) document.title = 'CWGMarkets'
  76. }, 500)
  77. // #endif
  78. })
  79. // 解析 URL 参数
  80. const parseUrlParams = () => {
  81. const params = {}
  82. // #ifdef H5
  83. // H5 端:直接从浏览器 URL 解析
  84. if (typeof window !== 'undefined' && window.location) {
  85. const href = window.location.href
  86. // 解析 hash 部分
  87. const hashIndex = href.indexOf('#')
  88. if (hashIndex !== -1) {
  89. const hash = href.substring(hashIndex + 1)
  90. // 解析路径
  91. const pathMatch = hash.match(/^\/([^/?]+)/)
  92. if (pathMatch) {
  93. params.path = pathMatch[1]
  94. }
  95. // 解析路径参数 signup/19628/RHOP4WVa/B0
  96. const pathParams = hash.match(/^\/signup\/([^/]+)\/?([^/]+)?\/?([^/]+)?/)
  97. if (pathParams) {
  98. params.path = 'signup'
  99. params.p1 = pathParams[1]
  100. params.p2 = pathParams[2]
  101. params.p3 = pathParams[3]
  102. }
  103. // 解析 query 参数
  104. const queryIndex = hash.indexOf('?')
  105. if (queryIndex !== -1) {
  106. const queryStr = hash.substring(queryIndex + 1)
  107. const pairs = queryStr.split('&')
  108. pairs.forEach(pair => {
  109. const [key, value] = pair.split('=')
  110. if (key && value) {
  111. params[key] = decodeURIComponent(value)
  112. }
  113. })
  114. }
  115. }
  116. }
  117. // #endif
  118. // #ifndef H5
  119. // App 端:从 options 参数获取
  120. if (typeof options === 'object' && options !== null) {
  121. Object.assign(params, options)
  122. }
  123. // #endif
  124. return params
  125. }
  126. // 处理 signup 和 signin 路径(仅 H5 端)
  127. const handleSignupRoute = (options) => {
  128. // #ifdef H5
  129. // 解析 URL 参数(从浏览器 URL 解析)
  130. const query = parseUrlParams()
  131. console.log('解析到的参数:', query);
  132. // 处理 signin 路径(自动登录)
  133. if (query.path === 'signin') {
  134. // 跳转到登录页面 有就携带 token 参数
  135. const loginUrl = query?.sysLoginToken ? `/pages/login/index?sysLoginToken=${encodeURIComponent(query.sysLoginToken)}` : '/pages/login/index'
  136. console.log('跳转到登录页面:', loginUrl);
  137. uni.reLaunch({
  138. url: loginUrl,
  139. success: () => {
  140. console.log('跳转成功');
  141. },
  142. fail: (err) => {
  143. console.error('跳转失败:', err);
  144. }
  145. })
  146. return
  147. }
  148. // 判断是否是 signup 路径
  149. const isSignup = query.path === 'signup' || query.s || query.signup || query.activeTab === '2'
  150. if (!isSignup) return
  151. // 获取参数
  152. const id = query.id || query.agentId || query.p1 || ''
  153. const subId = query.subId || query.w || query.p2 || ''
  154. const code = query.code || query.oc || query.p3 || ''
  155. // 构建登录页面 URL
  156. let loginUrl = '/pages/login/index?activeTab=2'
  157. // 携带参数
  158. if (id) loginUrl += `&id=${id}`
  159. if (subId) loginUrl += `&subId=${subId}`
  160. if (code) loginUrl += `&code=${code}`
  161. console.log('跳转到:', loginUrl);
  162. // 跳转到注册页面
  163. uni.reLaunch({
  164. url: loginUrl,
  165. success: () => {
  166. console.log('跳转成功');
  167. },
  168. fail: (err) => {
  169. console.error('跳转失败:', err);
  170. }
  171. })
  172. // #endif
  173. }
  174. watch(locale, () => {
  175. // const currentPath = route.path;
  176. // menu.value.forEach((item, index) => {
  177. // if (item.children) {
  178. // const isActive = item.children.some(child => child.path.includes(currentPath));
  179. // menu.value[index].isOpenMenu = isActive;
  180. // if (isActive) {
  181. // nextTick(() => {
  182. // updateSubmenuHeight(index);
  183. // });
  184. // }
  185. // }
  186. // });
  187. }, { immediate: true })
  188. // 检测版本号更新
  189. const checkWgtUpdate = async () => {
  190. try {
  191. const currentVersion = await getCurrentVersion()
  192. const res = await uni.request({
  193. url: `https://ucard.44a5c8109e4.com/wgt/list.json?_t=${Date.now()}`,
  194. method: 'GET',
  195. timeout: 5000
  196. })
  197. const files = res.data?.files || []
  198. if (!files.length) return
  199. const latestFile = files[files.length - 1]
  200. const latestVersion = latestFile
  201. if (!latestFile) return
  202. const lastInstalled = uni.getStorageSync('lastWgtVersion')
  203. console.log(lastInstalled, 'lastInstalled');
  204. if (latestVersion === lastInstalled) return
  205. if (compareVersion(latestVersion, currentVersion) > 0) {
  206. downloadAndInstall(latestVersion)
  207. }
  208. } catch (e) {
  209. console.log('[wgt] update check failed', e)
  210. }
  211. }
  212. // 下载并安装
  213. const downloadAndInstall = (version) => {
  214. //TODO: 需要根据版本来确定url
  215. const url = `https://ucard.44a5c8109e4.com/wgt/__UNI__EFA7490.wgt`
  216. console.log(url, 'downloadurl');
  217. uni.downloadFile({
  218. url,
  219. success: (res) => {
  220. if (res.statusCode === 200) {
  221. const filePath = res.tempFilePath
  222. plus.runtime.install(
  223. filePath, {
  224. force: true
  225. },
  226. () => {
  227. uni.setStorageSync('lastWgtVersion', version)
  228. console.log('[wgt] install success:', version)
  229. uni.setStorageSync('wgtNeedRestart', true)
  230. },
  231. (err) => {
  232. console.error('[wgt] install failed:', err)
  233. }
  234. )
  235. } else {
  236. console.error('[wgt] download status error:', res.statusCode)
  237. }
  238. },
  239. fail: (err) => {
  240. console.error('[wgt] download failed:', err)
  241. }
  242. })
  243. }
  244. // 获取当前版本
  245. const getCurrentVersion = async () => {
  246. return new Promise((resolve, reject) => {
  247. // #ifdef APP-PLUS
  248. try {
  249. plus.runtime.getProperty(plus.runtime.appid, (info) => {
  250. resolve(info.version)
  251. }, (error) => {
  252. reject(error)
  253. })
  254. } catch (error) {
  255. reject(error)
  256. }
  257. // #endif
  258. // #ifndef APP-PLUS
  259. reject(new Error('Not in APP-PLUS environment'))
  260. // #endif
  261. })
  262. }
  263. // 对比版本号
  264. const compareVersion = (v1, v2) => {
  265. const s1 = v1.split('.').map(Number)
  266. const s2 = v2.split('.').map(Number)
  267. const len = Math.max(s1.length, s2.length)
  268. for (let i = 0; i < len; i++) {
  269. const n1 = s1[i] || 0
  270. const n2 = s2[i] || 0
  271. if (n1 > n2) return 1
  272. if (n1 < n2) return -1
  273. }
  274. return 0
  275. }
  276. onMounted(() => {
  277. const sysInfo = uni.getSystemInfoSync();
  278. globalStore.setBarHeight(sysInfo.statusBarHeight || 60);
  279. // ---------- 新增 H5 端专属初始化 ----------
  280. // 仅在 H5 端执行(通过环境判断)
  281. // #ifdef H5
  282. if (typeof window !== 'undefined') {
  283. const instance = getCurrentInstance()
  284. if (instance) {
  285. window.vm = instance.proxy
  286. }
  287. }
  288. // #endif
  289. });
  290. </script>
  291. <style>
  292. /*每个页面公共css */
  293. </style>
  294. <style lang="scss">
  295. /* 注意要写在第一行,同时给style标签加入lang="scss"属性 */
  296. @import "uview-plus/index.scss";
  297. @import "@/static/scss/global/global.scss";
  298. @import "@/static/scss/global/vu.css";
  299. @import "/static/scss/style.scss";
  300. @font-face {
  301. font-family: 'Google Sans';
  302. src: url('/static/Google_Sans/GoogleSans-VariableFont_GRAD,opsz,wght.ttf') format('truetype-variations');
  303. font-weight: 100 900;
  304. font-style: normal;
  305. font-display: swap;
  306. }
  307. /* 全局字体,不破坏 uni-icons 图标 */
  308. view,
  309. text,
  310. button,
  311. input,
  312. textarea,
  313. label {
  314. font-family: 'Google Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  315. font-weight: 400;
  316. }
  317. /* 专门保护 uni-icons 不被覆盖 */
  318. .uni-icon,
  319. [class*="uni-icons-"],
  320. .uni-icons {
  321. font-family: uniicons !important;
  322. }
  323. /* 让整个项目文字都能选中 */
  324. * {
  325. -webkit-user-select: text !important;
  326. user-select: text !important;
  327. }
  328. /* 修复滚动层无法选中 */
  329. view,
  330. text,
  331. div,
  332. span {
  333. -webkit-user-select: text !important;
  334. user-select: text !important;
  335. }
  336. /* 强制修复 uni-datetime-picker 重复渲染双日历 */
  337. // .uni-calendar+.uni-calendar {
  338. // display: none !important;
  339. // }
  340. :deep(.u-toolbar__wrapper__confirm) {
  341. font-size: 20px !important;
  342. }
  343. :deep(.u-toolbar__wrapper__cancel) {
  344. font-size: 20px !important;
  345. }
  346. .page {
  347. /* padding: 31px 31px 110px 31px; */
  348. box-sizing: border-box;
  349. /* background: var(--main-bg); */
  350. }
  351. html {
  352. --bs-bg-opacity: 1;
  353. background-color: rgba(var(--bs-body-bg-rgb), var(--bs-bg-opacity)) !important;
  354. font-size: 16px !important;
  355. }
  356. uni-page-body {
  357. height: 100%;
  358. }
  359. page {
  360. --bs-bg-opacity: 1;
  361. background-color: rgba(var(--bs-body-bg-rgb), var(--bs-bg-opacity)) !important;
  362. }
  363. </style>