useModal.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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(uni.getLocale() == 'en' ? 'SMS sending event error' : '短信发送事件错误');
  59. return;
  60. }
  61. const duration = dayjs().unix() - lastSendTimer;
  62. const canSend = duration >= 60;
  63. if (!canSend) {
  64. $helper.toast(uni.getLocale() == 'en' ? 'Please try again later' : '请稍后再试');
  65. return;
  66. }
  67. // if (!test.mobile(mobile)) {
  68. // $helper.toast(uni.getLocale() == 'en' ? 'Mobile phone number format is incorrect' : '手机号码格式不正确');
  69. // return;
  70. // }
  71. const changeEvent = {
  72. changeMobile: 'changemobile',
  73. smsLogin: 'mobilelogin',
  74. resetPassword: 'resetpwd',
  75. smsRegister: 'register',
  76. }
  77. // 发送验证码 + 更新上次发送验证码时间
  78. if (changeEvent.hasOwnProperty(event)) {
  79. let eventData = changeEvent[event]
  80. $api.app.sendSms({
  81. mobile: mobile,
  82. event: eventData
  83. }).then((res) => {
  84. if (res.code === 1) {
  85. modalStore.$patch((state) => {
  86. state.lastTimer[event] = dayjs().unix();
  87. });
  88. }
  89. });
  90. } else {
  91. $helper.toast(uni.getLocale() == 'en' ? 'SMS sending event error' : '短信发送事件错误');
  92. }
  93. }
  94. // 获取短信验证码倒计时 -- 60秒
  95. export function getSmsTimer(event, mobile = '') {
  96. const modalStore = $store('modal');
  97. const lastSendTimer = modalStore.lastTimer[event];
  98. if (typeof lastSendTimer === 'undefined') {
  99. $helper.toast(uni.getLocale() == 'en' ? 'SMS sending event error' : '短信发送事件错误');
  100. return;
  101. }
  102. const duration = ref(dayjs().unix() - lastSendTimer - 60);
  103. const canSend = duration.value >= 0;
  104. if (canSend) {
  105. return uni.getLocale() == 'en' ? 'get code' : '获取验证码';
  106. }
  107. if (!canSend) {
  108. setTimeout(() => {
  109. duration.value++;
  110. }, 1000);
  111. return -duration.value.toString() + ' '+uni.getLocale() == 'en' ? 'second' : '秒';
  112. }
  113. }
  114. // 记录广告弹框历史
  115. export function saveAdvHistory(adv) {
  116. const modal = $store('modal');
  117. modal.$patch((state) => {
  118. if (!state.advHistory.includes(adv.src)) {
  119. state.advHistory.push(adv.src);
  120. }
  121. });
  122. }