useModal.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import $store from '@/sheep/store';
  2. import $helper from '@/sheep/helper';
  3. import dayjs from 'dayjs';
  4. import { ref } from 'vue';
  5. import test from '@/sheep/helper/test.js';
  6. import AuthUtil from '@/sheep/api/member/auth';
  7. // 打开授权弹框
  8. export function showAuthModal(type = 'smsLogin',isActive = 'smsLogin') {
  9. const modal = $store('modal');
  10. if (modal.auth !== '') {
  11. closeAuthModal();
  12. setTimeout(() => {
  13. modal.$patch((state) => {
  14. state.auth = type;
  15. state.isActive = isActive;
  16. });
  17. }, 100);
  18. } else {
  19. modal.$patch((state) => {
  20. state.auth = type;
  21. state.isActive = isActive;
  22. });
  23. }
  24. }
  25. // 关闭授权弹框
  26. export function closeAuthModal() {
  27. $store('modal').$patch((state) => {
  28. state.auth = '';
  29. });
  30. }
  31. // 打开分享弹框
  32. export function showShareModal() {
  33. $store('modal').$patch((state) => {
  34. state.share = true;
  35. });
  36. }
  37. // 关闭分享弹框
  38. export function closeShareModal() {
  39. $store('modal').$patch((state) => {
  40. state.share = false;
  41. });
  42. }
  43. // 打开快捷菜单
  44. export function showMenuTools() {
  45. $store('modal').$patch((state) => {
  46. state.menu = true;
  47. });
  48. }
  49. // 关闭快捷菜单
  50. export function closeMenuTools() {
  51. $store('modal').$patch((state) => {
  52. state.menu = false;
  53. });
  54. }
  55. // 发送短信验证码 60秒
  56. export function getSmsCode(event, mobile) {
  57. const modalStore = $store('modal');
  58. const lastSendTimer = modalStore.lastTimer[event];
  59. if (typeof lastSendTimer === 'undefined') {
  60. $helper.toast('短信发送事件错误');
  61. return;
  62. }
  63. const duration = dayjs().unix() - lastSendTimer;
  64. const canSend = duration >= 60;
  65. if (!canSend) {
  66. $helper.toast('请稍后再试');
  67. return;
  68. }
  69. // 只有 mobile 非空时才校验。因为部分场景(修改密码),不需要输入手机
  70. if (mobile && !test.mobile(mobile)) {
  71. $helper.toast('手机号码格式不正确');
  72. return;
  73. }
  74. // 发送验证码 + 更新上次发送验证码时间
  75. let scene = -1;
  76. switch (event) {
  77. case 'resetPassword':
  78. scene = 4;
  79. break;
  80. case 'changePassword':
  81. scene = 3;
  82. break;
  83. case 'changeMobile':
  84. scene = 2;
  85. break;
  86. case 'smsLogin':
  87. scene = 1;
  88. break;
  89. }
  90. AuthUtil.sendSmsCode(mobile, scene).then((res) => {
  91. if (res.code === 0) {
  92. modalStore.$patch((state) => {
  93. state.lastTimer[event] = dayjs().unix();
  94. });
  95. }
  96. });
  97. }
  98. // 获取短信验证码倒计时 -- 60秒
  99. export function getSmsTimer(event, mobile = '') {
  100. const modalStore = $store('modal');
  101. const lastSendTimer = modalStore.lastTimer[event];
  102. if (typeof lastSendTimer === 'undefined') {
  103. $helper.toast('短信发送事件错误');
  104. return;
  105. }
  106. const duration = ref(dayjs().unix() - lastSendTimer - 60);
  107. const canSend = duration.value >= 0;
  108. if (canSend) {
  109. return '获取验证码';
  110. }
  111. if (!canSend) {
  112. setTimeout(() => {
  113. duration.value++;
  114. }, 1000);
  115. return -duration.value.toString() + ' 秒';
  116. }
  117. }
  118. // 记录广告弹框历史
  119. export function saveAdvHistory(adv) {
  120. const modal = $store('modal');
  121. modal.$patch((state) => {
  122. if (!state.advHistory.includes(adv.imgUrl)) {
  123. state.advHistory.push(adv.imgUrl);
  124. }
  125. });
  126. }