package com.mrxu.admin.controller.base; import com.mrxu.admin.controller.AdminBaseController; import com.mrxu.base.dto.AttachmentDto; import com.mrxu.base.entity.Attachment; import com.mrxu.base.service.AttachmentService; import com.mrxu.framework.boot.bean.LayuiPage; import com.mrxu.framework.boot.bean.PageResult; import com.mrxu.framework.boot.bean.ResponseObj; import com.mrxu.framework.common.MrxuConst; import com.mrxu.framework.common.util.DateFunc; import com.mrxu.framework.common.util.MrxuAssert; import com.mrxu.framework.common.util.QiniuService; import com.mrxu.framework.common.util.StrFunc; import io.swagger.annotations.Api; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.RequiredArgsConstructor; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestBody; 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 javax.validation.Valid; import java.io.IOException; import java.util.List; @Api(tags = "附件管理") @Controller @RequestMapping("/base/attachment") @RequiredArgsConstructor(onConstructor = @__(@Autowired)) public class AttachmentController extends AdminBaseController { private final AttachmentService attachmentService; @RequiresPermissions("base:attachment:read") @RequestMapping("index.html") public String index(Model model) { return "base/attachment.html"; } @ResponseBody @RequestMapping("/page.json") public LayuiPage page(AttachmentDto queryDto) { queryDto.setCreatePerson(getUsername()); PageResult rs = attachmentService.page(getTenantId(),queryDto); return renderLayuiPage(rs); } @RequiresPermissions("base:attachment:update") @ResponseBody @RequestMapping("/save.json") public ResponseObj save(@Valid @RequestBody Attachment bean,BindingResult bindingResult) { MrxuAssert.check(bindingResult); return success(attachmentService.saveOrUpdate(getTenantId(),bean,getUsername())); } @RequiresPermissions("base:attachment:remove") @ResponseBody @RequestMapping("/remove.json") public ResponseObj remove(Integer id) { return success(attachmentService.remove(getTenantId(),id)); } @RequiresPermissions("base:attachment:remove") @ResponseBody @RequestMapping("/removeBatch.json") public ResponseObj removeBatch(@RequestBody List ids) { return success(attachmentService.removeBatch(getTenantId(),ids)); } @ResponseBody @RequestMapping("/upload.json") public UploadRs upload(MultipartFile file,@RequestParam(defaultValue = "/" )String dir) throws IOException { String tenantId = "6000"; String userName = MrxuConst.defaultPerson; try { tenantId = getTenantId(); userName = getUsername(); } catch (Exception e) { } String fileName = file.getOriginalFilename(); String fileExtension = fileName.substring(fileName.lastIndexOf(".")+1); String fileType = Attachment.getType(fileExtension); String filePath = DateFunc.getYear()+"/"+fileType+"/"+ StrFunc.randomString(64)+"."+fileExtension; filePath = QiniuService.upload(file.getBytes(),filePath); Attachment bean = new Attachment(); bean.setExtension(fileExtension); bean.setTenantId(tenantId); bean.setName(fileName); bean.setSize(file.getSize()); bean.setUrl(QiniuService.url+ "/" + filePath); bean.setTag(dir); bean.setType(fileType); attachmentService.saveOrUpdate(tenantId,bean,userName); return new UploadRs(dir,bean.getUrl()); } @Data class UploadRs{ UploadRs(String dir,String location) { this.dir = dir; this.location = location; } @ApiModelProperty(value = "状态码") private int code = 0; @ApiModelProperty(value = "提示") private String msg = "成功"; @ApiModelProperty(value = "路径") private String dir; @ApiModelProperty(value = "路径") private String location; } }