Browse Source

微信和小程序的工具包统一升到3.6.0版本,并进行相关配置的修改

dev
yujintao 6 years ago
parent
commit
ded817c802
  1. 2
      esua-epdc/epdc-commons/epdc-commons-tools-wx-ma/pom.xml
  2. 91
      esua-epdc/epdc-commons/epdc-commons-tools-wx-ma/src/main/java/com/elink/esua/epdc/wx/ma/WxMaConfig.java
  3. 55
      esua-epdc/epdc-commons/epdc-commons-tools-wx-ma/src/main/java/com/elink/esua/epdc/wx/ma/WxMaProperties.java
  4. 2
      esua-epdc/epdc-commons/epdc-commons-tools-wx-mp/pom.xml
  5. 11
      esua-epdc/epdc-module/epdc-api/epdc-api-server/pom.xml
  6. 17
      esua-epdc/epdc-module/epdc-api/epdc-api-server/src/main/java/com/elink/esua/epdc/async/WxMaSecCheckTask.java
  7. 9
      esua-epdc/epdc-module/epdc-api/epdc-api-server/src/main/java/com/elink/esua/epdc/service/impl/AppUserServiceImpl.java
  8. 6
      esua-epdc/epdc-module/epdc-api/epdc-api-server/src/main/java/com/elink/esua/epdc/service/impl/MessageServiceImpl.java
  9. 12
      esua-epdc/epdc-module/epdc-api/epdc-api-server/src/main/java/com/elink/esua/epdc/service/impl/WorkUserServiceImpl.java
  10. 44
      esua-epdc/epdc-module/epdc-api/epdc-api-server/src/main/java/com/elink/esua/epdc/utils/WxMaServiceUtils.java
  11. 23
      esua-epdc/epdc-module/epdc-api/epdc-api-server/src/main/resources/application.yml

2
esua-epdc/epdc-commons/epdc-commons-tools-wx-ma/pom.xml

@ -27,7 +27,7 @@
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>3.4.0</version>
<version>3.6.0</version>
</dependency>
</dependencies>

91
esua-epdc/epdc-commons/epdc-commons-tools-wx-ma/src/main/java/com/elink/esua/epdc/wx/ma/WxMaConfig.java

