useModal.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 = 'accountLogin') {
  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. if (!test.mobile(mobile)) {
  68. $helper.toast('手机号码格式不正确');
  69. return;
  70. }
  71. // 发送验证码 + 更新上次发送验证码时间
  72. $api.app.sendSms({ mobile, event }).then((res) => {
  73. if (res.error === 0) {
  74. modalStore.$patch((state) => {
  75. state.lastTimer[event] = dayjs().unix();
  76. });
  77. }
  78. });
  79. }
  80. // 获取短信验证码倒计时 -- 60秒
  81. export function getSmsTimer(event, mobile = '') {
  82. const modalStore = $store('modal');
  83. const lastSendTimer = modalStore.lastTimer[event];
  84. if (typeof lastSendTimer === 'undefined') {
  85. $helper.toast('短信发送事件错误');
  86. return;
  87. }
  88. const duration = ref(dayjs().unix() - lastSendTimer - 60);
  89. const canSend = duration.value >= 0;
  90. if (canSend) {
  91. return '获取验证码';
  92. }
  93. if (!canSend) {
  94. setTimeout(() => {
  95. duration.value++;
  96. }, 1000);
  97. return -duration.value.toString() + ' 秒';
  98. }
  99. }
  100. // 记录广告弹框历史
  101. export function saveAdvHistory(adv) {
  102. const modal = $store('modal');
  103. modal.$patch((state) => {
  104. if (!state.advHistory.includes(adv.src)) {
  105. state.advHistory.push(adv.src);
  106. }
  107. });
  108. }