diff --git a/austin-common/src/main/java/com/java3y/austin/common/constant/OfficialAccountParamConstant.java b/austin-common/src/main/java/com/java3y/austin/common/constant/OfficialAccountParamConstant.java index 9660f10..c62663c 100644 --- a/austin-common/src/main/java/com/java3y/austin/common/constant/OfficialAccountParamConstant.java +++ b/austin-common/src/main/java/com/java3y/austin/common/constant/OfficialAccountParamConstant.java @@ -36,4 +36,11 @@ public class OfficialAccountParamConstant { public static final String QR_CODE_SCENE_PREFIX = "qrscene_"; + /** + * 关注后服务号的:文案 + */ + public static final String SUBSCRIBE_TIPS = "关注公众号:Java3y 回复 austin 可获取项目笔记哟!"; + + + } diff --git a/austin-common/src/main/java/com/java3y/austin/common/enums/RespStatusEnum.java b/austin-common/src/main/java/com/java3y/austin/common/enums/RespStatusEnum.java index 556c833..c30424e 100644 --- a/austin-common/src/main/java/com/java3y/austin/common/enums/RespStatusEnum.java +++ b/austin-common/src/main/java/com/java3y/austin/common/enums/RespStatusEnum.java @@ -27,7 +27,8 @@ public enum RespStatusEnum { CLIENT_BAD_PARAMETERS("A0001", "客户端参数错误"), TEMPLATE_NOT_FOUND("A0002", "找不到模板或模板已被删除"), TOO_MANY_RECEIVER("A0003", "传入的接收者大于100个"), - NOT_LOGIN("A0004", "非测试环境,无须登录"), + DO_NOT_NEED_LOGIN("A0004", "非测试环境,无须登录"), + NO_LOGIN("A0005", "还未登录"), /** * 系统 diff --git a/austin-web/src/main/java/com/java3y/austin/web/config/WeChatLoginConfig.java b/austin-web/src/main/java/com/java3y/austin/web/config/WeChatLoginConfig.java index 20f1775..287c986 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/config/WeChatLoginConfig.java +++ b/austin-web/src/main/java/com/java3y/austin/web/config/WeChatLoginConfig.java @@ -35,7 +35,7 @@ public class WeChatLoginConfig { private String appId; @Value("${austin.login.official.account.secret}") private String secret; - @Value("${austin.login.official.account.secret}") + @Value("${austin.login.official.account.token}") private String token; @Autowired diff --git a/austin-web/src/main/java/com/java3y/austin/web/controller/OfficialAccountController.java b/austin-web/src/main/java/com/java3y/austin/web/controller/OfficialAccountController.java index 433ccf3..6c1784b 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/controller/OfficialAccountController.java +++ b/austin-web/src/main/java/com/java3y/austin/web/controller/OfficialAccountController.java @@ -20,6 +20,7 @@ import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; +import me.chanjar.weixin.mp.bean.result.WxMpUser; import me.chanjar.weixin.mp.bean.template.WxMpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; @@ -30,7 +31,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; import java.util.ArrayList; import java.util.List; @@ -110,11 +110,11 @@ public class OfficialAccountController { */ @RequestMapping(value = "/receipt", produces = {CommonConstant.CONTENT_TYPE_XML}) @ApiOperation("/接收微信的事件消息") - public String receiptMessage(HttpServletRequest request, HttpServletResponse response) { + public String receiptMessage(HttpServletRequest request) { try { WeChatLoginConfig configService = applicationContext.getBean(OfficialAccountParamConstant.WE_CHAT_LOGIN_CONFIG, WeChatLoginConfig.class); if (configService == null) { - return RespStatusEnum.NOT_LOGIN.getMsg(); + return RespStatusEnum.DO_NOT_NEED_LOGIN.getMsg(); } WxMpService wxMpService = configService.getOfficialAccountLoginService(); @@ -137,13 +137,13 @@ public class OfficialAccountController { WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(request.getInputStream()); log.info("raw inMessage:{}", JSON.toJSONString(inMessage)); WxMpXmlOutMessage outMessage = configService.getWxMpMessageRouter().route(inMessage); - response.getWriter().write(outMessage.toXml()); + return outMessage.toXml(); } else if (OfficialAccountParamConstant.AES.equals(encryptType)) { String msgSignature = request.getParameter(OfficialAccountParamConstant.MSG_SIGNATURE); WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(request.getInputStream(), configService.getConfig(), timestamp, nonce, msgSignature); log.info("aes inMessage:{}", JSON.toJSONString(inMessage)); WxMpXmlOutMessage outMessage = configService.getWxMpMessageRouter().route(inMessage); - response.getWriter().write(outMessage.toEncryptedXml(configService.getConfig())); + return outMessage.toEncryptedXml(configService.getConfig()); } return RespStatusEnum.SUCCESS.getMsg(); } catch (Exception e) { @@ -165,7 +165,7 @@ public class OfficialAccountController { try { WeChatLoginConfig configService = applicationContext.getBean(OfficialAccountParamConstant.WE_CHAT_LOGIN_CONFIG, WeChatLoginConfig.class); if (configService == null) { - return BasicResultVO.fail(RespStatusEnum.NOT_LOGIN); + return BasicResultVO.fail(RespStatusEnum.DO_NOT_NEED_LOGIN); } String id = IdUtil.getSnowflake().nextIdStr(); WxMpService wxMpService = configService.getOfficialAccountLoginService(); @@ -185,10 +185,14 @@ public class OfficialAccountController { */ @RequestMapping("/check/login") @ApiOperation("/检查是否已经登录") - public String checkLogin(String sceneId) { + public BasicResultVO checkLogin(String sceneId) { try { + String userInfo = redisTemplate.opsForValue().get(sceneId); - return Convert4Amis.getLoginJsonp(userInfo); + if (StrUtil.isBlank(userInfo)) { + return BasicResultVO.success(RespStatusEnum.NO_LOGIN); + } + return BasicResultVO.success(JSON.parseObject(userInfo, WxMpUser.class)); } catch (Exception e) { log.error("OfficialAccountController#checkLogin fail:{}", Throwables.getStackTraceAsString(e)); return null; diff --git a/austin-web/src/main/java/com/java3y/austin/web/handler/SubscribeHandler.java b/austin-web/src/main/java/com/java3y/austin/web/handler/SubscribeHandler.java index c5da5af..7fe32c3 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/handler/SubscribeHandler.java +++ b/austin-web/src/main/java/com/java3y/austin/web/handler/SubscribeHandler.java @@ -1,7 +1,5 @@ package com.java3y.austin.web.handler; -import cn.hutool.core.date.DateUtil; -import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.google.common.base.Throwables; import com.java3y.austin.common.constant.CommonConstant; @@ -44,7 +42,6 @@ public class SubscribeHandler implements WxMpMessageHandler { */ @Override public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map context, WxMpService wxMpService, WxSessionManager sessionManager) { - String content = DateUtil.now() + StrUtil.COLON + wxMessage.getFromUser() + StrUtil.COLON + OfficialAccountParamConstant.SUBSCRIBE_HANDLER; try { WxMpUser user = wxMpService.getUserService().userInfo(wxMessage.getFromUser()); String eventKey = wxMessage.getEventKey().replaceAll(OfficialAccountParamConstant.QR_CODE_SCENE_PREFIX, CommonConstant.EMPTY_STRING); @@ -53,6 +50,6 @@ public class SubscribeHandler implements WxMpMessageHandler { log.error("SubscribeHandler#handle fail:{}", Throwables.getStackTraceAsString(e)); } return WxMpXmlOutMessage.TEXT().fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()) - .content(content).build(); + .content(OfficialAccountParamConstant.SUBSCRIBE_TIPS).build(); } } diff --git a/austin-web/src/main/java/com/java3y/austin/web/utils/Convert4Amis.java b/austin-web/src/main/java/com/java3y/austin/web/utils/Convert4Amis.java index 1290f30..5fb0d2c 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/utils/Convert4Amis.java +++ b/austin-web/src/main/java/com/java3y/austin/web/utils/Convert4Amis.java @@ -313,34 +313,34 @@ public class Convert4Amis { /** * 【这个方法不用看】,纯粹为了适配amis前端 *

- * 得到微信服务号的【带参数】二维码返回给前端 + * 1、得到微信服务号的【带参数】二维码返回给前端 + * 2、让前端轮询请求 接口看是否已登录 * * @return */ public static CommonAmisVo getWxMpQrCode(String url, String id) { CommonAmisVo image = CommonAmisVo.builder().type("static-image").value(url).originalSrc(url).name("image").label("扫描关注").fixedSize(true).fixedSizeClassName(url).fixedSizeClassName("h-32").build(); - CommonAmisVo service = CommonAmisVo.builder().type("service").api("${ls:backend_url}/officialAccount/check/login?sceneId=" + id).interval(2000).build(); - return CommonAmisVo.builder().type("form").title("登录").mode("horizontal").body(Arrays.asList(image,service)).build(); - } - /** - * 【这个方法不用看】,纯粹为了适配amis前端 - *

- * 得到微信服务号的【带参数】二维码返回给前端 - * - * @return - */ - public static String getLoginJsonp(String userInfo) { - if (StrUtil.isBlank(userInfo)) { - log.error("can't get userInfo!"); - return "(function() {})();"; - } else { - return "(function() {\n" + - "\tlocalStorage.setItem(\"openId\", \"123\");\n" + - "\tlocalStorage.setItem(\"userName\", \"333\");\n" + - "\twindow.location.href='index.html';\n" + - "})();"; - } + String requestAdaptor = "var openId = localStorage.getItem(\"openId\");\n" + + "if (openId != null && openId != 'null' && openId != '' && openId !== undefined) {\n" + + " alert(\"已登录,你的ID是:\" + openId);\n" + + " window.location.href = 'index.html';\n" + + " return api;\n" + + "}"; + + + String adaptor = "if (payload.data != 'NO_LOGIN' && payload.status == '0') {\n" + + " localStorage.setItem(\"openId\", payload.data.openId);\n" + + " alert(\"扫码已登录成功,你的ID是:\" + payload.data.openId);\n" + + " window.location.href = 'index.html';\n" + + "}\n" + + "return payload;"; + + CommonAmisVo service = CommonAmisVo.builder().type("service").api(CommonAmisVo.ApiDTO.builder().url("${ls:backend_url}/officialAccount/check/login?sceneId=" + id) + .adaptor(adaptor).requestAdaptor(requestAdaptor).build()).interval(2000).silentPolling(true).build(); + + return CommonAmisVo.builder().type("form").title("登录").mode("horizontal").body(Arrays.asList(image, service)).build(); } + } diff --git a/austin-web/src/main/java/com/java3y/austin/web/vo/amis/CommonAmisVo.java b/austin-web/src/main/java/com/java3y/austin/web/vo/amis/CommonAmisVo.java index 8e523f2..0d0193a 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/vo/amis/CommonAmisVo.java +++ b/austin-web/src/main/java/com/java3y/austin/web/vo/amis/CommonAmisVo.java @@ -26,9 +26,9 @@ public class CommonAmisVo { private String label; private String value; - private String api; private String schemaApi; + private String mode; private String name; private boolean fixedSize; @@ -38,6 +38,7 @@ public class CommonAmisVo { private Integer interval; private boolean required; + private boolean silentPolling; private String size; private String target; @@ -62,6 +63,7 @@ public class CommonAmisVo { private List body; + private ApiDTO api; /** * columns */ @@ -108,4 +110,34 @@ public class CommonAmisVo { private Boolean quickEdit; } + + /** + * ApiDTO + */ + @NoArgsConstructor + @Data + @AllArgsConstructor + @Builder + public static class ApiDTO { + /** + * adaptor + */ + @JSONField(name = "adaptor") + private String adaptor; + + /** + * adaptor + */ + @JSONField(name = "requestAdaptor") + private String requestAdaptor; + + /** + * url + */ + @JSONField(name = "url") + private String url; + + } + + }