订阅消息推送

This commit is contained in:
huangdeliang
2021-05-15 14:31:30 +08:00
parent 1df264e603
commit c8714a5354
20 changed files with 429 additions and 49 deletions

View File

@ -148,4 +148,11 @@ public class SysRecipesPlan {
// 0-普通 1-模板
private Integer type;
/**
* 订阅推送
*/
private Integer subscribed;
private String remark;
}

View File

@ -25,4 +25,10 @@ public class SysRecipesPlanListInfo {
private List<SysRecipesPlan> menus;
private Integer sendFlag;
private Integer subscribed;
private Integer status;
}

View File

@ -0,0 +1,18 @@
package com.stdiet.custom.domain.wechat;
import com.google.gson.JsonObject;
import lombok.Data;
import java.util.Date;
@Data
public class WxSubscribePostLog {
Long id;
String appid;
String openid;
Long planId;
Date sendTime;
Integer errcode;
String errmsg;
JsonObject data;
}

View File

@ -0,0 +1,11 @@
package com.stdiet.custom.mapper;
import com.stdiet.custom.domain.wechat.WxSubscribePostLog;
import java.util.List;
public interface SysWxSubscribePostLogMapper {
public int insertWxSubscribePostLog(WxSubscribePostLog postLog);
public List<WxSubscribePostLog> selectWxSubscribePostLog(WxSubscribePostLog postLog);
}

View File

