mall-app-t/peach/hooks/useModal.js

174 lines
3.7 KiB
JavaScript

import { ref } from 'vue'
import dayjs from 'dayjs'
import $store from '@/peach/store'
import $helper from '@/peach/helper'
import test from '@/peach/helper/test.js'
import AuthUtil from '@/peach/api/member/auth'
/**
* @author Ankkaya
* @description 打开授权弹窗
* @param {string} type - 弹窗类型
* @returns {Type}
*/
export function showAuthModal(type = 'smsLogin') {
const modal = $store('modal')
if (modal.auth !== '') {
closeAuthModal()
setTimeout(() => {
modal.auth = type
}, 100)
} else {
modal.auth = type
}
}
/**
* @author Ankkaya
* @description 关闭授权弹窗
* @param {Type} -
* @returns {Type}
*/
export function closeAuthModal() {
$store('modal').auth = ''
}
/**
* @author Ankkaya
* @description 打开分享弹窗
* @param {Type} -
* @returns {Type}
*/
export function showShareModal() {
$store('modal').share = true
}
/**
* @author Ankkaya
* @description 关闭分享弹窗
* @param {Type} -
* @returns {Type}
*/
export function closeShareModal() {
$store('modal').share = false
}
/**
* @author Ankkaya
* @description 打开快捷菜单
* @param {Type} -
* @returns {Type}
*/
export function showMenuTools() {
$store('modal').menu = true
}
/**
* @author Ankkaya
* @description 关闭快捷菜单
* @param {Type} -
* @returns {Type}
*/
// 关闭快捷菜单
export function closeMenuTools() {
$store('modal').menu = false
}
/**
* @author Ankkaya
* @description 发送验证码
* @param {string} event - 事件类型
* @param {string} mobile - 手机号码
* @returns {Type}
*/
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.lastTimer[event] = dayjs().unix()
// }
})
}
/**
* @author Ankkaya
* @description 获取验证码倒计时
* @param {string} event - 事件类型
* @returns {Type}
*/
export function getSmsTimer(event) {
const modalStore = $store('modal')
const lastSendTimer = modalStore.lastTimer[event]
if (typeof lastSendTimer === 'undefined') {
$helper.toast('短信发送事件错误')
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() + ' 秒'
}
}
/**
* @author Ankkaya
* @description 广告弹框历史
* @param {Type} -
* @returns {Type}
*/
export function saveAdvHistory(adv) {
const modal = $store('modal')
modal.$patch((state) => {
if (!state.advHistory.includes(adv.imgUrl)) {
state.advHistory.push(adv.imgUrl)
}
})
}