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. const modal = $store('modal');
  103. modal.$patch((state) => {
  104. state.subscribe = true;
  105. });
  106. }
  107. // 关闭关注公众号弹窗
  108. export function closeSubscribeModal() {
  109. const modal = $store('modal');
  110. modal.$patch((state) => {
  111. state.subscribe = false;
  112. });
  113. }
  114. // 打开授权弹框
  115. export function showAuthModal(type = 'accountLogin', isActive = 'accountLogin') {
  116. const modal = $store('modal');
  117. if (modal.auth !== '') {
  118. closeAuthModal();
  119. setTimeout(() => {
  120. modal.$patch((state) => {
  121. state.auth = type;
  122. state.isActive = isActive;
  123. });
  124. }, 100);
  125. } else {
  126. modal.$patch((state) => {
  127. state.auth = type;
  128. state.isActive = isActive;
  129. });
  130. }
  131. }
  132. // 关闭授权弹框
  133. export function closeAuthModal() {
  134. $store('modal').$patch((state) => {
  135. state.auth = '';
  136. });
  137. }
  138. // 打开分享弹框
  139. export function showShareModal(spuId = 0) {
  140. $store('modal').$patch((state) => {
  141. state.share = true;
  142. state.shareInfo.spuId = spuId;
  143. });
  144. }
  145. // 关闭分享弹框
  146. export function closeShareModal() {
  147. $store('modal').$patch((state) => {
  148. state.share = false;
  149. });
  150. }
  151. // 打开快捷菜单
  152. export function showMenuTools() {
  153. $store('modal').$patch((state) => {
  154. state.menu = true;
  155. });
  156. }
  157. // 关闭快捷菜单
  158. export function closeMenuTools() {
  159. $store('modal').$patch((state) => {
  160. state.menu = false;
  161. });
  162. }
  163. // 发送短信验证码 60秒
  164. export function getSmsCode(event, mobile) {
  165. const modalStore = $store('modal');
  166. const lastSendTimer = modalStore.lastTimer[event];
  167. if (typeof lastSendTimer === 'undefined') {
  168. $helper.toast('短信发送事件错误');
  169. return;
  170. }
  171. const duration = dayjs().unix() - lastSendTimer;
  172. const canSend = duration >= 60;
  173. if (!canSend) {
  174. $helper.toast('请稍后再试');
  175. return;
  176. }
  177. // 只有 mobile 非空时才校验。因为部分场景(修改密码),不需要输入手机
  178. if (mobile && !test.mobile(mobile)) {
  179. $helper.toast('手机号码格式不正确');
  180. return;
  181. }
  182. // 发送验证码 + 更新上次发送验证码时间
  183. let scene = -1;
  184. switch (event) {
  185. case 'resetPassword':
  186. scene = 4;
  187. break;
  188. case 'changePassword':
  189. scene = 3;
  190. break;
  191. case 'changeMobileOld':
  192. scene = 2;
  193. break;
  194. case 'changeMobileNew':
  195. scene = 5;
  196. break;
  197. case 'smsLogin':
  198. scene = 1;
  199. break;
  200. case 'consumptionTransfers':
  201. scene = 11;
  202. break;
  203. case 'zeroBuy':
  204. scene = 12;
  205. break;
  206. }
  207. AuthUtil.sendSmsCode(mobile, scene).then((res) => {
  208. if (res.code === 0) {
  209. modalStore.$patch((state) => {
  210. state.lastTimer[event] = dayjs().unix();
  211. });
  212. }
  213. });
  214. }
  215. // 获取短信验证码倒计时 -- 60秒
  216. export function getSmsTimer(event, mobile = '') {
  217. const modalStore = $store('modal');
  218. const lastSendTimer = modalStore.lastTimer[event];
  219. if (typeof lastSendTimer === 'undefined') {
  220. $helper.toast('短信发送事件错误');
  221. return;
  222. }
  223. const duration = ref(dayjs().unix() - lastSendTimer - 60);
  224. const canSend = duration.value >= 0;
  225. if (canSend) {
  226. return '获取验证码';
  227. }
  228. if (!canSend) {
  229. setTimeout(() => {
  230. duration.value++;
  231. }, 1000);
  232. return -duration.value.toString() + ' 秒';
  233. }
  234. }
  235. // 记录广告弹框历史
  236. export function saveAdvHistory(adv) {
  237. const modal = $store('modal');
  238. modal.$patch((state) => {
  239. if (!state.advHistory.includes(adv.imgUrl)) {
  240. state.advHistory.push(adv.imgUrl);
  241. }
  242. });
  243. }