广丰卷烟厂数采质量分析系统
zhuguifei
2026-03-02 80ff784bf60637cd348ae665fc907f7b1e527dd8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { computed } from 'vue';
import { useCountDown, useLoading } from '@sa/hooks';
import { REG_PHONE } from '@/constants/reg';
import { $t } from '@/locales';
 
export function useCaptcha() {
  const { loading, startLoading, endLoading } = useLoading();
  const { count, start, stop, isCounting } = useCountDown(10);
 
  const label = computed(() => {
    let text = $t('page.login.codeLogin.getCode');
 
    const countingLabel = $t('page.login.codeLogin.reGetCode', { time: count.value });
 
    if (loading.value) {
      text = '';
    }
 
    if (isCounting.value) {
      text = countingLabel;
    }
 
    return text;
  });
 
  function isPhoneValid(phone: string) {
    if (phone.trim() === '') {
      window.$message?.error?.($t('form.phone.required'));
 
      return false;
    }
 
    if (!REG_PHONE.test(phone)) {
      window.$message?.error?.($t('form.phone.invalid'));
 
      return false;
    }
 
    return true;
  }
 
  async function getCaptcha(phone: string) {
    const valid = isPhoneValid(phone);
 
    if (!valid || loading.value) {
      return;
    }
 
    startLoading();
 
    // request
    await new Promise(resolve => {
      setTimeout(resolve, 500);
    });
 
    window.$message?.success?.($t('page.login.codeLogin.sendCodeSuccess'));
 
    start();
 
    endLoading();
  }
 
  return {
    label,
    start,
    stop,
    isCounting,
    loading,
    getCaptcha
  };
}