You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
10 KiB

  1. package com.iformall.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.iformall.common.ErrorCode;
  5. import com.iformall.common.Result;
  6. import com.iformall.common.ResultData;
  7. import com.iformall.domain.po.WxAuthorizerInfo;
  8. import com.iformall.enums.EnumEnableType;
  9. import com.iformall.enums.EnumWxAuthorizationStatus;
  10. import com.iformall.service.WxAuthorizerInfoService;
  11. import com.iformall.service.wechat.FmOpenService;
  12. import io.swagger.annotations.Api;
  13. import io.swagger.annotations.ApiOperation;
  14. import me.chanjar.weixin.common.error.WxErrorException;
  15. import me.chanjar.weixin.open.api.WxOpenMaService;
  16. import me.chanjar.weixin.open.bean.result.*;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.web.bind.annotation.*;
  22. import java.util.Date;
  23. import java.util.List;
  24. import java.util.Map;
  25. /**
  26. * Stormeye Wu
  27. */
  28. @RestController
  29. @RequestMapping("/weappSet")
  30. @Api(description = "微信第三方开发平台-小程序设置")
  31. public class WechatWeappSetController {
  32. private final Logger logger = LoggerFactory.getLogger(getClass());
  33. @Autowired
  34. private FmOpenService openService;
  35. @Autowired
  36. private WxAuthorizerInfoService authorizerInfoService;
  37. @ApiOperation("获取小程序的信息")
  38. @GetMapping("/getInfo")
  39. public ResultData getAccountBasicInfo(String appId) {
  40. try {
  41. WxOpenMaService openMaService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId);
  42. String openRet = openMaService.getAccountBasicInfo();
  43. logger.info(openRet.toString());
  44. return new ResultData(openRet);
  45. } catch (WxErrorException e) {
  46. logger.error(e.getMessage());
  47. }
  48. return new ResultData(Result.ERROR);
  49. }
  50. @ApiOperation("获取小程序服务器域名")
  51. @GetMapping("/getDomain")
  52. public ResultData getDomain(String appId) {
  53. try {
  54. WxOpenMaService openMaService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId);
  55. WxOpenResult openRet = openMaService.getDomain();
  56. logger.info(openRet.toString());
  57. return new ResultData(openRet);
  58. } catch (WxErrorException e) {
  59. logger.error(e.getMessage());
  60. return new ResultData(Result.ERROR, e.getMessage());
  61. }
  62. }
  63. @ApiOperation("获取小程序服务器域名")
  64. @GetMapping("/getWebDomain")
  65. public ResultData getWebDomain(String appId) {
  66. try {
  67. WxOpenMaService openMaService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId);
  68. String openRet = openMaService.getWebViewDomain();
  69. logger.info(openRet);
  70. return new ResultData(openRet);
  71. } catch (WxErrorException e) {
  72. logger.error(e.getMessage());
  73. return new ResultData(Result.ERROR, e.getMessage());
  74. }
  75. }
  76. @ApiOperation(value = "设置小程序服务器域名", notes = "{\n" +
  77. " \"action\":\"add/set/delete/get\",\n" +
  78. " \"requestdomain\":[\"https://www.qq.com\",\"https://www.qq.com\"],\n" +
  79. " \"wsrequestdomain\":[\"wss://www.qq.com\",\"wss://www.qq.com\"],\n" +
  80. " \"uploaddomain\":[\"https://www.qq.com\",\"https://www.qq.com\"],\n" +
  81. " \"downloaddomain\":[\"https://www.qq.com\",\"https://www.qq.com\"],\n" +
  82. " }")
  83. @PostMapping("/modifyDomain")
  84. public ResultData modifyDomain(String appId, String action,
  85. List<String> requestDomain, List<String> wsrequestdomain,
  86. List<String> uploaddomain, List<String> downloaddomain) {
  87. WxAuthorizerInfo authorizerInfo = authorizerInfoService.getByAppId(appId);
  88. if(authorizerInfo == null) {
  89. return new ResultData(ErrorCode.WEAPP_APPID_NOT_AUTH);
  90. }
  91. if(!authorizerInfo.getAuthorizationStatus().equals(EnumWxAuthorizationStatus.AUTHORIZED.getCode())) {
  92. return new ResultData(ErrorCode.WEAPP_APPID_NOT_AUTH);
  93. }
  94. try {
  95. WxOpenMaService openMaService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId);
  96. WxOpenMaDomainResult openRet = openMaService.modifyDomain(action,
  97. requestDomain, wsrequestdomain,
  98. uploaddomain, downloaddomain);
  99. logger.info(openRet.toString());
  100. if(openRet.isSuccess()) {
  101. logger.info("设置服务器域名成功");
  102. authorizerInfo.setDomainStatus(EnumEnableType.SUCC.getCode());
  103. authorizerInfo.setDomainTime(new Date());
  104. authorizerInfo.setUpdateTime(new Date());
  105. } else {
  106. logger.info("设置服务器域名成功");
  107. authorizerInfo.setDomainStatus(EnumEnableType.FAIL.getCode());
  108. authorizerInfo.setDomainTime(new Date());
  109. authorizerInfo.setUpdateTime(new Date());
  110. }
  111. authorizerInfoService.updateDomainUrl(authorizerInfo);
  112. return new ResultData(openRet);
  113. } catch (WxErrorException e) {
  114. logger.error(e.getMessage());
  115. }
  116. return new ResultData(Result.ERROR);
  117. }
  118. @ApiOperation(value = "设置小程序业务域名", notes = "{\n" +
  119. " \"action\":\"add/set/delete/get\",\n" +
  120. " \"webviewdomain\":[\"https://www.qq.com\",\"https://m.qq.com\"]\n" +
  121. " }")
  122. @PostMapping("/modifyWebViewDomain")
  123. public ResultData modifyWebViewDomain(String appId, String action,
  124. List<String> urlList) {
  125. WxAuthorizerInfo authorizerInfo = authorizerInfoService.getByAppId(appId);
  126. if(authorizerInfo == null) {
  127. return new ResultData(ErrorCode.WEAPP_APPID_NOT_AUTH);
  128. }
  129. if(!authorizerInfo.getAuthorizationStatus().equals(EnumWxAuthorizationStatus.AUTHORIZED.getCode())) {
  130. return new ResultData(ErrorCode.WEAPP_APPID_NOT_AUTH);
  131. }
  132. try {
  133. WxOpenMaService openMaService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId);
  134. String openRet = openMaService.setWebViewDomain(action, urlList);
  135. logger.info(openRet);
  136. JSONObject webRetObj = JSON.parseObject(openRet);
  137. if(webRetObj.getInteger("errcode").equals(0)) {
  138. logger.info("设置服务器业务域名成功");
  139. authorizerInfo.setWebdomainStatus(EnumEnableType.SUCC.getCode());
  140. authorizerInfo.setWebdomainTime(new Date());
  141. authorizerInfo.setUpdateTime(new Date());
  142. } else {
  143. logger.info("设置服务器业务域名失败");
  144. authorizerInfo.setWebdomainStatus(EnumEnableType.FAIL.getCode());
  145. authorizerInfo.setWebdomainTime(new Date());
  146. authorizerInfo.setUpdateTime(new Date());
  147. }
  148. authorizerInfoService.updateWebDomainUrl(authorizerInfo);
  149. return new ResultData(openRet);
  150. } catch (WxErrorException e) {
  151. logger.error(e.getMessage());
  152. }
  153. return new ResultData(Result.ERROR);
  154. }
  155. @ApiOperation(value = "绑定微信用户为小程序体验者", notes = "{\"appId\":\"string\", \"wechatId\":\"string\"}")
  156. @PostMapping("/bindTester")
  157. public ResultData bindTester(@RequestBody Map<String, String> params) {
  158. String appId = params.get("appId");
  159. String wechatId = params.get("wechatId");
  160. if(StringUtils.isBlank(appId)) {
  161. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "appId不能为空");
  162. }
  163. WxAuthorizerInfo authorizerInfo = authorizerInfoService.getByAppId(appId);
  164. if(authorizerInfo == null) {
  165. return new ResultData(ErrorCode.WEAPP_APPID_NOT_AUTH);
  166. }
  167. if(!authorizerInfo.getAuthorizationStatus().equals(EnumWxAuthorizationStatus.AUTHORIZED.getCode())) {
  168. return new ResultData(ErrorCode.WEAPP_APPID_NOT_AUTH);
  169. }
  170. if(StringUtils.isBlank(wechatId)) {
  171. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "wechatId不能为空");
  172. }
  173. try {
  174. WxOpenMaService openMaService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId);
  175. WxOpenResult openRet = openMaService.bindTester(wechatId);
  176. logger.info(openRet.toString());
  177. return new ResultData(openRet);
  178. } catch (WxErrorException e) {
  179. logger.error(e.getMessage());
  180. return new ResultData(e.getMessage());
  181. }
  182. }
  183. @ApiOperation(value = "解除绑定小程序的体验者", notes = "")
  184. @PostMapping("/unbindTester")
  185. public ResultData unbindTester(@RequestBody Map<String, String> params) {
  186. String appId = params.get("appId");
  187. String wechatId = params.get("wechatId");
  188. if(StringUtils.isBlank(appId)) {
  189. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "appId不能为空");
  190. }
  191. if(StringUtils.isBlank(wechatId)) {
  192. return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "wechatId不能为空");
  193. }
  194. try {
  195. WxOpenMaService openMaService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId);
  196. WxOpenResult openRet = openMaService.unbindTester(wechatId);
  197. logger.info(openRet.toString());
  198. return new ResultData(openRet);
  199. } catch (WxErrorException e) {
  200. logger.error(e.getMessage());
  201. return new ResultData(e.getMessage());
  202. }
  203. }
  204. @ApiOperation(value = "获取体验者列表", notes = "")
  205. @GetMapping("/getTesterList")
  206. public ResultData getTesterList(@RequestParam(value = "appId") String appId) {
  207. try {
  208. WxOpenMaService openMaService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId);
  209. WxOpenResult openRet = openMaService.getTesterList();
  210. logger.info(openRet.toString());
  211. return new ResultData(openRet);
  212. } catch (WxErrorException e) {
  213. logger.error(e.getMessage());
  214. }
  215. return new ResultData(Result.ERROR);
  216. }
  217. }