|
|
@ -21,16 +21,21 @@ import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.epmet.commons.tools.constant.NumConstant; |
|
|
|
import com.epmet.commons.tools.enums.EnvEnum; |
|
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
|
import com.epmet.commons.tools.exception.ValidateException; |
|
|
|
import com.epmet.commons.tools.utils.ConvertUtils; |
|
|
|
import com.epmet.commons.tools.utils.HttpClientManager; |
|
|
|
import com.epmet.commons.tools.utils.Result; |
|
|
|
import com.epmet.commons.tools.validator.ValidatorUtils; |
|
|
|
import com.epmet.constant.WxmpMessageConstant; |
|
|
|
import com.epmet.dao.WxmpResiUserSubscribeDao; |
|
|
|
import com.epmet.dao.WxmpTemplateMsgSubscribeStatusDao; |
|
|
|
import com.epmet.dao.WxmpWorkUserSubscribeDao; |
|
|
|
import com.epmet.dto.form.GetTemplateListFormDTO; |
|
|
|
import com.epmet.dto.form.StaffBasicInfoFormDTO; |
|
|
|
import com.epmet.dto.form.UserBasicInfoFormDTO; |
|
|
|
import com.epmet.dto.form.WxSubscribeMessageFormDTO; |
|
|
|
import com.epmet.dto.result.GetTemplateListResultDTO; |
|
|
|
import com.epmet.entity.WxmpMsgSendRecordEntity; |
|
|
|
import com.epmet.entity.WxmpTemplateMsgSubscribeStatusEntity; |
|
|
|
import com.epmet.exception.WxSubscribeException; |
|
|
@ -81,8 +86,8 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { |
|
|
|
private WxmpMsgSendRecordService wxmpMsgSendRecordService; |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 保存系统授权信息 |
|
|
|
* @return void |
|
|
|
* @Description 保存系统授权信息 |
|
|
|
* @author wxz |
|
|
|
* @date 2020.10.21 17:29 |
|
|
|
*/ |
|
|
@ -121,7 +126,7 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { |
|
|
|
String userId = msg.getUserId(); |
|
|
|
String clientType = msg.getClientType(); |
|
|
|
String customerId = msg.getCustomerId(); |
|
|
|
String templateId = msg.getTemplateId(); |
|
|
|
String templateId = null; |
|
|
|
String openId = null; |
|
|
|
//通过userId获取openId
|
|
|
|
try { |
|
|
@ -134,14 +139,14 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { |
|
|
|
staffBasicInfoFormDTO.setStaffId(userId); |
|
|
|
openId = epmetUserOpenFeignClient.getStaffBasicInfo(staffBasicInfoFormDTO).getData().getOpenId(); |
|
|
|
} else { |
|
|
|
throw new WxSubscribeException("clientType有误",openId); |
|
|
|
throw new WxSubscribeException("clientType有误", "", openId); |
|
|
|
} |
|
|
|
}catch (Exception e){ |
|
|
|
throw new WxSubscribeException("连接User服务失败",""); |
|
|
|
} catch (Exception e) { |
|
|
|
throw new WxSubscribeException("连接User服务失败", "", ""); |
|
|
|
} |
|
|
|
|
|
|
|
if (StringUtils.isBlank(openId)){ |
|
|
|
throw new WxSubscribeException("openId获取失败",""); |
|
|
|
if (StringUtils.isBlank(openId)) { |
|
|
|
throw new WxSubscribeException("openId获取失败", "", ""); |
|
|
|
} |
|
|
|
|
|
|
|
//获取accessToken
|
|
|
@ -149,12 +154,22 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { |
|
|
|
Map<String, Object> authorizerRefreshToken = new HashMap<>(); |
|
|
|
try { |
|
|
|
authorizerRefreshToken = wxmpMessageRedis.getAuthorizerRefreshToken(key.toString()); |
|
|
|
}catch (Exception e){ |
|
|
|
throw new WxSubscribeException("连接缓存服务器失败",openId); |
|
|
|
} catch (Exception e) { |
|
|
|
throw new WxSubscribeException("连接缓存服务器失败", "", openId); |
|
|
|
} |
|
|
|
String accessToken = (String) authorizerRefreshToken.get(WxmpMessageConstant.AUTHORIZER_ACCESS_TOKEN); |
|
|
|
if (StringUtils.isBlank(accessToken)) { |
|
|
|
throw new WxSubscribeException("accessToken获取失败",openId); |
|
|
|
throw new WxSubscribeException("accessToken获取失败", "", openId); |
|
|
|
} |
|
|
|
|
|
|
|
//获取模板id
|
|
|
|
if (WxmpMessageConstant.RESI.equals(clientType)) { |
|
|
|
templateId = wxmpResiUserSubscribeDao.getResiSubscribeTemplateId(openId, templateId, customerId); |
|
|
|
} else if (WxmpMessageConstant.WORK.equals(clientType)) { |
|
|
|
templateId = wxmpWorkUserSubscribeDao.getWorkSubscribeTemplateId(openId, templateId, customerId); |
|
|
|
} |
|
|
|
if (StringUtils.isBlank(templateId)) { |
|
|
|
throw new WxSubscribeException("获取模板id失败", "", openId); |
|
|
|
} |
|
|
|
|
|
|
|
//判断用户是否有次数
|
|
|
@ -166,10 +181,10 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { |
|
|
|
} |
|
|
|
if (count == null) { |
|
|
|
//用户未订阅
|
|
|
|
throw new WxSubscribeException("用户未订阅",openId); |
|
|
|
throw new WxSubscribeException("用户未订阅", templateId, openId); |
|
|
|
} |
|
|
|
if (count == 0) { |
|
|
|
throw new WxSubscribeException("用户可用额度不足",openId); |
|
|
|
throw new WxSubscribeException("用户可用额度不足", templateId, openId); |
|
|
|
} |
|
|
|
|
|
|
|
//发送消息
|
|
|
@ -179,9 +194,9 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { |
|
|
|
jsonObject.put(WxmpMessageConstant.ACCESS_TOKEN, accessToken); |
|
|
|
jsonObject.put(WxmpMessageConstant.TOUSER, openId); |
|
|
|
jsonObject.put(WxmpMessageConstant.TEMPLATE_ID, templateId); |
|
|
|
data.put(WxmpMessageConstant.TITLE, new JSONObject().put("value", "您有一条" + msg.getBehaviorType())); |
|
|
|
data.put(WxmpMessageConstant.MESSAGE_CONTENT, new JSONObject().put("value", msg.getMessageContent())); |
|
|
|
data.put(WxmpMessageConstant.MESSAGE_TIME, new JSONObject().put("value", msg.getMessageTime())); |
|
|
|
data.put(WxmpMessageConstant.TITLE, new JSONObject().put("value", ("您有一条" + msg.getBehaviorType()).substring(0, WxmpMessageConstant.TITLE_LIMIT))); |
|
|
|
data.put(WxmpMessageConstant.MESSAGE_CONTENT, new JSONObject().put("value", msg.getMessageContent().substring(0, WxmpMessageConstant.MESSAGE_CONTENT_LIMIT))); |
|
|
|
data.put(WxmpMessageConstant.MESSAGE_TIME, new JSONObject().put("value", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()))); |
|
|
|
jsonObject.put(WxmpMessageConstant.DATA, data); |
|
|
|
EnvEnum envEnum = EnvEnum.getCurrentEnv(); |
|
|
|
//选填项
|
|
|
@ -191,11 +206,11 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { |
|
|
|
jsonObject.put(WxmpMessageConstant.PAGE, WxmpMessageConstant.PAGE_WORK); |
|
|
|
} |
|
|
|
//开发环境
|
|
|
|
if ("dev".equals(envEnum.getCode())){ |
|
|
|
if ("dev".equals(envEnum.getCode())) { |
|
|
|
jsonObject.put(WxmpMessageConstant.MINIPROGRAM_STATE, WxmpMessageConstant.STATE_DEV); |
|
|
|
} |
|
|
|
//测试环境
|
|
|
|
if ("test".equals(envEnum.getCode())){ |
|
|
|
if ("test".equals(envEnum.getCode())) { |
|
|
|
jsonObject.put(WxmpMessageConstant.MINIPROGRAM_STATE, WxmpMessageConstant.STATE_TEST); |
|
|
|
} |
|
|
|
|
|
|
@ -216,7 +231,7 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { |
|
|
|
logger.error("消息{}发送成功但订阅条数-1失败", JSON.toJSONString(msg)); |
|
|
|
} |
|
|
|
//存表
|
|
|
|
int saveRes = wxmpMsgSendRecordService.saveRecord(initRecord(msg,openId,WxmpMessageConstant.SUCCESS)); |
|
|
|
int saveRes = wxmpMsgSendRecordService.saveRecord(initRecord(msg, templateId, openId, WxmpMessageConstant.SUCCESS)); |
|
|
|
if (saveRes == 0) { |
|
|
|
logger.error("消息{}发送成功但存入记录表失败", JSON.toJSONString(msg)); |
|
|
|
} |
|
|
@ -236,7 +251,7 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { |
|
|
|
} |
|
|
|
|
|
|
|
//抛出错误
|
|
|
|
throw new WxSubscribeException(String.valueOf(resultMap.get(WxmpMessageConstant.ERR_MSG)),openId); |
|
|
|
throw new WxSubscribeException(String.valueOf(resultMap.get(WxmpMessageConstant.ERR_MSG)),templateId, openId); |
|
|
|
} |
|
|
|
|
|
|
|
succecssCount++; |
|
|
@ -246,9 +261,9 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { |
|
|
|
// if (StringUtils.isBlank(errMsg) && e instanceof ValidateException) {
|
|
|
|
// errMsg = ((ValidateException) e).getMsg();
|
|
|
|
// }
|
|
|
|
if (e instanceof WxSubscribeException){ |
|
|
|
if (e instanceof WxSubscribeException) { |
|
|
|
//存表
|
|
|
|
WxmpMsgSendRecordEntity wxmpMsgSendRecordEntity = initRecord(msg, ((WxSubscribeException) e).getOpenId(), WxmpMessageConstant.ERROR); |
|
|
|
WxmpMsgSendRecordEntity wxmpMsgSendRecordEntity = initRecord(msg, ((WxSubscribeException) e).getTemplateId(), ((WxSubscribeException) e).getOpenId(), WxmpMessageConstant.ERROR); |
|
|
|
wxmpMsgSendRecordEntity.setReason(errMsg); |
|
|
|
int saveRes = wxmpMsgSendRecordService.saveRecord(wxmpMsgSendRecordEntity); |
|
|
|
if (saveRes == 0) { |
|
|
@ -262,23 +277,46 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { |
|
|
|
} |
|
|
|
logger.info("{}条消息中的{}条发送成功", msgList.size(), succecssCount); |
|
|
|
} |
|
|
|
|
|
|
|
//初始化记录对象
|
|
|
|
private WxmpMsgSendRecordEntity initRecord(WxSubscribeMessageFormDTO msg, String openId, String status){ |
|
|
|
private WxmpMsgSendRecordEntity initRecord(WxSubscribeMessageFormDTO msg, String templateId, String openId, String status) { |
|
|
|
WxmpMsgSendRecordEntity wxmpMsgSendRecordEntity = new WxmpMsgSendRecordEntity(); |
|
|
|
wxmpMsgSendRecordEntity.setCustomerId(msg.getCustomerId()); |
|
|
|
wxmpMsgSendRecordEntity.setClientType(msg.getClientType()); |
|
|
|
wxmpMsgSendRecordEntity.setTemplateId(msg.getTemplateId()); |
|
|
|
wxmpMsgSendRecordEntity.setTemplateId(templateId); |
|
|
|
wxmpMsgSendRecordEntity.setUserId(msg.getUserId()); |
|
|
|
wxmpMsgSendRecordEntity.setWxOpenId(openId); |
|
|
|
wxmpMsgSendRecordEntity.setBehaviorType(msg.getBehaviorType()); |
|
|
|
wxmpMsgSendRecordEntity.setTitle("您有一条" + msg.getBehaviorType()); |
|
|
|
wxmpMsgSendRecordEntity.setMessageContent(msg.getMessageContent()); |
|
|
|
try { |
|
|
|
wxmpMsgSendRecordEntity.setMessageTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(msg.getMessageTime())); |
|
|
|
} catch (ParseException e) { |
|
|
|
wxmpMsgSendRecordEntity.setMessageTime(new Date()); |
|
|
|
} |
|
|
|
wxmpMsgSendRecordEntity.setResult(status); |
|
|
|
return wxmpMsgSendRecordEntity; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @return |
|
|
|
* @Description 居民端、工作端-获取客户小程序模板列表 |
|
|
|
* @author sun |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public List<GetTemplateListResultDTO> templateList(GetTemplateListFormDTO formDTO) { |
|
|
|
GetTemplateListFormDTO dto = ConvertUtils.sourceToTarget(formDTO, GetTemplateListFormDTO.class); |
|
|
|
String url = "https://epmet-cloud.elinkservice.cn/api/third/personaltemplate/templatelist"; |
|
|
|
//String url = "http://localhost:8080/api/third/personaltemplate/templatelist";
|
|
|
|
String data = HttpClientManager.getInstance().sendPostByJSON(url, JSON.toJSONString(dto)).getData(); |
|
|
|
logger.info("ThirdLoginServiceImpl.getUserWeChat:httpclient->url:"+url+",结果->"+data); |
|
|
|
JSONObject toResult = JSON.parseObject(data); |
|
|
|
Result mapToResult = ConvertUtils.mapToEntity(toResult, Result.class); |
|
|
|
if (null != toResult.get("code")) { |
|
|
|
mapToResult.setCode(((Integer) toResult.get("code")).intValue()); |
|
|
|
} |
|
|
|
if (!mapToResult.success()) { |
|
|
|
logger.error("调用epmet_third服务获取小程序消息订阅模板数据失败"); |
|
|
|
throw new RenException(mapToResult.getCode()); |
|
|
|
} |
|
|
|
List<GetTemplateListResultDTO> resultList = (List<GetTemplateListResultDTO>) mapToResult.getData(); |
|
|
|
return resultList; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|