From 2b03eda4e6588a6a2b02ea3e2bb1b03ceabb83eb Mon Sep 17 00:00:00 2001 From: pgeb4n7tr <1094966870@qq.com> Date: Tue, 17 Jun 2025 16:36:21 +0800 Subject: [PATCH] ADD file via upload --- 通知管理系统的控制器.txt | 235 +++++++++++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 通知管理系统的控制器.txt diff --git a/通知管理系统的控制器.txt b/通知管理系统的控制器.txt new file mode 100644 index 0000000..3b909c4 --- /dev/null +++ b/通知管理系统的控制器.txt @@ -0,0 +1,235 @@ +package com.jubilantz.controller; + +import com.jubilantz.entity.EasNotice; +import com.jubilantz.services.EasNoticeService; +import com.jubilantz.utils.PageUtil; +import org.apache.shiro.authz.annotation.RequiresPermissions; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 通知管理控制器 + * 处理通知的查询、添加、修改、删除以及图片上传等功能 + * @Author JubilantZ + * @Date: 2021/4/27 11:35 + */ +@RequestMapping("/easNotice") +@Controller +public class EasNoticeController { + @Autowired + private EasNoticeService easNoticeService; + + /** + * 进入通知管理首页 + * @RequiresPermissions 注解用于权限控制,只有拥有notice:query权限的用户才能访问 + * @return 通知管理页面路径 + * @throws Exception 抛出可能发生的异常 + */ + @RequestMapping("/index") + @RequiresPermissions("notice:query") + public String index() throws Exception{ + return "system/notice/adminNoticeList"; + } + + /** + * 获取通知列表 + * @param page 当前页码,默认值为1 + * @param limit 每页显示数量,默认值为10 + * @param searchKey 搜索关键词,默认值为空字符串 + * @return 包含通知列表、总数、状态码和消息的Map对象 + * @throws Exception 抛出可能发生的异常 + */ + @ResponseBody + @RequestMapping("/list") + public Map getNoticeList( + @RequestParam(defaultValue = "1") Integer page, + @RequestParam(defaultValue = "10") Integer limit, + @RequestParam(defaultValue="")String searchKey) throws Exception { + Map map = new HashMap<>(); + // 权限类型:3表示获取全部权限的通知(假设1、2、3分别代表不同权限等级) + int type = 3; + // 获取符合条件的通知总数 + int count = easNoticeService.getCountByType(type,searchKey); + + // 初始化分页工具 + PageUtil pageUtil = new PageUtil(page,limit); + // 根据权限类型和搜索关键词获取通知列表 + List list = easNoticeService.getNoticeListByType(type,searchKey,pageUtil); + + + map.put("count",count); + map.put("data",list); + map.put("code",0); + map.put("msg",""); + + return map; + } + + /** + * 显示通知详情页面 + * @return 通知详情页面的ModelAndView对象 + */ + @RequestMapping("/look") + public ModelAndView showNotice(){ + return new ModelAndView("system/notice/notice"); + } + + /** + * 跳转到添加通知页面 + * @return 添加通知页面的ModelAndView对象 + */ + @RequestMapping("/addPage") + public ModelAndView toAddPage() { + return new ModelAndView("system/notice/noticeAdd"); + } + + + /** + * 添加或更新通知 + * @param opType 操作类型:0表示添加,1表示更新,默认值为2(需要确认默认值的业务含义) + * @param easNotice 通知实体对象 + * @return 包含操作结果和消息的Map对象 + * @throws Exception 抛出可能发生的异常 + */ + @RequestMapping("/addNotice") + @ResponseBody + public Map addNotice(@RequestParam(defaultValue="2")Integer opType, EasNotice easNotice) throws Exception { + Map map = new HashMap<>(); + // 打印调试信息(已注释,生产环境建议移除) +// System.out.println("通知id:"+easNotice.getId()); +// System.out.println("opType为:"+opType); +// System.out.println("content为:"+easNotice.getContent()); + + int res = 0; + // 根据操作类型执行添加或更新操作 + if (opType == 0) { + try { + res = easNoticeService.addNotice(easNotice); + } catch (Exception e) { + // 添加失败时设置结果为false(打印语句已注释) +// System.out.println("添加失败!"); + map.put("result",false); + } + if (res > 0){ + map.put("result",true); + }else{ + map.put("result",false); + } + + } else if (opType == 1) { + res = easNoticeService.updateNotice(easNotice); + if (res > 0) { + map.put("result",true); + }else{ + map.put("result",false); + } + + }; + + return map; + } + + /** + * 删除单个通知 + * @param easNotice 包含要删除通知ID的实体对象 + * @return 包含删除结果和消息的Map对象 + */ + @ResponseBody + @RequestMapping("/deleteNotice") + public Map deleteNotice(EasNotice easNotice) { + Map map = new HashMap<>(); + if (easNoticeService.deleteNotice(easNotice) > 0) { + map.put("result",true); + map.put("msg","删除成功"); + }else { + map.put("result",false); + map.put("msg","删除失败"); + } + return map; + } + + /** + * 批量删除通知 + * @param nIds 以逗号分隔的通知ID字符串 + * @return 包含删除结果和消息的Map对象 + */ + @ResponseBody + @RequestMapping("/deleteList") + public Map deleteNoticeList(String nIds) { + Map map = new HashMap<>(); + List list = new ArrayList(); + try { + // 分割ID字符串为数组 + String[] ids = nIds.split(","); + // 转换为Integer列表 + for (String id: ids) { + list.add(Integer.parseInt(id)); + } + if (easNoticeService.deleteNoticeList(list) > 0) { + map.put("result",true); + map.put("msg","批量删除成功"); + }else { + map.put("result",false); + map.put("msg","批量删除失败"); + } + } catch (Exception e) { + e.printStackTrace(); + map.put("result",false); + map.put("msg","批量删除失败"); + } + return map; + } + + /** + * 上传通知图片 + * @param file 上传的文件对象 + * @param request HTTP请求对象 + * @return 包含上传结果的JSON字符串 + * @throws IOException 处理文件操作时可能抛出的IO异常 + */ + @ResponseBody + @RequestMapping(value="/uploadImg") + public String uploadImg(MultipartFile file, HttpServletRequest request) throws IOException { + System.out.println("comming!"); + // 获取图片存储的物理路径 + String path = request.getSession().getServletContext().getRealPath("/images"); + System.out.println("path>>"+path); + // 获取上传图片的原始名称 + String fileName = file.getOriginalFilename(); + System.out.println("fileName>>"+fileName); + // 以下代码为生成唯一文件名的注释(当前未启用) +// //获取图片的后缀 例:.jpg +// fileName = fileName.substring(fileName.lastIndexOf("."), fileName.length()); +// System.out.println("fileName1>>"+fileName); +// //生成图片名称(使用当前时间戳+后缀,避免文件名冲突) +// fileName = System.currentTimeMillis() + fileName; + System.out.println("fileName2>>"+fileName); + // 创建文件对象 + File dir = new File(path, fileName); + System.out.println("File>>"+dir); + // 若目录不存在则创建 + if(!dir.exists()){ + dir.mkdirs(); + } + // 保存上传的文件 + file.transferTo(dir); + + // 构造并返回包含图片路径的JSON响应 + String jsonStr = "{\"code\":0,\"msg\":\"\",\"count\":" + null + ",\"data\":" + "{\"src\":\"" + "/images/" + fileName + "\"}" + "}"; + + return jsonStr; + } +} \ No newline at end of file