Files
delivery-uniapp/sheep/hooks/useModal.js

149 lines
3.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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/member/auth';
// 打开授权弹框
export function showAuthModal(type = 'accountLogin') {
const modal = $store('modal');
if (modal.auth !== '') {
// 注意:延迟修改,保证下面的 closeAuthModal 先执行掉
setTimeout(() => {
modal.$patch((state) => {
state.auth = type;
});
}, 500);
closeAuthModal();
} else {
modal.$patch((state) => {
state.auth = type;
});
}
}
// 关闭授权弹框
export function closeAuthModal() {
$store('modal').$patch((state) => {
state.auth = '';
});
}
// 打开分享弹框
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];
// 如果场景未初始化先初始化为0表示可以发送
if (typeof lastSendTimer === 'undefined') {
modalStore.$patch((state) => {
if (typeof state.lastTimer[event] === 'undefined') {
state.lastTimer[event] = 0;
}
});
}
const duration = dayjs().unix() - (modalStore.lastTimer[event] || 0);
const canSend = duration >= 60;
if (!canSend) {
$helper.toast('请稍后再试');
return;
}
// 只有 mobile 非空时才校验。因为部分场景(修改密码),不需要输入手机
if (mobile && !test.mobile(mobile)) {
$helper.toast('手机号码格式不正确');
return;
}
// 发送验证码 + 更新上次发送验证码时间
let scene = -1;
switch (event) {
case 'registerDelivery':
scene = 5;
break;
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();
});
}
});
}
// 获取短信验证码倒计时 -- 60秒
export function getSmsTimer(event, mobile = '') {
const modalStore = $store('modal');
const lastSendTimer = modalStore.lastTimer[event];
// 如果场景未初始化,返回"获取验证码"
if (typeof lastSendTimer === 'undefined') {
return '获取验证码';
}
const duration = ref(dayjs().unix() - lastSendTimer - 60);
const canSend = duration.value >= 0;
if (canSend) {
return '获取验证码';
}
if (!canSend) {
setTimeout(() => {
duration.value++;
}, 1000);
return -duration.value.toString() + ' 秒';
}
}
// 记录广告弹框历史
export function saveAdvHistory(adv) {
const modal = $store('modal');
modal.$patch((state) => {
if (!state.advHistory.includes(adv.imgUrl)) {
state.advHistory.push(adv.imgUrl);
}
});
}