zhengyiming
2025-02-10 0f686ea1fe4700a909a6159efcf1fcb0e1f88a17
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<template>
  <div class="verification-code-login-form-wrapper">
    <nut-form class="verification-code-login-form" ref="formRef" :model-value="form" :rules="rules">
      <nut-form-item label="" class="bole-form-item" prop="phoneNumber" required>
        <nut-input
          v-model.trim="form.phoneNumber"
          class="bole-input-text"
          placeholder="请输入手机号"
          type="text"
        />
      </nut-form-item>
      <nut-form-item label="" class="bole-form-item" prop="verificationCode" required>
        <nut-input
          v-model.trim="form.verificationCode"
          class="bole-input-text"
          placeholder="请输入验证码"
          type="number"
        >
          <template #right>
            <ProFormCaptcha
              :onGetCaptcha="onGetCaptcha"
              phonePropName="phoneNumber"
              :validateField="formRef?.validate"
            ></ProFormCaptcha>
          </template>
        </nut-input>
      </nut-form-item>
    </nut-form>
    <LargeButton class="login-btn" @click="handleLogin" :loading="form.loading">登录</LargeButton>
    <div class="go-register-btn" @click="goRegister">立即注册</div>
  </div>
</template>
 
<script setup lang="ts">
import * as commonServices from '@12333/services/api/Common';
import { FormRules } from '@nutui/nutui-taro/dist/types/__VUE/form/types';
import { FormValidator, Message } from '@12333/utils';
import { LargeButton } from '@/components';
import { ProFormCaptcha } from 'senin-mini/components';
import { VerificationCodeBusinessType } from '@12333/constants';
import { useLoginedJump } from '@/hooks';
import { useUserStore } from '@/stores/modules/user';
import Taro from '@tarojs/taro';
 
defineOptions({
  name: 'verificationCodeLoginForm',
});
 
type Props = {
  policyChecked?: boolean;
};
 
const props = withDefaults(defineProps<Props>(), {});
 
const userStore = useUserStore();
 
const { jump } = useLoginedJump();
 
const formRef = ref(null);
 
const form = reactive({
  loading: false,
  phoneNumber: '',
  verificationCode: '',
});
 
const rules = reactive<FormRules>({
  phoneNumber: [
    { required: true, message: '请输入手机号' },
    { validator: FormValidator.validatorPhoneNumber, message: '请输入正确的手机号' },
  ],
  verificationCode: [{ required: true, message: '请输入验证码' }],
});
 
async function onGetCaptcha() {
  await commonServices.sendVerificationCode(
    {
      phoneNumber: form.phoneNumber,
      businessType: VerificationCodeBusinessType.PhoneMesssageCodeLogin,
    },
    { showLoading: false }
  );
}
 
async function handleLogin() {
  try {
    if (props.policyChecked) {
      const { valid } = await formRef.value.validate();
      if (valid) {
        form.loading = true;
        await userStore.loginByUsername({
          phoneNumber: form.phoneNumber,
          code: form.verificationCode,
        });
        jump();
      }
    } else {
      noAccess();
    }
  } catch (error) {
  } finally {
    form.loading = false;
  }
}
 
function noAccess() {
  Message.warning('请先阅读并勾选协议');
}
 
function goRegister() {
  Taro.navigateTo({
    url: RouterPath.registerForm,
  });
}
</script>