main.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * 项目启动入口方法
  3. *
  4. * @Author: DCCloud
  5. * @Date: 2022-09-06 20:59:23
  6. */
  7. import '/@/assets/styles/tailwindcss.css'
  8. import { createApp } from 'vue';
  9. import VantUi from 'vant'
  10. import "vant/lib/index.css";
  11. import Antd, { message } from 'ant-design-vue';
  12. import * as antIcons from '@ant-design/icons-vue';
  13. import lodash from 'lodash';
  14. import JsonViewer from 'vue3-json-viewer';
  15. import 'vue3-json-viewer/dist/index.css';
  16. import App from './App.vue';
  17. import { smartSentry } from '/@/lib/smart-sentry';
  18. import { loginApi } from '/src/api/system/login-api';
  19. import constantsInfo from '/@/constants/index';
  20. import { privilegeDirective, loadingDirective } from '/@/directives/privilege';
  21. import i18n from '/@/i18n/index';
  22. import privilegePlugin from '/@/plugins/privilege-plugin';
  23. import smartEnumPlugin from '/@/plugins/smart-enums-plugin';
  24. import { buildRoutes, router } from '/@/router';
  25. import { store } from '/@/store';
  26. import { useUserStore } from '/@/store/modules/system/user';
  27. import 'ant-design-vue/dist/reset.css';
  28. import 'vue3-tabs-chrome/dist/vue3-tabs-chrome.css';
  29. import '/@/theme/index.less';
  30. import { localRead } from '/@/utils/local-util.js';
  31. import LocalStorageKeyConst from '/@/constants/local-storage-key-const.js';
  32. import '/@/assets/iconfont/iconfont.js';
  33. // 导入样式
  34. import "/@/views/flow/stFormDesign/styles/form-design.less";
  35. // 导出本地iconfont
  36. import "/@/views/flow/stFormDesign/static/icons/iconfont";
  37. // 引入表单设计器
  38. import { StFormDesign, StFormMobileDesign } from "/@/views/flow/stFormDesign/packages/index.js";
  39. import StFormBuild from "/@/views/flow/stFormDesign/packages/StFormBuild/index.js";
  40. // 引入vxe-table
  41. import VxeUI from 'vxe-pc-ui'
  42. import 'vxe-pc-ui/lib/style.css'
  43. import VxeUITable from 'vxe-table'
  44. import 'vxe-table/lib/style.css'
  45. import '/@/components/BsUi/Render/index.js'
  46. import BsUi from "/@/components/BsUi"
  47. import "/@/components/BsUi/styles/index.less"
  48. // 引入common-table
  49. import CommonTable from './components/common/common-table/index.vue';
  50. // 引入common-drawer
  51. import CommonDrawer from './components/common/common-drawer/index.vue';
  52. // 引入common-card
  53. import CommonCard from './components/common/common-card/index.vue';
  54. // 引入无分页common-table
  55. import CommonNoPageTable from './components/common/common-table-no-page/index.vue';
  56. /*
  57. * -------------------- ※ 着重 解释说明下main.js的初始化逻辑 begin ※ --------------------
  58. *
  59. * 1、在main.js里很多框架都是 直接调用初始化的vue方法,创建vue实例,然后挂载路由router、状态管理store等等,但是关于router这块是有问题的;
  60. * 2、因为现在大部分路由都是从后端接口返回的,如若直接初始化挂载路由,这时前端还没有从后端请求路由的数据,所以只能写到路由拦截器里,这样很绕很不清晰;
  61. * 正确的做法流程应该是:
  62. * 2.1)如果存在登录信息,则先ajax请求用户的所有路由,然后加载,再去创建vue实例和挂载路由
  63. * 2.2)如果不存在路由信息,则创建vue实例和挂载路由(此时的路由应该只有login页面,因为用户拥有哪些路由是登录之后才知道的)
  64. * 3、以上,在main.js里两个方法,一个是 获取登录信息getLoginInfo,另一个初始化vue: initVue,在最下的if操作里
  65. *
  66. * -------------------- ※ 着重 解释说明下main.js的初始化逻辑 end ※ --------------------
  67. */
  68. /**
  69. * 获取用户信息和用户权限对应的路由,构建动态路由
  70. */
  71. async function getLoginInfo() {
  72. try {
  73. let res = {}
  74. const url = window.location.href
  75. const key = "_loginKey_"
  76. const loginInfoRes = localStorage.getItem(key)
  77. if (url.indexOf("formWork") > 0 && loginInfoRes && loginInfoRes.length > 20) {
  78. res = JSON.parse(loginInfoRes)
  79. } else {
  80. res = await loginApi.getLoginInfo();
  81. localStorage.setItem(key, JSON.stringify(res))
  82. }
  83. //构建系统的路由
  84. let menuRouterList = res.data.menuList.filter((e) => e.path || e.frameUrl);
  85. buildRoutes(menuRouterList);
  86. initVue();
  87. //更新用户信息到pinia
  88. useUserStore().setUserLoginInfo(res.data);
  89. } catch (e) {
  90. message.error(e.data ? e.data.msg : e.message);
  91. smartSentry.captureError(e);
  92. initVue();
  93. }
  94. }
  95. function initVue() {
  96. let vueApp = createApp(App);
  97. let app = vueApp.use(router).use(store).use(i18n).use(Antd).use(smartEnumPlugin, constantsInfo).use(privilegePlugin).use(JsonViewer);
  98. app.use(VxeUI).use(VxeUITable);
  99. app.use(StFormDesign);
  100. app.use(StFormMobileDesign);
  101. app.use(StFormBuild);
  102. app.component('CommonTable', CommonTable);
  103. app.component('CommonDrawer', CommonDrawer);
  104. app.component('CommonCard', CommonCard);
  105. app.component('CommonNoPageTable', CommonNoPageTable);
  106. app.use(VantUi);
  107. //注入权限
  108. app.directive('privilege', {
  109. mounted(el, binding) {
  110. privilegeDirective(el, binding);
  111. },
  112. });
  113. //注入loading
  114. app.directive('loading', {
  115. updated(el, binding) {
  116. loadingDirective(el, binding);
  117. }
  118. });
  119. // 注册图标组件
  120. Object.keys(antIcons).forEach((key) => {
  121. app.component(key, antIcons[key]);
  122. });
  123. // 注册BsUi组件
  124. app.use(BsUi);
  125. //全局
  126. app.config.globalProperties.$antIcons = antIcons;
  127. app.config.globalProperties.$lodash = lodash;
  128. //挂载
  129. app.mount('#app');
  130. }
  131. //不需要获取用户信息、用户菜单、用户菜单动态路由,直接初始化vue即可
  132. let token = localRead(LocalStorageKeyConst.USER_TOKEN);
  133. if (!token) {
  134. initVue();
  135. } else {
  136. getLoginInfo();
  137. }