Compare commits

...
6 Commits
Author SHA1 Message Date
wonder 4d4663e405 🐛Fix: 修复前后端数据格式不一致问题 2025-10-08 00:40:21 +08:00
wonder ab5d895ca1 🐛Fix: 修复请求路径错误 2025-10-08 00:30:40 +08:00
wonder 5a3df648a2 🔄Update: 配置了跨域 2025-10-08 00:21:48 +08:00
wonder 5d6023a628 🔧Chore: 删除冗余导入 2025-10-08 00:18:26 +08:00
wonder 0ac267316b 🔄Update: 完成邮件发送接口 2025-10-08 00:17:22 +08:00
wonder c72a374f21 🔄Update: 删除附件功能 2025-10-08 00:17:05 +08:00
6 changed files with 59 additions and 48 deletions
+5
View File
@@ -35,6 +35,11 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>
@@ -1,7 +1,5 @@
package cn.hezhaohui.mail; package cn.hezhaohui.mail;
import cn.hezhaohui.mail.service.MailService;
import jakarta.annotation.Resource;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -0,0 +1,19 @@
package cn.hezhaohui.mail.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // 应用到所有路径
.allowedOrigins("http://localhost:5173") // 允许前端开发服务器的源
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的HTTP方法
.allowedHeaders("*") // 允许所有header
.allowCredentials(true) // 允许发送凭证信息(如cookies)
.maxAge(3600); // 预检请求的有效期,单位为秒
}
}
@@ -0,0 +1,23 @@
package cn.hezhaohui.mail.controller;
import cn.hezhaohui.mail.entity.Response;
import cn.hezhaohui.mail.service.MailService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/email")
@Slf4j
public class MailController {
@Resource
private MailService mailService;
@PostMapping("/send")
public Response sendMail(@RequestParam String to, @RequestParam String subject, @RequestParam String content) {
mailService.sendMessage(to, subject, content);
log.info("[To]: {}\n[Subject]: {}\n[Content]: {}\n", to, subject, content);
return new Response(200, "success");
}
}
@@ -0,0 +1,10 @@
package cn.hezhaohui.mail.entity;
import lombok.Data;
@Data
public class Response {
final private int status;
final private String message;
}
+2 -46
View File
@@ -36,26 +36,6 @@
/> />
</el-form-item> </el-form-item>
<!-- 附件 -->
<el-form-item label="附件">
<el-upload
v-model:file-list="fileList"
action="/api/upload"
:auto-upload="false"
:on-change="handleFileChange"
:on-remove="handleFileRemove"
multiple
:limit="5"
:on-exceed="handleExceed"
>
<el-button type="primary">选择文件</el-button>
<template #tip>
<div class="el-upload__tip">
支持格式:.jpg, .png, .pdf, .docx,最多5个文件,每个文件不超过10MB
</div>
</template>
</el-upload>
</el-form-item>
<!-- 提交按钮 --> <!-- 提交按钮 -->
<el-form-item> <el-form-item>
@@ -85,7 +65,7 @@
<script setup> <script setup>
import { ref, reactive } from 'vue' import { ref, reactive } from 'vue'
import { ElForm, ElFormItem, ElInput, ElButton, ElUpload, ElAlert } from 'element-plus' import { ElForm, ElFormItem, ElInput, ElButton, ElAlert } from 'element-plus'
import axios from 'axios' import axios from 'axios'
// 表单引用 // 表单引用
@@ -98,9 +78,6 @@ const emailForm = reactive({
content: '' content: ''
}) })
// 文件列表
const fileList = ref([])
// 提交状态 // 提交状态
const submitting = ref(false) const submitting = ref(false)
@@ -122,26 +99,10 @@ const rules = {
] ]
} }
// 处理文件变化
const handleFileChange = (file, fileList) => {
// 可以在这里添加文件验证逻辑
console.log('文件变化:', file, fileList)
}
// 处理文件移除
const handleFileRemove = (file, fileList) => {
console.log('文件移除:', file, fileList)
}
// 处理文件超出限制
const handleExceed = () => {
ElMessage.warning('最多只能上传5个文件')
}
// 重置表单 // 重置表单
const resetForm = () => { const resetForm = () => {
formRef.value.resetFields() formRef.value.resetFields()
fileList.value = []
message.value = '' message.value = ''
} }
@@ -159,16 +120,11 @@ const submitForm = async () => {
formData.append('subject', emailForm.subject) formData.append('subject', emailForm.subject)
formData.append('content', emailForm.content) formData.append('content', emailForm.content)
// 添加附件
fileList.value.forEach(file => {
formData.append('attachments', file.raw)
})
submitting.value = true submitting.value = true
message.value = '' message.value = ''
// 发送请求 // 发送请求
const response = await axios.post('http:localhost:8080/api/email/send', formData, { const response = await axios.post('http://localhost:8080/api/email/send', formData, {
headers: { headers: {
'Content-Type': 'multipart/form-data' 'Content-Type': 'multipart/form-data'
} }