WxController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. val btn32 = new WxMenuButton();
  146. btn32.setName("管理员手册");
  147. btn32.setType(WxConsts.MenuButtonType.VIEW);
  148. btn32.setUrl(
  149. wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  150. "/home/adminHelp.html",
  151. WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  152. );
  153. val btn33 = new WxMenuButton();
  154. btn33.setName("出入管理");
  155. btn33.setType(WxConsts.MenuButtonType.VIEW);
  156. btn33.setUrl(
  157. wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  158. "/home/outHelp.html",
  159. WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  160. );
  161. val btn34 = new WxMenuButton();
  162. btn34.setName("简介");
  163. btn34.setType(WxConsts.MenuButtonType.VIEW);
  164. btn34.setUrl(
  165. wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  166. "/home/desc.html",
  167. WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  168. );
  169. btnDesc.getSubButtons().add(btn31);
  170. btnDesc.getSubButtons().add(btn32);
  171. btnDesc.getSubButtons().add(btn33);
  172. btnDesc.getSubButtons().add(btn34);
  173. // btn3.getSubButtons().add(btnDesc);
  174. // btn3.getSubButtons().add(btnHelp);
  175. // val btn2 = new WxMenuButton();
  176. // btn2.setName("业委会");
  177. //
  178. // val btn21 = new WxMenuButton();
  179. // btn21.setName("每日上报");
  180. // btn21.setType(WxConsts.MenuButtonType.VIEW);
  181. // btn21.setUrl(
  182. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  183. // "/cust/commission.html",
  184. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  185. // );
  186. //
  187. // val btn22 = new WxMenuButton();
  188. // btn22.setName("求助");
  189. // btn22.setType(WxConsts.MenuButtonType.VIEW);
  190. // btn22.setUrl(
  191. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  192. // "/cust/history.html",
  193. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  194. // );
  195. //
  196. // btn2.getSubButtons().add(btn21);
  197. // btn2.getSubButtons().add(btn22);
  198. //
  199. //
  200. // //我的
  201. // val btn3 = new WxMenuButton();
  202. // btn3.setName("我的家");
  203. //
  204. // val btn31 = new WxMenuButton();
  205. // btn31.setName("健康日报");
  206. // btn31.setType(WxConsts.MenuButtonType.VIEW);
  207. // btn31.setUrl(
  208. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  209. // "/cust/account.html",
  210. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  211. // );
  212. //
  213. // val btn32 = new WxMenuButton();
  214. // btn32.setName("家庭求助");
  215. // btn32.setType(WxConsts.MenuButtonType.VIEW);
  216. // btn32.setUrl(
  217. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  218. // "/cust/sub.html",
  219. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  220. // );
  221. //
  222. // val btn33 = new WxMenuButton();
  223. // btn33.setName("我的二维码");
  224. // btn33.setType(WxConsts.MenuButtonType.VIEW);
  225. // btn33.setUrl(
  226. // wxService.oauth2buildAuthorizationUrl(weChatProperties.getDomain() +
  227. // "/cust/user_place_list.html",
  228. // WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
  229. // );
  230. //
  231. // btn3.getSubButtons().add(btn31);
  232. // btn3.getSubButtons().add(btn32);
  233. // btn3.getSubButtons().add(btn33);
  234. menu.getButtons().add(btn1);
  235. menu.getButtons().add(bt2);
  236. menu.getButtons().add(btnDesc);
  237. // menu.getButtons().add(btn2);
  238. // menu.getButtons().add(btn3);
  239. wxService.getMenuService().menuCreate(menu);
  240. return ModelOps.newSuccess();
  241. }
  242. /*
  243. @RequestMapping(value = "/wx/create_menu", method = RequestMethod.GET)
  244. public boolean createMenu() throws IOException, URISyntaxException {
  245. Menu menu = new Menu();
  246. //推介客户
  247. val btn1 = menu.addButton("推介客户");
  248. btn1.setType(Menu.VIEW);
  249. btn1.setUrl(
  250. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/market.htm", "")
  251. );
  252. //佣金
  253. val btn2 = menu.addButton("佣金");
  254. val btn21 = btn2.addSubButton("我的佣金");
  255. btn21.setType(Menu.VIEW);
  256. btn21.setUrl(
  257. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/commission.htm", "")
  258. );
  259. val btn22 = btn2.addSubButton("历史订单");
  260. btn22.setType(Menu.VIEW);
  261. btn22.setUrl(
  262. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/history.htm", "")
  263. );
  264. //我的
  265. val btn3 = menu.addButton("我的");
  266. val btn31 = btn3.addSubButton("我的账号");
  267. btn31.setType(Menu.VIEW);
  268. btn31.setUrl(
  269. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/account.htm", "")
  270. );
  271. val btn32 = btn3.addSubButton("二级分销");
  272. btn32.setType(Menu.VIEW);
  273. btn32.setUrl(
  274. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/sub.htm", "")
  275. );
  276. val btn33 = btn3.addSubButton("我的二维码");
  277. btn33.setType(Menu.VIEW);
  278. btn33.setUrl(
  279. weChatService.createGetOpenIdUrl(properties.getDomain() + "/cust/qr.htm", "")
  280. );
  281. weChatService.createMenu(menu);
  282. return true;
  283. }
  284. */
  285. /**
  286. * 获取定位相关的参数*/
  287. @GetMapping("/wx/jsApiConfig")
  288. public Model<WxJsapiSignature> sdkConfig(@Pd(name = "url")String url) throws WxErrorException {
  289. return Model.newSuccess(wxService.createJsapiSignature(url));
  290. }
  291. }