useModal.js 5.0 KB

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