guard.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { Router } from "vue-router";
  2. import store from "@/store";
  3. import dayjs from "dayjs";
  4. import { Toast } from "vant";
  5. import { useLogin, useUpdateUserInfo } from "@/hooks";
  6. import { userInfo } from "@/api";
  7. const guard = (router: Router) => {
  8. router.beforeEach(async (to, from, next) => {
  9. //如果是marked页面,不需要任何判断,直接进入
  10. if (to.path === "/marked") {
  11. next();
  12. return;
  13. }
  14. //监听到登陆事件
  15. if (to.query.state === "LOGIN") {
  16. // const toast = Toast.loading({
  17. // duration: 0, // 持续展示 toast
  18. // forbidClick: true,
  19. // message: "登陆中",
  20. // });
  21. await useLogin(to.query);
  22. // Toast("登录成功")
  23. next("./")
  24. // try {
  25. // await useLogin(to.query);
  26. // toast.message = `登陆成功`;
  27. // setTimeout(() => {
  28. // Toast.clear();
  29. // }, 500);
  30. // } catch (error) {
  31. // toast.message = `登陆失败`;
  32. // setTimeout(() => {
  33. // Toast.clear();
  34. // location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx67ca1b8c9816ef28&redirect_uri=https://jpcj-h5.zzxcx.net/home/test&response_type=code&scope=snsapi_userinfo&state=LOGIN#wechat_redirect`);
  35. // }, 1000);
  36. // }
  37. }
  38. //更新用户的信息
  39. useUpdateUserInfo();
  40. //检测token状态,token失效直接跳转至登陆链接
  41. // try {
  42. // useUpdateUserInfo();
  43. // const authTest = await userInfo();
  44. // if (authTest.data.code !== 200) {
  45. // throw authTest.data.msg;
  46. // }
  47. // }
  48. // catch (error) {
  49. // //监听到登陆事件
  50. // if (to.query.state === "LOGIN") {
  51. // const toast = Toast.loading({
  52. // duration: 0, // 持续展示 toast
  53. // forbidClick: true,
  54. // message: "登陆中",
  55. // });
  56. // try {
  57. // await useLogin(to.query);
  58. // toast.message = `登陆成功`;
  59. // setTimeout(() => {
  60. // Toast.clear();
  61. // }, 500);
  62. // } catch (error) {
  63. // console.log(error)
  64. // toast.message = `登陆失败`;
  65. // setTimeout(() => {
  66. // Toast.clear();
  67. // location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx67ca1b8c9816ef28&redirect_uri=https://jpcj-h5.zzxcx.net/home/test&response_type=code&scope=snsapi_userinfo&state=LOGIN#wechat_redirect`);
  68. // }, 1000);
  69. // }
  70. // } else {
  71. // location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx67ca1b8c9816ef28&redirect_uri=${location.href}&response_type=code&scope=snsapi_userinfo&state=LOGIN#wechat_redirect`);
  72. // }
  73. // }
  74. console.log("会员")
  75. try {
  76. const userTime: number = dayjs(store.getters.getUserData.expireTime).valueOf();
  77. const currentTime: number = dayjs().valueOf();
  78. const vipPathSet = new Set(["/exercise", "/mockTest"]);
  79. if (vipPathSet.has(to.path)) {
  80. if (to.query.title === '顺序练习') {
  81. next();
  82. }
  83. else if (to.query.title === '分类练习') {
  84. next();
  85. }
  86. else if (to.query.title === '模拟考试') {
  87. next();
  88. }
  89. else if (userTime - currentTime > 0) {
  90. next()
  91. }
  92. else {
  93. Toast.fail("会员到期");
  94. next("/buyVip");
  95. }
  96. } else {
  97. next();
  98. }
  99. } catch (error) {
  100. }
  101. });
  102. };
  103. export default guard;