WxController.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package com.bofeng.wx;
  2. import com.bofeng.support.WeChatProperties;
  3. import com.yvan.ModelOps;
  4. import lombok.extern.slf4j.Slf4j;
  5. import lombok.val;
  6. import me.chanjar.weixin.common.api.WxConsts;
  7. import me.chanjar.weixin.common.bean.menu.WxMenu;
  8. import me.chanjar.weixin.common.bean.menu.WxMenuButton;
  9. import me.chanjar.weixin.common.error.WxErrorException;
  10. import me.chanjar.weixin.mp.api.WxMpMessageRouter;
  11. import me.chanjar.weixin.mp.api.WxMpService;
  12. import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
  13. import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
  14. import org.apache.commons.lang3.StringUtils;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.web.bind.annotation.*;
  17. /**
  18. * Created by luoyifan on 2017/8/24.
  19. */
  20. @RestController
  21. @Slf4j
  22. public class WxController {
  23. private final WeChatProperties weChatProperties;
  24. private final WxMpService wxService;
  25. private final WxMpMessageRouter wxMpMessageRouter;
  26. @Autowired
  27. public WxController(WeChatProperties weChatProperties, WxMpService wxService, WxMpMessageRouter wxMpMessageRouter) {
  28. this.weChatProperties = weChatProperties;
  29. this.wxService = wxService;
  30. this.wxMpMessageRouter = wxMpMessageRouter;
  31. }
  32. @GetMapping("/wx/mp")
  33. public String authGet(@RequestParam(name = "signature", required = false) String signature,
  34. @RequestParam(name = "timestamp", required = false) String timestamp,
  35. @RequestParam(name = "nonce", required = false) String nonce,
  36. @RequestParam(name = "echostr", required = false) String echostr) {
  37. log.info("\n接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature, timestamp, nonce, echostr);
  38. if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
  39. throw new IllegalArgumentException("请求参数非法,请核实!");
  40. }
  41. if (wxService.checkSignature(timestamp, nonce, signature)) {
  42. return echostr;
  43. }
  44. return "非法请求";
  45. }
  46. @PostMapping("/wx/mp")
  47. public String post(@RequestBody String requestBody,
  48. @RequestParam("signature") String signature,
  49. @RequestParam("timestamp") String timestamp,
  50. @RequestParam("nonce") String nonce,
  51. @RequestParam("openid") String openid,
  52. @RequestParam(name = "encrypt_type", required = false) String encType,
  53. @RequestParam(name = "msg_signature", required = false) String msgSignature) {
  54. log.info("\n接收微信请求:[openid=[{}], [signature=[{}], encType=[{}], msgSignature=[{}],"
  55. + " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
  56. openid, signature, encType, msgSignature, timestamp, nonce, requestBody);
  57. if (!wxService.checkSignature(timestamp, nonce, signature)) {
  58. throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
  59. }
  60. String out = null;
  61. if (encType == null) {
  62. // 明文传输的消息
  63. WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
  64. WxMpXmlOutMessage outMessage = this.route(inMessage);
  65. if (outMessage == null) {
  66. return "";
  67. }
  68. out = outMessage.toXml();
  69. } else if ("aes".equalsIgnoreCase(encType)) {
  70. // aes加密的消息
  71. WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody, wxService.getWxMpConfigStorage(),
  72. timestamp, nonce, msgSignature);
  73. log.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
  74. WxMpXmlOutMessage outMessage = this.route(inMessage);
  75. if (outMessage == null) {
  76. return "";
  77. }
  78. out = outMessage.toEncryptedXml(wxService.getWxMpConfigStorage());
  79. }
  80. log.debug("\n组装回复信息:{}", out);
  81. return out;
  82. }
  83. private WxMpXmlOutMessage route(WxMpXmlMessage message) {
  84. try {
  85. return wxMpMessageRouter.route(message);
  86. } catch (Exception e) {
  87. log.error("路由消息时出现异常!", e);
  88. }
  89. return null;
  90. }
  91. @RequestMapping(value = "/wx/create_menu", method = RequestMethod.GET)
  92. public ModelOps createMenu() throws WxErrorException {
  93. val menu = new WxMenu();
  94. //推介客户
  95. val btn1 = new WxMenuButton();
  96. btn1.setName("进入");
  97. btn1.setType(WxConsts.MenuButtonType.VIEW);
  98. btn1.setUrl(
  99. wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  100. "/user/home.html",
  101. WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  102. );
  103. // val btn2 = new WxMenuButton();
  104. // btn2.setName("业委会");
  105. //
  106. // val btn21 = new WxMenuButton();
  107. // btn21.setName("每日上报");
  108. // btn21.setType(WxConsts.MenuButtonType.VIEW);
  109. // btn21.setUrl(
  110. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  111. // "/cust/commission.html",
  112. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  113. // );
  114. //
  115. // val btn22 = new WxMenuButton();
  116. // btn22.setName("求助");
  117. // btn22.setType(WxConsts.MenuButtonType.VIEW);
  118. // btn22.setUrl(
  119. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  120. // "/cust/history.html",
  121. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  122. // );
  123. //
  124. // btn2.getSubButtons().add(btn21);
  125. // btn2.getSubButtons().add(btn22);
  126. //
  127. //
  128. // //我的
  129. // val btn3 = new WxMenuButton();
  130. // btn3.setName("我的家");
  131. //
  132. // val btn31 = new WxMenuButton();
  133. // btn31.setName("健康日报");
  134. // btn31.setType(WxConsts.MenuButtonType.VIEW);
  135. // btn31.setUrl(
  136. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  137. // "/cust/account.html",
  138. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  139. // );
  140. //
  141. // val btn32 = new WxMenuButton();
  142. // btn32.setName("家庭求助");
  143. // btn32.setType(WxConsts.MenuButtonType.VIEW);
  144. // btn32.setUrl(
  145. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  146. // "/cust/sub.html",
  147. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  148. // );
  149. //
  150. // val btn33 = new WxMenuButton();
  151. // btn33.setName("我的二维码");
  152. // btn33.setType(WxConsts.MenuButtonType.VIEW);
  153. // btn33.setUrl(
  154. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  155. // "/cust/user_place_list.html",
  156. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  157. // );
  158. //
  159. // btn3.getSubButtons().add(btn31);
  160. // btn3.getSubButtons().add(btn32);
  161. // btn3.getSubButtons().add(btn33);
  162. menu.getButtons().add(btn1);
  163. // menu.getButtons().add(btn2);
  164. // menu.getButtons().add(btn3);
  165. wxService.getMenuService().menuCreate(menu);
  166. return ModelOps.newSuccess();
  167. }
  168. /*
  169. @RequestMapping(value = "/wx/create_menu", method = RequestMethod.GET)
  170. public boolean createMenu() throws IOException, URISyntaxException {
  171. Menu menu = new Menu();
  172. //推介客户
  173. val btn1 = menu.addButton("推介客户");
  174. btn1.setType(Menu.VIEW);
  175. btn1.setUrl(
  176. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/market.htm", "")
  177. );
  178. //佣金
  179. val btn2 = menu.addButton("佣金");
  180. val btn21 = btn2.addSubButton("我的佣金");
  181. btn21.setType(Menu.VIEW);
  182. btn21.setUrl(
  183. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/commission.htm", "")
  184. );
  185. val btn22 = btn2.addSubButton("历史订单");
  186. btn22.setType(Menu.VIEW);
  187. btn22.setUrl(
  188. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/history.htm", "")
  189. );
  190. //我的
  191. val btn3 = menu.addButton("我的");
  192. val btn31 = btn3.addSubButton("我的账号");
  193. btn31.setType(Menu.VIEW);
  194. btn31.setUrl(
  195. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/account.htm", "")
  196. );
  197. val btn32 = btn3.addSubButton("二级分销");
  198. btn32.setType(Menu.VIEW);
  199. btn32.setUrl(
  200. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/sub.htm", "")
  201. );
  202. val btn33 = btn3.addSubButton("我的二维码");
  203. btn33.setType(Menu.VIEW);
  204. btn33.setUrl(
  205. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/qr.htm", "")
  206. );
  207. weChatService.createMenu(menu);
  208. return true;
  209. }
  210. */
  211. }