@ -5,18 +5,22 @@ import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateData;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import cn.binarywang.wx.miniapp.message.WxMaMessageHandler;
import cn.binarywang.wx.miniapp.message.WxMaMessageRouter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author <a href="https://github.com/binarywang">Binary Wang</a>
@ -27,31 +31,51 @@ public class WxMaConfig {
private WxMaProperties properties;
private static Map<String, WxMaMessageRouter> routers = Maps.newHashMap();
private static Map<String, WxMaService> maServices = Maps.newHashMap();
@Autowired
public WxMaConfig(WxMaProperties properties) {
this.properties = properties;
}
@Bean
public WxMaService wxMaService() {
WxMaProperties wxMaProperties = this.properties;
if (wxMaProperties == null) {
public static WxMaService getMaService(String appid) {
WxMaService wxService = maServices.get(appid);
if (wxService == null) {
throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid));
}
return wxService;
}
public static WxMaMessageRouter getRouter(String appid) {
return routers.get(appid);
}
@PostConstruct
public void init() {
List<WxMaProperties.Config> configs = this.properties.getConfigs();
if (configs == null) {
throw new RuntimeException("大哥,拜托先看下项目首页的说明(readme文件),添加下相关配置,注意别配错了!");
}
WxMaInMemoryConfig config = new WxMaInMemoryConfig();
config.setAppid(wxMaProperties.getAppid());
config.setSecret(wxMaProperties.getSecret());
config.setToken(wxMaProperties.getToken());
config.setAesKey(wxMaProperties.getAesKey());
config.setMsgDataFormat(wxMaProperties.getMsgDataFormat());
WxMaService maService = new WxMaServiceImpl();
maService.setWxMaConfig(config);
return maService;
maServices = configs.stream()
.map(a -> {
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
config.setAppid(a.getAppid());
config.setSecret(a.getSecret());
config.setToken(a.getToken());
config.setAesKey(a.getAesKey());
config.setMsgDataFormat(a.getMsgDataFormat());
WxMaService service = new WxMaServiceImpl();
service.setWxMaConfig(config);
routers.put(a.getAppid(), this.newRouter(service));
return service;
}).collect(Collectors.toMap(s -> s.getWxMaConfig().getAppid(), a -> a));
}
@Bean
public WxMaMessageRouter newRouter(WxMaService service) {
private WxMaMessageRouter newRouter(WxMaService service) {
final WxMaMessageRouter router = new WxMaMessageRouter(service);
router
.rule().handler(logHandler).next()
@ -62,24 +86,29 @@ public class WxMaConfig {
return router;
}
private final WxMaMessageHandler templateMsgHandler = (wxMessage, context, service, sessionManager) ->
service.getMsgService().sendTemplateMsg(WxMaTemplateMessage.builder()
.templateId("此处更换为自己的模板id")
.formId("自己替换可用的formid")
.data(Lists.newArrayList(
new WxMaTemplateData("keyword1", "339208499", "#173177")))
.toUser(wxMessage.getFromUser())
.build());
private final WxMaMessageHandler templateMsgHandler = (wxMessage, context, service, sessionManager) -> {
service.getMsgService().sendTemplateMsg(WxMaTemplateMessage.builder()
.templateId("此处更换为自己的模板id")
.formId("自己替换可用的formid")
.data(Lists.newArrayList(
new WxMaTemplateData("keyword1", "339208499", "#173177")))
.toUser(wxMessage.getFromUser())
.build());
return null;
};
private final WxMaMessageHandler logHandler = (wxMessage, context, service, sessionManager) -> {
System.out.println("收到消息:" + wxMessage.toString());
service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("收到信息为:" + wxMessage.toJson())
.toUser(wxMessage.getFromUser()).build());
return null;
};
private final WxMaMessageHandler textHandler = (wxMessage, context, service, sessionManager) ->
service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("回复文本消息")
.toUser(wxMessage.getFromUser()).build());
private final WxMaMessageHandler textHandler = (wxMessage, context, service, sessionManager) -> {
service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("回复文本消息")
.toUser(wxMessage.getFromUser()).build());
return null;
};
private final WxMaMessageHandler picHandler = (wxMessage, context, service, sessionManager) -> {
try {
@ -95,6 +124,8 @@ public class WxMaConfig {
} catch (WxErrorException e) {
e.printStackTrace();
}
return null;
};
private final WxMaMessageHandler qrcodeHandler = (wxMessage, context, service, sessionManager) -> {
@ -110,6 +141,8 @@ public class WxMaConfig {
} catch (WxErrorException e) {
e.printStackTrace();
}
return null;
};
}

55
esua-epdc/epdc-commons/epdc-commons-tools-wx-ma/src/main/java/com/elink/esua/epdc/wx/ma/WxMaProperties.java

@ -3,6 +3,8 @@ package com.elink.esua.epdc.wx.ma;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
/**
* 微信小程序框架配置类
*
@ -13,29 +15,34 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "wx.ma")
public class WxMaProperties {
/**
* 设置微信小程序的appid
*/
private String appid;
/**
* 设置微信小程序的Secret
*/
private String secret;
/**
* 设置微信小程序消息服务器配置的token
*/
private String token;
/**
* 设置微信小程序消息服务器配置的EncodingAESKey
*/
private String aesKey;
/**
* 消息格式XML或者JSON
*/
private String msgDataFormat;
private List<Config> configs;
@Data
public static class Config {
/**
* 设置微信小程序的appid
*/
private String appid;
/**
* 设置微信小程序的Secret
*/
private String secret;
/**
* 设置微信小程序消息服务器配置的token
*/
private String token;
/**
* 设置微信小程序消息服务器配置的EncodingAESKey
*/
private String aesKey;
/**
* 消息格式XML或者JSON
*/
private String msgDataFormat;
}
}

2
esua-epdc/epdc-commons/epdc-commons-tools-wx-mp/pom.xml

@ -17,7 +17,7 @@
<!-- <description>基于 WxJava 和 Spring Boot 实现的微信公众号后端开发演示项目</description>-->
<properties>
<weixin-java-mp.version>3.5.0</weixin-java-mp.version>
<weixin-java-mp.version>3.6.0</weixin-java-mp.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>

11
esua-epdc/epdc-module/epdc-api/epdc-api-server/pom.xml

