WxController.java 11 KB

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