Files
it-tools-pro/src/components/FeedbackModal.vue
T

268 lines
6.2 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { ref, watch } from 'vue';
import { useMessage } from 'naive-ui';
// 站内留言反馈弹窗(与授权系统无关,任何人可提交)
const props = defineProps<{ show: boolean }>();
const emit = defineEmits<{ (e: 'update:show', v: boolean): void }>();
const message = useMessage();
const TYPES = ['功能建议', '意见反馈'];
const typeOptions = TYPES.map(t => ({ label: t, value: t }));
const type = ref('功能建议');
const content = ref('');
const contact = ref('');
const captchaId = ref('');
const captchaImg = ref('');
const captchaCode = ref('');
const err = ref('');
const submitting = ref(false);
function close() {
emit('update:show', false);
}
async function loadCaptcha() {
try {
const res = await fetch('/relay/feedback/captcha');
const b = await res.json();
if (b.code === 200) {
captchaId.value = b.data.id;
captchaImg.value = `data:image/png;base64,${b.data.img}`;
err.value = '';
}
else {
captchaImg.value = '';
err.value = b.message || '验证码加载失败';
}
}
catch {
captchaImg.value = '';
err.value = '网络异常,验证码加载失败';
}
}
watch(() => props.show, (v) => {
if (v) {
err.value = '';
captchaCode.value = '';
loadCaptcha();
}
});
async function submit() {
if (submitting.value) {
return;
}
if (!content.value.trim()) {
err.value = '请填写留言内容';
return;
}
if (!captchaCode.value.trim()) {
err.value = '请输入图形验证码';
return;
}
submitting.value = true;
err.value = '';
try {
const res = await fetch('/relay/feedback', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
type: type.value,
content: content.value,
contact: contact.value,
captcha_id: captchaId.value,
captcha_code: captchaCode.value,
}),
});
const b = await res.json().catch(() => ({} as any));
if (b.code === 200) {
message.success('提交成功,我们会通过你留的联系方式回复');
content.value = '';
contact.value = '';
captchaCode.value = '';
close();
return;
}
err.value = b.message || `提交失败(${res.status})`;
// 验证码一次性:除了限频(429)以外都换一张,避免用户对着旧码重试
if (res.status !== 429) {
loadCaptcha();
captchaCode.value = '';
}
}
catch {
err.value = '网络异常,请稍后重试';
}
finally {
submitting.value = false;
}
}
</script>
<template>
<n-modal
:show="show"
preset="card"
class="fb-modal"
title="💬 留言反馈"
:style="{ width: 'min(580px, 94vw)' }"
:bordered="false"
@update:show="(v: boolean) => emit('update:show', v)"
>
<div class="fb-hint">与授权系统无关,任何人都可以留言;内容我们会在后台查看,留了联系方式会回复你。</div>
<div class="fb-row">
<label class="fb-label">留言类型</label>
<n-select v-model:value="type" :options="typeOptions" size="medium" />
</div>
<div class="fb-row">
<label class="fb-label">留言内容 <i>*</i></label>
<n-input
v-model:value="content"
type="textarea"
:rows="6"
maxlength="1000"
show-count
placeholder="请描述你的功能建议或遇到的问题,越具体越好…"
/>
</div>
<div class="fb-row">
<label class="fb-label">联系方式(可选)</label>
<n-input
v-model:value="contact"
:maxlength="64"
placeholder="QQ / 微信 / 邮箱,方便我们回复你"
/>
</div>
<div class="fb-row">
<label class="fb-label">图形验证码 <i>*</i></label>
<div class="fb-captcha-line">
<n-input
v-model:value="captchaCode"
:maxlength="4"
placeholder="4 位字符"
class="fb-captcha-input"
@keyup.enter="submit"
/>
<img
v-if="captchaImg"
class="fb-captcha-img"
:src="captchaImg"
alt="图形验证码"
title="点击换一张"
@click="loadCaptcha"
>
<span v-else class="fb-captcha-img fb-captcha-loading" @click="loadCaptcha">加载中…</span>
<span class="fb-captcha-refresh" role="button" tabindex="0" @click="loadCaptcha">换一张</span>
</div>
</div>
<div v-if="err" class="fb-err">{{ err }}</div>
<template #footer>
<div class="fb-foot">
<span class="fb-limit">同一 IP 一小时内限提交 1 次</span>
<span class="fb-foot-btns">
<n-button quaternary @click="close">取消</n-button>
<n-button type="primary" :loading="submitting" @click="submit">提交留言</n-button>
</span>
</div>
</template>
</n-modal>
</template>
<!-- n-modal 会 teleport 到 body,故这里用非 scoped 样式(类名统一 fb- 前缀) -->
<style lang="less">
.fb-modal {
.fb-hint {
font-size: 14.5px;
line-height: 1.6;
opacity: 0.75;
margin-bottom: 16px;
}
.fb-row {
margin-bottom: 14px;
}
.fb-label {
display: block;
font-size: 14.5px;
margin-bottom: 6px;
i {
color: #e0115f;
font-style: normal;
}
}
.fb-captcha-line {
display: flex;
align-items: center;
gap: 10px;
}
.fb-captcha-input {
width: 140px;
flex: none;
}
.fb-captcha-img {
height: 40px;
border-radius: 6px;
cursor: pointer;
border: 1px solid rgba(128, 128, 128, 0.35);
background: rgba(128, 128, 128, 0.12);
padding: 0 12px;
display: inline-flex;
align-items: center;
justify-content: center;
font-size: 14.5px;
user-select: none;
}
.fb-captcha-refresh {
font-size: 14.5px;
cursor: pointer;
opacity: 0.8;
text-decoration: underline;
&:hover {
opacity: 1;
}
}
.fb-err {
margin-top: 10px;
font-size: 14.5px;
color: #d03050;
word-break: break-all;
}
.fb-foot {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
flex-wrap: wrap;
}
.fb-limit {
font-size: 14px;
opacity: 0.6;
}
.fb-foot-btns {
display: inline-flex;
gap: 10px;
}
}
</style>