net.mingsoft.mweixin.service.SubscribeService Maven / Gradle / Ivy
package net.mingsoft.mweixin.service;
import java.util.Map;
import javax.annotation.Resource;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import net.mingsoft.basic.util.StringUtil;
import net.mingsoft.mweixin.biz.IFileBiz;
import net.mingsoft.mweixin.biz.INewsBiz;
import net.mingsoft.mweixin.constant.Const;
import net.mingsoft.mweixin.entity.*;
import net.mingsoft.mweixin.builder.ImageBuilder;
import net.mingsoft.mweixin.builder.VideoBuilder;
import net.mingsoft.mweixin.builder.VoiceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
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.WxMpUser;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.mweixin.biz.IMessageBiz;
import net.mingsoft.mweixin.biz.IWeixinPeopleBiz;
import net.mingsoft.mweixin.builder.TextBuilder;
/**
*
* @author Binary Wang
*
*/
@Component
public class SubscribeService extends AbstractService {
@Resource(name="netWeixinPeopleBiz")
private IWeixinPeopleBiz weixinPeopleBiz;
/**
* 注入微信被动消息回复业务层
*/
@Resource(name="netMessageBizImpl")
private IMessageBiz messageBiz;
/**
* 文件业务层注入
*/
@Autowired
private IFileBiz fileBiz;
/**
* 素材业务层注入
*/
@Autowired
private INewsBiz newsBiz;
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map context, WxMpService wxMpService,
WxSessionManager sessionManager) throws WxErrorException {
WeixinEntity weixin = ((PortalService) wxMpService).getWeixin();
this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser());
PortalService weixinService = (PortalService) wxMpService;
// 获取微信用户基本信息
WxMpUser userWxInfo = weixinService.getUserService().userInfo(wxMessage.getFromUser(), null);
if (userWxInfo != null) {
// TODO 可以添加关注用户到本地
this.logger.debug("保存用户信息");
int weixinId = weixinService.getWeixin().getWeixinId();
weixinPeopleBiz.saveOrUpdate(userWxInfo, weixinId);
}
WxMpXmlOutMessage responseResult = null;
try {
responseResult = handleSpecial(wxMessage);
} catch (Exception e) {
this.logger.error(e.getMessage(), e);
}
if (responseResult != null) {
return responseResult;
}
MessageEntity passiveMessage = new MessageEntity();
passiveMessage.setPmWeixinId(weixinService.getWeixin().getWeixinId());
passiveMessage.setPmAppId(BasicUtil.getAppId());
//获取设置关注回复的内容
passiveMessage.setPmType(MessageEntity.Type.TYPE_ATTENTION);
passiveMessage = (MessageEntity) messageBiz.getEntity(passiveMessage);
if(passiveMessage == null){
try {
return new TextBuilder().build("感谢关注", wxMessage, weixinService);
} catch (Exception e) {
this.logger.error(e.getMessage(), e);
}
}else{
//被动回复
switch (passiveMessage.getPmNewType()) {
case Const.MESSAGE_TEXT:
return new TextBuilder().build(passiveMessage.getPmContent(), wxMessage, weixinService);
case Const.MESSAGE_IMAGE:
FileEntity fileImage = (FileEntity) fileBiz.getEntity(Integer.valueOf(passiveMessage.getPmContent()));
return new ImageBuilder().build(fileImage.getFileMediaId(), wxMessage, weixinService);
//图片消息的配置
case Const.MESSAGE_VOICE:
FileEntity fileVoice = (FileEntity) fileBiz.getEntity(Integer.valueOf(passiveMessage.getPmContent()));
if (StringUtil.isBlank(fileVoice.getFileMediaId())) {
fileBiz.weiXinFileUpload(weixin, fileVoice);
fileVoice = (FileEntity) fileBiz.getEntity(Integer.valueOf(passiveMessage.getPmContent()));
}
return new VoiceBuilder().build(fileVoice.getFileMediaId(), wxMessage, weixinService);
//语音消息的配置
case Const.MESSAGE_VIDEO:
FileEntity fileVideo = (FileEntity) fileBiz.getEntity(Integer.valueOf(passiveMessage.getPmContent()));
if (StringUtil.isBlank(fileVideo.getFileMediaId())) {
fileBiz.weiXinFileUpload(weixin, fileVideo);
fileVideo = (FileEntity) fileBiz.getEntity(Integer.valueOf(passiveMessage.getPmContent()));
}
return new VideoBuilder().build(fileVideo.getFileMediaId(), wxMessage, weixinService);
//视频消息的配置
case Const.MESSAGE_NEWS:
//获取关键字对应的素材
NewsEntity newsEntity = newsBiz.getNewsByNewsId(Integer.valueOf(passiveMessage.getPmContent()));
if (StringUtil.isBlank(newsEntity.getNewsMediaId())) {
String mediaId = newsBiz.uploadWeiXinNews(newsEntity.getNewsId(), weixinService);
newsEntity.setNewsMediaId(mediaId);
newsBiz.updateEntity(newsEntity);
}
WxMpKefuMessage message = WxMpKefuMessage.MPNEWS()
.toUser(wxMessage.getFromUser())
.build();
message.setMpNewsMediaId(newsEntity.getNewsMediaId());
wxMpService.getKefuService().sendKefuMessage(message);
break;
}
}
return responseResult;
}
/**
* 处理特殊请求,比如如果是扫码进来的,可以做相应处理
*/
protected WxMpXmlOutMessage handleSpecial(WxMpXmlMessage wxMessage) throws Exception {
//TODO
return null;
}
}