useModal.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import $store from '@/sheep/store';
  2. import $helper from '@/sheep/helper';
  3. import dayjs from 'dayjs';
  4. import { ref,computed } from 'vue';
  5. import test from '@/sheep/helper/test.js';
  6. import AuthUtil from '@/sheep/api/member/auth';
  7. import sheep from '@/sheep';
  8. import SignInApi from '@/sheep/api/member/signin';
  9. let time = 30;
  10. let timer = null;
  11. // 登录
  12. async function onSign() {
  13. console.log("onSign")
  14. const {
  15. code,
  16. data
  17. } = await SignInApi.createSignInRecord();
  18. if (code === 0) {
  19. showSignUpModal(data)
  20. if (timer) {
  21. clearInterval(timer);
  22. }
  23. uni.setStorageSync('isRun', false);
  24. uni.setStorageSync('isSign', true);
  25. }
  26. }
  27. // 每日重置签到状态
  28. export function resetSignStatusIfNeeded() {
  29. const today = new Date().toISOString().slice(0, 10); // 获取当前日期,格式为 YYYY-MM-DD
  30. const lastCheckDate = uni.getStorageSync('lastCheckDate');
  31. if (today !== lastCheckDate) {
  32. // 如果跨天了,重置 isSign
  33. uni.setStorageSync('isSign', false);
  34. uni.setStorageSync('lastCheckDate', today); // 更新最后检查的日期
  35. }
  36. }
  37. // 自动签到
  38. export function autoSign(){
  39. resetSignStatusIfNeeded();
  40. const isLogin = computed(() => sheep.$store('user').isLogin);
  41. // 如果登录了才开始倒计时
  42. if (isLogin.value) {
  43. // 倒计时是否在进行
  44. uni.setStorageSync('isRun', true);
  45. if (uni.getStorageSync('isRun')&& !uni.getStorageSync('isSign')) {
  46. console.log('开始倒计时')
  47. // 开始一个新的定时器
  48. timer = setInterval(() => {
  49. time--;
  50. console.log("time", time);
  51. if (time <= 0) {
  52. clearInterval(timer);
  53. onSign();
  54. }
  55. }, 1000);
  56. }
  57. }
  58. }
  59. // 取消自动签到
  60. export function cancelAutoSign() {
  61. console.log("取消")
  62. if (timer) {
  63. clearInterval(timer);
  64. }
  65. time = 30;
  66. uni.setStorageSync('isRun', false);
  67. }
  68. // 打开签到弹框
  69. export function showSignUpModal(obj) {
  70. $store('modal').$patch((state) => {
  71. state.signUp = true;
  72. state.signUpInfo = obj;
  73. });
  74. }
  75. // 关闭签到弹框
  76. export function colseSignUpModal() {
  77. $store('modal').$patch((state) => {
  78. state.signUp = false;
  79. });
  80. }
  81. // 打开授权弹框
  82. export function showAuthModal(type = 'smsLogin',isActive = 'smsLogin') {
  83. const modal = $store('modal');
  84. if (modal.auth !== '') {
  85. closeAuthModal();
  86. setTimeout(() => {
  87. modal.$patch((state) => {
  88. state.auth = type;
  89. state.isActive = isActive;
  90. });
  91. }, 100);
  92. } else {
  93. modal.$patch((state) => {
  94. state.auth = type;
  95. state.isActive = isActive;
  96. });
  97. }
  98. }
  99. // 关闭授权弹框
  100. export function closeAuthModal() {
  101. $store('modal').$patch((state) => {
  102. state.auth = '';
  103. });
  104. }
  105. // 打开分享弹框
  106. export function showShareModal() {
  107. $store('modal').$patch((state) => {
  108. state.share = true;
  109. });
  110. }
  111. // 关闭分享弹框
  112. export function closeShareModal() {
  113. $store('modal').$patch((state) => {
  114. state.share = false;
  115. });
  116. }
  117. // 打开快捷菜单
  118. export function showMenuTools() {
  119. $store('modal').$patch((state) => {
  120. state.menu = true;
  121. });
  122. }
  123. // 关闭快捷菜单
  124. export function closeMenuTools() {
  125. $store('modal').$patch((state) => {
  126. state.menu = false;
  127. });
  128. }
  129. // 发送短信验证码 60秒
  130. export function getSmsCode(event, mobile) {
  131. const modalStore = $store('modal');
  132. const lastSendTimer = modalStore.lastTimer[event];
  133. if (typeof lastSendTimer === 'undefined') {
  134. $helper.toast('短信发送事件错误');
  135. return;
  136. }
  137. const duration = dayjs().unix() - lastSendTimer;
  138. const canSend = duration >= 60;
  139. if (!canSend) {
  140. $helper.toast('请稍后再试');
  141. return;
  142. }
  143. // 只有 mobile 非空时才校验。因为部分场景(修改密码),不需要输入手机
  144. if (mobile && !test.mobile(mobile)) {
  145. $helper.toast('手机号码格式不正确');
  146. return;
  147. }
  148. // 发送验证码 + 更新上次发送验证码时间
  149. let scene = -1;
  150. switch (event) {
  151. case 'resetPassword':
  152. scene = 4;
  153. break;
  154. case 'changePassword':
  155. scene = 3;
  156. break;
  157. case 'changeMobile':
  158. scene = 2;
  159. break;
  160. case 'smsLogin':
  161. scene = 1;
  162. break;
  163. }
  164. AuthUtil.sendSmsCode(mobile, scene).then((res) => {
  165. if (res.code === 0) {
  166. modalStore.$patch((state) => {
  167. state.lastTimer[event] = dayjs().unix();
  168. });
  169. }
  170. });
  171. }
  172. // 获取短信验证码倒计时 -- 60秒
  173. export function getSmsTimer(event, mobile = '') {
  174. const modalStore = $store('modal');
  175. const lastSendTimer = modalStore.lastTimer[event];
  176. if (typeof lastSendTimer === 'undefined') {
  177. $helper.toast('短信发送事件错误');
  178. return;
  179. }
  180. const duration = ref(dayjs().unix() - lastSendTimer - 60);
  181. const canSend = duration.value >= 0;
  182. if (canSend) {
  183. return '获取验证码';
  184. }
  185. if (!canSend) {
  186. setTimeout(() => {
  187. duration.value++;
  188. }, 1000);
  189. return -duration.value.toString() + ' 秒';
  190. }
  191. }
  192. // 记录广告弹框历史
  193. export function saveAdvHistory(adv) {
  194. const modal = $store('modal');
  195. modal.$patch((state) => {
  196. if (!state.advHistory.includes(adv.imgUrl)) {
  197. state.advHistory.push(adv.imgUrl);
  198. }
  199. });
  200. }