UserPhoneModal.vue
8.2 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<template>
<BasicModal v-bind="$attrs" @register="registerModal" width="500px" :title="title" :showCancelBtn="false" :showOkBtn="false">
<a-form v-if="type==='updatePhone'" class="antd-modal-form" ref="updateFormRef" :model="updateFormState"
:rules="updateValidatorRules">
<div v-if="current === 0">
<a-form-item name="phoneText">
<span class="black font-size-13">原手机号</span>
<div class="phone-padding">
<span>{{ updateFormState.phoneText }}</span>
</div>
</a-form-item>
<a-form-item name="smscode">
<span class="black font-size-13">验证码</span>
<CountdownInput class="phone-padding" size="large" v-model:value="updateFormState.smscode" placeholder="输入6位验证码"
:sendCodeApi="()=>updateSendCodeApi('verifyOriginalPhone')"/>
</a-form-item>
<a-form-item>
<a-button size="large" type="primary" block @click="nextStepClick">
下一步
</a-button>
</a-form-item>
</div>
<div v-else-if="current === 1">
<a-form-item name="newPhone">
<span class="black font-size-13">新手机号</span>
<div class="phone-padding">
<a-input v-model:value="updateFormState.newPhone" placeholder="请输入新手机号"/>
</div>
</a-form-item>
<a-form-item name="smscode">
<span class="black font-size-13">验证码</span>
<CountdownInput class="phone-padding" size="large" v-model:value="updateFormState.smscode" placeholder="输入6位验证码"
:sendCodeApi="()=>updateSendCodeApi('updatePhone')"/>
</a-form-item>
<a-form-item>
<a-button size="large" type="primary" block @click="finishedClick">
完成
</a-button>
</a-form-item>
</div>
</a-form>
<a-form v-else-if="type==='bindPhone'" class="antd-modal-form" ref="formRef" :model="formState" :rules="validatorRules">
<a-form-item name="phone">
<a-input size="large" v-model:value="formState.phone" placeholder="请输入手机号" />
</a-form-item>
<a-form-item name="smscode">
<CountdownInput size="large" v-model:value="formState.smscode" placeholder="输入6位验证码" :sendCodeApi="sendCodeApi" />
</a-form-item>
<a-form-item>
<a-button size="large" type="primary" block @click="updatePhone">
确认
</a-button>
</a-form-item>
</a-form>
</BasicModal>
</template>
<script lang="ts" setup name="user-replace-phone-modal">
import BasicModal from "/@/components/Modal/src/BasicModal.vue";
import { CountdownInput } from '/@/components/CountDown';
import { useUserStore } from "/@/store/modules/user";
import { useMessage } from "/@/hooks/web/useMessage";
import { defineEmits, ref, reactive, toRaw } from "vue";
import { useModalInner } from "/@/components/Modal";
import { getCaptcha } from "/@/api/sys/user";
import { SmsEnum } from "/@/views/sys/login/useLogin";
import { Rule } from "/@/components/Form";
import { rules } from "/@/utils/helper/validator";
import { Form } from "ant-design-vue";
import { updateMobile, changePhone } from "../UserSetting.api";
import { duplicateCheck } from "/@/views/system/user/user.api";
import {defHttp} from "@/utils/http/axios";
import { ExceptionEnum } from "@/enums/exceptionEnum";
const userStore = useUserStore();
const { createMessage } = useMessage();
const formState = reactive<Record<string, any>>({
phone:'',
smscode:''
});
//修改手机号
const updateFormState = reactive<Record<string, any>>({
phone:'',
smscode:'',
newPhone:'',
phoneText:'',
newSmsCode:''
});
const formRef = ref();
const userData = ref<any>({})
const validatorRules: Record<string, Rule[]> = {
phone: [{...rules.duplicateCheckRule("sys_user",'phone',formState,{ label:'手机号' })[0]},{ pattern: /^1[3456789]\d{9}$/, message: '手机号码格式有误' }],
smscode: [{ required: true,message:'请输入验证码' }],
};
//修改手机号验证规则
const updateValidatorRules: Record<string, Rule[]> = {
newPhone: [{...rules.duplicateCheckRule("sys_user",'phone',formState,{ label:'手机号' })[0]},{ pattern: /^1[3456789]\d{9}$/, message: '手机号码格式有误' }],
smscode: [{ required: true,message:'请输入验证码' }],
newSmsCode: [{ required: true,message:'请输入验证码' }],
};
const useForm = Form.useForm;
const title = ref<string>('');
const emit = defineEmits(['register','success']);
//修改手机号还是绑定手机号
const type = ref<string>('updatePhone');
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
setModalProps({ confirmLoading: false });
if(data.record.phone){
updateFormState.phone = "";
updateFormState.smscode = "";
current.value = 0;
title.value = '修改手机号';
type.value = "updatePhone";
Object.assign(updateFormState, data.record);
}else{
title.value = '绑定手机号';
type.value = "bindPhone"
//赋值
data.record.smscode = '';
Object.assign(formState, data.record);
setTimeout(()=>{
formRef.value.resetFields();
formRef.value.clearValidate();
},300)
}
userData.value = data.record;
});
/**
* 倒计时执行前的函数
*/
function sendCodeApi() {
return getCaptcha({ mobile: formState.phone, smsmode: SmsEnum.REGISTER });
}
/**
* 倒计时执行前的函数
*
* @param type 类型 verifyOriginalPhone 验证员手机号 updatePhone 修改手机号
*/
function updateSendCodeApi(type) {
let phone = ""
if(current.value === 0){
phone = updateFormState.phone;
}else{
phone = updateFormState.newPhone;
}
let params = { phone: phone, type: type };
return new Promise((resolve, reject) => {
defHttp.post({ url: "/sys/user/sendChangePhoneSms", params }, { isTransformResponse: false }).then((res) => {
console.log(res);
if (res.success) {
resolve(true);
} else {
//update-begin---author:wangshuai---date:2024-04-18---for:【QQYUN-9005】同一个IP,1分钟超过5次短信,则提示需要验证码---
if(res.code != ExceptionEnum.PHONE_SMS_FAIL_CODE){
createMessage.error(res.message || '未知问题');
reject();
}
reject(res);
//update-end---author:wangshuai---date:2024-04-18---for:【QQYUN-9005】同一个IP,1分钟超过5次短信,则提示需要验证码---
}
}).catch((res)=>{
createMessage.error(res.message || '未知问题');
reject();
});
});
}
/**
* 更新手机号
*/
async function updatePhone() {
await formRef.value.validateFields();
updateMobile(formState).then((res) =>{
if(res.success){
createMessage.success(type.value === "updatePhone"?"修改手机号成功":"绑定手机号成功")
emit("success")
closeModal();
}else{
createMessage.warning(res.message)
}
})
}
//走到第几步
const current = ref<number>(0);
const updateFormRef = ref();
/**
* 下一步点击事件
*/
async function nextStepClick() {
let params = { phone: updateFormState.phone, smscode: updateFormState.smscode, type: 'verifyOriginalPhone' };
changeAndVerifyPhone(params,1)
}
/**
* 完成
*/
function finishedClick() {
changeAndVerifyPhone({ phone: updateFormState.phone, newPhone: updateFormState.newPhone, smscode: updateFormState.smscode, type: 'updatePhone' },0);
}
/**
* 修改并验证手机号
* @param params
* @param index
*/
async function changeAndVerifyPhone(params, index) {
await updateFormRef.value.validateFields();
changePhone(params).then((res)=>{
if(res.success){
current.value = index;
if(index == 0){
createMessage.success(res.message);
emit("success");
closeModal();
}
updateFormState.smscode = "";
}else{
createMessage.warn(res.message);
}
}).catch((res) =>{
createMessage.warn(res.message);
})
}
</script>
<style lang="less" scoped>
.antd-modal-form {
padding: 10px 24px 10px 24px;
}
.black {
color: #000000;
}
.font-size-13 {
font-size: 13px;
line-height: 15px;
}
.phone-padding {
padding-top: 10px;
padding-bottom: 10px;
}
:deep(.ant-form-item){
margin-bottom: 10px;
}
</style>