这里后台需要处理专栏的Logo图片的信息,根据上一章节的方式处理,即有UploadGroupLogoHandler类,如:
/** * 上传专栏Logo处理类 * * @author lzj * @since 1.0 * @date [2019-07-23] */ @Slf4j @Component("_groupLogo") public class UploadGroupLogoHandler implements IUploadHandler { @Resource(name = "configCache") private ICache<Config> configCache; @Override public Object upload(MultipartFile file, String distType, String userId) throws Exception { Map<String, Object> result = new HashMap<String, Object>(); try { // 获取图片的原始名称 String originalName = file.getOriginalFilename(); // 判断图片的类型 if (!(originalName.endsWith(".jpg") || originalName.endsWith(".JPG") || originalName.endsWith(".png") || originalName.endsWith(".PNG") || originalName.endsWith(".gif") || originalName.endsWith(".GIF") || originalName.endsWith(".jpeg") || originalName.endsWith(".JPEG"))) { throw new TipException("您上传的图片类型有误,请上传格式为jpg、png或gif"); } // 获取图片的大小 long fileSize = file.getSize(); // 图片大小不能超过2M, 2M = 2 * 1024 * 1024B = 2097152B if (fileSize > 2097152L) { throw new TipException("您上传的图片超过2M"); } Config config = configCache.get(Config.CONFIG_IMG_GROUP_LOGO_PATH); // 保存头像的根目录 String basePath = config.getConfigValue(); if (!(basePath.endsWith("http://www.likecs.com/") || basePath.endsWith("\\"))) { basePath += "http://www.likecs.com/"; } // 根据当前时间构建yyyyMM的文件夹,建立到月的文件夹 String dateDirName = DateUtil.date2Str(new Date(), DateUtil.YEAR_MONTH_FORMAT); basePath += dateDirName; File imageDir = new File(basePath); if (!imageDir.exists()) { imageDir.mkdirs(); } String fileNewName = IdGenarator.guid() + originalName.substring(originalName.lastIndexOf(".")); FileUtil.copy(file.getInputStream(), new FileOutputStream(new File(imageDir, fileNewName))); result.put("url", dateDirName + "http://www.likecs.com/" + fileNewName); result.put("msg", "上传成功"); } catch (TipException e) { result.put("url", ""); result.put("msg", e.getMessage()); } catch (Exception e) { log.error("上传失败", e); result.put("url", ""); result.put("msg", "上传失败"); } return result; } @Override public void download(String fileId, HttpServletResponse response) throws Exception { } @Override public Object list(String distType, String userId) throws Exception { return null; } }最后创建专栏的核心代码如下:
/** * 创建专栏 * * @param request * @param session * @return */ @RequestMapping(value = "/user/group/add", method = RequestMethod.POST) @ResponseBody public Result add(HttpServletRequest request, HttpSession session) { Result result = new Result(); try { // 接收参数 String categoryId = request.getParameter("categoryId"); String name = request.getParameter("name"); String logo = request.getParameter("logo"); String introduce = request.getParameter("introduce"); // 校验参数 if (StringUtils.isEmpty(categoryId) || StringUtils.isEmpty(name) || StringUtils.isEmpty(logo) || StringUtils.isEmpty(introduce)) { throw new TipException("缺少必要参数"); } // 获取登录信息 User tempUser = (User) session.getAttribute(Const.SESSION_USER); String userId = tempUser.getUserId(); // 构建专栏对象 Group group = new Group(); group.setGroupId(IdGenarator.longIdStr()); group.setName(name); group.setLogo(logo); group.setIntroduce(introduce); group.setCategoryId(categoryId); group.setCreator(userId); group.setCreateTime(new Date()); // 从系统配置项获取专栏是否审核 Config config = configCache.get(Config.CONFIG_GROUP_AUDIT); if (config != null && "1".equals(config.getConfigValue())) { // 需要审核 group.setStatus(Group.STATUS_NO); } else { // 不需要审核 group.setStatus(Group.STATUS_SUCCESS); } // 保存专栏信息 boolean flag = groupService.save(group); if (!flag) { throw new TipException("创建专栏失败"); } result.setCode(Result.CODE_SUCCESS); result.setMsg("成功创建专栏"); } catch (TipException e) { result.setCode(Result.CODE_EXCEPTION); result.setMsg(e.getMessage()); } catch (Exception e) { log.error("创建专栏失败", e); result.setCode(Result.CODE_EXCEPTION); result.setMsg("创建专栏失败"); } return result; } 2、修改专栏有了前面新增专栏的基础,其实修改专栏的功能就相对简单很多了,此处只列出处理修改的核心代码即可,如:
/** * 修改专栏 * * @param groupId * @param request * @param session * @return */ @RequestMapping(value = "/user/group/edit/{groupId}", method = RequestMethod.POST) @ResponseBody public Result edit(@PathVariable("groupId") String groupId, HttpServletRequest request, HttpSession session) { Result result = new Result(); try { // 根据id获取专栏信息 Group group = groupService.getById(groupId); if (group == null || StringUtils.isEmpty(group.getGroupId())) { log.error("groupId: " + groupId + ": 该专栏不存在"); throw new TipException("该专栏不存在"); } // 获取用户信息 User tempUser = (User) session.getAttribute(Const.SESSION_USER); String userId = tempUser.getUserId(); if (!userId.equals(group.getCreator())) { log.error("userId: " + userId + "修改别人的groupId: " + groupId); throw new TipException("不能修改别人的专栏"); } // 接收参数 String categoryId = request.getParameter("categoryId"); String name = request.getParameter("name"); String introduce = request.getParameter("introduce"); String logo = request.getParameter("logo"); // 校验参数 if (StringUtils.isEmpty(categoryId) || StringUtils.isEmpty(name) || StringUtils.isEmpty(introduce)) { throw new TipException("缺少必要参数"); } group.setCategoryId(categoryId); group.setName(name); group.setIntroduce(introduce); if (!StringUtils.isEmpty(logo)) { group.setLogo(logo); } group.setUpdateTime(new Date()); // 修改 boolean flag = groupService.updateById(group); if (!flag) { throw new TipException("修改专栏失败"); } result.setCode(Result.CODE_SUCCESS); result.setMsg("修改专栏成功"); } catch (TipException e) { result.setCode(Result.CODE_EXCEPTION); result.setMsg(e.getMessage()); } catch (Exception e) { log.error("修改专栏失败", e); result.setCode(Result.CODE_EXCEPTION); result.setMsg("修改专栏失败"); } return result; } 3、删除专栏