useModal.js 5.0 KB

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