guard.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.message = `登陆成功`;
  23. setTimeout(() => {
  24. Toast.clear();
  25. })
  26. next("./")
  27. // try {
  28. // await useLogin(to.query);
  29. // toast.message = `登陆成功`;
  30. // setTimeout(() => {
  31. // Toast.clear();
  32. // }, 500);
  33. // } catch (error) {
  34. // toast.message = `登陆失败`;
  35. // setTimeout(() => {
  36. // Toast.clear();
  37. // 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`);
  38. // }, 1000);
  39. // }
  40. }
  41. //更新用户的信息
  42. useUpdateUserInfo();
  43. //检测token状态,token失效直接跳转至登陆链接
  44. // try {
  45. // useUpdateUserInfo();
  46. // const authTest = await userInfo();
  47. // if (authTest.data.code !== 200) {
  48. // throw authTest.data.msg;
  49. // }
  50. // }
  51. // catch (error) {
  52. // //监听到登陆事件
  53. // if (to.query.state === "LOGIN") {
  54. // const toast = Toast.loading({
  55. // duration: 0, // 持续展示 toast
  56. // forbidClick: true,
  57. // message: "登陆中",
  58. // });
  59. // try {
  60. // await useLogin(to.query);
  61. // toast.message = `登陆成功`;
  62. // setTimeout(() => {
  63. // Toast.clear();
  64. // }, 500);
  65. // } catch (error) {
  66. // console.log(error)
  67. // toast.message = `登陆失败`;
  68. // setTimeout(() => {
  69. // Toast.clear();
  70. // 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`);
  71. // }, 1000);
  72. // }
  73. // } else {
  74. // 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`);
  75. // }
  76. // }
  77. try {
  78. const userTime = dayjs(store.getters.getUserData.expireTime).valueOf();
  79. const currentTime = dayjs().valueOf();
  80. const vipPathSet = new Set(["/exercise", "/mockTest"]);
  81. if (vipPathSet.has(to.path)) {
  82. if (userTime - currentTime > 0) {
  83. next();
  84. } else {
  85. Toast.fail("会员到期");
  86. next("/buyVip");
  87. }
  88. } else {
  89. next();
  90. }
  91. } catch (error) {}
  92. });
  93. };
  94. export default guard;