import {appSmsSendPost} from "@/service";

// 获取短信验证码的自定义 Hook
export default function useGetMsgCode() {
    const {t} = useI18n()
    const isSend = ref(0)

    async function getMsgCode({
                                  type, phone, areaCode
                              }: {
        type: string | number;
        phone: string | number,
        areaCode: string | number,

    }) {
        const res = await appSmsSendPost({
            body: {
                areaCode,
                phone,
                // 1 :用户端绑定手机号 2 :用户端邮箱注册 3 :用户端忘记密码 4 :用户端忘记支付密码 5 :用户端设置支付密码
                type,
            }
        })
        if (res) {
            await uni.showToast({title: t('common.verification-code-sent-successfully'), icon: 'none'})
            isSend.value = 60
            const timer = setInterval(() => {
                if (isSend.value === 0) {
                    clearInterval(timer)
                } else {
                    isSend.value -= 1
                }
            }, 1000)
        }
    }

    return {
        isSend,
        getMsgCode
    }
}
