WeChatController.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. package com.demo.wjj.controller;
  2. import com.demo.wjj.bo.*;
  3. import com.demo.wjj.po.Agent;
  4. import com.demo.wjj.po.Sale;
  5. import com.demo.wjj.service.*;
  6. import com.demo.wjj.utils.ApiResult;
  7. import com.demo.wjj.utils.ExecuteResult;
  8. import com.demo.wjj.utils.Result;
  9. import com.demo.wjj.utils.wechat.AesException;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.dom4j.DocumentException;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.web.bind.annotation.*;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. /**
  19. * 微信api
  20. * @author wangqing
  21. * @date 2019.01.30
  22. */
  23. @RestController
  24. @RequestMapping("/wechat")
  25. public class WeChatController {
  26. private final Logger LOG = LoggerFactory.getLogger(getClass());
  27. @Autowired
  28. private WeChatService weChatService;
  29. @Autowired
  30. private WeiXinMessageService weiXinMessageService;
  31. @Autowired
  32. private AgentService agentService;
  33. @Autowired
  34. private SaleService saleService;
  35. @Autowired
  36. private WeiXinMsgPushService weiXinMsgPushService;
  37. @Autowired
  38. private DiggerAgentDisplaceManageService diggerAgentDisplaceManageService;
  39. private String appId = "wx94660c86d38f6bc3";
  40. @RequestMapping("/auth/callback")
  41. public String ticketCallback(@RequestBody String xml, @RequestParam(name = "msg_signature", required = false) String msgSignature,
  42. @RequestParam(name = "timestamp", required = false) String timestamp,
  43. @RequestParam(name = "nonce", required = false) String nonce) {
  44. LOG.info("调用第三方平台授权票据回调(/wechat/auth/callback)接口, xml:{}, msgSignature:{}, timestamp:{}, nonce:{}", xml, msgSignature, timestamp, nonce);
  45. if (StringUtils.isBlank(xml)) {
  46. LOG.info("第三方平台授权票据回调xml为空");
  47. return "error";
  48. }
  49. if (StringUtils.isBlank(msgSignature)) {
  50. LOG.info("第三方平台授权票据回调msg_signature为空");
  51. return "error";
  52. }
  53. if (StringUtils.isBlank(timestamp)) {
  54. LOG.info("第三方平台授权票据回调timestamp为空");
  55. return "error";
  56. }
  57. if (StringUtils.isBlank(nonce)) {
  58. LOG.info("第三方平台授权票据回调nonce为空");
  59. return "error";
  60. }
  61. CallbackTicketBo callbackTicketBo = new CallbackTicketBo();
  62. callbackTicketBo.setMsgSignature(msgSignature);
  63. callbackTicketBo.setTimestamp(timestamp);
  64. callbackTicketBo.setNonce(nonce);
  65. callbackTicketBo.setPostdata(xml);
  66. try {
  67. final boolean parseResult = weChatService.parseCallbackTicket(callbackTicketBo);
  68. LOG.info("解析第三方平台授权回调票据结果:{}", parseResult);
  69. return parseResult ? "success" : "error";
  70. } catch (AesException e) {
  71. LOG.error("解密第三方平台授权回调票据异常", e);
  72. return "error";
  73. } catch (DocumentException e) {
  74. LOG.error("解析第三方平台授权回调票据异常", e);
  75. return "error";
  76. }
  77. }
  78. @RequestMapping("/event/{appId}/callback")
  79. public String eventCallback(@PathVariable String appId) {
  80. return "success";
  81. }
  82. @RequestMapping("/createAuthorizationUrl")
  83. public ApiResult createAuthorizationUrl(@RequestParam String authType) {
  84. LOG.info("调用创建微信第三方平台授权url(/wechat/createAuthorizationUrl)接口, autuType:{}", authType);
  85. final AuthorizationUrlBo authorizationUrlBo = new AuthorizationUrlBo();
  86. authorizationUrlBo.setAuthType(authType);
  87. try {
  88. final String url = weChatService.generateAuthorizationUrl(authorizationUrlBo);
  89. final ApiResult apiResult = ApiResult.createSuccess(url);
  90. LOG.info("调用创建微信第三方平台授权url(/wechat/createAuthorizationUrl)接口成功, apiResult:{}", apiResult);
  91. return apiResult;
  92. } catch (Exception e) {
  93. LOG.error("调用创建微信第三方平台授权url(/wechat/createAuthorizationUrl)接口异常", e);
  94. return ApiResult.createFailure();
  95. }
  96. }
  97. @RequestMapping(value = "/authorization/code/callback", produces = "application/json;charset=utf-8")
  98. public String codeCallback(@RequestParam(name = "auth_code") String authCode, @RequestParam String id) {
  99. LOG.info("调用第三方平台授权回调(/authorization/code/callback)接口, authCode:{}", authCode);
  100. try {
  101. final boolean result = weChatService.resolveCallbackAuthorization(authCode, id, "1");
  102. LOG.info("调用第三方平台授权回调(/authorization/code/callback)接口成功, result:{}", result);
  103. return result ? "授权成功" : "授权失败";
  104. } catch (Exception e) {
  105. LOG.error("调用第三方平台授权回调(/authorization/code/callback)接口异常");
  106. return "授权失败";
  107. }
  108. }
  109. @RequestMapping(value = "/authorization/code/xcx/callback", produces = "application/json;charset=utf-8")
  110. public String xcxCodeCallback(@RequestParam(name = "auth_code") String authCode, @RequestParam String id) {
  111. LOG.info("调用第三方平台小程序授权回调(/authorization/code/callback)接口, authCode:{}", authCode);
  112. try {
  113. final boolean result = weChatService.resolveCallbackAuthorization(authCode, id, "2");
  114. LOG.info("调用第三方平台小程序授权回调(/authorization/code/callback)接口成功, result:{}", result);
  115. return result ? "授权成功" : "授权失败";
  116. } catch (Exception e) {
  117. LOG.error("调用第三方平台小程序授权回调(/authorization/code/callback)接口异常");
  118. return "授权失败";
  119. }
  120. }
  121. @RequestMapping("/getMenus")
  122. public ApiResult getMenus(@RequestParam String authorizationAppId) {
  123. LOG.info("调用获取公众号菜单(/wechat/getMenus)接口, authorizationAppId:{}", authorizationAppId);
  124. if (StringUtils.isBlank(authorizationAppId)) {
  125. LOG.info("授权方appId为空");
  126. return ApiResult.createFailure();
  127. }
  128. try {
  129. final String menus = weChatService.getMenus(authorizationAppId);
  130. ApiResult apiResult;
  131. if (StringUtils.isBlank(menus)) {
  132. apiResult = ApiResult.createFailure();
  133. } else {
  134. apiResult = ApiResult.createSuccess(menus);
  135. }
  136. LOG.info("调用获取公众号菜单(/wechat/getMenus)接口成功, apiResult:{}", apiResult);
  137. return apiResult;
  138. } catch (Exception e) {
  139. LOG.error("调用获取公众号菜单(/wechat/getMenus)接口异常", e);
  140. return ApiResult.createFailure();
  141. }
  142. }
  143. @PostMapping("/deleteMenus")
  144. public ApiResult deleteMenus(@RequestParam String authorizationAppId) {
  145. LOG.info("调用删除公众号菜单(/wechat/deleteMenus)接口, authorizationAppId:{}", authorizationAppId);
  146. if (StringUtils.isBlank(authorizationAppId)) {
  147. LOG.info("授权方appId为空");
  148. return ApiResult.createFailure();
  149. }
  150. try {
  151. final String menus = weChatService.deleteMenus(authorizationAppId);
  152. ApiResult apiResult;
  153. if (menus == null) {
  154. apiResult = ApiResult.createFailure();
  155. } else {
  156. apiResult = ApiResult.createSuccess(menus);
  157. }
  158. LOG.info("调用删除公众号菜单(/wechat/deleteMenus)接口成功, apiResult:{}", apiResult);
  159. return apiResult;
  160. } catch (Exception e) {
  161. LOG.error("调用删除公众号菜单(/wechat/deleteMenus)接口异常", e);
  162. return ApiResult.createFailure();
  163. }
  164. }
  165. @PostMapping("/createMenus")
  166. public ApiResult createMenus(@RequestParam String authorizationAppId, @RequestParam String menusJson) {
  167. LOG.info("调用创建公众号菜单(/wechat/createMenus)接口, authorizationAppId:{}, menusJson:{}", authorizationAppId, menusJson);
  168. if (StringUtils.isBlank(authorizationAppId)) {
  169. LOG.info("授权方appId为空");
  170. return ApiResult.createFailure();
  171. }
  172. if (StringUtils.isBlank(menusJson)) {
  173. LOG.info("菜单内容为空");
  174. return ApiResult.createFailure();
  175. }
  176. try {
  177. final String menus = weChatService.createMenus(authorizationAppId, menusJson);
  178. ApiResult apiResult;
  179. if (menus == null) {
  180. apiResult = ApiResult.createFailure();
  181. } else {
  182. apiResult = ApiResult.createSuccess(menus);
  183. }
  184. LOG.info("调用创建公众号菜单(/wechat/createMenus)接口成功, apiResult:{}", apiResult);
  185. return apiResult;
  186. } catch (Exception e) {
  187. LOG.error("调用创建公众号菜单(/wechat/createMenus)接口异常", e);
  188. return ApiResult.createFailure();
  189. }
  190. }
  191. @PostMapping("/pushDisplaceAuditMessage")
  192. public ApiResult pushDisplaceAuditMessage(String agentId, String saleId, String displaceId) {
  193. LOG.info("调用发送置换设备审核推送消息(/wechat/pushDisplaceAuditMessage)接口, agentId:{}, saleId:{}, displaceId:{}", agentId, saleId, displaceId);
  194. final Agent agent = agentService.getAgent(agentId);
  195. if (agent == null) {
  196. final ApiResult apiResult = ApiResult.createFailure();
  197. apiResult.setMessage("未查询到商家信息");
  198. return apiResult;
  199. }
  200. final Sale sale = saleService.getSale(saleId);
  201. if (sale == null) {
  202. final ApiResult apiResult = ApiResult.createFailure();
  203. apiResult.setMessage("未查询到销售员信息");
  204. return apiResult;
  205. }
  206. final DisplaceDetailBo displaceDetailBo = diggerAgentDisplaceManageService.queryDetails(displaceId);
  207. if (displaceDetailBo == null) {
  208. final ApiResult apiResult = ApiResult.createFailure();
  209. apiResult.setMessage("未查询到置换设备信息");
  210. return apiResult;
  211. }
  212. SaveDisplaceBo saveDisplaceBo = new SaveDisplaceBo();
  213. saveDisplaceBo.setBrand(displaceDetailBo.getBrand());
  214. saveDisplaceBo.setModel(displaceDetailBo.getModel());
  215. saveDisplaceBo.setDeviceType(displaceDetailBo.getDeviceType());
  216. try {
  217. final boolean result = weiXinMessageService.send(agent, sale, saveDisplaceBo);
  218. final ApiResult apiResult = result ? ApiResult.createSuccess(null) : ApiResult.createFailure();
  219. LOG.info("调用发送置换设备审核推送消息(/wechat/pushDisplaceAuditMessage)接口成功, apiResult:{}", apiResult);
  220. return apiResult;
  221. } catch (Exception e) {
  222. LOG.error("调用发送置换设备审核推送消息(/wechat/pushDisplaceAuditMessage)接口异常", e);
  223. return ApiResult.createFailure();
  224. }
  225. }
  226. /**
  227. * 推送待上架审核消息
  228. * @param id 置换id
  229. * @return
  230. */
  231. @PostMapping("/pushWaitShelvesMessage")
  232. public ApiResult pushWaitShelvesMessage(@RequestParam String id, @RequestParam String displaceId) {
  233. LOG.info("调用发送待上架微信消息(/pushWaitShelvesMessage)接口, id:{}, displaceId:{}", id, displaceId);
  234. if (StringUtils.isBlank(id)) {
  235. LOG.info("置换id为空");
  236. return ApiResult.createFailure();
  237. }
  238. if (StringUtils.isBlank(displaceId)) {
  239. LOG.info("置换id为空");
  240. return ApiResult.createFailure();
  241. }
  242. try {
  243. final boolean result = weChatService.pushWaitShelvesMessage(id, displaceId);
  244. final ApiResult apiResult = result ? ApiResult.createSuccess(null) : ApiResult.createFailure();
  245. LOG.info("调用发送待上架微信消息(/pushWaitShelvesMessage)接口成功, apiResult:{}", apiResult);
  246. return apiResult;
  247. } catch (Exception e) {
  248. LOG.error("调用发送待上架微信消息(/pushWaitShelvesMessage)接口异常", e);
  249. return ApiResult.createFailure();
  250. }
  251. }
  252. /**
  253. * 获取微信公共模板id
  254. * @param id
  255. * @param templateName
  256. * @return
  257. */
  258. @RequestMapping(value = "/getTemplateId", method = RequestMethod.POST)
  259. public ApiResult getTemplateId(@RequestParam String id, @RequestParam String templateName) {
  260. LOG.info("调用获取微信推送模板(/wechat/getTemplateId)接口, id:{}, templateName:{}", id, templateName);
  261. try {
  262. final Map<String, String> template = weChatService.getTemplate(id, templateName);
  263. ApiResult apiResult;
  264. if (template == null) {
  265. apiResult = ApiResult.createFailure();
  266. } else {
  267. apiResult = ApiResult.createSuccess(template);
  268. }
  269. LOG.info("调用获取微信推送模板(/wechat/getTemplateId)接口成功, apiResult:{}", apiResult);
  270. return apiResult;
  271. } catch (Exception e) {
  272. LOG.info("调用获取微信推送模板(/wechat/getTemplateId)接口异常", e);
  273. return ApiResult.createFailure();
  274. }
  275. }
  276. @PostMapping("/uploadCode")
  277. public ApiResult uploadCode(@RequestParam String authorizationAppId) {
  278. LOG.info("调用上传小程序代码(/wechat/uploadCode)接口, authorizationAppId:{}", authorizationAppId);
  279. try {
  280. final String content = weChatService.uploadCode(authorizationAppId);
  281. final ApiResult<String> apiResult = ApiResult.createSuccess(content);
  282. LOG.info("调用上传小程序代码(/wechat/uploadCode)成功, apiResult:{}", apiResult);
  283. return apiResult;
  284. } catch (Exception e) {
  285. LOG.error("调用上传小程序代码(/wechat/uploadCode)接口异常", e);
  286. return ApiResult.createFailure();
  287. }
  288. }
  289. @PostMapping("/getCategory")
  290. public ApiResult getCategory(@RequestParam String authorizationAppId) {
  291. LOG.info("调用获取小程序已设置类目(/wechat/getCategory)接口, authorizationAppId:{}", authorizationAppId);
  292. try {
  293. final String category = weChatService.getCategory(authorizationAppId);
  294. ApiResult apiResult = ApiResult.createSuccess(category);
  295. LOG.info("调用获取小程序已设置类目(/wechat/getCategory)接口成功, apiResult:{}", apiResult);
  296. return apiResult;
  297. } catch (Exception e) {
  298. LOG.error("调用获取小程序已设置类目(/wechat/getCategory)接口异常", e);
  299. return ApiResult.createFailure();
  300. }
  301. }
  302. @PostMapping("/getPages")
  303. public ApiResult getPages(@RequestParam String authorizationAppId) {
  304. LOG.info("调用获取小程序pages(/wechat/getPages)接口, authorizationAppId:{}", authorizationAppId);
  305. try {
  306. final String pages = weChatService.getPages(authorizationAppId);
  307. final ApiResult<String> apiResult = ApiResult.createSuccess(pages);
  308. LOG.info("调用获取小程序pages(/wechat/getPages)接口成功, apiResult:{}", apiResult);
  309. return apiResult;
  310. } catch (Exception e) {
  311. LOG.error("调用获取小程序pages(/wechat/getPages)接口异常", e);
  312. return ApiResult.createFailure();
  313. }
  314. }
  315. @PostMapping("/submitAudit")
  316. public ApiResult submitAudit(@RequestParam String authorizationAppId, @RequestParam String json) {
  317. LOG.info("调用获取小程序提交审核(/wechat/submitAudit)接口, authorizationAppId:{}, json:{}", authorizationAppId, json);
  318. try {
  319. final String submitAudit = weChatService.submitAudit(authorizationAppId, json);
  320. final ApiResult<String> apiResult = ApiResult.createSuccess(submitAudit);
  321. LOG.info("调用获取小程序提交审核(/wechat/submitAudit)接口成功, apiResult:{}", apiResult);
  322. return apiResult;
  323. } catch (Exception e) {
  324. LOG.error("调用获取小程序提交审核(/wechat/submitAudit)接口异常", e);
  325. return ApiResult.createFailure();
  326. }
  327. }
  328. @PostMapping("/publishCode")
  329. public ApiResult submitAudit(@RequestParam String authorizationAppId) {
  330. LOG.info("调用发布小程序(/wechat/publishCode)接口, authorizationAppId:{}", authorizationAppId);
  331. try {
  332. final String submitAudit = weChatService.publishCode(authorizationAppId);
  333. final ApiResult<String> apiResult = ApiResult.createSuccess(submitAudit);
  334. LOG.info("调用调用发布小程序(/wechat/publishCode)接口成功, apiResult:{}", apiResult);
  335. return apiResult;
  336. } catch (Exception e) {
  337. LOG.error("调用发布小程序(/wechat/publishCode)接口异常", e);
  338. return ApiResult.createFailure();
  339. }
  340. }
  341. @PostMapping("/getToken")
  342. public ApiResult getToken() {
  343. LOG.info("调用获取第三方平台token(/wechat/getToken)接口, appId:{}", appId);
  344. Map<String, String> tokenMap = new HashMap<>();
  345. try {
  346. final String token = weChatService.getToken(appId);
  347. ApiResult apiResult;
  348. if (token == null) {
  349. LOG.info("调用获取第三方平台token,缓存中的token为空");
  350. apiResult = ApiResult.createFailure();
  351. } else {
  352. tokenMap.put("appId", appId);
  353. tokenMap.put("token", token);
  354. apiResult = ApiResult.createSuccess(tokenMap);
  355. }
  356. LOG.info("调用获取第三方平台token(/wechat/getToken)接口成功, apiResult:{}", apiResult);
  357. return apiResult;
  358. } catch (Exception e) {
  359. LOG.error("调用获取第三方平台token(/wechat/getToken)接口异常", e);
  360. return ApiResult.createFailure();
  361. }
  362. }
  363. @PostMapping("/redirectUrl")
  364. public ApiResult redirectUrl(@RequestParam String agentId,
  365. @RequestParam String id,
  366. @RequestParam String openId,
  367. @RequestParam String displaceId,
  368. @RequestParam String goodsName) {
  369. LOG.info("调用获取第三方平台token(/wechat/getToken)接口, appId:{}", appId);
  370. final Agent agent = agentService.getAgent(agentId);
  371. if (agent == null) {
  372. final ApiResult apiResult = ApiResult.createFailure();
  373. apiResult.setMessage("未查询到商家信息");
  374. return apiResult;
  375. }
  376. final String appId = agent.getMchAppId();
  377. if (StringUtils.isBlank(appId)) {
  378. final ApiResult apiResult = ApiResult.createFailure();
  379. apiResult.setMessage("商家appId为空");
  380. return apiResult;
  381. }
  382. final DisplaceDetailBo displaceDetailBo = diggerAgentDisplaceManageService.queryDetails(displaceId);
  383. if (displaceDetailBo == null) {
  384. final ApiResult apiResult = ApiResult.createFailure();
  385. apiResult.setMessage("未查询到置换设备信息");
  386. return apiResult;
  387. }
  388. SaveDisplaceBo saveDisplaceBo = new SaveDisplaceBo();
  389. saveDisplaceBo.setBrand(displaceDetailBo.getBrand());
  390. saveDisplaceBo.setModel(displaceDetailBo.getModel());
  391. saveDisplaceBo.setDeviceType(displaceDetailBo.getDeviceType());
  392. //拼装参数
  393. String redirectUrl="/pages/download/index?agentId="+agentId+"&id="+id+"&openId="+openId;
  394. // weiXinMsgPushService.send(agent,redirectUrl,displaceId,goodsName);
  395. // ApiResult apiResult;
  396. try {
  397. final boolean result = weiXinMsgPushService.send(agent,redirectUrl,saveDisplaceBo,goodsName);
  398. final ApiResult apiResult = result ? ApiResult.createSuccess(null) : ApiResult.createFailure();
  399. LOG.info("调用发送置换设备审核推送消息(/wechat/redirectUrl)接口成功, apiResult:{}", apiResult);
  400. return apiResult;
  401. } catch (Exception e) {
  402. LOG.error("调用发送置换设备审核推送消息(/wechat/redirectUrl)接口异常", e);
  403. return ApiResult.createFailure();
  404. }
  405. }
  406. }