index.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { computed, ref } from "vue";
  2. import { useStore } from "vuex";
  3. import { useRoute, useRouter, LocationQuery } from "vue-router";
  4. import { Howl } from "howler";
  5. import * as API from "@/api";
  6. import store from "@/store";
  7. /** 用户通过微信code登陆 */
  8. export const useLogin = async (query: LocationQuery) => {
  9. //登陆
  10. const res = await API.login(query.code);
  11. store.commit("setToken", res.data.token);
  12. store.commit("setUserData", res.data.wxUserInfo);
  13. //更新用户信息
  14. useUpdateUserInfo();
  15. };
  16. /**更新用户信息 */
  17. export const useUpdateUserInfo = async () => {
  18. //获取用户信息
  19. const userDataRes = await API.userInfo();
  20. store.commit("setUserData", {
  21. ...store.getters.getUserData,
  22. ...userDataRes.data.data,
  23. });
  24. };
  25. /**
  26. * 获取用户会员到期时间
  27. */
  28. export const useExpireTime = () => {
  29. const store = useStore();
  30. const expireTime = computed(() => store.getters.getUserData.expireTime);
  31. return {
  32. expireTime,
  33. };
  34. };
  35. /**
  36. * 获取用户可提现金额
  37. */
  38. export const useProfitPrice = () => {
  39. const store = useStore();
  40. const profitPrice = computed(() => store.getters.getUserData.profitPrice);
  41. return {
  42. profitPrice,
  43. };
  44. };
  45. export const useSound = (videoSrc: string) => {
  46. const sound = new Howl({
  47. autoplay: true,
  48. src: videoSrc,
  49. });
  50. return sound.unload;
  51. };
  52. /**
  53. * 路由转发汇总class模式书写,需要注意this指向
  54. * @returns
  55. */
  56. export class RouterBus {
  57. router;
  58. route;
  59. constructor() {
  60. this.router = useRouter();
  61. this.route = useRoute();
  62. }
  63. /**
  64. * 会员购买页
  65. */
  66. goBuyVip = () => {
  67. this.router.push("/buyVip");
  68. };
  69. /**
  70. * 分成提现页
  71. */
  72. goCashOut = () => {
  73. this.router.push("/cashOut");
  74. };
  75. /**
  76. * 我的推广积分
  77. */
  78. goMyIntegral = () => {
  79. this.router.push("/myIntegral");
  80. };
  81. /**
  82. * 我的下级页
  83. */
  84. goMyBranch = () => {
  85. this.router.push("/myBranch");
  86. };
  87. /**
  88. * 模拟考试页
  89. */
  90. goMockTest = () => {
  91. this.router.push({ path: "/mockTest", query: this.route.query });
  92. };
  93. /** 错题重阅 */
  94. goWrongReview = (ids: number | number[]) => {
  95. this.router.push({
  96. path: "/wrongReview",
  97. query: { ...this.route.query, wrongIds: ids },
  98. });
  99. };
  100. }