仰望星辰工作室

better-staridc-MNBT

better-staridc-MNBT/ templates/tdesign/spa/src/user/components/MailBindDialog.vue 3.1 KB · 130 行 原始文件
Z zfhsh first commit 2 个月前
1<template>
2 <t-dialog
3 v-model:visible="innerVisible"
4 header="邮箱绑定"
5 :close-on-click-overlay="false"
6 :close-on-esc-keydown="!required"
7 :close-btn="!required"
8 :confirm-btn="{ content: '提交绑定', loading: saving }"
9 :cancel-btn="required ? null : '取消'"
10 width="460px"
11 @confirm="onSubmit"
12 >
13 <div class="td-mail-bind">
14 <div class="td-mail-bind-tip">
15 <i class="mdi mdi-email-alert-outline"></i>
16 <div>
17 <strong>{{ tipTitle }}</strong>
18 <p>{{ tipDesc }}</p>
19 </div>
20 </div>
21 <t-form ref="formRef" :data="form" :rules="rules" label-width="0" @submit="onSubmit">
22 <t-form-item name="mail">
23 <t-input
24 v-model="form.mail"
25 placeholder="请输入您的邮箱"
26 clearable
27 size="large"
28 @enter="onSubmit"
29 >
30 <template #prefix-icon>
31 <i class="mdi mdi-email-outline"></i>
32 </template>
33 </t-input>
34 </t-form-item>
35 </t-form>
36 </div>
37 </t-dialog>
38</template>
39
40<script setup>
41import { ref, reactive, computed, watch } from 'vue'
42import { MessagePlugin } from 'tdesign-vue-next'
43import { bindEmail } from '@/user/api/site'
44
45const props = defineProps({
46 visible: { type: Boolean, default: false },
47 /** 是否为强制绑定(邮箱为空时自动弹出且不允许关闭) */
48 required: { type: Boolean, default: false },
49 /** 标题副文案自定义 */
50 tipTitle: { type: String, default: '绑定您的邮箱' },
51 tipDesc: {
52 type: String,
53 default: '绑定邮箱后可接收监控通知与找回密码',
54 },
55})
56
57const emit = defineEmits(['update:visible', 'success'])
58
59const innerVisible = computed({
60 get: () => props.visible,
61 set: v => emit('update:visible', v),
62})
63
64const formRef = ref(null)
65const saving = ref(false)
66const form = reactive({ mail: '' })
67
68const rules = {
69 mail: [
70 { required: true, message: '请输入邮箱', trigger: 'blur' },
71 {
72 pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
73 message: '邮箱格式不正确',
74 trigger: 'blur',
75 },
76 ],
77}
78
79watch(
80 () => props.visible,
81 v => {
82 if (v) form.mail = ''
83 },
84)
85
86async function onSubmit(ctx) {
87 if (ctx && ctx.e) ctx.e.preventDefault()
88 const valid = await formRef.value?.validate()
89 if (valid !== true) return
90 saving.value = true
91 const r = await bindEmail(form.mail.trim())
92 saving.value = false
93 if (r.ok) {
94 MessagePlugin.success('邮箱绑定成功')
95 emit('success', form.mail.trim())
96 innerVisible.value = false
97 }
98}
99</script>
100
101<style scoped>
102.td-mail-bind-tip {
103 display: flex;
104 gap: 10px;
105 padding: 12px 14px;
106 margin-bottom: 14px;
107 background: #fff9e6;
108 border: 1px solid #ffe69b;
109 border-radius: 8px;
110 color: #8a6d3b;
111}
112.td-mail-bind-tip > i {
113 font-size: 22px;
114 color: #e37318;
115 flex-shrink: 0;
116 margin-top: 2px;
117}
118.td-mail-bind-tip strong {
119 display: block;
120 font-size: 13px;
121 color: #665028;
122 margin-bottom: 2px;
123}
124.td-mail-bind-tip p {
125 margin: 0;
126 font-size: 12px;
127 color: #8a6d3b;
128 line-height: 1.5;
129}
130</style>