WxController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 bt2 = new WxMenuButton();
  112. bt2.setName("相关查询");
  113. val bt21 = new WxMenuButton();
  114. bt21.setName("外出查询");
  115. bt21.setType(WxConsts.MenuButtonType.VIEW);
  116. bt21.setUrl(
  117. wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  118. "/user/goOut.html",
  119. WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  120. );
  121. val bt22 = new WxMenuButton();
  122. bt22.setName("复工查询");
  123. bt22.setType(WxConsts.MenuButtonType.VIEW);
  124. bt22.setUrl(
  125. wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  126. "/user/returnWork.html",
  127. WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  128. );
  129. bt2.getSubButtons().add(bt21);
  130. bt2.getSubButtons().add(bt22);
  131. val btnDesc = new WxMenuButton();
  132. btnDesc.setName("平台简介");
  133. val btn31 = new WxMenuButton();
  134. btn31.setName("家庭手册");
  135. btn31.setType(WxConsts.MenuButtonType.VIEW);
  136. btn31.setUrl(
  137. wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  138. "/home/homeHelp.html",
  139. WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  140. );
  141. val btn32 = new WxMenuButton();
  142. btn32.setName("管理员手册");
  143. btn32.setType(WxConsts.MenuButtonType.VIEW);
  144. btn32.setUrl(
  145. wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  146. "/home/adminHelp.html",
  147. WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  148. );
  149. val btn33 = new WxMenuButton();
  150. btn33.setName("出入管理");
  151. btn33.setType(WxConsts.MenuButtonType.VIEW);
  152. btn33.setUrl(
  153. wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  154. "/home/outHelp.html",
  155. WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  156. );
  157. val btn34 = new WxMenuButton();
  158. btn34.setName("简介");
  159. btn34.setType(WxConsts.MenuButtonType.VIEW);
  160. btn34.setUrl(
  161. wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  162. "/home/desc.html",
  163. WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  164. );
  165. btnDesc.getSubButtons().add(btn31);
  166. btnDesc.getSubButtons().add(btn32);
  167. btnDesc.getSubButtons().add(btn33);
  168. btnDesc.getSubButtons().add(btn34);
  169. // btn3.getSubButtons().add(btnDesc);
  170. // btn3.getSubButtons().add(btnHelp);
  171. // val btn2 = new WxMenuButton();
  172. // btn2.setName("业委会");
  173. //
  174. // val btn21 = new WxMenuButton();
  175. // btn21.setName("每日上报");
  176. // btn21.setType(WxConsts.MenuButtonType.VIEW);
  177. // btn21.setUrl(
  178. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  179. // "/cust/commission.html",
  180. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  181. // );
  182. //
  183. // val btn22 = new WxMenuButton();
  184. // btn22.setName("求助");
  185. // btn22.setType(WxConsts.MenuButtonType.VIEW);
  186. // btn22.setUrl(
  187. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  188. // "/cust/history.html",
  189. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  190. // );
  191. //
  192. // btn2.getSubButtons().add(btn21);
  193. // btn2.getSubButtons().add(btn22);
  194. //
  195. //
  196. // //我的
  197. // val btn3 = new WxMenuButton();
  198. // btn3.setName("我的家");
  199. //
  200. // val btn31 = new WxMenuButton();
  201. // btn31.setName("健康日报");
  202. // btn31.setType(WxConsts.MenuButtonType.VIEW);
  203. // btn31.setUrl(
  204. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  205. // "/cust/account.html",
  206. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  207. // );
  208. //
  209. // val btn32 = new WxMenuButton();
  210. // btn32.setName("家庭求助");
  211. // btn32.setType(WxConsts.MenuButtonType.VIEW);
  212. // btn32.setUrl(
  213. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  214. // "/cust/sub.html",
  215. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  216. // );
  217. //
  218. // val btn33 = new WxMenuButton();
  219. // btn33.setName("我的二维码");
  220. // btn33.setType(WxConsts.MenuButtonType.VIEW);
  221. // btn33.setUrl(
  222. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  223. // "/cust/user_place_list.html",
  224. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  225. // );
  226. //
  227. // btn3.getSubButtons().add(btn31);
  228. // btn3.getSubButtons().add(btn32);
  229. // btn3.getSubButtons().add(btn33);
  230. menu.getButtons().add(btn1);
  231. menu.getButtons().add(bt2);
  232. menu.getButtons().add(btnDesc);
  233. // menu.getButtons().add(btn2);
  234. // menu.getButtons().add(btn3);
  235. wxService.getMenuService().menuCreate(menu);
  236. return ModelOps.newSuccess();
  237. }
  238. /*
  239. @RequestMapping(value = "/wx/create_menu", method = RequestMethod.GET)
  240. public boolean createMenu() throws IOException, URISyntaxException {
  241. Menu menu = new Menu();
  242. //推介客户
  243. val btn1 = menu.addButton("推介客户");
  244. btn1.setType(Menu.VIEW);
  245. btn1.setUrl(
  246. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/market.htm", "")
  247. );
  248. //佣金
  249. val btn2 = menu.addButton("佣金");
  250. val btn21 = btn2.addSubButton("我的佣金");
  251. btn21.setType(Menu.VIEW);
  252. btn21.setUrl(
  253. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/commission.htm", "")
  254. );
  255. val btn22 = btn2.addSubButton("历史订单");
  256. btn22.setType(Menu.VIEW);
  257. btn22.setUrl(
  258. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/history.htm", "")
  259. );
  260. //我的
  261. val btn3 = menu.addButton("我的");
  262. val btn31 = btn3.addSubButton("我的账号");
  263. btn31.setType(Menu.VIEW);
  264. btn31.setUrl(
  265. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/account.htm", "")
  266. );
  267. val btn32 = btn3.addSubButton("二级分销");
  268. btn32.setType(Menu.VIEW);
  269. btn32.setUrl(
  270. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/sub.htm", "")
  271. );
  272. val btn33 = btn3.addSubButton("我的二维码");
  273. btn33.setType(Menu.VIEW);
  274. btn33.setUrl(
  275. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/qr.htm", "")
  276. );
  277. weChatService.createMenu(menu);
  278. return true;
  279. }
  280. */
  281. }