MiniController.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package com.cyksj.web.controller.mini;
  2. import com.cyksj.common.exception.BusinessRuntimeException;
  3. import com.cyksj.common.util.IoKit;
  4. import com.cyksj.common.util.StringUtil;
  5. import com.cyksj.dto.Result;
  6. import com.cyksj.enums.GatewayResponse;
  7. import com.cyksj.model.entity.User;
  8. import com.cyksj.model.request.MiniArticleReq;
  9. import com.cyksj.model.request.MiniProgramLoginPost;
  10. import com.cyksj.model.request.MiniSchemaReq;
  11. import com.cyksj.model.views.ArticleDetailView;
  12. import com.cyksj.model.views.ArticleFrontView;
  13. import com.cyksj.model.views.ArticleSelfFrontView;
  14. import com.cyksj.service.mini.MiniService;
  15. import com.cyksj.web.util.StpUserUtil;
  16. import com.ejlchina.searcher.BeanSearcher;
  17. import com.ejlchina.searcher.SearchResult;
  18. import com.ejlchina.searcher.util.MapBuilder;
  19. import com.ejlchina.searcher.util.MapUtils;
  20. import lombok.RequiredArgsConstructor;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.apache.commons.lang3.StringUtils;
  23. import org.springframework.validation.annotation.Validated;
  24. import org.springframework.web.bind.annotation.*;
  25. import javax.servlet.http.HttpServletRequest;
  26. import javax.servlet.http.HttpServletResponse;
  27. import java.io.OutputStream;
  28. /*
  29. *项目名: netflix
  30. *文件名: MiniController
  31. *创建者: JavaZou
  32. *创建时间:2023/5/11 18:03
  33. */
  34. @RestController
  35. @RequestMapping("/applets/mini")
  36. @RequiredArgsConstructor
  37. @Slf4j
  38. public class MiniController {
  39. private final MiniService miniService;
  40. private final BeanSearcher beanSearcher;
  41. private final HttpServletRequest request;
  42. /**
  43. * 生成小程序跳转链接二维码
  44. */
  45. @RequestMapping(value = "/linkUrl", method = RequestMethod.GET)
  46. public Result<String> getMiniLinkURL(String path, String query) throws Exception {
  47. String linkUrl = miniService.getMiniURLLink(path, query);
  48. return GatewayResponse.SUCCESS.newBuilder().toResult(linkUrl);
  49. }
  50. /**
  51. * 生成小程序schema
  52. */
  53. @GetMapping("/generate/schema")
  54. public Result<String> generateSchema(String dsCode) throws Exception {
  55. MiniSchemaReq req = new MiniSchemaReq();
  56. req.setDsCode(dsCode);
  57. String url = miniService.generateSchema(req);
  58. return GatewayResponse.SUCCESS.newBuilder().toResult(url);
  59. }
  60. /**
  61. * 生成小程序code
  62. */
  63. @GetMapping("/generate/code")
  64. public void generateCode(String dsCode, HttpServletResponse response) throws Exception {
  65. MiniSchemaReq req = new MiniSchemaReq();
  66. req.setDsCode(dsCode);
  67. byte [] s = miniService.generateCode(req);
  68. response.reset();
  69. // 若直接在浏览器预览 需要注释下面的response header
  70. //response.addHeader("Content-Disposition", "attachment;filename=share.jpeg");
  71. response.setContentType("image/jpeg");
  72. // 处理文件 返回流
  73. OutputStream out = null;
  74. try {
  75. out = response.getOutputStream();
  76. IoKit.write(s, out);
  77. } catch (Throwable e) {
  78. throw BusinessRuntimeException.getInstance(StringUtil.getErrorMsg(e));
  79. } finally {
  80. IoKit.close(out);
  81. }
  82. }
  83. /**
  84. * 银河小程序 静默授权登录
  85. */
  86. @PostMapping("/user/login")
  87. public Result<String> userLogin(@RequestBody MiniProgramLoginPost miniDataParams) throws Exception {
  88. String methodName = "银河小程序静默授权";
  89. log.info("{}[start] params:{}", methodName, miniDataParams);
  90. if (StringUtils.isBlank(miniDataParams.getCode())) {
  91. throw BusinessRuntimeException.getInstance("需要登录凭证code.");
  92. }
  93. User user = miniService.wxlogin(miniDataParams.getCode(), miniDataParams.getAppId());
  94. StpUserUtil.login(user.getId());
  95. log.info("{}[end] uuid:{}", methodName, user.getId());
  96. return GatewayResponse.SUCCESS.newBuilder().toResult(StpUserUtil.getTokenValue());
  97. }
  98. /**
  99. * 文章列表
  100. */
  101. @GetMapping("/get/article")
  102. public Result<SearchResult<ArticleFrontView>> getArticleFrontView(@RequestParam(defaultValue = "0") Boolean login) {
  103. Long userId = null;
  104. if (login) {
  105. userId = StpUserUtil.getLoginIdAsLong();
  106. }
  107. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  108. if (userId != null) builder.put("user_id", String.format("and aum2.user_id = %s", userId));
  109. SearchResult<ArticleFrontView> search = beanSearcher.search(ArticleFrontView.class, builder
  110. .orderBy(ArticleFrontView::getReleaseTime).desc()
  111. .build());
  112. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  113. }
  114. /**
  115. * 文章标记
  116. */
  117. @PostMapping("/mark/article")
  118. public Result<String> markArticle(@RequestBody @Validated MiniArticleReq miniArticleReq) {
  119. Integer type = miniArticleReq.getType();
  120. long userId = StpUserUtil.getLoginIdAsLong();
  121. if (type < 0 || type > 2) {
  122. return GatewayResponse.SUCCESS.newBuilder().toResult();
  123. }
  124. miniArticleReq.setUserId(userId);
  125. return miniService.markArticle(miniArticleReq);
  126. }
  127. /**
  128. * 文章筛选自身标记列表
  129. */
  130. @GetMapping("/get/mark/article")
  131. public Result<SearchResult<ArticleSelfFrontView>> getArticleByType(@RequestParam(defaultValue = "1") Integer type) {
  132. long userId = StpUserUtil.getLoginIdAsLong();
  133. if (type < 0 || type > 2) {
  134. return GatewayResponse.SUCCESS.newBuilder().toResult();
  135. }
  136. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  137. String conditionSql = String.format(" where user_id = %s", userId);
  138. if (type == 1) {
  139. conditionSql += " and is_dz is true";
  140. }
  141. if (type == 2) {
  142. conditionSql += " and is_col is true";
  143. }
  144. builder.put("condition", conditionSql);
  145. SearchResult<ArticleSelfFrontView> search = beanSearcher.search(ArticleSelfFrontView.class, builder.build());
  146. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  147. }
  148. /**
  149. * 文章详情
  150. */
  151. @GetMapping("/get/article/detail/{articleId}")
  152. public Result<ArticleDetailView> getArticleDetailView(@PathVariable Long articleId, @RequestParam(defaultValue = "0") Boolean login) {
  153. Long userId = null;
  154. if (login) {
  155. userId = StpUserUtil.getLoginIdAsLong();
  156. }
  157. ArticleDetailView view = miniService.getArticleDetailView(articleId, userId);
  158. return GatewayResponse.SUCCESS.newBuilder().toResult(view);
  159. }
  160. }