@ -13,7 +13,7 @@
<packaging>jar</packaging>
<properties>
<weixin-java-mp.version>3.5.0</weixin-java-mp.version>
<!-- <weixin-java-mp.version>3.5.0</weixin-java-mp.version>-->
</properties>
<dependencies>
@ -159,6 +159,9 @@
<!--小程序配置-->
<wx.ma.appId>wxdd8530c5f4926766</wx.ma.appId>
<wx.ma.secret>5bf4fb813145431b3493a10aa7e041e9</wx.ma.secret>
<!--工作端小程序-->
<work.wx.ma.appId>wx29b074840ef4bfd9</work.wx.ma.appId>
<work.wx.ma.secret>4adb1afccc69f205cdf5b521d74e2aca</work.wx.ma.secret>
</properties>
</profile>
@ -190,6 +193,9 @@
<!--小程序配置-->
<wx.ma.appId>wxdd8530c5f4926766</wx.ma.appId>
<wx.ma.secret>5bf4fb813145431b3493a10aa7e041e9</wx.ma.secret>
<!--工作端小程序-->
<work.wx.ma.appId>wx29b074840ef4bfd9</work.wx.ma.appId>
<work.wx.ma.secret>4adb1afccc69f205cdf5b521d74e2aca</work.wx.ma.secret>
</properties>
</profile>
@ -221,6 +227,9 @@
<!--小程序配置-->
<wx.ma.appId>wxdd8530c5f4926766</wx.ma.appId>
<wx.ma.secret>5bf4fb813145431b3493a10aa7e041e9</wx.ma.secret>
<!--工作端小程序-->
<work.wx.ma.appId>wx29b074840ef4bfd9</work.wx.ma.appId>
<work.wx.ma.secret>4adb1afccc69f205cdf5b521d74e2aca</work.wx.ma.secret>
</properties>
</profile>
</profiles>

17
esua-epdc/epdc-module/epdc-api/epdc-api-server/src/main/java/com/elink/esua/epdc/async/WxMaSecCheckTask.java

