useModal.js 6.1 KB

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