AttachmentController.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package com.mrxu.admin.controller.base;
  2. import com.mrxu.admin.controller.AdminBaseController;
  3. import com.mrxu.base.dto.AttachmentDto;
  4. import com.mrxu.base.entity.Attachment;
  5. import com.mrxu.base.service.AttachmentService;
  6. import com.mrxu.framework.boot.bean.LayuiPage;
  7. import com.mrxu.framework.boot.bean.PageResult;
  8. import com.mrxu.framework.boot.bean.ResponseObj;
  9. import com.mrxu.framework.common.MrxuConst;
  10. import com.mrxu.framework.common.util.DateFunc;
  11. import com.mrxu.framework.common.util.MrxuAssert;
  12. import com.mrxu.framework.common.util.QiniuService;
  13. import com.mrxu.framework.common.util.StrFunc;
  14. import io.swagger.annotations.Api;
  15. import io.swagger.annotations.ApiModelProperty;
  16. import lombok.Data;
  17. import lombok.RequiredArgsConstructor;
  18. import org.apache.shiro.authz.annotation.RequiresPermissions;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Controller;
  21. import org.springframework.ui.Model;
  22. import org.springframework.validation.BindingResult;
  23. import org.springframework.web.bind.annotation.RequestBody;
  24. import org.springframework.web.bind.annotation.RequestMapping;
  25. import org.springframework.web.bind.annotation.RequestParam;
  26. import org.springframework.web.bind.annotation.ResponseBody;
  27. import org.springframework.web.multipart.MultipartFile;
  28. import javax.validation.Valid;
  29. import java.io.IOException;
  30. import java.util.List;
  31. @Api(tags = "附件管理")
  32. @Controller
  33. @RequestMapping("/base/attachment")
  34. @RequiredArgsConstructor(onConstructor = @__(@Autowired))
  35. public class AttachmentController extends AdminBaseController {
  36. private final AttachmentService attachmentService;
  37. @RequiresPermissions("base:attachment:read")
  38. @RequestMapping("index.html")
  39. public String index(Model model) {
  40. return "base/attachment.html";
  41. }
  42. @ResponseBody
  43. @RequestMapping("/page.json")
  44. public LayuiPage<Attachment> page(AttachmentDto queryDto) {
  45. queryDto.setCreatePerson(getUsername());
  46. PageResult<Attachment> rs = attachmentService.page(getTenantId(),queryDto);
  47. return renderLayuiPage(rs);
  48. }
  49. @RequiresPermissions("base:attachment:update")
  50. @ResponseBody
  51. @RequestMapping("/save.json")
  52. public ResponseObj<Boolean> save(@Valid @RequestBody Attachment bean,BindingResult bindingResult) {
  53. MrxuAssert.check(bindingResult);
  54. return success(attachmentService.saveOrUpdate(getTenantId(),bean,getUsername()));
  55. }
  56. @RequiresPermissions("base:attachment:remove")
  57. @ResponseBody
  58. @RequestMapping("/remove.json")
  59. public ResponseObj<Boolean> remove(Integer id) {
  60. return success(attachmentService.remove(getTenantId(),id));
  61. }
  62. @RequiresPermissions("base:attachment:remove")
  63. @ResponseBody
  64. @RequestMapping("/removeBatch.json")
  65. public ResponseObj<Boolean> removeBatch(@RequestBody List<Integer> ids) {
  66. return success(attachmentService.removeBatch(getTenantId(),ids));
  67. }
  68. @ResponseBody
  69. @RequestMapping("/upload.json")
  70. public UploadRs upload(MultipartFile file,@RequestParam(defaultValue = "/" )String dir) throws IOException {
  71. String tenantId = "6000";
  72. String userName = MrxuConst.defaultPerson;
  73. try {
  74. tenantId = getTenantId();
  75. userName = getUsername();
  76. }
  77. catch (Exception e) {
  78. }
  79. String fileName = file.getOriginalFilename();
  80. String fileExtension = fileName.substring(fileName.lastIndexOf(".")+1);
  81. String fileType = Attachment.getType(fileExtension);
  82. String filePath = DateFunc.getYear()+"/"+fileType+"/"+ StrFunc.randomString(64)+"."+fileExtension;
  83. filePath = QiniuService.upload(file.getBytes(),filePath);
  84. Attachment bean = new Attachment();
  85. bean.setExtension(fileExtension);
  86. bean.setTenantId(tenantId);
  87. bean.setName(fileName);
  88. bean.setSize(file.getSize());
  89. bean.setUrl(QiniuService.url+ "/" + filePath);
  90. bean.setTag(dir);
  91. bean.setType(fileType);
  92. attachmentService.saveOrUpdate(tenantId,bean,userName);
  93. return new UploadRs(dir,bean.getUrl());
  94. }
  95. @Data
  96. class UploadRs{
  97. UploadRs(String dir,String location) {
  98. this.dir = dir;
  99. this.location = location;
  100. }
  101. @ApiModelProperty(value = "状态码")
  102. private int code = 0;
  103. @ApiModelProperty(value = "提示")
  104. private String msg = "成功";
  105. @ApiModelProperty(value = "路径")
  106. private String dir;
  107. @ApiModelProperty(value = "路径")
  108. private String location;
  109. }
  110. }