main.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { createApp, computed } from 'vue'
  2. import './style.css'
  3. import './global.scss'
  4. import App from './App.vue'
  5. import { getAssetsFile } from '@/utils/imgResolve'
  6. import router from './router'
  7. import store, { useStore } from './store/index'
  8. import ElementPlus from 'element-plus'
  9. import 'element-plus/dist/index.css'
  10. import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
  11. import 'vant/lib/index.css'
  12. import '@vant/touch-emulator'
  13. import * as ElementPlusIconsVue from '@element-plus/icons-vue'
  14. const setHtmlFontSize = () => {
  15. // store.commit('screenChange');
  16. const htmlDom = document.getElementsByTagName('html')[0]
  17. let htmlWidth = document.documentElement.clientWidth || document.body.clientWidth
  18. console.log(htmlWidth)
  19. if (htmlWidth >= 750) {
  20. htmlWidth = 750
  21. }
  22. if (htmlWidth <= 450) {
  23. htmlWidth = 450 // 12px
  24. }
  25. htmlDom.style.fontSize = `${htmlWidth / 37.5}px`
  26. }
  27. window.onresize = setHtmlFontSize
  28. setHtmlFontSize()
  29. const whteList = ['/home', '/login', '/my_game', '/cate', '/']
  30. const platform = computed(() => {
  31. const u = navigator.userAgent
  32. return {
  33. mobile: !!u.match(/AppleWebKit.*Mobile.*/), // 是否为移动终端
  34. ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), // ios终端
  35. android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1, // android终端
  36. iPhone: u.indexOf('iPhone') > -1, // 是否为iPhone或者QQHD浏览器
  37. iPad: u.indexOf('iPad') > -1, // 是否iPad
  38. webApp: u.indexOf('Safari') === -1, // 是否web应该程序,没有头部与底部
  39. weixin: u.indexOf('MicroMessenger') > -1 // 是否微信
  40. }
  41. })
  42. const app = createApp(App)
  43. app.provide('img', getAssetsFile)
  44. app.use(store)
  45. app.use(router)
  46. for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  47. app.component(key, component)
  48. }
  49. app.use(ElementPlus, {
  50. locale: zhCn,
  51. size: 'large', zIndex: 3000
  52. })
  53. app.mount('#app')
  54. const { user } = useStore()
  55. router.beforeEach((to, _from, next) => {
  56. // ...
  57. // 返回 false 以取消导航
  58. // return false
  59. console.log(platform.value.mobile)
  60. console.log('store', user.loginFormVisible)
  61. document.title = to.meta.title as string
  62. const token = sessionStorage.getItem('token')
  63. if (whteList.includes(to.path)) {
  64. next()
  65. } else {
  66. if (!token) {
  67. if (platform.value.mobile) {
  68. next('/login')
  69. } else {
  70. user.loginFormVisible = true
  71. next(false)
  72. }
  73. } else {
  74. next()
  75. }
  76. }
  77. })