@ -2,6 +2,8 @@ package com.elink.esua.epdc.async;
import cn.binarywang.wx.miniapp.api.WxMaSecCheckService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import com.elink.esua.epdc.utils.WxMaServiceUtils;
import com.elink.esua.epdc.wx.ma.WxMaConfig;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
@ -11,6 +13,7 @@ import java.io.File;
/**
* 微信内容安全检查
*
* @Author LC
* @Date 2019/9/18 9:58
*/
@ -18,10 +21,11 @@ import java.io.File;
public class WxMaSecCheckTask {
@Autowired
private WxMaService wxMaService;
private WxMaServiceUtils wxMaServiceUtils;
/**
* 微信上传文本安全检查
*
* @Params: [message]
* @Return: void
* @Author: liuchuang
@ -29,12 +33,17 @@ public class WxMaSecCheckTask {
*/
@Async
public void checkMessage(String message) {
WxMaSecCheckService wxMaSecCheckService = wxMaService.getSecCheckService();
wxMaSecCheckService.checkMessage(message);
WxMaSecCheckService wxMaSecCheckService = wxMaServiceUtils.normalWxMaService().getSecCheckService();
try {
wxMaSecCheckService.checkMessage(message);
} catch (WxErrorException e) {
e.printStackTrace();
}
}
/**
* 微信上传图片安全检查
*
* @Params: [file]
* @Return: void
* @Author: liuchuang
@ -42,7 +51,7 @@ public class WxMaSecCheckTask {
*/
@Async
public void checkImage(File file) {
WxMaSecCheckService wxMaSecCheckService = wxMaService.getSecCheckService();
WxMaSecCheckService wxMaSecCheckService = wxMaServiceUtils.normalWxMaService().getSecCheckService();
try {
wxMaSecCheckService.checkImage(file);
} catch (WxErrorException e) {

9
esua-epdc/epdc-module/epdc-api/epdc-api-server/src/main/java/com/elink/esua/epdc/service/impl/AppUserServiceImpl.java

@ -36,6 +36,7 @@ import com.elink.esua.epdc.jwt.JwtTokenProperties;
import com.elink.esua.epdc.jwt.JwtTokenUtils;
import com.elink.esua.epdc.redis.AppUserRedis;
import com.elink.esua.epdc.service.AppUserService;
import com.elink.esua.epdc.utils.WxMaServiceUtils;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
@ -69,7 +70,7 @@ public class AppUserServiceImpl implements AppUserService {
private WxMpService wxMpService;
@Autowired
private WxMaService wxMaService;
private WxMaServiceUtils wxMaServiceUtils;
@Autowired
private AdminFeignClient adminFeignClient;
@ -473,7 +474,7 @@ public class AppUserServiceImpl implements AppUserService {
private WxMaJscode2SessionResult getWxMaUser(String wxCode) {
WxMaJscode2SessionResult wxMaJscode2SessionResult = null;
try {
wxMaJscode2SessionResult = wxMaService.jsCode2SessionInfo(wxCode);
wxMaJscode2SessionResult = wxMaServiceUtils.normalWxMaService().jsCode2SessionInfo(wxCode);
} catch (WxErrorException e) {
log.error("->[getMaOpenId]::error[{}]", "解析微信code失败");
}
@ -836,7 +837,7 @@ public class AppUserServiceImpl implements AppUserService {
return new Result().error("获取用户信息失败");
}
String sessionKey = this.getUserSessionKey(formDto.getWxCode());
WxMaUserInfo wxMaUserInfo = wxMaService.getUserService().getUserInfo(sessionKey, formDto.getEncryptedData(), formDto.getIv());
WxMaUserInfo wxMaUserInfo = wxMaServiceUtils.normalWxMaService().getUserService().getUserInfo(sessionKey, formDto.getEncryptedData(), formDto.getIv());
UserDTO userDTO = new UserDTO();
userDTO.setId(userDetail.getUserId());
userDTO.setNickname(wxMaUserInfo.getNickName());
@ -871,7 +872,7 @@ public class AppUserServiceImpl implements AppUserService {
*/
private String getUserSessionKey(String code) {
try {
WxMaJscode2SessionResult wxMaJscode2SessionResult = wxMaService.jsCode2SessionInfo(code);
WxMaJscode2SessionResult wxMaJscode2SessionResult = wxMaServiceUtils.normalWxMaService().jsCode2SessionInfo(code);
if (wxMaJscode2SessionResult != null) {
return wxMaJscode2SessionResult.getSessionKey();
}

6
esua-epdc/epdc-module/epdc-api/epdc-api-server/src/main/java/com/elink/esua/epdc/service/impl/MessageServiceImpl.java

@ -1,6 +1,5 @@
package com.elink.esua.epdc.service.impl;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateData;
import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage;
import com.elink.esua.epdc.commons.tools.utils.Result;
@ -10,6 +9,7 @@ import com.elink.esua.epdc.enums.AppUserAuditStateEnum;
import com.elink.esua.epdc.feign.MessageFeignClient;
import com.elink.esua.epdc.feign.UserFeignClient;
import com.elink.esua.epdc.service.MessageService;
import com.elink.esua.epdc.utils.WxMaServiceUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
@ -35,7 +35,7 @@ public class MessageServiceImpl implements MessageService {
private UserFeignClient userFeignClient;
@Autowired
private WxMaService wxMaService;
private WxMaServiceUtils wxMaServiceUtils;
/**
* 注册审核失败服务消息模版
@ -118,7 +118,7 @@ public class MessageServiceImpl implements MessageService {
.emphasisKeyword(success ? "keyword5.DATA" : "keyword6.DATA")
.build();
try {
wxMaService.getMsgService().sendUniformMsg(message);
wxMaServiceUtils.normalWxMaService().getMsgService().sendUniformMsg(message);
} catch (WxErrorException e) {
log.error("->sendRegisterResult::发送注册{}服务消息失败::userId::{}::errorMsg::{}",
success ? "成功" : "失败", registerAuditResultDto.getUserId(), e.getMessage());

12
esua-epdc/epdc-module/epdc-api/epdc-api-server/src/main/java/com/elink/esua/epdc/service/impl/WorkUserServiceImpl.java

@ -1,6 +1,5 @@
package com.elink.esua.epdc.service.impl;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.elink.esua.epdc.common.token.dto.WorkUserAuthorizationDTO;
import com.elink.esua.epdc.common.token.dto.WorkUserLoginDTO;
@ -16,17 +15,15 @@ import com.elink.esua.epdc.common.token.dto.TokenDto;
import com.elink.esua.epdc.dto.form.EpdcWorkAppUserMaTokenFormDTO;
import com.elink.esua.epdc.feign.AdminFeignClient;
import com.elink.esua.epdc.feign.ResourceFeignClient;
import com.elink.esua.epdc.feign.AdminFeignClient;
import com.elink.esua.epdc.feign.UserFeignClient;
import com.elink.esua.epdc.service.WorkUserService;
import com.elink.esua.epdc.utils.WxMaServiceUtils;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* 工作端用户登录用户管理 相关业务实现
@ -38,14 +35,15 @@ import java.util.Map;
public class WorkUserServiceImpl implements WorkUserService {
@Autowired
private UserFeignClient userFeignClient;
@Autowired
private AdminFeignClient adminFeignClient;
@Autowired
private WxMaService wxMaService;
private ResourceFeignClient resourceFeignClient;
@Autowired
private ResourceFeignClient resourceFeignClient;
private WxMaServiceUtils wxMaServiceUtils;
@Override
public Result<WorkUserAuthorizationDTO> getMaToken(EpdcWorkAppUserMaTokenFormDTO formDto) {
@ -141,7 +139,7 @@ public class WorkUserServiceImpl implements WorkUserService {
private WxMaJscode2SessionResult getWxMaUser(String wxCode) {
WxMaJscode2SessionResult wxMaJscode2SessionResult = null;
try {
wxMaJscode2SessionResult = wxMaService.jsCode2SessionInfo(wxCode);
wxMaJscode2SessionResult = wxMaServiceUtils.workWxMaService().jsCode2SessionInfo(wxCode);
} catch (WxErrorException e) {
e.printStackTrace();
}

44
esua-epdc/epdc-module/epdc-api/epdc-api-server/src/main/java/com/elink/esua/epdc/utils/WxMaServiceUtils.java

@ -0,0 +1,44 @@
package com.elink.esua.epdc.utils;
import cn.binarywang.wx.miniapp.api.WxMaService;
import com.elink.esua.epdc.wx.ma.WxMaConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author work@yujt.net.cn
* @date 1 1
*/
@Component
public class WxMaServiceUtils {
@Value("${wx.ma.appId.normal}")
private String APPID_NORMAL;
@Value("${wx.ma.appId.work}")
private String APPID_WORK;
/**
* 获取党群e家 普通居民端配置
*
* @return cn.binarywang.wx.miniapp.api.WxMaService
* @author work@yujt.net.cn
* @date 2019/11/25 16:09
*/
public final WxMaService normalWxMaService() {
final WxMaService wxMaService = WxMaConfig.getMaService(APPID_NORMAL);
return wxMaService;
}
/**
* 获取党群e家 工作端配置
*
* @return cn.binarywang.wx.miniapp.api.WxMaService
* @author work@yujt.net.cn
* @date 2019/11/25 16:10
*/
public final WxMaService workWxMaService() {
final WxMaService wxMaService = WxMaConfig.getMaService(APPID_WORK);
return wxMaService;
}
}

23
esua-epdc/epdc-module/epdc-api/epdc-api-server/src/main/resources/application.yml

@ -94,11 +94,24 @@ wx:
token: @wx.mp.configs.token@
aesKey: @wx.mp.configs.aesKey@
ma:
appid: @wx.ma.appId@
secret: @wx.ma.secret@
token: #微信小程序消息服务器配置的token
aesKey: #微信小程序消息服务器配置的EncodingAESKey
msgDataFormat: JSON
configs:
- appid: @wx.ma.appId@
secret: @wx.ma.secret@
token: #微信小程序消息服务器配置的token
aesKey: #微信小程序消息服务器配置的EncodingAESKey
msgDataFormat: JSON
- appid: @work.wx.ma.appId@
secret: @work.wx.ma.secret@
token: #微信小程序消息服务器配置的token
aesKey: #微信小程序消息服务器配置的EncodingAESKey
msgDataFormat: JSON
appId:
# 普通居民端的appId
normal: @wx.ma.appId@
# 工作端的appId
work: @work.wx.ma.appId@
token:
expire: 21600

Loading…
Cancel
Save