|
@@ -0,0 +1,412 @@
|
|
|
+package com.demo.wjj.controller;
|
|
|
+
|
|
|
+import com.demo.wjj.bo.AuthorizationUrlBo;
|
|
|
+import com.demo.wjj.bo.CallbackTicketBo;
|
|
|
+import com.demo.wjj.bo.DisplaceDetailBo;
|
|
|
+import com.demo.wjj.bo.SaveDisplaceBo;
|
|
|
+import com.demo.wjj.po.Agent;
|
|
|
+import com.demo.wjj.po.Sale;
|
|
|
+import com.demo.wjj.service.*;
|
|
|
+import com.demo.wjj.utils.ApiResult;
|
|
|
+import com.demo.wjj.utils.wechat.AesException;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.dom4j.DocumentException;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+
|
|
|
+ * 微信api
|
|
|
+ * @author wangqing
|
|
|
+ * @date 2019.01.30
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("/wechat")
|
|
|
+public class WeChatController {
|
|
|
+
|
|
|
+ private final Logger LOG = LoggerFactory.getLogger(getClass());
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private WeChatService weChatService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private WeiXinMessageService weiXinMessageService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AgentService agentService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SaleService saleService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DiggerAgentDisplaceManageService diggerAgentDisplaceManageService;
|
|
|
+
|
|
|
+ private String appId = "wx94660c86d38f6bc3";
|
|
|
+
|
|
|
+ @RequestMapping("/auth/callback")
|
|
|
+ public String ticketCallback(@RequestBody String xml, @RequestParam(name = "msg_signature", required = false) String msgSignature,
|
|
|
+ @RequestParam(name = "timestamp", required = false) String timestamp,
|
|
|
+ @RequestParam(name = "nonce", required = false) String nonce) {
|
|
|
+ LOG.info("调用第三方平台授权票据回调(/wechat/auth/callback)接口, xml:{}, msgSignature:{}, timestamp:{}, nonce:{}", xml, msgSignature, timestamp, nonce);
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(xml)) {
|
|
|
+ LOG.info("第三方平台授权票据回调xml为空");
|
|
|
+ return "error";
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(msgSignature)) {
|
|
|
+ LOG.info("第三方平台授权票据回调msg_signature为空");
|
|
|
+ return "error";
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(timestamp)) {
|
|
|
+ LOG.info("第三方平台授权票据回调timestamp为空");
|
|
|
+ return "error";
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(nonce)) {
|
|
|
+ LOG.info("第三方平台授权票据回调nonce为空");
|
|
|
+ return "error";
|
|
|
+ }
|
|
|
+
|
|
|
+ CallbackTicketBo callbackTicketBo = new CallbackTicketBo();
|
|
|
+ callbackTicketBo.setMsgSignature(msgSignature);
|
|
|
+ callbackTicketBo.setTimestamp(timestamp);
|
|
|
+ callbackTicketBo.setNonce(nonce);
|
|
|
+ callbackTicketBo.setPostdata(xml);
|
|
|
+
|
|
|
+ try {
|
|
|
+ final boolean parseResult = weChatService.parseCallbackTicket(callbackTicketBo);
|
|
|
+ LOG.info("解析第三方平台授权回调票据结果:{}", parseResult);
|
|
|
+ return parseResult ? "success" : "error";
|
|
|
+ } catch (AesException e) {
|
|
|
+ LOG.error("解密第三方平台授权回调票据异常", e);
|
|
|
+ return "error";
|
|
|
+ } catch (DocumentException e) {
|
|
|
+ LOG.error("解析第三方平台授权回调票据异常", e);
|
|
|
+ return "error";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/event/{appId}/callback")
|
|
|
+ public String eventCallback(@PathVariable String appId) {
|
|
|
+ return "success";
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/createAuthorizationUrl")
|
|
|
+ public ApiResult createAuthorizationUrl(@RequestParam String authType) {
|
|
|
+ LOG.info("调用创建微信第三方平台授权url(/wechat/createAuthorizationUrl)接口, autuType:{}", authType);
|
|
|
+ final AuthorizationUrlBo authorizationUrlBo = new AuthorizationUrlBo();
|
|
|
+ authorizationUrlBo.setAuthType(authType);
|
|
|
+ try {
|
|
|
+ final String url = weChatService.generateAuthorizationUrl(authorizationUrlBo);
|
|
|
+ final ApiResult apiResult = ApiResult.createSuccess(url);
|
|
|
+ LOG.info("调用创建微信第三方平台授权url(/wechat/createAuthorizationUrl)接口成功, apiResult:{}", apiResult);
|
|
|
+ return apiResult;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error("调用创建微信第三方平台授权url(/wechat/createAuthorizationUrl)接口异常", e);
|
|
|
+ return ApiResult.createFailure();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(value = "/authorization/code/callback", produces = "application/json;charset=utf-8")
|
|
|
+ public String codeCallback(@RequestParam(name = "auth_code") String authCode, @RequestParam String id) {
|
|
|
+ LOG.info("调用第三方平台授权回调(/authorization/code/callback)接口, authCode:{}", authCode);
|
|
|
+ try {
|
|
|
+ final boolean result = weChatService.resolveCallbackAuthorization(authCode, id, "1");
|
|
|
+ LOG.info("调用第三方平台授权回调(/authorization/code/callback)接口成功, result:{}", result);
|
|
|
+ return result ? "授权成功" : "授权失败";
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error("调用第三方平台授权回调(/authorization/code/callback)接口异常");
|
|
|
+ return "授权失败";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(value = "/authorization/code/xcx/callback", produces = "application/json;charset=utf-8")
|
|
|
+ public String xcxCodeCallback(@RequestParam(name = "auth_code") String authCode, @RequestParam String id) {
|
|
|
+ LOG.info("调用第三方平台小程序授权回调(/authorization/code/callback)接口, authCode:{}", authCode);
|
|
|
+ try {
|
|
|
+ final boolean result = weChatService.resolveCallbackAuthorization(authCode, id, "2");
|
|
|
+ LOG.info("调用第三方平台小程序授权回调(/authorization/code/callback)接口成功, result:{}", result);
|
|
|
+ return result ? "授权成功" : "授权失败";
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error("调用第三方平台小程序授权回调(/authorization/code/callback)接口异常");
|
|
|
+ return "授权失败";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/getMenus")
|
|
|
+ public ApiResult getMenus(@RequestParam String authorizationAppId) {
|
|
|
+ LOG.info("调用获取公众号菜单(/wechat/getMenus)接口, authorizationAppId:{}", authorizationAppId);
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(authorizationAppId)) {
|
|
|
+ LOG.info("授权方appId为空");
|
|
|
+ return ApiResult.createFailure();
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ final String menus = weChatService.getMenus(authorizationAppId);
|
|
|
+ ApiResult apiResult;
|
|
|
+ if (StringUtils.isBlank(menus)) {
|
|
|
+ apiResult = ApiResult.createFailure();
|
|
|
+ } else {
|
|
|
+ apiResult = ApiResult.createSuccess(menus);
|
|
|
+ }
|
|
|
+ LOG.info("调用获取公众号菜单(/wechat/getMenus)接口成功, apiResult:{}", apiResult);
|
|
|
+ return apiResult;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error("调用获取公众号菜单(/wechat/getMenus)接口异常", e);
|
|
|
+ return ApiResult.createFailure();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/deleteMenus")
|
|
|
+ public ApiResult deleteMenus(@RequestParam String authorizationAppId) {
|
|
|
+ LOG.info("调用删除公众号菜单(/wechat/deleteMenus)接口, authorizationAppId:{}", authorizationAppId);
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(authorizationAppId)) {
|
|
|
+ LOG.info("授权方appId为空");
|
|
|
+ return ApiResult.createFailure();
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ final String menus = weChatService.deleteMenus(authorizationAppId);
|
|
|
+ ApiResult apiResult;
|
|
|
+ if (menus == null) {
|
|
|
+ apiResult = ApiResult.createFailure();
|
|
|
+ } else {
|
|
|
+ apiResult = ApiResult.createSuccess(menus);
|
|
|
+ }
|
|
|
+ LOG.info("调用删除公众号菜单(/wechat/deleteMenus)接口成功, apiResult:{}", apiResult);
|
|
|
+ return apiResult;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error("调用删除公众号菜单(/wechat/deleteMenus)接口异常", e);
|
|
|
+ return ApiResult.createFailure();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/createMenus")
|
|
|
+ public ApiResult createMenus(@RequestParam String authorizationAppId, @RequestParam String menusJson) {
|
|
|
+ LOG.info("调用创建公众号菜单(/wechat/createMenus)接口, authorizationAppId:{}, menusJson:{}", authorizationAppId, menusJson);
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(authorizationAppId)) {
|
|
|
+ LOG.info("授权方appId为空");
|
|
|
+ return ApiResult.createFailure();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(menusJson)) {
|
|
|
+ LOG.info("菜单内容为空");
|
|
|
+ return ApiResult.createFailure();
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ final String menus = weChatService.createMenus(authorizationAppId, menusJson);
|
|
|
+ ApiResult apiResult;
|
|
|
+ if (menus == null) {
|
|
|
+ apiResult = ApiResult.createFailure();
|
|
|
+ } else {
|
|
|
+ apiResult = ApiResult.createSuccess(menus);
|
|
|
+ }
|
|
|
+ LOG.info("调用创建公众号菜单(/wechat/createMenus)接口成功, apiResult:{}", apiResult);
|
|
|
+ return apiResult;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error("调用创建公众号菜单(/wechat/createMenus)接口异常", e);
|
|
|
+ return ApiResult.createFailure();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/pushDisplaceAuditMessage")
|
|
|
+ public ApiResult pushDisplaceAuditMessage(String agentId, String saleId, String displaceId) {
|
|
|
+ LOG.info("调用发送置换设备审核推送消息(/wechat/pushDisplaceAuditMessage)接口, agentId:{}, saleId:{}, displaceId:{}", agentId, saleId, displaceId);
|
|
|
+ final Agent agent = agentService.getAgent(agentId);
|
|
|
+ if (agent == null) {
|
|
|
+ final ApiResult apiResult = ApiResult.createFailure();
|
|
|
+ apiResult.setMessage("未查询到商家信息");
|
|
|
+ return apiResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ final Sale sale = saleService.getSale(saleId);
|
|
|
+ if (sale == null) {
|
|
|
+ final ApiResult apiResult = ApiResult.createFailure();
|
|
|
+ apiResult.setMessage("未查询到销售员信息");
|
|
|
+ return apiResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ final DisplaceDetailBo displaceDetailBo = diggerAgentDisplaceManageService.queryDetails(displaceId);
|
|
|
+ if (displaceDetailBo == null) {
|
|
|
+ final ApiResult apiResult = ApiResult.createFailure();
|
|
|
+ apiResult.setMessage("未查询到置换设备信息");
|
|
|
+ return apiResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ SaveDisplaceBo saveDisplaceBo = new SaveDisplaceBo();
|
|
|
+ saveDisplaceBo.setBrand(displaceDetailBo.getBrand());
|
|
|
+ saveDisplaceBo.setModel(displaceDetailBo.getModel());
|
|
|
+ saveDisplaceBo.setDeviceType(displaceDetailBo.getDeviceType());
|
|
|
+ try {
|
|
|
+ final boolean result = weiXinMessageService.send(agent, sale, saveDisplaceBo);
|
|
|
+ final ApiResult apiResult = result ? ApiResult.createSuccess(null) : ApiResult.createFailure();
|
|
|
+ LOG.info("调用发送置换设备审核推送消息(/wechat/pushDisplaceAuditMessage)接口成功, apiResult:{}", apiResult);
|
|
|
+ return apiResult;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error("调用发送置换设备审核推送消息(/wechat/pushDisplaceAuditMessage)接口异常", e);
|
|
|
+ return ApiResult.createFailure();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 推送待上架审核消息
|
|
|
+ * @param id 置换id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @PostMapping("/pushWaitShelvesMessage")
|
|
|
+ public ApiResult pushWaitShelvesMessage(@RequestParam String id, @RequestParam String displaceId) {
|
|
|
+ LOG.info("调用发送待上架微信消息(/pushWaitShelvesMessage)接口, id:{}, displaceId:{}", id, displaceId);
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(id)) {
|
|
|
+ LOG.info("置换id为空");
|
|
|
+ return ApiResult.createFailure();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(displaceId)) {
|
|
|
+ LOG.info("置换id为空");
|
|
|
+ return ApiResult.createFailure();
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ final boolean result = weChatService.pushWaitShelvesMessage(id, displaceId);
|
|
|
+ final ApiResult apiResult = result ? ApiResult.createSuccess(null) : ApiResult.createFailure();
|
|
|
+ LOG.info("调用发送待上架微信消息(/pushWaitShelvesMessage)接口成功, apiResult:{}", apiResult);
|
|
|
+ return apiResult;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error("调用发送待上架微信消息(/pushWaitShelvesMessage)接口异常", e);
|
|
|
+ return ApiResult.createFailure();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取微信公共模板id
|
|
|
+ * @param id
|
|
|
+ * @param templateName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/getTemplateId", method = RequestMethod.POST)
|
|
|
+ public ApiResult getTemplateId(@RequestParam String id, @RequestParam String templateName) {
|
|
|
+ LOG.info("调用获取微信推送模板(/wechat/getTemplateId)接口, id:{}, templateName:{}", id, templateName);
|
|
|
+ try {
|
|
|
+ final Map<String, String> template = weChatService.getTemplate(id, templateName);
|
|
|
+ ApiResult apiResult;
|
|
|
+ if (template == null) {
|
|
|
+ apiResult = ApiResult.createFailure();
|
|
|
+ } else {
|
|
|
+ apiResult = ApiResult.createSuccess(template);
|
|
|
+ }
|
|
|
+ LOG.info("调用获取微信推送模板(/wechat/getTemplateId)接口成功, apiResult:{}", apiResult);
|
|
|
+ return apiResult;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.info("调用获取微信推送模板(/wechat/getTemplateId)接口异常", e);
|
|
|
+ return ApiResult.createFailure();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/uploadCode")
|
|
|
+ public ApiResult uploadCode(@RequestParam String authorizationAppId) {
|
|
|
+ LOG.info("调用上传小程序代码(/wechat/uploadCode)接口, authorizationAppId:{}", authorizationAppId);
|
|
|
+
|
|
|
+ try {
|
|
|
+ final String content = weChatService.uploadCode(authorizationAppId);
|
|
|
+ final ApiResult<String> apiResult = ApiResult.createSuccess(content);
|
|
|
+ LOG.info("调用上传小程序代码(/wechat/uploadCode)成功, apiResult:{}", apiResult);
|
|
|
+ return apiResult;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error("调用上传小程序代码(/wechat/uploadCode)接口异常", e);
|
|
|
+ return ApiResult.createFailure();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/getCategory")
|
|
|
+ public ApiResult getCategory(@RequestParam String authorizationAppId) {
|
|
|
+ LOG.info("调用获取小程序已设置类目(/wechat/getCategory)接口, authorizationAppId:{}", authorizationAppId);
|
|
|
+
|
|
|
+ try {
|
|
|
+ final String category = weChatService.getCategory(authorizationAppId);
|
|
|
+ ApiResult apiResult = ApiResult.createSuccess(category);
|
|
|
+ LOG.info("调用获取小程序已设置类目(/wechat/getCategory)接口成功, apiResult:{}", apiResult);
|
|
|
+ return apiResult;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error("调用获取小程序已设置类目(/wechat/getCategory)接口异常", e);
|
|
|
+ return ApiResult.createFailure();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/getPages")
|
|
|
+ public ApiResult getPages(@RequestParam String authorizationAppId) {
|
|
|
+ LOG.info("调用获取小程序pages(/wechat/getPages)接口, authorizationAppId:{}", authorizationAppId);
|
|
|
+ try {
|
|
|
+ final String pages = weChatService.getPages(authorizationAppId);
|
|
|
+ final ApiResult<String> apiResult = ApiResult.createSuccess(pages);
|
|
|
+ LOG.info("调用获取小程序pages(/wechat/getPages)接口成功, apiResult:{}", apiResult);
|
|
|
+ return apiResult;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error("调用获取小程序pages(/wechat/getPages)接口异常", e);
|
|
|
+ return ApiResult.createFailure();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/submitAudit")
|
|
|
+ public ApiResult submitAudit(@RequestParam String authorizationAppId, @RequestParam String json) {
|
|
|
+ LOG.info("调用获取小程序提交审核(/wechat/submitAudit)接口, authorizationAppId:{}, json:{}", authorizationAppId, json);
|
|
|
+
|
|
|
+ try {
|
|
|
+ final String submitAudit = weChatService.submitAudit(authorizationAppId, json);
|
|
|
+ final ApiResult<String> apiResult = ApiResult.createSuccess(submitAudit);
|
|
|
+ LOG.info("调用获取小程序提交审核(/wechat/submitAudit)接口成功, apiResult:{}", apiResult);
|
|
|
+ return apiResult;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error("调用获取小程序提交审核(/wechat/submitAudit)接口异常", e);
|
|
|
+ return ApiResult.createFailure();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/publishCode")
|
|
|
+ public ApiResult submitAudit(@RequestParam String authorizationAppId) {
|
|
|
+ LOG.info("调用发布小程序(/wechat/publishCode)接口, authorizationAppId:{}", authorizationAppId);
|
|
|
+
|
|
|
+ try {
|
|
|
+ final String submitAudit = weChatService.publishCode(authorizationAppId);
|
|
|
+ final ApiResult<String> apiResult = ApiResult.createSuccess(submitAudit);
|
|
|
+ LOG.info("调用调用发布小程序(/wechat/publishCode)接口成功, apiResult:{}", apiResult);
|
|
|
+ return apiResult;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error("调用发布小程序(/wechat/publishCode)接口异常", e);
|
|
|
+ return ApiResult.createFailure();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/getToken")
|
|
|
+ public ApiResult getToken() {
|
|
|
+ LOG.info("调用获取第三方平台token(/wechat/getToken)接口, appId:{}", appId);
|
|
|
+ Map<String, String> tokenMap = new HashMap<>();
|
|
|
+ try {
|
|
|
+ final String token = weChatService.getToken(appId);
|
|
|
+ ApiResult apiResult;
|
|
|
+ if (token == null) {
|
|
|
+ LOG.info("调用获取第三方平台token,缓存中的token为空");
|
|
|
+ apiResult = ApiResult.createFailure();
|
|
|
+ } else {
|
|
|
+ tokenMap.put("appId", appId);
|
|
|
+ tokenMap.put("token", token);
|
|
|
+ apiResult = ApiResult.createSuccess(tokenMap);
|
|
|
+ }
|
|
|
+ LOG.info("调用获取第三方平台token(/wechat/getToken)接口成功, apiResult:{}", apiResult);
|
|
|
+ return apiResult;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOG.error("调用获取第三方平台token(/wechat/getToken)接口异常", e);
|
|
|
+ return ApiResult.createFailure();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|