index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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: () =>
  44. import("@/views/analysisReport/studentPage/scrolReport/transcript"),
  45. meta: {
  46. title: "学生分析",
  47. },
  48. }
  49. ],
  50. },
  51. ],
  52. };
  53. const createRouter = () =>
  54. new Router({
  55. mode: "hash",
  56. routes: [
  57. ...constantRoutes,
  58. studentAnalysisReport
  59. ],
  60. scrollBehavior(to, from, savedPosition) {
  61. if (savedPosition) {
  62. return savedPosition;
  63. } else {
  64. return {
  65. x: 0,
  66. y: 0,
  67. };//路由切换时,滚动条回到顶部
  68. }
  69. },
  70. });
  71. // 添加统计分析脚本的函数
  72. function addAnalyticsScript() {
  73. // 移除已存在的统计脚本
  74. let existingScript = document.getElementById('_bxtj');
  75. // console.log('移除已存在的统计脚本...',existingScript);
  76. while (existingScript) {
  77. existingScript.remove();
  78. existingScript = document.getElementById('_bxtj');
  79. }
  80. // 移除已存在的统计脚本
  81. let existingScriptFrame= document.getElementById('_bxtjiframe');
  82. // console.log('移除已存在的统计脚本...',existingScriptFrame);
  83. while (existingScriptFrame) {
  84. existingScriptFrame.remove();
  85. existingScriptFrame = document.getElementById('_bxtjiframe');
  86. }
  87. console.log('添加统计分析脚本...');
  88. // 等待DOM加载完成
  89. setTimeout(() => {
  90. var _s = document.createElement('script');
  91. _s.setAttribute('type','text/javascript');
  92. _s.setAttribute('id','_bxtj');
  93. _s.setAttribute('async',true);
  94. // 获取当前用户信息
  95. const userInfo = store.state.user.userInfo || {};
  96. const currentUser = userInfo.loginName || ''; // 当前用户名
  97. const subSiteName = userInfo.schoolName || '';
  98. const mySiteId = localStorage.getItem("user_schoolWebSiteId") || ''; // 你的站点ID
  99. console.log("当前学校站点id",userInfo, mySiteId);
  100. if(mySiteId !='') {
  101. // 构建URL(按照原始代码格式)
  102. let src = `https://bjedures.bjedu.cn/bjjw_logdb/bxlog.js?user=${currentUser}&id=${mySiteId}`;
  103. if (subSiteName) {
  104. src += `&subSiteName=${encodeURIComponent(subSiteName)}`;
  105. }
  106. _s.setAttribute('src', src);
  107. var body = document.getElementsByTagName('body');
  108. body[0].appendChild(_s);
  109. console.log('统计分析脚本已添加到body中。');
  110. }
  111. }, 1000);//1秒后添加
  112. }
  113. let router = createRouter();
  114. router.beforeEach((to, from, next) => {
  115. if (to.meta.title) {
  116. document.title = "慧教研-" + to.meta.title;
  117. }
  118. next();
  119. });
  120. // 路由后置守卫:添加统计脚本
  121. router.afterEach(() => {
  122. // 添加统计分析脚本
  123. addAnalyticsScript()
  124. });
  125. export default router;