useModal.js 7.3 KB

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