12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- import { ref } from 'vue'
- import { sendSmsCode } from '@/api/common'
- import { modalStore } from '@/store/modal'
- import dayjs from 'dayjs';
- import test from '@/utils/test'
- const modal = modalStore()
- // 发送验证码
- export const getSmsCode = (event, mobile) => {
- const lastSendTimer = modal.lastTimer[event];
- if (typeof lastSendTimer === 'undefined') {
- uni.showToast({
- title: '短信发送事件错误'
- })
- return;
- }
- const duration = dayjs().unix() - lastSendTimer;
- const canSend = duration >= 60;
- if (!canSend) {
- uni.showToast({
- title: '请稍后再试'
- })
- return;
- }
- // 只有 mobile 非空时才校验。因为部分场景(修改密码),不需要输入手机
- if (mobile && !test.mobile(mobile)) {
- uni.showToast({
- title: '手机号码格式不正确'
- })
- return;
- }
- // 发送验证码 + 更新上次发送验证码时间
- let scene = -1;
- switch (event) {
- case 'resetPassword':
- scene = 4;
- break;
- case 'changePassword':
- scene = 3;
- break;
- case 'changeMobile':
- scene = 2;
- break;
- case 'smsLogin':
- scene = 30; // 对接门墩短信登录
- break;
- }
- sendSmsCode({ phone: mobile, scene }).then((res) => {
- if (res.code === 0) {
- uni.showToast({
- title: '发送成功,请注意查收',
- icon: 'success',
- duration: 2000
- })
- modal.$patch((state) => {
- state.lastTimer[event] = dayjs().unix();
- })
- }
- })
- }
- // 验证码倒计时
- export const getSmsTimer = (event) => {
- const lastSendTimer = modal.lastTimer[event]
- if (typeof lastSendTimer === 'undefined') {
- uni.showToast({
- title: '短信发送事件错误'
- })
- 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() + ' 秒'
- }
- }
|