BackendArticleController.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package com.fuint.module.backendApi.controller;
  2. import com.fuint.common.dto.AccountInfo;
  3. import com.fuint.common.dto.ArticleDto;
  4. import com.fuint.common.service.ArticleService;
  5. import com.fuint.common.service.StoreService;
  6. import com.fuint.common.util.TokenUtil;
  7. import com.fuint.framework.web.BaseController;
  8. import com.fuint.framework.web.ResponseObject;
  9. import com.fuint.common.Constants;
  10. import com.fuint.common.enums.StatusEnum;
  11. import com.fuint.common.service.SettingService;
  12. import com.fuint.framework.pagination.PaginationRequest;
  13. import com.fuint.framework.pagination.PaginationResponse;
  14. import com.fuint.framework.exception.BusinessCheckException;
  15. import com.fuint.repository.model.MtArticle;
  16. import com.fuint.repository.model.MtStore;
  17. import com.fuint.utils.StringUtil;
  18. import io.swagger.annotations.Api;
  19. import io.swagger.annotations.ApiOperation;
  20. import lombok.AllArgsConstructor;
  21. import org.springframework.security.access.prepost.PreAuthorize;
  22. import org.springframework.web.bind.annotation.*;
  23. import javax.servlet.http.HttpServletRequest;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. /**
  28. * 文章管理类controller
  29. *
  30. * Created by FSQ
  31. * CopyRight https://www.fuint.cn
  32. */
  33. @Api(tags="管理端-文章相关接口")
  34. @RestController
  35. @AllArgsConstructor
  36. @RequestMapping(value = "/backendApi/article")
  37. public class BackendArticleController extends BaseController {
  38. /**
  39. * 文章服务接口
  40. */
  41. private ArticleService articleService;
  42. /**
  43. * 系统设置服务接口
  44. * */
  45. private SettingService settingService;
  46. /**
  47. * 店铺服务接口
  48. */
  49. private StoreService storeService;
  50. /**
  51. * 文章列表查询
  52. */
  53. @ApiOperation(value = "文章列表查询")
  54. @RequestMapping(value = "/list", method = RequestMethod.GET)
  55. @CrossOrigin
  56. @PreAuthorize("@pms.hasPermission('content:article:index')")
  57. public ResponseObject list(HttpServletRequest request) throws BusinessCheckException {
  58. String token = request.getHeader("Access-Token");
  59. Integer page = request.getParameter("page") == null ? Constants.PAGE_NUMBER : Integer.parseInt(request.getParameter("page"));
  60. Integer pageSize = request.getParameter("pageSize") == null ? Constants.PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize"));
  61. String title = request.getParameter("title");
  62. String status = request.getParameter("status");
  63. String searchStoreId = request.getParameter("storeId");
  64. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  65. Integer storeId = accountInfo.getStoreId();;
  66. PaginationRequest paginationRequest = new PaginationRequest();
  67. paginationRequest.setCurrentPage(page);
  68. paginationRequest.setPageSize(pageSize);
  69. Map<String, Object> params = new HashMap<>();
  70. if (StringUtil.isNotEmpty(title)) {
  71. params.put("title", title);
  72. }
  73. if (StringUtil.isNotEmpty(status)) {
  74. params.put("status", status);
  75. }
  76. if (StringUtil.isNotEmpty(searchStoreId)) {
  77. params.put("storeId", searchStoreId);
  78. }
  79. if (storeId != null && storeId > 0) {
  80. params.put("storeId", storeId);
  81. }
  82. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  83. params.put("merchantId", accountInfo.getMerchantId());
  84. }
  85. paginationRequest.setSearchParams(params);
  86. PaginationResponse<ArticleDto> paginationResponse = articleService.queryArticleListByPagination(paginationRequest);
  87. List<MtStore> storeList = storeService.getMyStoreList(accountInfo.getMerchantId(), accountInfo.getStoreId(), StatusEnum.ENABLED.getKey());
  88. Map<String, Object> result = new HashMap<>();
  89. result.put("dataList", paginationResponse);
  90. result.put("imagePath", settingService.getUploadBasePath());
  91. result.put("storeList", storeList);
  92. return getSuccessResult(result);
  93. }
  94. /**
  95. * 更新文章状态
  96. */
  97. @ApiOperation(value = "更新文章状态")
  98. @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
  99. @CrossOrigin
  100. @PreAuthorize("@pms.hasPermission('content:article:edit')")
  101. public ResponseObject updateStatus(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
  102. String token = request.getHeader("Access-Token");
  103. String status = params.get("status") != null ? params.get("status").toString() : StatusEnum.ENABLED.getKey();
  104. Integer id = params.get("id") == null ? 0 : Integer.parseInt(params.get("id").toString());
  105. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  106. MtArticle mtArticle = articleService.queryArticleById(id);
  107. if (mtArticle == null) {
  108. return getFailureResult(201);
  109. }
  110. ArticleDto article = new ArticleDto();
  111. article.setOperator(accountInfo.getAccountName());
  112. article.setId(id);
  113. article.setStatus(status);
  114. articleService.updateArticle(article);
  115. return getSuccessResult(true);
  116. }
  117. /**
  118. * 保存文章
  119. */
  120. @ApiOperation(value = "保存文章")
  121. @RequestMapping(value = "/save", method = RequestMethod.POST)
  122. @CrossOrigin
  123. @PreAuthorize("@pms.hasPermission('content:article:add')")
  124. public ResponseObject saveHandler(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
  125. String token = request.getHeader("Access-Token");
  126. String id = params.get("id") == null ? "" : params.get("id").toString();
  127. String title = params.get("title") == null ? "" : params.get("title").toString();
  128. String description = params.get("description") == null ? "" : params.get("description").toString();
  129. String image = params.get("image") == null ? "" : params.get("image").toString();
  130. String url = params.get("url") == null ? "" : params.get("url").toString();
  131. String status = params.get("status") == null ? "" : params.get("status").toString();
  132. String storeId = params.get("storeId") == null ? "0" : params.get("storeId").toString();
  133. String sort = params.get("sort") == null ? "0" : params.get("sort").toString();
  134. String brief = params.get("brief") == null ? "" : params.get("brief").toString();
  135. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  136. ArticleDto articleDto = new ArticleDto();
  137. articleDto.setTitle(title);
  138. articleDto.setDescription(description);
  139. articleDto.setImage(image);
  140. articleDto.setUrl(url);
  141. articleDto.setOperator(accountInfo.getAccountName());
  142. articleDto.setStatus(status);
  143. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  144. articleDto.setMerchantId(accountInfo.getMerchantId());
  145. }
  146. if (accountInfo.getStoreId() != null && accountInfo.getStoreId() > 0) {
  147. storeId = accountInfo.getStoreId().toString();
  148. }
  149. if (StringUtil.isNotEmpty(storeId)) {
  150. articleDto.setStoreId(Integer.parseInt(storeId));
  151. }
  152. articleDto.setSort(Integer.parseInt(sort));
  153. articleDto.setBrief(brief);
  154. if (StringUtil.isNotEmpty(id)) {
  155. articleDto.setId(Integer.parseInt(id));
  156. articleService.updateArticle(articleDto);
  157. } else {
  158. articleService.addArticle(articleDto);
  159. }
  160. return getSuccessResult(true);
  161. }
  162. /**
  163. * 获取文章详情
  164. */
  165. @ApiOperation(value = "获取文章详情")
  166. @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
  167. @CrossOrigin
  168. @PreAuthorize("@pms.hasPermission('content:article:index')")
  169. public ResponseObject info(HttpServletRequest request, @PathVariable("id") Integer id) throws BusinessCheckException {
  170. String token = request.getHeader("Access-Token");
  171. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  172. MtArticle articleInfo = articleService.queryArticleById(id);
  173. String imagePath = settingService.getUploadBasePath();
  174. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  175. if (!articleInfo.getMerchantId().equals(accountInfo.getMerchantId())) {
  176. return getFailureResult(1004);
  177. }
  178. }
  179. Map<String, Object> result = new HashMap<>();
  180. result.put("articleInfo", articleInfo);
  181. result.put("imagePath", imagePath);
  182. return getSuccessResult(result);
  183. }
  184. }