WxController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 btn1 = new WxMenuButton();
  104. btn1.setName("健康日报");
  105. btn1.setType(WxConsts.MenuButtonType.VIEW);
  106. btn1.setUrl(
  107. wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  108. "/user/home.html",
  109. WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  110. );
  111. val btn2 = new WxMenuButton();
  112. btn2.setName("商品团购");
  113. btn2.setType(WxConsts.MenuButtonType.VIEW);
  114. btn2.setUrl(
  115. wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  116. "/tuangou/home.html",
  117. WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  118. );
  119. val btn3 = new WxMenuButton();
  120. btn3.setName("我的");
  121. val btnDesc = new WxMenuButton();
  122. btnDesc.setName("平台简介");
  123. btnDesc.setType(WxConsts.MenuButtonType.VIEW);
  124. btnDesc.setUrl(
  125. wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  126. "/home/desc.html",
  127. WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  128. );
  129. val btnHelp = new WxMenuButton();
  130. btnHelp.setName("帮助");
  131. btnHelp.setType(WxConsts.MenuButtonType.VIEW);
  132. btnHelp.setUrl(
  133. wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  134. "/home/homeHelp.html",
  135. WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  136. );
  137. btn3.getSubButtons().add(btnDesc);
  138. btn3.getSubButtons().add(btnHelp);
  139. // val btn2 = new WxMenuButton();
  140. // btn2.setName("业委会");
  141. //
  142. // val btn21 = new WxMenuButton();
  143. // btn21.setName("每日上报");
  144. // btn21.setType(WxConsts.MenuButtonType.VIEW);
  145. // btn21.setUrl(
  146. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  147. // "/cust/commission.html",
  148. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  149. // );
  150. //
  151. // val btn22 = new WxMenuButton();
  152. // btn22.setName("求助");
  153. // btn22.setType(WxConsts.MenuButtonType.VIEW);
  154. // btn22.setUrl(
  155. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  156. // "/cust/history.html",
  157. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  158. // );
  159. //
  160. // btn2.getSubButtons().add(btn21);
  161. // btn2.getSubButtons().add(btn22);
  162. //
  163. //
  164. // //我的
  165. // val btn3 = new WxMenuButton();
  166. // btn3.setName("我的家");
  167. //
  168. // val btn31 = new WxMenuButton();
  169. // btn31.setName("健康日报");
  170. // btn31.setType(WxConsts.MenuButtonType.VIEW);
  171. // btn31.setUrl(
  172. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  173. // "/cust/account.html",
  174. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  175. // );
  176. //
  177. // val btn32 = new WxMenuButton();
  178. // btn32.setName("家庭求助");
  179. // btn32.setType(WxConsts.MenuButtonType.VIEW);
  180. // btn32.setUrl(
  181. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  182. // "/cust/sub.html",
  183. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  184. // );
  185. //
  186. // val btn33 = new WxMenuButton();
  187. // btn33.setName("我的二维码");
  188. // btn33.setType(WxConsts.MenuButtonType.VIEW);
  189. // btn33.setUrl(
  190. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  191. // "/cust/user_place_list.html",
  192. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  193. // );
  194. //
  195. // btn3.getSubButtons().add(btn31);
  196. // btn3.getSubButtons().add(btn32);
  197. // btn3.getSubButtons().add(btn33);
  198. menu.getButtons().add(btn1);
  199. menu.getButtons().add(btn2);
  200. menu.getButtons().add(btn3);
  201. // menu.getButtons().add(btn2);
  202. // menu.getButtons().add(btn3);
  203. wxService.getMenuService().menuCreate(menu);
  204. return ModelOps.newSuccess();
  205. }
  206. /*
  207. @RequestMapping(value = "/wx/create_menu", method = RequestMethod.GET)
  208. public boolean createMenu() throws IOException, URISyntaxException {
  209. Menu menu = new Menu();
  210. //推介客户
  211. val btn1 = menu.addButton("推介客户");
  212. btn1.setType(Menu.VIEW);
  213. btn1.setUrl(
  214. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/market.htm", "")
  215. );
  216. //佣金
  217. val btn2 = menu.addButton("佣金");
  218. val btn21 = btn2.addSubButton("我的佣金");
  219. btn21.setType(Menu.VIEW);
  220. btn21.setUrl(
  221. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/commission.htm", "")
  222. );
  223. val btn22 = btn2.addSubButton("历史订单");
  224. btn22.setType(Menu.VIEW);
  225. btn22.setUrl(
  226. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/history.htm", "")
  227. );
  228. //我的
  229. val btn3 = menu.addButton("我的");
  230. val btn31 = btn3.addSubButton("我的账号");
  231. btn31.setType(Menu.VIEW);
  232. btn31.setUrl(
  233. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/account.htm", "")
  234. );
  235. val btn32 = btn3.addSubButton("二级分销");
  236. btn32.setType(Menu.VIEW);
  237. btn32.setUrl(
  238. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/sub.htm", "")
  239. );
  240. val btn33 = btn3.addSubButton("我的二维码");
  241. btn33.setType(Menu.VIEW);
  242. btn33.setUrl(
  243. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/qr.htm", "")
  244. );
  245. weChatService.createMenu(menu);
  246. return true;
  247. }
  248. */
  249. }