useModal.js 6.1 KB

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