@ -19,6 +19,8 @@ public interface SysWxUserInfoMapper
*/
public SysWxUserInfo selectSysWxUserInfoById(String openid);
public SysWxUserInfo selectSysWxUserInfoByCusId(Long cusId);
/**
* 查询微信用户列表
*

View File

@ -19,6 +19,8 @@ public interface ISysWxUserInfoService
*/
public SysWxUserInfo selectSysWxUserInfoById(String openid);
public SysWxUserInfo selectSysWxUserInfoByCusId(Long cusId);
/**
* 查询微信用户列表
*

View File

@ -2,7 +2,7 @@ package com.stdiet.custom.service;
public interface IWechatAppletService {
public String getAccessToken(String appId) throws Exception;
public String getAccessToken(String appId);
public void postRecipesMessage(String appId, String openId, String name, String startDate, String endDate, String remark) throws Exception;
public String postRecipesMessage(Long cusId, Long planId, String name, String startDate, String endDate, String remark);
}

View File

@ -0,0 +1,11 @@
package com.stdiet.custom.service;
import com.stdiet.custom.domain.wechat.WxSubscribePostLog;
import java.util.List;
public interface IWxSubscribePostLogService {
public int insertWxSubscribePostLog(WxSubscribePostLog postLog);
public List<WxSubscribePostLog> selectWxSubscribePostLog(WxSubscribePostLog postLog);
}

View File

@ -12,6 +12,7 @@ import com.stdiet.custom.mapper.SysRecipesPlanMapper;
import com.stdiet.custom.service.ISysOrderPauseService;
import com.stdiet.custom.service.ISysOrderService;
import com.stdiet.custom.service.ISysRecipesPlanService;
import com.stdiet.custom.service.IWechatAppletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@ -40,6 +41,8 @@ public class SysRecipesPlanServiceImpl implements ISysRecipesPlanService {
private ISysOrderPauseService sysOrderPauseService;
@Autowired
private SynchrolockUtil synchrolockUtil;
@Autowired
private IWechatAppletService wechatAppletService;
/**
* 查询食谱计划
@ -83,10 +86,20 @@ public class SysRecipesPlanServiceImpl implements ISysRecipesPlanService {
*/
@Override
public int updateSysRecipesPlan(SysRecipesPlan sysRecipesPlan) {
SysRecipesPlan recipesPlan = sysRecipesPlanMapper.selectSysRecipesPlanById(sysRecipesPlan.getId());
sysRecipesPlan.setUpdateTime(DateUtils.getNowDate());
//目前只能修改发送状态,所以修改时加上发送时间
sysRecipesPlan.setSendTime(DateUtils.getNowDate());
return sysRecipesPlanMapper.updateSysRecipesPlan(sysRecipesPlan);
int row = sysRecipesPlanMapper.updateSysRecipesPlan(sysRecipesPlan);
if (row > 0 && sysRecipesPlan.getSendFlag() == 1 && StringUtils.isNull(recipesPlan.getSendTime())) {
// 未发送过
String name = "" + recipesPlan.getStartNumDay() + "" + recipesPlan.getEndNumDay() + "";
String startDate = recipesPlan.getStartDate().toString();
String endDate = recipesPlan.getEndDate().toString();
wechatAppletService.postRecipesMessage(sysRecipesPlan.getCusId(), sysRecipesPlan.getId(), name, startDate, endDate, recipesPlan.getRemark());
}
return row;
}
/**
@ -236,11 +249,11 @@ public class SysRecipesPlanServiceImpl implements ISysRecipesPlanService {
if (beforeOrderLastPlan != null) {
long differDay = ChronoUnit.DAYS.between(DateUtils.dateToLocalDate(beforeOrderLastPlan.getEndDate()), serverStartDate);
//检查之前食谱的结束时间和目前该订单的开始时间是否连续
if(differDay <= 1){
if (differDay <= 1) {
//判断前一个订单食谱是否满七天,不满则需要接上
int differNum = beforeOrderLastPlan.getEndNumDay() - beforeOrderLastPlan.getStartNumDay();
if(differNum < 6){
if (differNum < 6) {
//更新该食谱计划
beforeOrderLastPlan.setEndNumDay(beforeOrderLastPlan.getStartNumDay() + 6);
beforeOrderLastPlan.setEndDate(DateUtils.localDateToDate(DateUtils.dateToLocalDate(beforeOrderLastPlan.getEndDate()).plusDays(6 - differNum)));

View File

@ -1,12 +1,13 @@
package com.stdiet.custom.service.impl;
import java.util.List;
import com.stdiet.common.utils.DateUtils;
import com.stdiet.custom.domain.SysWxUserInfo;
import com.stdiet.custom.mapper.SysWxUserInfoMapper;
import com.stdiet.custom.service.ISysWxUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.stdiet.custom.mapper.SysWxUserInfoMapper;
import com.stdiet.custom.domain.SysWxUserInfo;
import com.stdiet.custom.service.ISysWxUserInfoService;
import java.util.List;
/**
* 微信用户Service业务层处理
@ -15,8 +16,7 @@ import com.stdiet.custom.service.ISysWxUserInfoService;
* @date 2020-11-28
*/
@Service
public class SysWxUserInfoServiceImpl implements ISysWxUserInfoService
{
public class SysWxUserInfoServiceImpl implements ISysWxUserInfoService {
@Autowired
private SysWxUserInfoMapper sysWxUserInfoMapper;
@ -27,11 +27,15 @@ public class SysWxUserInfoServiceImpl implements ISysWxUserInfoService
* @return 微信用户
*/
@Override
public SysWxUserInfo selectSysWxUserInfoById(String openid)
{
public SysWxUserInfo selectSysWxUserInfoById(String openid) {
return sysWxUserInfoMapper.selectSysWxUserInfoById(openid);
}
@Override
public SysWxUserInfo selectSysWxUserInfoByCusId(Long cusId) {
return sysWxUserInfoMapper.selectSysWxUserInfoByCusId(cusId);
}
/**
* 查询微信用户列表
*
@ -39,8 +43,7 @@ public class SysWxUserInfoServiceImpl implements ISysWxUserInfoService
* @return 微信用户
*/
@Override
public List<SysWxUserInfo> selectSysWxUserInfoList(SysWxUserInfo sysWxUserInfo)
{
public List<SysWxUserInfo> selectSysWxUserInfoList(SysWxUserInfo sysWxUserInfo) {
return sysWxUserInfoMapper.selectSysWxUserInfoList(sysWxUserInfo);
}
@ -56,8 +59,7 @@ public class SysWxUserInfoServiceImpl implements ISysWxUserInfoService
* @return 结果
*/
@Override
public int insertSysWxUserInfo(SysWxUserInfo sysWxUserInfo)
{
public int insertSysWxUserInfo(SysWxUserInfo sysWxUserInfo) {
sysWxUserInfo.setCreateTime(DateUtils.getNowDate());
return sysWxUserInfoMapper.insertSysWxUserInfo(sysWxUserInfo);
}
@ -69,8 +71,7 @@ public class SysWxUserInfoServiceImpl implements ISysWxUserInfoService
* @return 结果
*/
@Override
public int updateSysWxUserInfo(SysWxUserInfo sysWxUserInfo)
{
public int updateSysWxUserInfo(SysWxUserInfo sysWxUserInfo) {
sysWxUserInfo.setUpdateTime(DateUtils.getNowDate());
return sysWxUserInfoMapper.updateSysWxUserInfo(sysWxUserInfo);
}
@ -82,8 +83,7 @@ public class SysWxUserInfoServiceImpl implements ISysWxUserInfoService
* @return 结果
*/
@Override
public int deleteSysWxUserInfoByIds(String[] openids)
{
public int deleteSysWxUserInfoByIds(String[] openids) {
return sysWxUserInfoMapper.deleteSysWxUserInfoByIds(openids);
}
@ -94,8 +94,7 @@ public class SysWxUserInfoServiceImpl implements ISysWxUserInfoService
* @return 结果
*/
@Override
public int deleteSysWxUserInfoById(String openid)
{
public int deleteSysWxUserInfoById(String openid) {
return sysWxUserInfoMapper.deleteSysWxUserInfoById(openid);
}
}

View File

@ -3,8 +3,13 @@ package com.stdiet.custom.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.stdiet.common.core.redis.RedisCache;
import com.stdiet.common.utils.DateUtils;
import com.stdiet.common.utils.StringUtils;
import com.stdiet.custom.domain.SysWxUserInfo;
import com.stdiet.custom.domain.wechat.WxSubscribePostLog;
import com.stdiet.custom.service.ISysWxUserInfoService;
import com.stdiet.custom.service.IWechatAppletService;
import com.stdiet.custom.service.IWxSubscribePostLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
@ -23,8 +28,14 @@ public class WeChartAppletServiceImp implements IWechatAppletService {
@Autowired
private RestTemplate restTemplate;
@Autowired
private ISysWxUserInfoService sysWxUserInfoService;
@Autowired
private IWxSubscribePostLogService wxSubscribePostLogService;
@Override
public String getAccessToken(String appId) throws Exception {
public String getAccessToken(String appId) {
String accessToken = redisCache.getCacheObject(appId);
if (StringUtils.isNull(accessToken)) {
String appSecret = "";
@ -49,26 +60,31 @@ public class WeChartAppletServiceImp implements IWechatAppletService {
}
@Override
public void postRecipesMessage(String appId, String openId, String name, String startDate, String endDate, String remark) throws Exception {
String accessToken = getAccessToken(appId);
if (StringUtils.isNull(accessToken)) {
return;
public String postRecipesMessage(Long cusId, Long planId, String name, String startDate, String endDate, String remark) {
SysWxUserInfo sysWxUserInfo = sysWxUserInfoService.selectSysWxUserInfoByCusId(cusId);
if (StringUtils.isNull(sysWxUserInfo)) {
return null;
}
String tmpId = "";
String accessToken = getAccessToken(sysWxUserInfo.getAppid());
if (StringUtils.isNull(accessToken)) {
return "";
}
String tmpId = "EWeha9m0ggpnhMANDLHtl2ezLfPWKY_9PsJubbG_6eA";
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
JsonObject param = new JsonObject();
param.addProperty("access_token", accessToken);
param.addProperty("touser", openId);
param.addProperty("touser", sysWxUserInfo.getOpenid());
param.addProperty("template_id", tmpId);
param.addProperty("page", "pages/recipes/index");
JsonObject dataParam = new JsonObject();
dataParam.addProperty("key1", name);
dataParam.addProperty("key2", startDate);
dataParam.addProperty("key3", endDate);
dataParam.addProperty("key4", remark);
dataParam.addProperty("phrase1", name);
dataParam.addProperty("date3", startDate);
dataParam.addProperty("thing6", endDate);
// dataParam.addProperty("thing6", remark);
param.add("data", dataParam);
@ -76,7 +92,17 @@ public class WeChartAppletServiceImp implements IWechatAppletService {
JSONObject resultObj = JSONObject.parseObject(entity.getBody());
System.out.println(resultObj.toJSONString());
WxSubscribePostLog postLog = new WxSubscribePostLog();
postLog.setAppid(sysWxUserInfo.getAppid());
postLog.setOpenid(sysWxUserInfo.getOpenid());
postLog.setErrcode(resultObj.getInteger("errcode"));
postLog.setErrmsg(resultObj.getString("errmsg"));
postLog.setSendTime(DateUtils.getNowDate());
postLog.setPlanId(planId);
postLog.setData(dataParam);
wxSubscribePostLogService.insertWxSubscribePostLog(postLog);
return resultObj.toJSONString();
// Integer errcode = resultObj.getInteger("errcode");
}

View File

@ -0,0 +1,26 @@
package com.stdiet.custom.service.impl;
import com.stdiet.custom.domain.wechat.WxSubscribePostLog;
import com.stdiet.custom.mapper.SysWxSubscribePostLogMapper;
import com.stdiet.custom.service.IWxSubscribePostLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class WxSubscribePostLogServiceImp implements IWxSubscribePostLogService {
@Autowired
SysWxSubscribePostLogMapper sysWxSubscribePostLogMapper;
@Override
public int insertWxSubscribePostLog(WxSubscribePostLog postLog) {
return sysWxSubscribePostLogMapper.insertWxSubscribePostLog(postLog);
}
@Override
public List<WxSubscribePostLog> selectWxSubscribePostLog(WxSubscribePostLog postLog) {
return sysWxSubscribePostLogMapper.selectWxSubscribePostLog(postLog);
}
}