Files
zgty-mas-m/sheep/hooks/useModal.js
2025-09-30 00:08:23 +08:00

167 lines
3.9 KiB
JavaScript

import $store from '@/sheep/store';
import $helper from '@/sheep/helper';
import dayjs from 'dayjs';
import { ref } from 'vue';
import test from '@/sheep/helper/test.js';
import AuthUtil from '@/sheep/api/system/auth';
import sheep from '@/sheep';
// 打开授权弹框
export function showAuthModal(type = 'accountLogin') {
sheep.$router.go('/pages/login/index', { authType: type });
}
// 关闭授权弹框
export function closeAuthModal() {
sheep.$router.back();
}
// 打开分享弹框
export function showShareModal() {
$store('modal').$patch((state) => {
state.share = true;
});
}
// 关闭分享弹框
export function closeShareModal() {
$store('modal').$patch((state) => {
state.share = false;
});
}
// 打开快捷菜单
export function showMenuTools() {
$store('modal').$patch((state) => {
state.menu = true;
});
}
// 关闭快捷菜单
export function closeMenuTools() {
$store('modal').$patch((state) => {
state.menu = false;
});
}
// 发送短信验证码 60秒
export function getSmsCode(event, mobile) {
const modalStore = $store('modal');
const lastSendTimer = modalStore.lastTimer[event];
if (typeof lastSendTimer === 'undefined') {
$helper.toast('短信发送事件错误');
return;
}
const duration = dayjs().unix() - lastSendTimer;
const canSend = duration >= 60;
if (!canSend) {
$helper.toast('请稍后再试');
return;
}
// 只有 mobile 非空时才校验。因为部分场景(修改密码),不需要输入手机
if (mobile && !test.mobile(mobile)) {
$helper.toast('手机号码格式不正确');
return;
}
// 发送验证码 + 更新上次发送验证码时间
let scene = -1;
switch (event) {
case 'resetPassword':
scene = 4;
break;
case 'changePassword':
scene = 3;
break;
case 'changeMobile':
scene = 2;
break;
case 'smsLogin':
scene = 1;
break;
}
AuthUtil.sendSmsCode(mobile, scene).then((res) => {
if (res.code === 0) {
modalStore.$patch((state) => {
state.lastTimer[event] = dayjs().unix();
});
} else {
$helper.toast(res.msg || '验证码发送失败,请稍后重试');
}
}).catch((error) => {
$helper.toast('验证码发送失败,请稍后重试');
});
}
// 获取短信验证码倒计时 -- 60秒
export function getSmsTimer(event, mobile = '') {
const modalStore = $store('modal');
const lastSendTimer = modalStore.lastTimer[event];
if (typeof lastSendTimer === 'undefined') {
return '获取验证码';
}
const currentTime = dayjs().unix();
const duration = currentTime - lastSendTimer;
const canSend = duration >= 60;
if (canSend) {
return '获取验证码';
}
const remainingTime = 60 - duration;
return `${remainingTime}`;
}
// 创建响应式的短信验证码计时器
export function useSmsTimer(event) {
const modalStore = $store('modal');
const timer = ref('获取验证码');
const updateTimer = () => {
const lastSendTimer = modalStore.lastTimer[event];
if (typeof lastSendTimer === 'undefined') {
timer.value = '获取验证码';
return;
}
const currentTime = dayjs().unix();
const duration = currentTime - lastSendTimer;
const canSend = duration >= 60;
if (canSend) {
timer.value = '获取验证码';
} else {
const remainingTime = 60 - duration;
timer.value = `${remainingTime}`;
}
};
// 初始化
updateTimer();
// 每秒更新一次
const intervalId = setInterval(updateTimer, 1000);
// 清理定时器的函数
const cleanup = () => {
clearInterval(intervalId);
};
return { timer, cleanup };
}
// 记录广告弹框历史
export function saveAdvHistory(adv) {
const modal = $store('modal');
modal.$patch((state) => {
if (!state.advHistory.includes(adv.imgUrl)) {
state.advHistory.push(adv.imgUrl);
}
});
}