当使用后台操作系统给用户回复反馈消息之后,根据弹框确定是否需要给用户推送公众号消息。
1.在service层实现推送消息操作
public boolean replyFeedback(FeedbackBaseInfo feedbackBaseInfo) {
//打印日志
log.info("---log replyFeedback---回复内容:" + feedbackBaseInfo.getReplyContent());
//数据库添加恢复信息
int i = feedbackBaseInfoMapper.updateByPrimaryKey(feedbackBaseInfo);
if (i == 1) {
//添加成功给用户公众号推送消息(封装方法)
sendPushes(feedbackBaseInfo);
return true;
} else {
return false;
}
}
2.公众号推送放,公众号消息推送失败时发送短信
public boolean sendPushes(FeedbackBaseInfo feedbackBaseInfo){
//前端传递的消息,判断是否需要推送消息
if(feedbackBaseInfo.getIsPush()==false){
return true;
}
//公众号推送的消息页面,点击跳转的页面
String pageUrl = ToMiniPageUrlConstant.Pages_feedback;
//对应的反馈信息
String mainTitle = "您的反馈得到了平台的回复!";
//用户反馈的内容
String mainContent = feedbackBaseInfo.getDesc();
//平台回复的内容
String replyContent = feedbackBaseInfo.getReplyContent();
//需要推送消息的用户Id
Long userId = getById(feedbackBaseInfo.getId()).getUserId();
//从redis中根据用户Id获取用户信息(下面有封装的代码)
UserBaseInfoDubbo userBaseInfoDubbo = userBaseInfoDubboService.selectRedisUserByUid(userId);
//bak1为微信小程序的openId
String sendUserOpenId = userBaseInfoDubbo.getBak1();
//设置消息中用户反馈信息需要显示的长度(避免信息太长)
String userDesc="";
if (mainContent.length()>10){
userDesc=mainContent.substring(0,10)+"...";
}else {
userDesc=mainContent;
}
//设置平台反馈信息的长度
String content="";
if (replyContent.length()>20){
content=replyContent.substring(0,20)+"...";
}else {
content=replyContent;
}
//发送公众号推送消息
boolean b = wechatMsgSendDubboService.sendFormResultMsgService(sendUserOpenId, pageUrl, mainTitle
, userDesc, userBaseInfoDubbo.getNickName() + ":" + userDesc, content, false);
if (b){
return b;
}else if (!b){
// 公众号发送失败,发短信
//获取用户手机号码
String phoneNumber= String.valueOf(userBaseInfoDubbo.getPhoneNumber());
//判断号码是否可用
if (phoneNumber==null || phoneNumber.length()!=11){
throw new RuntimeException("公众号/短信推送都失败");
}
//设置信息显示长度
String reply="";
if (replyContent.length()>20){
reply=replyContent.substring(0,20)+"...";
}else {
reply=replyContent;
}
//定义好的模板
/*String information="尊敬的用户:您好,您在平台上提出的反馈,"+
"平台已予以回复:“"+reply+"”。详细信息,请登录小程序查看。";*/
//获取短信模板
String information = systemParameterInfoDubboService.selectValueByKey("sms_context_feedback");
//将反馈内容插入到模板中
Map map =new HashMap();
map.put("reply",reply);
String informationNew = StringUtil.processTemplate(information, map);
//发送短信
boolean s = commonMsgRecordDubboService.sendSms(userId,phoneNumber,informationNew);
if (s){
return true;
}else {
throw new RuntimeException("公众号/短信推送都失败");
}
}
return true;
}
从redist中获取用户信息的方法
public UserBaseInfoDubbo selectRedisUserByUid(long userId) {
if(userId == 0){
return null;
}
String redisUserKey = getRedisUserKeyByUserId(userId);
if(StringUtil.isEmpty(redisUserKey)){
return null;
}
UserBaseInfoDubbo userBaseInfoDubbo = null;
if(redisUserKey != null){
try {
userBaseInfoDubbo = selectRedisUserByRedisUserKey(redisUserKey);
}catch (Exception e){
logger.warn("user新增字段redis更新"+e.getMessage());
updateRedisUserByUid(userId);
userBaseInfoDubbo = selectRedisUserByRedisUserKey(redisUserKey);
}
if(userBaseInfoDubbo.getPersonState().equals(CommonConstant.UserPersonState_Auth)){
// //暂时默认认证用户都是会员
// userBaseInfoDubbo.setBak3(CommonConstant.Y);
//根据系统参数表设置
String isMember = systemParameterInfoDubboService.selectValueByKey(ConstantSystemParameterKey.isOpenAllUserMember);
if(isMember.equals(CommonConstant.Y)){
userBaseInfoDubbo.setBak3(CommonConstant.Y);
}
}
if(userBaseInfoDubbo.getBak5() != null && userBaseInfoDubbo.getBak5().equals("无群")){//无群的人 相当于也是游客
//userBaseInfoDubbo.setPersonState(CommonConstant.UserPersonState_Visitor);
}
return userBaseInfoDubbo;
}else{
return null;
}
}
小程序消息推送(小程序与公众号时两个不同程序)
public boolean sendFormResultMsgService(String sendUserOpenId,String toPageUrl
,String remarkTitle,String remarkConetent,String replyName,String replyContent,boolean isToXcx) {
try {
if(isToXcx) {//是否小程序发送通知
String templateId = systemParameterInfoDubboService.
selectValueByKey(ConstantSystemParameterKey.MiniSubscribe_Apply_weixin_notifyid_forum);
JSONObject json = new JSONObject();
json.put("touser", sendUserOpenId);
json.put("template_id", templateId);
// 跳转地址,没有则不填,如果有:必须是小程序内页面地址,支持带参数
json.put("page", toPageUrl);
JSONObject jsonData = new JSONObject();
jsonData.put("thing1", initJson(remarkTitle)); // 评论主题
if(StringUtil.isEmpty(remarkConetent)){
remarkConetent = "无";
}
jsonData.put("thing2", initJson(remarkConetent)); // 评论内容
jsonData.put("name3", initJson(replyName)); // 回复者
if(StringUtil.isEmpty(replyContent)){
replyContent = "无";
}
jsonData.put("thing4", initJson(replyContent)); // 回复者内容
json.put("data",jsonData);
long orgId = 0;
List<UserBaseInfoDubbo> list = userBaseInfoMapper.selectAllByXcxOpenId(sendUserOpenId);
if(list.isEmpty()){
return false;
}
for(UserBaseInfoDubbo user:list){
orgId = user.getOrgId();
}
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + systemSetConfigDubboService.getWxMiniAccessToken(orgId);
String response = HttpExecuteUtil.sendPost(url, json.toJSONString());
logger.info("\n\n================请求参数:{},\n\n================微信消息推送返回值:{}", json.toJSONString(), response);
JSONObject parse = JSONObject.parseObject(response);
int errcode = (Integer) parse.get("errcode");
String errmsg = (String) parse.get("errmsg");
if(errcode != 0){//小程序发送成功情况
logger.warn("小程序推送失败 mini send Subscribe notice error:" +errmsg);
}
}
remarkConetent = StringUtil.isEmpty(remarkConetent) ? replyContent:remarkConetent;
if(!remarkTitle.contains("您收到1条新的悄悄话")){
if(!StringUtil.isEmpty(replyContent) && !replyContent.contains("点击查看详情")){
remarkConetent = replyContent;
}
}
//公众号推送
return sendPublicReplyMsg(sendUserOpenId,remarkTitle,replyName
,remarkConetent,toPageUrl,"xcx");
} catch (Exception e) {
logger.error("\n\n====================微信消息推送异常消息:{}", e);
return false;
}
}
公众号推送方法
public boolean sendPublicReplyMsg(String sendUserOpenId,String remarkTitle,
String replyName,String replyContent,
String toPageUrl,String type){
String publiOpenId = null;
long orgId = 0;
List<UserBaseInfoDubbo> list = userBaseInfoMapper.selectAllByXcxOpenId(sendUserOpenId);
if(list.isEmpty()){
return false;
}
for(UserBaseInfoDubbo user:list){
orgId = user.getOrgId();
if(!StringUtil.isEmpty(user.getWeixinOpenid())){
publiOpenId = user.getWeixinOpenid();
break;
}
}
logger.info("2获得微信公众号openId:" +publiOpenId);
if(!StringUtil.isEmpty(publiOpenId)){//titleDesc,String checktype,String checkResult
String remarkDesc = "快点击查看";
if(remarkTitle.contains("您收到1条新的悄悄话")){
remarkDesc = "快点击,给TA回复1条悄悄话吧~";
}
SystemSetConfigDubbo systemSetConfigDubbo = systemSetConfigDubboService.getObject(orgId);
replyContent = getCon(replyContent,toPageUrl);
boolean resule = false;
if(type.equals("xcx")){
resule = WeixinPusUserReplyNotice.pushToWeixin( remarkTitle, "刚刚",
"操作人:" + replyName, replyContent,remarkDesc,
systemParameterInfoDubboService.selectValueByKey(ConstantSystemParameterKey.Weixin_public_notifyid_forum+orgId),
publiOpenId, null,systemSetConfigDubbo.getBak4(),systemSetConfigDubbo.getWeixinMiniAppId(),toPageUrl);
}else{
resule = WeixinPusUserReplyNotice.pushToWeixin( remarkTitle, "刚刚",
"操作人:" + replyName, replyContent,remarkDesc,
systemParameterInfoDubboService.selectValueByKey(ConstantSystemParameterKey.Weixin_public_notifyid_forum+orgId),
publiOpenId, toPageUrl,systemSetConfigDubbo.getBak4(),null,toPageUrl);
}
if(!resule){
logger.error("公众号推送失败,push public notify error publiOpenId="+publiOpenId);
//推送失败 都推送给群主
publiOpenId = "odmtd0hIdxFwbCP4Ewh9bnvYuBL8";//群主的公众号openId
boolean resule2 = WeixinPusUserReplyNotice.pushToWeixin("原推送失败,推送给管理员"+ remarkTitle, "刚刚",
"操作者:" + replyName, replyContent,"快点击查看",
systemParameterInfoDubboService.selectValueByKey(ConstantSystemParameterKey.Weixin_public_notifyid_forum+orgId),
publiOpenId, null,systemSetConfigDubbo.getBak4(),systemSetConfigDubbo.getWeixinMiniAppId(),toPageUrl);
if(!resule2){
logger.error("推送群主公众号都失败!公众号推送失败,push public notify error publiOpenId="+publiOpenId);
}
return false;
}else{
logger.info("公众号推送成功,succ public notice:" +publiOpenId);
return true;
}
}else{
return false;
}
}