index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import Vue from "vue";
  2. import Router from "vue-router";
  3. import store from '../store'
  4. Vue.use(Router);
  5. const originalPush = Router.prototype.push;
  6. Router.prototype.push = function push(location) {
  7. return originalPush.call(this, location).catch((err) => err);
  8. };
  9. // 其他路由
  10. let constantRoutes = [
  11. {
  12. path: "/index",
  13. component: () => import("@/views/login/login"),
  14. meta: {
  15. title: "otherLogin",
  16. },
  17. },
  18. {
  19. path: "/",
  20. redirect: "/index"//redirect: "/studentAnalysisReport"
  21. },
  22. ];
  23. // 学生端分析报告
  24. let studentAnalysisReport = {
  25. path: "/studentAnalysisReport",
  26. redirect: "/studentAnalysisReport/list",
  27. component: () => import("@/views/analysisReport/index"),
  28. children: [
  29. {
  30. path: "/studentAnalysisReport/list", // 报告列表
  31. meta: {
  32. title: "分析报告",
  33. },
  34. component: () => import("@/views/analysisReport/studentPage/list/mainPage"),
  35. },
  36. {
  37. name: "reportDetails",
  38. path: "/studentAnalysisReport/reportDetails",
  39. component: () => import("@/views/analysisReport/studentPage/mainPage"),
  40. children: [
  41. {
  42. path: "scrolReport",
  43. component: () => import("@/views/analysisReport/studentPage/scrolReport/transcript"),
  44. meta: {
  45. title: "学生分析",
  46. },
  47. },
  48. {
  49. path: "personalWrongQuestions",
  50. component: () => import("@/views/analysisReport/wrongQuestion/index"),
  51. meta: {
  52. title: "个性化错题",
  53. },
  54. },
  55. ],
  56. },
  57. ],
  58. };
  59. const createRouter = () =>
  60. new Router({
  61. mode: "hash",
  62. routes: [
  63. ...constantRoutes,
  64. studentAnalysisReport
  65. ],
  66. scrollBehavior(to, from, savedPosition) {
  67. if (savedPosition) {
  68. return savedPosition;
  69. } else {
  70. return {
  71. x: 0,
  72. y: 0,
  73. };//路由切换时,滚动条回到顶部
  74. }
  75. },
  76. });
  77. // 添加统计分析脚本的函数
  78. function addAnalyticsScript() {
  79. // 移除已存在的统计脚本
  80. let existingScript = document.getElementById('_bxtj');
  81. // console.log('移除已存在的统计脚本...',existingScript);
  82. while (existingScript) {
  83. existingScript.remove();
  84. existingScript = document.getElementById('_bxtj');
  85. }
  86. // 移除已存在的统计脚本
  87. let existingScriptFrame = document.getElementById('_bxtjiframe');
  88. // console.log('移除已存在的统计脚本...',existingScriptFrame);
  89. while (existingScriptFrame) {
  90. existingScriptFrame.remove();
  91. existingScriptFrame = document.getElementById('_bxtjiframe');
  92. }
  93. console.log('添加统计分析脚本...');
  94. // 等待DOM加载完成
  95. setTimeout(() => {
  96. var _s = document.createElement('script');
  97. _s.setAttribute('type', 'text/javascript');
  98. _s.setAttribute('id', '_bxtj');
  99. _s.setAttribute('async', true);
  100. // 获取当前用户信息
  101. const userInfo = store.state.user.userInfo || {};
  102. const currentUser = userInfo.loginName || ''; // 当前用户名
  103. const subSiteName = userInfo.schoolName || '';
  104. const mySiteId = localStorage.getItem("user_schoolWebSiteId") || ''; // 你的站点ID
  105. console.log("当前学校站点id", userInfo, mySiteId);
  106. if (mySiteId != '') {
  107. // 构建URL(按照原始代码格式)
  108. let src = `https://bjedures.bjedu.cn/bjjw_logdb/bxlog.js?user=${currentUser}&id=${mySiteId}`;
  109. if (subSiteName) {
  110. src += `&subSiteName=${encodeURIComponent(subSiteName)}`;
  111. }
  112. _s.setAttribute('src', src);
  113. var body = document.getElementsByTagName('body');
  114. body[0].appendChild(_s);
  115. console.log('统计分析脚本已添加到body中。');
  116. }
  117. }, 1000);//1秒后添加
  118. }
  119. let router = createRouter();
  120. router.beforeEach((to, from, next) => {
  121. if (to.meta.title) {
  122. document.title = "慧教研-" + to.meta.title;
  123. }
  124. next();
  125. });
  126. // 路由后置守卫:添加统计脚本
  127. router.afterEach(() => {
  128. // 添加统计分析脚本
  129. addAnalyticsScript()
  130. });
  131. export default router;