Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

176 righe
7.8 KiB

  1. package com.iformall.controller;
  2. import cn.binarywang.wx.miniapp.api.WxMaService;
  3. import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateAddResult;
  4. import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryGetResult;
  5. import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryListResult;
  6. import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateListResult;
  7. import com.iformall.common.ErrorCode;
  8. import com.iformall.common.Result;
  9. import com.iformall.common.ResultData;
  10. import com.iformall.domain.po.WxAppinfo;
  11. import com.iformall.domain.po.WxAuthorizerInfo;
  12. import com.iformall.domain.po.WxTemplateMsg;
  13. import com.iformall.enums.EnumWxAuthorizationStatus;
  14. import com.iformall.service.WxAppinfoService;
  15. import com.iformall.service.WxAuthorizerInfoService;
  16. import com.iformall.service.WxTemplateMsgService;
  17. import com.iformall.service.wechat.FmOpenService;
  18. import io.swagger.annotations.Api;
  19. import io.swagger.annotations.ApiImplicitParam;
  20. import io.swagger.annotations.ApiImplicitParams;
  21. import io.swagger.annotations.ApiOperation;
  22. import me.chanjar.weixin.common.error.WxErrorException;
  23. import org.apache.commons.lang3.StringUtils;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.web.bind.annotation.*;
  28. import java.util.Date;
  29. import java.util.List;
  30. import java.util.Map;
  31. /**
  32. * Stormeye Wu
  33. */
  34. @RestController
  35. @RequestMapping("/weappMsgTemplate")
  36. @Api(description = "微信第三方开发平台-小程序-消息模板设置")
  37. public class WechatWeappMsgTemplateController {
  38. private final Logger logger = LoggerFactory.getLogger(getClass());
  39. @Autowired
  40. private FmOpenService openService;
  41. @Autowired
  42. private WxAuthorizerInfoService authorizerInfoService;
  43. @Autowired
  44. private WxAppinfoService appinfoService;
  45. @Autowired
  46. private WxTemplateMsgService templateMsgService;
  47. @ApiOperation("获取小程序模板库标题列表")
  48. @GetMapping("getTmpLibList")
  49. @ApiImplicitParams({
  50. @ApiImplicitParam(name = "appId", value = "appId", dataType = "String", paramType = "query", required = true),
  51. @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
  52. @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
  53. public ResultData getTmpLibList(String appId, Integer pageNum, Integer pageSize) {
  54. try {
  55. WxMaService maService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId);
  56. WxMaTemplateLibraryListResult ret = maService.getTemplateService().findTemplateLibraryList(pageNum, pageSize);
  57. return new ResultData(ret);
  58. } catch (WxErrorException e) {
  59. logger.error(e.getMessage());
  60. return new ResultData(Result.ERROR, e.getMessage());
  61. }
  62. }
  63. @ApiOperation("获取模板库某个模板标题下关键词库")
  64. @GetMapping("getTemplateLibraryKeywordList")
  65. @ApiImplicitParams({
  66. @ApiImplicitParam(name = "appId", value = "appId", dataType = "String", paramType = "query", required = true),
  67. @ApiImplicitParam(name = "id", value = "模板标题id", dataType = "String", paramType = "query", required = true)})
  68. public ResultData findTemplateLibraryKeywordList(String appId, String id) {
  69. try {
  70. WxMaService maService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId);
  71. WxMaTemplateLibraryGetResult ret = maService.getTemplateService().findTemplateLibraryKeywordList(id);
  72. return new ResultData(ret);
  73. } catch (WxErrorException e) {
  74. logger.error(e.getMessage());
  75. return new ResultData(Result.ERROR, e.getMessage());
  76. }
  77. }
  78. @ApiOperation("组合模板并添加至帐号下的个人模板库")
  79. @PostMapping("addTemplate")
  80. public ResultData addTemplate(String appId, String id, List<Integer> keywordIdList) {
  81. WxAuthorizerInfo authorizerInfo = authorizerInfoService.getByAppId(appId);
  82. if(authorizerInfo == null) {
  83. return new ResultData(ErrorCode.WEAPP_APPID_NOT_AUTH);
  84. }
  85. if(!authorizerInfo.getAuthorizationStatus().equals(EnumWxAuthorizationStatus.AUTHORIZED.getCode())) {
  86. return new ResultData(ErrorCode.WEAPP_APPID_NOT_AUTH);
  87. }
  88. try {
  89. WxMaService maService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId);
  90. WxMaTemplateAddResult ret = maService.getTemplateService().addTemplate(id, keywordIdList);
  91. authorizerInfo.setTemplateStatus(0);
  92. authorizerInfo.setTemplateTime(new Date());
  93. authorizerInfo.setUpdateTime(new Date());
  94. authorizerInfoService.updateTemplate(authorizerInfo);
  95. return new ResultData(ret);
  96. } catch (WxErrorException e) {
  97. logger.error(e.getMessage());
  98. authorizerInfo.setTemplateStatus(1);
  99. authorizerInfo.setTemplateTime(new Date());
  100. authorizerInfo.setUpdateTime(new Date());
  101. authorizerInfoService.updateTemplate(authorizerInfo);
  102. return new ResultData(Result.ERROR, e.getMessage());
  103. }
  104. }
  105. @ApiOperation("获取帐号下已存在的模板列表")
  106. @GetMapping("getTemplateList")
  107. @ApiImplicitParams({
  108. @ApiImplicitParam(name = "appId", value = "appId", dataType = "String", paramType = "query", required = true),
  109. @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
  110. @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
  111. public ResultData findTemplateList(String appId, Integer pageNum, Integer pageSize) {
  112. try {
  113. WxMaService maService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId);
  114. if(pageNum > 0) {
  115. pageNum = pageNum - 1;
  116. }
  117. WxMaTemplateListResult ret = maService.getTemplateService().findTemplateList(pageNum, pageSize);
  118. logger.info(ret.toString());
  119. return new ResultData(ret);
  120. } catch (WxErrorException e) {
  121. logger.error(e.getMessage());
  122. return new ResultData(Result.ERROR, e.getMessage());
  123. }
  124. }
  125. @ApiOperation("删除帐号下的某个模板")
  126. @PostMapping("delTemplate")
  127. public ResultData delTemplate(@RequestBody Map<String, String> params) {
  128. String appId = params.get("appId");
  129. String templateId = params.get("templateId");
  130. if(StringUtils.isBlank(appId)) {
  131. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
  132. }
  133. if(StringUtils.isBlank(templateId)) {
  134. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
  135. }
  136. // check app
  137. WxAppinfo appinfo = appinfoService.getByAppId(appId);
  138. if(appinfo == null) {
  139. String msg = "此appId未纳入管理,请联系管理员";
  140. logger.error(msg);
  141. return new ResultData(Result.ERROR, msg);
  142. }
  143. WxTemplateMsg msgQ = new WxTemplateMsg();
  144. msgQ.setTemplateId(templateId);
  145. msgQ.setTenantId(appinfo.getTenantId());
  146. WxTemplateMsg msg = templateMsgService.getByObj(msgQ);
  147. if(msg != null) {
  148. String msgStr = "此模板正被使用,请不要删除";
  149. logger.error(msgStr);
  150. return new ResultData(Result.ERROR, msgStr);
  151. }
  152. try {
  153. WxMaService maService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId);
  154. maService.getTemplateService().delTemplate(templateId);
  155. return new ResultData();
  156. } catch (WxErrorException e) {
  157. logger.error(e.getMessage());
  158. return new ResultData(Result.ERROR, e.getMessage());
  159. }
  160. }
  161. }