Commit 24b4eec4 authored by xiaoguang.xu's avatar xiaoguang.xu

微信绑定关系推送

parent 6532c593
package cn.quantgroup.xyqb.config.mq; package cn.quantgroup.xyqb.config.mq;
import cn.quantgroup.tech.brave.service.ITechRabbitBuilder; import cn.quantgroup.tech.brave.service.ITechRabbitBuilder;
import org.springframework.amqp.core.*; import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory.CacheMode;
import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin; import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
/** /**
* Created by xuran on 2017/9/7. * Created by xuran on 2017/9/7.
...@@ -113,4 +121,35 @@ public class RegisterMqConfig { ...@@ -113,4 +121,35 @@ public class RegisterMqConfig {
template.setExchange(exchange4Gdt); template.setExchange(exchange4Gdt);
return template; return template;
} }
@Bean(name = "commonConnectionFactory")
public ConnectionFactory commonConnectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host, port);
connectionFactory.setCacheMode(CacheMode.CHANNEL);
//根据使用情况动态调整。
connectionFactory.setChannelCacheSize(100);
connectionFactory.setUsername(user);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost("/");
connectionFactory.setPublisherReturns(false);
connectionFactory.setPublisherConfirms(false);
return connectionFactory;
}
@Bean(name = "wechatRabbitTemplate")
public RabbitTemplate wechatRabbitTemplate(@Qualifier("commonConnectionFactory") ConnectionFactory commonConnectionFactory) {
RabbitTemplate rabbitTemplate = techRabbitBuilder.createRabbitTemplate(commonConnectionFactory);
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
RabbitAdmin rabbitAdmin = new RabbitAdmin(commonConnectionFactory);
String queueName = "wechat_bind";
Map<String, Object> queueArgs = new HashMap<>();
//数据 1 天有效期
queueArgs.put("x-message-ttl", 24_60_60_000);
Queue wechatBind = new Queue(queueName, true, false, false, queueArgs);
rabbitAdmin.declareQueue(wechatBind);
// rabbitTemplate.setQueue(queueName);
rabbitTemplate.setRoutingKey(queueName);
return rabbitTemplate;
}
} }
package cn.quantgroup.xyqb.event;
import cn.quantgroup.xyqb.entity.User;
import cn.quantgroup.xyqb.model.webchat.WechatEventMsg;
import cn.quantgroup.xyqb.repository.IUserRepository;
import com.google.common.collect.Maps;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Map;
/**
* 微信绑定关系变动。通知某系统
*/
@Component
public class NotifyWechatBindEventListener implements ApplicationListener<WechatBindEvent> {
@Resource
private RabbitTemplate wechatRabbitTemplate;
@Resource
private IUserRepository userRepository;
@Override
public void onApplicationEvent(WechatBindEvent event) {
WechatEventMsg wechatEventMsg = event.getWechatEventMsg();
User user = userRepository.findById(wechatEventMsg.getUserId());
String uuid = user.getUuid();
Map<String, Object> msg = Maps.newHashMapWithExpectedSize(3);
msg.put("uuid", uuid);
msg.put("openId", wechatEventMsg.getOpenId());
msg.put("timestamp", event.getTimestamp());
wechatRabbitTemplate.convertAndSend(msg);
}
}
package cn.quantgroup.xyqb.event;
import cn.quantgroup.xyqb.model.webchat.WechatEventMsg;
import lombok.Getter;
import lombok.Setter;
import org.springframework.context.ApplicationEvent;
/**
* 微信用户绑定信息创建或更新事件
*
* @author ag
*/
@Getter
@Setter
public class WechatBindEvent extends ApplicationEvent {
private WechatEventMsg wechatEventMsg;
/**
* Create a new ApplicationEvent.
*
* @param source the object on which the event initially occurred (never {@code null})
*/
public WechatBindEvent(Object source) {
super(source);
}
public WechatBindEvent(Object source, WechatEventMsg wechatEventMsg) {
super(source);
this.wechatEventMsg = wechatEventMsg;
}
}
package cn.quantgroup.xyqb.model.webchat;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class WechatEventMsg {
private Long userId;
private String openId;
}
...@@ -2,13 +2,13 @@ package cn.quantgroup.xyqb.service.wechat.impl; ...@@ -2,13 +2,13 @@ package cn.quantgroup.xyqb.service.wechat.impl;
import cn.quantgroup.xyqb.Constants; import cn.quantgroup.xyqb.Constants;
import cn.quantgroup.xyqb.entity.WechatUserInfo; import cn.quantgroup.xyqb.entity.WechatUserInfo;
import cn.quantgroup.xyqb.event.WechatBindEvent;
import cn.quantgroup.xyqb.exception.WechatRelateUserException; import cn.quantgroup.xyqb.exception.WechatRelateUserException;
import cn.quantgroup.xyqb.model.webchat.AccessTokenResponse; import cn.quantgroup.xyqb.model.webchat.AccessTokenResponse;
import cn.quantgroup.xyqb.model.webchat.WechatEventMsg;
import cn.quantgroup.xyqb.repository.IWeChatUserRepository; import cn.quantgroup.xyqb.repository.IWeChatUserRepository;
import cn.quantgroup.xyqb.service.http.IHttpService; import cn.quantgroup.xyqb.service.http.IHttpService;
import cn.quantgroup.xyqb.service.session.ISessionService;
import cn.quantgroup.xyqb.service.wechat.IWechatService; import cn.quantgroup.xyqb.service.wechat.IWechatService;
import cn.quantgroup.xyqb.util.EmojiUtil;
import cn.quantgroup.xyqb.util.ValidationUtil; import cn.quantgroup.xyqb.util.ValidationUtil;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -16,6 +16,7 @@ import org.apache.commons.lang3.StringUtils; ...@@ -16,6 +16,7 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -47,6 +48,9 @@ public class WechatServiceImpl implements IWechatService { ...@@ -47,6 +48,9 @@ public class WechatServiceImpl implements IWechatService {
@Qualifier("stringRedisTemplate") @Qualifier("stringRedisTemplate")
private RedisTemplate<String, String> redisTemplate; private RedisTemplate<String, String> redisTemplate;
@Resource
private ApplicationEventPublisher applicationEventPublisher;
@PostConstruct @PostConstruct
private void init() { private void init() {
accessTokenUrl = String.format("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&grant_type=authorization_code&code=", appId, secret) + "%s"; accessTokenUrl = String.format("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&grant_type=authorization_code&code=", appId, secret) + "%s";
...@@ -143,7 +147,13 @@ public class WechatServiceImpl implements IWechatService { ...@@ -143,7 +147,13 @@ public class WechatServiceImpl implements IWechatService {
// String city = EmojiUtil.filterUnicode4(userInfo.getCity()); // String city = EmojiUtil.filterUnicode4(userInfo.getCity());
// userInfo.setCity(city); // userInfo.setCity(city);
userInfo = userInfo.convertEmoji(); userInfo = userInfo.convertEmoji();
return weChatUserRepository.save(userInfo); WechatUserInfo wechatUserInfo = weChatUserRepository.save(userInfo);
WechatEventMsg wechatEventMsg = WechatEventMsg.builder()
.userId(wechatUserInfo.getUserId())
.openId(wechatUserInfo.getOpenId())
.build();
applicationEventPublisher.publishEvent(new WechatBindEvent(this, wechatEventMsg));
return wechatUserInfo;
} }
@Override @Override
...@@ -170,6 +180,11 @@ public class WechatServiceImpl implements IWechatService { ...@@ -170,6 +180,11 @@ public class WechatServiceImpl implements IWechatService {
log.error("微信关联失败:绑定条数<1:[service]:userId:{},phoneNo:{},openId:{}", userId, phoneNo, openId); log.error("微信关联失败:绑定条数<1:[service]:userId:{},phoneNo:{},openId:{}", userId, phoneNo, openId);
throw new WechatRelateUserException("微信关联失败"); throw new WechatRelateUserException("微信关联失败");
} }
WechatEventMsg wechatEventMsg = WechatEventMsg.builder()
.userId(userId)
.openId(openId)
.build();
applicationEventPublisher.publishEvent(new WechatBindEvent(this,wechatEventMsg));
// Todo : 如果当前openId已关联其他用户,则解绑成功后要注销其登录session -- 考虑后暂时不执行,影响太大 // Todo : 如果当前openId已关联其他用户,则解绑成功后要注销其登录session -- 考虑后暂时不执行,影响太大
log.info("微信关联成功:[service]:userId:{},phoneNo:{},openId:{},dissociate:{},relate:{},Old-WechatUserInfo:{}", userId, phoneNo, openId, dissociate, relate, wechatUserInfo); log.info("微信关联成功:[service]:userId:{},phoneNo:{},openId:{},dissociate:{},relate:{},Old-WechatUserInfo:{}", userId, phoneNo, openId, dissociate, relate, wechatUserInfo);
return relate; return relate;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment