App.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <template>
  2. <el-config-provider :size="getGlobalComponentSize" :locale="zhCn" :message="messageConfig" :button="buttonConfig">
  3. <router-view v-show="setLockScreen" />
  4. <LockScreen v-if="themeConfig.isLockScreen" />
  5. <SetTings ref="setTingsRef" v-show="setLockScreen" />
  6. <CloseFull v-if="!themeConfig.isLockScreen" />
  7. </el-config-provider>
  8. </template>
  9. <script lang="tsx" name="app" setup>
  10. import { computed, ref, onBeforeMount, onMounted, onBeforeUnmount, nextTick, watch, reactive, defineAsyncComponent } from 'vue';
  11. import { useRoute, useRouter } from 'vue-router';
  12. import zhCn from 'element-plus/es/locale/lang/zh-cn';
  13. import { storeToRefs } from 'pinia';
  14. import { useTagsViewRoutes } from '@/stores/tagsViewRoutes';
  15. import { useThemeConfig } from '@/stores/themeConfig';
  16. import other from '@/utils/other';
  17. import checkUpdate from '@/utils/checkUpdate';
  18. import mittBus from '@/utils/mitt';
  19. import { Session, Local, Cookie } from '@/utils/storage';
  20. import setIntroduction from '@/utils/setIconfont';
  21. import { loginPageInfo } from '@/api/login';
  22. import { downloadFileByStream, getImageUrl } from '@/utils/tools';
  23. import { useKeepALiveNames } from '@/stores/keepAliveNames';
  24. import { useFavicon, useDark } from '@vueuse/core';
  25. import { NextLoading } from '@/utils/loading';
  26. import { ElMessageBox, ElNotification } from 'element-plus';
  27. import { formatAxis, formatDate } from '@/utils/formatTime';
  28. import { initFrontEndControlRoutes } from '@/router/frontEnd';
  29. import { initBackEndControlRoutes } from '@/router/backEnd';
  30. import { VxeUI, VxeButton } from 'vxe-pc-ui';
  31. import MenuSvgIcon from '@/views/system/menu/components/Menu-svgIcon.vue';
  32. import {getTokenByUrl} from "@/api/home";
  33. // 式化日期,默认 yyyy-MM-dd HH:mm:ss
  34. VxeUI.formats.add('formatDate', {
  35. tableCellFormatMethod({ cellValue }, format?: string) {
  36. return formatDate(cellValue, format || 'YYYY-mm-dd HH:MM:SS');
  37. },
  38. });
  39. // 创建一个简单的工具栏-右侧工具渲染
  40. VxeUI.renderer.add('exportCurrent', {
  41. renderToolbarTool(renderOpts: any, params: any) {
  42. return (
  43. <VxeButton
  44. title="导出当前页"
  45. circle
  46. onClick={() => {
  47. const { $table } = params;
  48. const columns = $table.getColumns();
  49. const tableParams = $table.getParams();
  50. if (!tableParams) {
  51. VxeUI.modal.message({
  52. content: `参数错误请检查`,
  53. status: 'warning',
  54. });
  55. return;
  56. }
  57. const exportNewColumns = columns
  58. .map((item: any) => {
  59. return {
  60. prop: item.field,
  61. name: item.title,
  62. type: item.type,
  63. };
  64. })
  65. .filter((item: any) => item.prop && item.name && !['seq', 'checkbox'].includes(item.type));
  66. ElMessageBox.confirm(`工单已留痕,禁止泄露工单信息,否则将依法依规追究责任。您确定导出当前页数据?`, '提示', {
  67. confirmButtonText: '确认',
  68. cancelButtonText: '取消',
  69. type: 'warning',
  70. draggable: true,
  71. cancelButtonClass: 'default-button',
  72. autofocus: false,
  73. })
  74. .then(() => {
  75. VxeUI.modal.message({
  76. content: `导出中,请稍等`,
  77. status: 'loading',
  78. id: 'exportCurrent',
  79. duration: -1,
  80. });
  81. const request = {
  82. queryDto: tableParams.exportParams,
  83. columnInfos: exportNewColumns,
  84. isExportAll: false,
  85. };
  86. tableParams.exportMethod &&
  87. tableParams
  88. .exportMethod(request)
  89. .then((res: any) => {
  90. downloadFileByStream(res);
  91. VxeUI.modal.close('exportCurrent');
  92. VxeUI.modal.message({
  93. content: `导出成功`,
  94. status: 'success',
  95. });
  96. })
  97. .catch((e: any) => {
  98. console.log(`导出失败:${e}`);
  99. VxeUI.modal.close('exportCurrent');
  100. VxeUI.modal.message({
  101. content: `导出失败`,
  102. status: 'error',
  103. });
  104. });
  105. })
  106. .catch(() => {});
  107. }}
  108. >
  109. <MenuSvgIcon name="iconfont icon-daochu" />
  110. </VxeButton>
  111. );
  112. },
  113. });
  114. VxeUI.renderer.add('exportAll', {
  115. renderToolbarTool(renderOpts: any, params: any) {
  116. return (
  117. <VxeButton
  118. className="mr10"
  119. title="导出全部"
  120. circle
  121. onClick={() => {
  122. const { $table } = params;
  123. const columns = $table.getColumns();
  124. const tableParams = $table.getParams();
  125. if (!tableParams) {
  126. VxeUI.modal.message({
  127. content: `参数错误请检查`,
  128. status: 'warning',
  129. });
  130. return;
  131. }
  132. let request = {};
  133. if (tableParams.isSpecialExport) {
  134. // 特殊导出请求参数
  135. const exportNewColumns = columns.filter((item: any) => item.field && item.title && !['seq', 'checkbox'].includes(item.type));
  136. const addColumnName = exportNewColumns.map((item: any) => item.title);
  137. request = {
  138. ...tableParams.exportParams,
  139. addColumnName,
  140. };
  141. } else {
  142. const exportNewColumns = columns
  143. .map((item: any) => {
  144. return {
  145. prop: item.field,
  146. name: item.title,
  147. type: item.type,
  148. };
  149. })
  150. .filter((item: any) => item.prop && item.name && !['seq', 'checkbox'].includes(item.type));
  151. request = {
  152. queryDto: tableParams.exportParams,
  153. columnInfos: exportNewColumns,
  154. isExportAll: true,
  155. };
  156. }
  157. ElMessageBox.confirm(`工单已留痕,禁止泄露工单信息,否则将依法依规追究责任。您确定要导出全部数据?`, '提示', {
  158. confirmButtonText: '确认',
  159. cancelButtonText: '取消',
  160. type: 'warning',
  161. draggable: true,
  162. cancelButtonClass: 'default-button',
  163. autofocus: false,
  164. })
  165. .then(() => {
  166. VxeUI.modal.message({
  167. content: `导出中,请稍等`,
  168. status: 'loading',
  169. id: 'exportAll',
  170. duration: -1,
  171. });
  172. tableParams.exportMethod &&
  173. tableParams
  174. .exportMethod(request)
  175. .then((res: any) => {
  176. downloadFileByStream(res);
  177. VxeUI.modal.close('exportAll');
  178. VxeUI.modal.message({
  179. content: `导出成功`,
  180. status: 'success',
  181. });
  182. })
  183. .catch((e: any) => {
  184. console.log(`导出失败:${e}`);
  185. VxeUI.modal.close('exportAll');
  186. VxeUI.modal.message({
  187. content: `导出失败`,
  188. status: 'error',
  189. });
  190. });
  191. })
  192. .catch(() => {});
  193. }}
  194. >
  195. <MenuSvgIcon name="iconfont icon-export" />
  196. </VxeButton>
  197. );
  198. },
  199. });
  200. // 引入组件
  201. const LockScreen = defineAsyncComponent(() => import('@/layout/lockScreen/index.vue'));
  202. const SetTings = defineAsyncComponent(() => import('@/layout/navBars/breadcrumb/setings.vue'));
  203. const CloseFull = defineAsyncComponent(() => import('@/layout/navBars/breadcrumb/closeFull.vue'));
  204. const route = useRoute();
  205. const stores = useTagsViewRoutes();
  206. const storesThemeConfig = useThemeConfig();
  207. const { themeConfig } = storeToRefs(storesThemeConfig);
  208. const storesKeepALiveNames = useKeepALiveNames();
  209. const storesTagsViewRoutes = useTagsViewRoutes();
  210. const { tagsViewRoutes } = storeToRefs(storesTagsViewRoutes);
  211. // 设置锁屏时组件显示隐藏
  212. const setLockScreen = computed(() => {
  213. // 防止锁屏后,刷新出现不相关界面
  214. // https://gitee.com/lyt-top/vue-next-admin/issues/I6AF8P
  215. return !themeConfig.value.isLockScreen;
  216. });
  217. // 可同时显示的消息最大数量
  218. const messageConfig = reactive<any>({
  219. max: 3,
  220. });
  221. // 自动在两个中文字符之间插入空格
  222. const buttonConfig = reactive<any>({
  223. autoInsertSpace: false,
  224. });
  225. // 获取全局组件大小
  226. const getGlobalComponentSize = computed(() => {
  227. return other.globalComponentSize();
  228. });
  229. // 布局配置弹窗打开
  230. const setTingsRef = ref<RefType>();
  231. const openSetTingsDrawer = () => {
  232. setTingsRef.value.openDrawer();
  233. };
  234. // 设置初始化,防止刷新时恢复默认
  235. onBeforeMount(async () => {
  236. try {
  237. // 获取登录页的背景图和系统名称等
  238. const { result } = await loginPageInfo();
  239. const globalTitle = result.sysName ?? ''; // 标题名称
  240. const loginImage = result.loginImage ? result.loginImage : `${getImageUrl('default/login_bg.png')}`; // 登录页背景图
  241. const isLoginMessageCode = result.isLoginMessageCode; // 是否开启短信验证码
  242. const appScope = result.appScope ?? 'YiBin'; // 应用范围
  243. const cityName = result.cityName ?? ''; // 城市名称
  244. const cityCode = result.cityCode ?? ''; // 城市编码
  245. const cityAbbr = result.cityAbbr ?? ''; // 城市简称
  246. const operate = result.operate ?? ''; // 管理运营 页脚
  247. const techSupport = result.techSupport ?? ''; // 技术支持 页脚
  248. const recordNumber = result.recordNumber ?? ''; // 备案号 页脚
  249. const faviconImage = result.faviconImage ?? ``; // favicon 浏览器标签图标
  250. const menuLogoImage = result.menuLogoImage ?? ``; // 菜单logo
  251. const menuLogoImageMini = result.menuLogoImageMini ?? ``; // 菜单logo-mini
  252. const changPwdImage = result.changPwdImage ?? ``; // 修改密码图片
  253. storesThemeConfig.setThemeConfig(
  254. Object.assign(themeConfig.value, {
  255. globalTitle,
  256. loginImage,
  257. isLoginMessageCode,
  258. appScope,
  259. cityName,
  260. cityCode,
  261. cityAbbr,
  262. operate,
  263. techSupport,
  264. recordNumber,
  265. faviconImage,
  266. menuLogoImage,
  267. menuLogoImageMini,
  268. changPwdImage,
  269. })
  270. );
  271. } catch (e) {
  272. console.log(e);
  273. }
  274. // 设置批量第三方 icon 图标
  275. setIntroduction.cssCdn();
  276. // 设置批量第三方 js
  277. setIntroduction.jsCdn();
  278. });
  279. const router = useRouter();
  280. // 登录成功后的跳转
  281. const signInSuccess = (isNoPower: boolean | undefined) => {
  282. window.history.replaceState({}, document.title, window.location.pathname);
  283. if (isNoPower) {
  284. ElNotification({
  285. title: '提示',
  286. message: '抱歉,您没有登录权限,请联系管理员',
  287. type: 'warning',
  288. });
  289. Session.clear();
  290. Cookie.clear();
  291. Local.clear();
  292. setTimeout(() => {
  293. window.location.reload();
  294. }, 2000);
  295. } else {
  296. router.push('/');
  297. // 设置登录成功后的时间问候语
  298. const signInText = '欢迎回来!';
  299. ElNotification({
  300. type: 'success',
  301. title: `${formatAxis(new Date())}`,
  302. message: `${signInText}`,
  303. position: 'bottom-right',
  304. });
  305. NextLoading.start();
  306. }
  307. };
  308. // 页面加载时
  309. onMounted(() => {
  310. nextTick(async () => {
  311. try {
  312. // 获取缓存中的布局配置
  313. if (Local.get('themeConfig')) {
  314. storesThemeConfig.setThemeConfig(Local.get('themeConfig'));
  315. document.documentElement.style.cssText = Local.get('themeConfigStyle');
  316. }
  317. // 开发环境不提示更新
  318. if (import.meta.env.VITE_MODE_NAME != 'development') {
  319. // 监听是否更新 半个小时检查一次
  320. await checkUpdate(1800);
  321. }
  322. // 监听布局配置弹窗点击打开
  323. mittBus.on('openSetTingsDrawer', () => {
  324. openSetTingsDrawer();
  325. });
  326. // 获取缓存中的全屏配置
  327. if (Session.get('isTagsViewCurrenFull')) {
  328. stores.setCurrenFullscreen(Session.get('isTagsViewCurrenFull'));
  329. }
  330. // 清除某个页面的缓存
  331. mittBus.on('clearCache', (view: any) => {
  332. clearCacheTagsView(view);
  333. });
  334. // 解决火狐拖动打开新窗口
  335. document.body.ondrop = (event) => {
  336. event.preventDefault();
  337. event.stopPropagation();
  338. };
  339. // 动态修改icon
  340. const icon = useFavicon();
  341. icon.value = themeConfig.value.faviconImage; // 更改当前左上角角标
  342. const isDark = useDark();
  343. isDark.value = themeConfig.value.isIsDark; // 更改暗黑模式
  344. /*mittBus.on('*', (index, data) => {
  345. console.log(index, data);
  346. });*/
  347. /* // 通过url判断单点登录
  348. // 获取url特殊参数 实现登录跳转
  349. // 单点登录进来的参数
  350. const urlParams = new URLSearchParams(window.location.search);
  351. const source = urlParams.get('source');
  352. const username = urlParams.get('username');
  353. // 判断是否是单点登录
  354. const isSingleSignOn = source === 'oldHotline' && username;
  355. if (isSingleSignOn) {
  356. Cookie.remove('token');
  357. getTokenByUrl({ userName: username })
  358. .then(async (res: any) => {
  359. //登录
  360. // 存储 token 到浏览器缓存
  361. Cookie.set('token', res.result);
  362. window.history.replaceState({}, document.title, window.location.pathname);
  363. if (!themeConfig.value.isRequestRoutes) {
  364. // 前端控制路由,2、请注意执行顺序
  365. const isNoPower = await initFrontEndControlRoutes();
  366. signInSuccess(isNoPower);
  367. } else {
  368. // 模拟后端控制路由,isRequestRoutes 为 true,则开启后端控制路由
  369. // 添加完动态路由,再进行 router 跳转,否则可能报错 No match found for location with path "/"
  370. const isNoPower = await initBackEndControlRoutes();
  371. // 执行完 initBackEndControlRoutes,再执行 signInSuccess
  372. signInSuccess(isNoPower);
  373. }
  374. })
  375. .catch((err) => {
  376. const message = err.response.data.message;
  377. ElMessageBox.alert(message, '登录失败', {
  378. confirmButtonText: '确定',
  379. type: 'error',
  380. showClose: false,
  381. draggable: true,
  382. callback: () => {
  383. window.history.replaceState({}, document.title, window.location.pathname);
  384. // 清除缓存/token等
  385. Local.clear();
  386. Session.clear();
  387. Cookie.clear();
  388. // 使用 reload 时,不需要调用 resetRoute() 重置路由
  389. window.location.reload();
  390. },
  391. });
  392. });
  393. }*/
  394. // 通过url判断单点登录
  395. // 获取url特殊参数 实现登录跳转
  396. // 单点登录进来的参数
  397. const urlParams = new URLSearchParams(window.location.search);
  398. const source = urlParams.get('token');
  399. // 判断是否是单点登录
  400. if (source) {
  401. Cookie.remove('token');
  402. //登录
  403. // 存储 token 到浏览器缓存
  404. Cookie.set('token', source);
  405. if (!themeConfig.value.isRequestRoutes) {
  406. // 前端控制路由,2、请注意执行顺序
  407. const isNoPower = await initFrontEndControlRoutes();
  408. signInSuccess(isNoPower);
  409. } else {
  410. // 模拟后端控制路由,isRequestRoutes 为 true,则开启后端控制路由
  411. // 添加完动态路由,再进行 router 跳转,否则可能报错 No match found for location with path "/"
  412. const isNoPower = await initBackEndControlRoutes();
  413. // 执行完 initBackEndControlRoutes,再执行 signInSuccess
  414. signInSuccess(isNoPower);
  415. }
  416. }
  417. } catch (error) {
  418. console.log(error);
  419. }
  420. });
  421. });
  422. // 清除缓存 name
  423. const clearCacheTagsView = async (routeName: string) => {
  424. let item: EmptyObjectType | null | undefined;
  425. tagsViewRoutes.value.forEach((v: any) => {
  426. if (v.name === routeName) {
  427. item = v;
  428. }
  429. });
  430. if (!item) return false;
  431. await storesKeepALiveNames.delCachedView(item);
  432. if (item.meta?.isKeepAlive) await storesKeepALiveNames.addCachedView(item);
  433. };
  434. // 页面销毁时,关闭监听布局配置/i18n监听
  435. onBeforeUnmount(() => {
  436. mittBus.off('openSetTingsDrawer', () => {});
  437. mittBus.off('clearCache', () => {});
  438. });
  439. // 监听路由的变化,设置网站标题
  440. watch(
  441. () => route.path,
  442. () => {
  443. other.useTitle();
  444. },
  445. {
  446. deep: true,
  447. }
  448. );
  449. </script>