useModal.js 3.0 KB

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