wiki优化

This commit is contained in:
暮光:城中城
2019-03-05 23:15:47 +08:00
parent 7357058694
commit 7d8fa1ff82
20 changed files with 313 additions and 150 deletions

View File

@@ -11,14 +11,17 @@ import com.zyplayer.doc.data.service.manage.WikiPageFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.util.Date;
/**
@@ -41,30 +44,67 @@ public class WikiCommonController {
@PostMapping("/upload")
public ResponseJson<Object> upload(WikiPageFile wikiPageFile, @RequestParam("files") MultipartFile file) {
//通过CommonsMultipartFile的方法直接写文件注意这个时候
String fileName = file.getOriginalFilename();
String fileSuffix = "";
if (fileName != null && fileName.lastIndexOf(".") >= 0) {
fileSuffix = fileName.substring(fileName.lastIndexOf("."));
}
DocUserDetails currentUser = DocUserUtil.getCurrentUser();
String path = uploadPath + "/" + DateTime.now().toString("yyyy/MM/dd") + "/";
File newFile = new File(path);
if (!newFile.exists() && !newFile.mkdir()) {
return DocResponseJson.warn("创建文件夹失败");
}
path += RandomUtil.simpleUUID() + fileSuffix;
newFile = new File(path);
try {
String fileName = file.getOriginalFilename();
String fileSuffix = fileName.substring(fileName.lastIndexOf("."));
DocUserDetails currentUser = DocUserUtil.getCurrentUser();
String path = uploadPath + "/" + DateTime.now().toString("yyyy/MM/dd") + "/";
File newFile = new File(path);
if (!newFile.exists()) {
newFile.mkdir();
}
path += RandomUtil.simpleUUID() + fileSuffix;
newFile = new File(path);
file.transferTo(newFile);
wikiPageFile.setFileUrl(path);
wikiPageFile.setFileName(fileName);
wikiPageFile.setCreateTime(new Date());
wikiPageFile.setCreateUserId(currentUser.getUserId());
wikiPageFile.setCreateUserName(currentUser.getUsername());
wikiPageFile.setDelFlag(0);
} catch (Exception e) {
e.printStackTrace();
return DocResponseJson.warn("失败");
return DocResponseJson.warn("保存文件失败");
}
wikiPageFile.setFileUrl(path);
wikiPageFile.setFileName(fileName);
wikiPageFile.setCreateTime(new Date());
wikiPageFile.setCreateUserId(currentUser.getUserId());
wikiPageFile.setCreateUserName(currentUser.getUsername());
wikiPageFile.setDelFlag(0);
wikiPageFileService.save(wikiPageFile);
wikiPageFile.setFileUrl("zyplayer-doc-wiki/common/file?fileId=" + wikiPageFile.getId());
return DocResponseJson.ok(wikiPageFile);
}
@GetMapping("/file")
public ResponseJson<Object> file(Long fileId, HttpServletResponse response) {
if (fileId == null || fileId <= 0) {
return DocResponseJson.warn("请指定文件ID");
}
WikiPageFile pageFile = wikiPageFileService.getById(fileId);
if (pageFile == null) {
return DocResponseJson.warn("未找到指定文件");
}
try {
String fileName = pageFile.getFileName();
File file = new File(pageFile.getFileUrl());
String contentType = Files.probeContentType(file.toPath());
response.setContentType(contentType);
response.setHeader("Content-disposition", "inline;filename=" + URLEncoder.encode(fileName, "UTF-8"));
// response.setHeader("Content-disposition", "inline;filename=" + fileName);
// response.setHeader("Content-Disposition", "inline; fileName=" + fileName + ";filename*=utf-8''" + URLEncoder.encode(fileName, "UTF-8"));
InputStream inputStream = new FileInputStream(file);
OutputStream os = response.getOutputStream();
byte[] b = new byte[2048];
int length;
while ((length = inputStream.read(b)) > 0) {
os.write(b, 0, length);
}
os.close();
inputStream.close();
return null;
} catch (Exception e) {
logger.info("失败:{}", e.getMessage());
}
return DocResponseJson.warn("获取文件失败");
}
}

View File

@@ -8,9 +8,11 @@ import com.zyplayer.doc.data.config.security.DocUserUtil;
import com.zyplayer.doc.data.repository.manage.entity.WikiPage;
import com.zyplayer.doc.data.repository.manage.entity.WikiPageContent;
import com.zyplayer.doc.data.repository.manage.entity.WikiPageFile;
import com.zyplayer.doc.data.repository.manage.entity.WikiPageZan;
import com.zyplayer.doc.data.service.manage.WikiPageContentService;
import com.zyplayer.doc.data.service.manage.WikiPageFileService;
import com.zyplayer.doc.data.service.manage.WikiPageService;
import com.zyplayer.doc.data.service.manage.WikiPageZanService;
import com.zyplayer.doc.wiki.controller.vo.WikiPageContentVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -39,6 +41,8 @@ public class WikiPageController {
WikiPageContentService wikiPageContentService;
@Resource
WikiPageFileService wikiPageFileService;
@Resource
WikiPageZanService wikiPageZanService;
@PostMapping("/list")
public ResponseJson<List<WikiPage>> list(WikiPage wikiPage) {
@@ -54,21 +58,31 @@ public class WikiPageController {
@PostMapping("/detail")
public ResponseJson<WikiPageContentVo> detail(WikiPage wikiPage) {
DocUserDetails currentUser = DocUserUtil.getCurrentUser();
WikiPage wikiPageSel = wikiPageService.getById(wikiPage.getId());
UpdateWrapper<WikiPageContent> wrapper = new UpdateWrapper<>();
wrapper.eq("page_id", wikiPage.getId());
WikiPageContent pageContent = wikiPageContentService.getOne(wrapper);
// TODO 检查space是否开放访问
UpdateWrapper<WikiPageFile> wrapperFile = new UpdateWrapper<>();
wrapperFile.eq("page_id", wikiPage.getId());
wrapper.eq("del_flag", 0);
wrapperFile.eq("del_flag", 0);
List<WikiPageFile> pageFiles = wikiPageFileService.list(wrapperFile);
for (WikiPageFile pageFile : pageFiles) {
pageFile.setFileUrl("zyplayer-doc-wiki/common/file?fileId=" + pageFile.getId());
}
UpdateWrapper<WikiPageZan> wrapperZan = new UpdateWrapper<>();
wrapperZan.eq("page_id", wikiPage.getId());
wrapperZan.eq("create_user_id", currentUser.getUserId());
wrapperZan.eq("yn", 1);
WikiPageZan pageZan = wikiPageZanService.getOne(wrapperZan);
WikiPageContentVo vo = new WikiPageContentVo();
vo.setWikiPage(wikiPageSel);
vo.setPageContent(pageContent);
vo.setFileList(pageFiles);
vo.setSelfZan((pageZan != null) ? 1 : 0);
return DocResponseJson.ok(vo);
}

View File

@@ -1,21 +1,12 @@
package com.zyplayer.doc.wiki.controller;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zyplayer.doc.core.json.DocResponseJson;
import com.zyplayer.doc.core.json.ResponseJson;
import com.zyplayer.doc.data.config.security.DocUserDetails;
import com.zyplayer.doc.data.config.security.DocUserUtil;
import com.zyplayer.doc.data.repository.manage.entity.WikiPageFile;
import com.zyplayer.doc.data.service.manage.WikiPageFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
/**
* 文档控制器
@@ -31,28 +22,31 @@ public class WikiPageFileController {
@Resource
WikiPageFileService wikiPageFileService;
@PostMapping("/list")
public ResponseJson<List<WikiPageFile>> list(WikiPageFile wikiPageFile) {
// TODO 检查space是否开放访问
UpdateWrapper<WikiPageFile> wrapper = new UpdateWrapper<>();
wrapper.eq("del_flag", 0);
wrapper.eq("page_id", wikiPageFile.getPageId());
List<WikiPageFile> fileList = wikiPageFileService.list(wrapper);
return DocResponseJson.ok(fileList);
}
@PostMapping("/update")
public ResponseJson<Object> update(WikiPageFile wikiPageFile) {
Long id = wikiPageFile.getId();
if (id != null && id > 0) {
wikiPageFileService.updateById(wikiPageFile);
} else {
DocUserDetails currentUser = DocUserUtil.getCurrentUser();
wikiPageFile.setCreateTime(new Date());
wikiPageFile.setCreateUserId(currentUser.getUserId());
wikiPageFileService.save(wikiPageFile);
}
return DocResponseJson.ok();
}
// @PostMapping("/list")
// public ResponseJson<List<WikiPageFile>> list(WikiPageFile wikiPageFile) {
// // TODO 检查space是否开放访问
// UpdateWrapper<WikiPageFile> wrapper = new UpdateWrapper<>();
// wrapper.eq("del_flag", 0);
// wrapper.eq("page_id", wikiPageFile.getPageId());
// List<WikiPageFile> fileList = wikiPageFileService.list(wrapper);
// for (WikiPageFile pageFile : fileList) {
// pageFile.setFileUrl("zyplayer-doc-wiki/common/file?fileId=" + pageFile.getId());
// }
// return DocResponseJson.ok(fileList);
// }
//
// @PostMapping("/update")
// public ResponseJson<Object> update(WikiPageFile wikiPageFile) {
// Long id = wikiPageFile.getId();
// if (id != null && id > 0) {
// wikiPageFileService.updateById(wikiPageFile);
// } else {
// DocUserDetails currentUser = DocUserUtil.getCurrentUser();
// wikiPageFile.setCreateTime(new Date());
// wikiPageFile.setCreateUserId(currentUser.getUserId());
// wikiPageFileService.save(wikiPageFile);
// }
// return DocResponseJson.ok();
// }
}

View File

@@ -1,10 +1,7 @@
package com.zyplayer.doc.wiki.controller;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zyplayer.doc.core.json.DocResponseJson;
import com.zyplayer.doc.core.json.ResponseJson;
import com.zyplayer.doc.data.config.security.DocUserDetails;
import com.zyplayer.doc.data.config.security.DocUserUtil;
import com.zyplayer.doc.data.repository.manage.entity.WikiPageZan;
import com.zyplayer.doc.data.service.manage.WikiPageZanService;
import org.slf4j.Logger;
@@ -14,8 +11,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
/**
* 文档控制器
@@ -31,29 +26,18 @@ public class WikiPageZanController {
@Resource
WikiPageZanService wikiPageZanService;
@PostMapping("/list")
public ResponseJson<List<WikiPageZan>> list(WikiPageZan wikiPageZan) {
UpdateWrapper<WikiPageZan> wrapper = new UpdateWrapper<>();
wrapper.eq("page_id", wikiPageZan.getPageId());
wrapper.eq(wikiPageZan.getCommentId() != null, "comment_id", wikiPageZan.getCommentId());
List<WikiPageZan> zanList = wikiPageZanService.list(wrapper);
return DocResponseJson.ok(zanList);
}
// @PostMapping("/list")
// public ResponseJson<List<WikiPageZan>> list(WikiPageZan wikiPageZan) {
// UpdateWrapper<WikiPageZan> wrapper = new UpdateWrapper<>();
// wrapper.eq("page_id", wikiPageZan.getPageId());
// wrapper.eq(wikiPageZan.getCommentId() != null, "comment_id", wikiPageZan.getCommentId());
// List<WikiPageZan> zanList = wikiPageZanService.list(wrapper);
// return DocResponseJson.ok(zanList);
// }
@PostMapping("/update")
public ResponseJson<Object> update(WikiPageZan wikiPageZan) {
DocUserDetails currentUser = DocUserUtil.getCurrentUser();
UpdateWrapper<WikiPageZan> wrapper = new UpdateWrapper<>();
wrapper.eq("create_uid", currentUser.getUserId());
wrapper.eq("page_id", wikiPageZan.getPageId());
wrapper.eq(wikiPageZan.getCommentId() != null, "comment_id", wikiPageZan.getCommentId());
WikiPageZan pageZan = wikiPageZanService.getOne(wrapper);
if (pageZan != null) {
return DocResponseJson.warn("您已经赞过了哦~");
}
wikiPageZan.setCreateTime(new Date());
wikiPageZan.setCreateUserId(currentUser.getUserId());
wikiPageZanService.save(wikiPageZan);
wikiPageZanService.zanPage(wikiPageZan);
return DocResponseJson.ok();
}
}

View File

@@ -40,9 +40,9 @@ public class WikiSpaceController {
wrapper.in("type", 1, 2);
} else if(wikiSpace.getType() == 1 || wikiSpace.getType() == 2) {
wrapper.eq(wikiSpace.getType() != null, "type", wikiSpace.getType());
wrapper.eq(Objects.equals(wikiSpace.getType(), 2), "create_uid", wikiSpace.getCreateUserId());
wrapper.eq(Objects.equals(wikiSpace.getType(), 2), "create_user_id", wikiSpace.getCreateUserId());
} else if(wikiSpace.getType() == 3) {
wrapper.eq("create_uid", currentUser.getUserId());
wrapper.eq("create_user_id", currentUser.getUserId());
}
List<WikiSpace> authList = wikiSpaceService.list(wrapper);
return DocResponseJson.ok(authList);

View File

@@ -10,6 +10,7 @@ public class WikiPageContentVo {
private WikiPage wikiPage;
private WikiPageContent pageContent;
private List<WikiPageFile> fileList;
private Integer selfZan;
public WikiPage getWikiPage() {
return wikiPage;
@@ -34,4 +35,12 @@ public class WikiPageContentVo {
public void setFileList(List<WikiPageFile> fileList) {
this.fileList = fileList;
}
public Integer getSelfZan() {
return selfZan;
}
public void setSelfZan(Integer selfZan) {
this.selfZan = selfZan;
}
}