useModal.js 6.2 KB

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