BackendArticleController.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 org.springframework.beans.factory.annotation.Autowired;
  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. @RequestMapping(value = "/backendApi/article")
  36. public class BackendArticleController extends BaseController {
  37. /**
  38. * 文章服务接口
  39. */
  40. @Autowired
  41. private ArticleService articleService;
  42. /**
  43. * 系统设置服务接口
  44. * */
  45. @Autowired
  46. private SettingService settingService;
  47. /**
  48. * 店铺服务接口
  49. */
  50. @Autowired
  51. private StoreService storeService;
  52. /**
  53. * 文章列表查询
  54. *
  55. * @param request HttpServletRequest对象
  56. * @return 文章列表
  57. */
  58. @ApiOperation(value = "文章列表查询")
  59. @RequestMapping(value = "/list", method = RequestMethod.GET)
  60. @CrossOrigin
  61. @PreAuthorize("@pms.hasPermission('content:article:index')")
  62. public ResponseObject list(HttpServletRequest request) throws BusinessCheckException {
  63. String token = request.getHeader("Access-Token");
  64. Integer page = request.getParameter("page") == null ? Constants.PAGE_NUMBER : Integer.parseInt(request.getParameter("page"));
  65. Integer pageSize = request.getParameter("pageSize") == null ? Constants.PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize"));
  66. String title = request.getParameter("title");
  67. String status = request.getParameter("status");
  68. String searchStoreId = request.getParameter("storeId");
  69. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  70. Integer storeId;
  71. if (accountInfo == null) {
  72. return getFailureResult(1001, "请先登录");
  73. } else {
  74. storeId = accountInfo.getStoreId();
  75. }
  76. PaginationRequest paginationRequest = new PaginationRequest();
  77. paginationRequest.setCurrentPage(page);
  78. paginationRequest.setPageSize(pageSize);
  79. Map<String, Object> params = new HashMap<>();
  80. if (StringUtil.isNotEmpty(title)) {
  81. params.put("title", title);
  82. }
  83. if (StringUtil.isNotEmpty(status)) {
  84. params.put("status", status);
  85. }
  86. if (StringUtil.isNotEmpty(searchStoreId)) {
  87. params.put("storeId", searchStoreId);
  88. }
  89. if (storeId != null && storeId > 0) {
  90. params.put("storeId", storeId);
  91. }
  92. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  93. params.put("merchantId", accountInfo.getMerchantId());
  94. }
  95. paginationRequest.setSearchParams(params);
  96. PaginationResponse<ArticleDto> paginationResponse = articleService.queryArticleListByPagination(paginationRequest);
  97. String imagePath = settingService.getUploadBasePath();
  98. Map<String, Object> paramsStore = new HashMap<>();
  99. paramsStore.put("status", StatusEnum.ENABLED.getKey());
  100. if (accountInfo.getStoreId() != null && accountInfo.getStoreId() > 0) {
  101. paramsStore.put("storeId", accountInfo.getStoreId().toString());
  102. }
  103. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  104. paramsStore.put("merchantId", accountInfo.getMerchantId());
  105. }
  106. List<MtStore> storeList = storeService.queryStoresByParams(paramsStore);
  107. Map<String, Object> result = new HashMap<>();
  108. result.put("dataList", paginationResponse);
  109. result.put("imagePath", imagePath);
  110. result.put("storeList", storeList);
  111. return getSuccessResult(result);
  112. }
  113. /**
  114. * 更新文章状态
  115. *
  116. * @return
  117. */
  118. @ApiOperation(value = "更新文章状态")
  119. @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
  120. @CrossOrigin
  121. @PreAuthorize("@pms.hasPermission('content:article:edit')")
  122. public ResponseObject updateStatus(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
  123. String token = request.getHeader("Access-Token");
  124. String status = params.get("status") != null ? params.get("status").toString() : StatusEnum.ENABLED.getKey();
  125. Integer id = params.get("id") == null ? 0 : Integer.parseInt(params.get("id").toString());
  126. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  127. if (accountInfo == null) {
  128. return getFailureResult(1001, "请先登录");
  129. }
  130. MtArticle mtArticle = articleService.queryArticleById(id);
  131. if (mtArticle == null) {
  132. return getFailureResult(201);
  133. }
  134. String operator = accountInfo.getAccountName();
  135. ArticleDto article = new ArticleDto();
  136. article.setOperator(operator);
  137. article.setId(id);
  138. article.setStatus(status);
  139. articleService.updateArticle(article);
  140. return getSuccessResult(true);
  141. }
  142. /**
  143. * 保存文章
  144. *
  145. * @param request HttpServletRequest对象
  146. * @return
  147. */
  148. @ApiOperation(value = "保存文章")
  149. @RequestMapping(value = "/save", method = RequestMethod.POST)
  150. @CrossOrigin
  151. @PreAuthorize("@pms.hasPermission('content:article:add')")
  152. public ResponseObject saveHandler(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
  153. String token = request.getHeader("Access-Token");
  154. String id = params.get("id") == null ? "" : params.get("id").toString();
  155. String title = params.get("title") == null ? "" : params.get("title").toString();
  156. String description = params.get("description") == null ? "" : params.get("description").toString();
  157. String image = params.get("image") == null ? "" : params.get("image").toString();
  158. String url = params.get("url") == null ? "" : params.get("url").toString();
  159. String status = params.get("status") == null ? "" : params.get("status").toString();
  160. String storeId = params.get("storeId") == null ? "0" : params.get("storeId").toString();
  161. String sort = params.get("sort") == null ? "0" : params.get("sort").toString();
  162. String brief = params.get("brief") == null ? "" : params.get("brief").toString();
  163. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  164. if (accountInfo == null) {
  165. return getFailureResult(1001, "请先登录");
  166. }
  167. ArticleDto info = new ArticleDto();
  168. info.setTitle(title);
  169. info.setDescription(description);
  170. info.setImage(image);
  171. info.setUrl(url);
  172. info.setOperator(accountInfo.getAccountName());
  173. info.setStatus(status);
  174. info.setStoreId(Integer.parseInt(storeId));
  175. info.setSort(Integer.parseInt(sort));
  176. info.setBrief(brief);
  177. if (StringUtil.isNotEmpty(id)) {
  178. info.setId(Integer.parseInt(id));
  179. articleService.updateArticle(info);
  180. } else {
  181. articleService.addArticle(info);
  182. }
  183. return getSuccessResult(true);
  184. }
  185. /**
  186. * 获取文章详情
  187. *
  188. * @param id
  189. * @return
  190. */
  191. @ApiOperation(value = "获取文章详情")
  192. @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
  193. @CrossOrigin
  194. @PreAuthorize("@pms.hasPermission('content:article:index')")
  195. public ResponseObject info(HttpServletRequest request, @PathVariable("id") Integer id) throws BusinessCheckException {
  196. String token = request.getHeader("Access-Token");
  197. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  198. if (accountInfo == null) {
  199. return getFailureResult(1001, "请先登录");
  200. }
  201. MtArticle articleInfo = articleService.queryArticleById(id);
  202. String imagePath = settingService.getUploadBasePath();
  203. Map<String, Object> result = new HashMap<>();
  204. result.put("articleInfo", articleInfo);
  205. result.put("imagePath", imagePath);
  206. return getSuccessResult(result);
  207. }
  208. }