提交 e902b98e 编写于 作者: L linkwechat8856@163.com

修复群发相关bug;整合后端应用token获取相关接口;

上级 a75c179a
......@@ -223,5 +223,155 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
}
/**
* 获取当前时间的时间戳
*/
public static int getCurrentTimeIntValue() {
return (int) (System.currentTimeMillis() / 1000);
}
/**
* 获取days天后的当前时间 时间戳
*/
public static int addDaysTimeStamp(int days) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DATE, days);
return (int) (cal.getTimeInMillis() / 1000);
}
/**
* 获取今日零点的时间戳
*/
public static int getStartTimeStamp() {
Calendar todayStart = Calendar.getInstance();
todayStart.set(Calendar.HOUR_OF_DAY, 0);
todayStart.set(Calendar.MINUTE, 0);
todayStart.set(Calendar.SECOND, 0);
todayStart.set(Calendar.MILLISECOND, 0);
long time = todayStart.getTimeInMillis()/1000;
return (int)time;
}
/**
* 获取今日23:59:59的时间戳
*/
public static int getEndTimeStamp() {
Calendar todayEnd = Calendar.getInstance();
todayEnd.set(Calendar.HOUR_OF_DAY, 23);
todayEnd.set(Calendar.MINUTE, 59);
todayEnd.set(Calendar.SECOND, 59);
todayEnd.set(Calendar.MILLISECOND, 999);
long time = todayEnd.getTimeInMillis() / 1000;
return (int) time;
}
/**
* 获取指定时间零点的时间戳
*/
public static int getStartTimeStamp(Date date) {
Calendar todayStart = Calendar.getInstance();
todayStart.setTime(date);
todayStart.set(Calendar.HOUR_OF_DAY, 0);
todayStart.set(Calendar.MINUTE, 0);
todayStart.set(Calendar.SECOND, 0);
todayStart.set(Calendar.MILLISECOND, 0);
long time = todayStart.getTimeInMillis()/1000;
return (int)time;
}
/**
* 获取指定时间23:59:59的时间戳
*/
public static int getEndTimeStamp(Date date) {
Calendar todayEnd = Calendar.getInstance();
todayEnd.setTime(date);
todayEnd.set(Calendar.HOUR_OF_DAY, 23);
todayEnd.set(Calendar.MINUTE, 59);
todayEnd.set(Calendar.SECOND, 59);
todayEnd.set(Calendar.MILLISECOND, 999);
long time = todayEnd.getTimeInMillis() / 1000;
return (int) time;
}
/**
* 获取昨天零点的时间(字符串)
*/
public static String getYesterDayStartTimeStamp() {
SimpleDateFormat sdfYMD = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND,0); //这是将【秒】设置为0
calendar.set(Calendar.MINUTE,0); //这是将【分】设置为0
calendar.set(Calendar.HOUR_OF_DAY,0); //这是将【时】设置为0
calendar.add(Calendar.DATE,-1); //当前日期加一
String yesterday = sdfYMD.format(calendar.getTime()); //获取昨天的时间 如2021-02-25 00:00:00
return yesterday;
}
/**
* 获取昨天零点的时间戳
*/
public static Integer getBeforeStartTime(){
Calendar todayStart = Calendar.getInstance();
todayStart.set(Calendar.HOUR_OF_DAY, 0);
todayStart.set(Calendar.MINUTE, 0);
todayStart.set(Calendar.SECOND, 0);
todayStart.set(Calendar.MILLISECOND, 0);
long time = todayStart.getTimeInMillis()/1000;
return (int)time-86400;
}
/**
* 获取昨天23:59:59的时间(字符串)
*/
public static String getYesterDayEndTimeStamp() {
SimpleDateFormat sdfYMD = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND,59); //这是将当天的【秒】设置为0
calendar.set(Calendar.MINUTE,59); //这是将当天的【分】设置为0
calendar.set(Calendar.HOUR_OF_DAY,23); //这是将当天的【时】设置为0
calendar.add(Calendar.DATE,-1); //当前日期加一
String yesterday = sdfYMD.format(calendar.getTime()); //获取第二天的时间 2021-02-25 00:00:00
return yesterday;
}
/**
* 获取明天零点的时间戳
*/
public static Integer getAfterStartime()
{
Calendar todayStart = Calendar.getInstance();
todayStart.set(Calendar.HOUR_OF_DAY, 0);
todayStart.set(Calendar.MINUTE, 0);
todayStart.set(Calendar.SECOND, 0);
todayStart.set(Calendar.MILLISECOND, 0);
long time = todayStart.getTimeInMillis()/1000;
return (int)time+86400;
}
/**
* 将昨天凌晨时间转换为Date类型(下面设置成这样 new simpleDateFormat("yyyy-MM-dd HH:mm:ss") 数据中对应的字段类型就是DateTime)   * 这个方法中调用了上面的getYesterDayStartTimeStamp()方法哦
*/
public static Date getDateTime()
{
Date dateTime = null;
String yesterDayStartTimeStamp = getYesterDayStartTimeStamp();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try
{
dateTime = formatter.parse(yesterDayStartTimeStamp);
}
catch (ParseException e)
{
e.printStackTrace();
}
//Sun May 09 00:00:00 CST 2021
return dateTime;
}
}
package com.linkwechat.quartz.task;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.utils.DateUtils;
import com.linkwechat.wecom.domain.WeCustomerMessageTimeTask;
import com.linkwechat.wecom.mapper.WeCustomerMessageTimeTaskMapper;
import com.linkwechat.wecom.service.IWeCustomerMessageService;
import com.linkwechat.wecom.service.IWeCustomerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
......@@ -27,13 +26,11 @@ public class GroupMessageTask {
private IWeCustomerMessageService weCustomerMessageService;
/**
* 扫描群发消息定时任务
* 扫描群发消息定时任务(只是处理当天的群发消息)
*/
public void messageTask() {
//获的当前时间的毫秒数
long currentTime = System.currentTimeMillis();
//customerMessageTimeTaskMapper
List<WeCustomerMessageTimeTask> weCustomerMessageTimeTasks = customerMessageTimeTaskMapper.selectWeCustomerMessageTimeTaskGteSettingTime(currentTime);
List<WeCustomerMessageTimeTask> weCustomerMessageTimeTasks = customerMessageTimeTaskMapper.selectWeCustomerMessageTimeTaskGteSettingTime(DateUtils.getDateTime().getTime(),System.currentTimeMillis());
if (CollectionUtils.isNotEmpty(weCustomerMessageTimeTasks)) {
weCustomerMessageTimeTasks.forEach(
......
......@@ -39,8 +39,10 @@ public class WeAppAccessTokenInterceptor implements Interceptor<WeResultDto> {
*/
@Override
public boolean beforeExecute(ForestRequest request) {
String token = iWeAccessTokenService.findThirdAppAccessToken(request.getHeaderValue(WeConstans.THIRD_APP_PARAM_TIP));
request.replaceOrAddQuery("access_token", token);
// String token = iWeAccessTokenService.findThirdAppAccessToken(request.getHeaderValue(WeConstans.THIRD_APP_PARAM_TIP));
request.replaceOrAddQuery("access_token",
iWeAccessTokenService.findThirdAppAccessToken()
);
return true;
}
......
......@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.linkwechat.wecom.domain.WeCustomerMessageTimeTask;
import org.apache.ibatis.annotations.Param;
import java.util.Date;
import java.util.List;
/**
......@@ -20,7 +21,7 @@ public interface WeCustomerMessageTimeTaskMapper extends BaseMapper<WeCustomerMe
* @param timeMillis 当前时间毫秒数
* @return {@link WeCustomerMessageTimeTask}s
*/
List<WeCustomerMessageTimeTask> selectWeCustomerMessageTimeTaskGteSettingTime(@Param("timeMillis") long timeMillis);
List<WeCustomerMessageTimeTask> selectWeCustomerMessageTimeTaskGteSettingTime(@Param("beginTime") Long beginTime,@Param("endTime") Long endTime);
/**
* 保存群发任务
......
......@@ -21,7 +21,7 @@ public interface IWeAccessTokenService {
public String findChatAccessToken();
public String findThirdAppAccessToken(String agentId);
public String findThirdAppAccessToken();
......
......@@ -90,34 +90,38 @@ public class WeAccessTokenServiceImpl implements IWeAccessTokenService {
/**
* 获取应用所需要的token
* @param agentId
* 获取应用所需要的token(移除weapp,直接从account表中获取相关信息)
* @return
*/
@Override
public String findThirdAppAccessToken(String agentId) {
String token=redisCache.getCacheObject(WeConstans.WE_THIRD_APP_TOKEN+"::"+agentId);
if(StringUtils.isNotEmpty(token)){
return token;
}
public String findThirdAppAccessToken() {
WeApp weApp=null;
//首先表中查询提醒的appid
WeCorpAccount weCorpAccount = iWxCorpAccountService.findValidWeCorpAccount();
if(weCorpAccount != null && StringUtils.isNotEmpty(weCorpAccount.getAgentId())){
if(weCorpAccount.getAgentId().equals(agentId)){
String token=redisCache.getCacheObject(WeConstans.WE_THIRD_APP_TOKEN+"::"+weCorpAccount.getAgentId());
if(StringUtils.isNotEmpty(token)){
return token;
}else{
weApp=WeApp.builder()
.agentId(weCorpAccount.getAgentId())
.agentSecret(weCorpAccount.getAgentSecret())
.build();
}
// if(weCorpAccount.getAgentId().equals(agentId)){
//
// }
}
if(weApp==null){
weApp = iWeAppService.getOne(new LambdaQueryWrapper<WeApp>()
.eq(WeApp::getAgentId, agentId)
.eq(WeApp::getDelFlag, Constants.NORMAL_CODE)
.eq(WeApp::getStatus, Constants.NORMAL_CODE));
}
// if(weApp==null){
// weApp = iWeAppService.getOne(new LambdaQueryWrapper<WeApp>()
// .eq(WeApp::getAgentId, agentId)
// .eq(WeApp::getDelFlag, Constants.NORMAL_CODE)
// .eq(WeApp::getStatus, Constants.NORMAL_CODE));
// }
return findThirdAppAccessToken(weApp);
......
......@@ -33,6 +33,8 @@ import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @description: 群发消息服务类
......@@ -168,7 +170,9 @@ public class WeCustomerMessagePushServiceImpl implements IWeCustomerMessagePushS
//发送群发消息
//调用微信api发送消息
if (null == customerMessagePushDto.getSettingTime() || customerMessagePushDto.getSettingTime().equals("")) {
weCustomerMessageService.sendMessgae(customerMessagePushDto, messageId, customers, groups);
weCustomerMessageService.sendMessgae(customerMessagePushDto, messageId,customers, groups);
} else {
WeCustomerMessageTimeTask timeTask = new WeCustomerMessageTimeTask(messageId, customerMessagePushDto, customers, groups
, DateUtils.getMillionSceondsBydate(customerMessagePushDto.getSettingTime()));
......
package com.linkwechat.wecom.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import com.alibaba.fastjson.JSONObject;
......@@ -27,6 +28,7 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
......@@ -97,48 +99,60 @@ public class WeCustomerMessageServiceImpl extends ServiceImpl<WeCustomerMessageM
@Override
public void sendMessgae(CustomerMessagePushDto customerMessagePushDto, long messageId,List<WeCustomer> customers,List<WeGroup> groups) throws JsonProcessingException {
List<String> msgid = new ArrayList<>();
//发送群发消息
//发送类类型: 给单个客户发,群发
//发给客户
if (customerMessagePushDto.getPushType().equals(WeConstans.SEND_MESSAGE_CUSTOMER)) {
WeCustomerMessagePushDto messagePushDto = new WeCustomerMessagePushDto();
messagePushDto.setChat_type(ChatType.of(customerMessagePushDto.getPushType()).getName());
List<String> externalUserIds = customers.stream().map(WeCustomer::getExternalUserid).collect(Collectors.toList());
messagePushDto.setExternal_userid(externalUserIds);
messagePushDto.setSender(customerMessagePushDto.getStaffId());
childMessage(messagePushDto, customerMessagePushDto);
SendMessageResultDto sendMessageResultDto = weCustomerMessagePushClient.sendCustomerMessageToUser(messagePushDto);
if (WeConstans.WE_SUCCESS_CODE.equals(sendMessageResultDto.getErrcode())) {
msgid.add(sendMessageResultDto.getMsgid());
}
}
//发给客户
if(CollectionUtils.isNotEmpty(customers)){
customers.stream().collect(Collectors.groupingBy(WeCustomer::getUserId)).forEach((k,v)->{
//发给客户群
if (customerMessagePushDto.getPushType().equals(WeConstans.SEND_MESSAGE_GROUP)) {
customerMessagePushDto.setStaffId(k);
//发送群发消息
//发送类类型: 给单个客户发,群发
if (customerMessagePushDto.getPushType().equals(WeConstans.SEND_MESSAGE_CUSTOMER)) {
if (CollectionUtils.isNotEmpty(groups)) {
List<String> owners = groups.stream().map(WeGroup::getOwner).collect(Collectors.toList());
for (String owner : owners) {
WeCustomerMessagePushDto messagePushDto = new WeCustomerMessagePushDto();
messagePushDto.setChat_type(ChatType.of(customerMessagePushDto.getPushType()).getName());
//客户群的员工id
messagePushDto.setSender(owner);
List<String> externalUserIds = v.stream().map(WeCustomer::getExternalUserid).collect(Collectors.toList());
messagePushDto.setExternal_userid(externalUserIds);
messagePushDto.setSender(customerMessagePushDto.getStaffId());
childMessage(messagePushDto, customerMessagePushDto);
SendMessageResultDto sendMessageResultDto = weCustomerMessagePushClient.sendCustomerMessageToUser(messagePushDto);
if (WeConstans.WE_SUCCESS_CODE.equals(sendMessageResultDto.getErrcode())) {
//发送的msgId
msgid.add(sendMessageResultDto.getMsgid());
}
}
}
});
}
//发给客户群
if(CollectionUtil.isNotEmpty(groups)){
if (customerMessagePushDto.getPushType().equals(WeConstans.SEND_MESSAGE_GROUP)) {
if (CollectionUtils.isNotEmpty(groups)) {
Set<String> owners = groups.stream().map(WeGroup::getOwner).collect(Collectors.toSet());
for (String owner : owners) {
WeCustomerMessagePushDto messagePushDto = new WeCustomerMessagePushDto();
messagePushDto.setChat_type(ChatType.of(customerMessagePushDto.getPushType()).getName());
//客户群的员工id
messagePushDto.setSender(owner);
childMessage(messagePushDto, customerMessagePushDto);
SendMessageResultDto sendMessageResultDto = weCustomerMessagePushClient.sendCustomerMessageToUser(messagePushDto);
if (WeConstans.WE_SUCCESS_CODE.equals(sendMessageResultDto.getErrcode())) {
//发送的msgId
msgid.add(sendMessageResultDto.getMsgid());
}
}
}
}
}
this.updateMsgId(messageId, msgid);
}
......@@ -160,8 +174,8 @@ public class WeCustomerMessageServiceImpl extends ServiceImpl<WeCustomerMessageM
if (customerMessagePushDto.getMessageType().equals(GroupMessageType.IMAGE.getType())) {
ImageMessageDto imageMessage = customerMessagePushDto.getImageMessage();
try {
WeMediaDto weMediaDto = weMaterialService.uploadTemporaryMaterial(imageMessage.getPic_url(),
FileUtil.getName(imageMessage.getPic_url()),GroupMessageType.IMAGE.getMessageType());
WeMediaDto weMediaDto = weMaterialService.uploadTemporaryMaterial(imageMessage.getPic_url(),GroupMessageType.IMAGE.getMessageType(),
FileUtil.getName(imageMessage.getPic_url()));
imageMessage.setMedia_id(weMediaDto.getMedia_id());
} catch (Exception e) {
}
......
......@@ -57,9 +57,7 @@
setting_time
FROM
we_customer_messageTimeTask
<where>
setting_time <![CDATA[<=]]> #{timeMillis} AND solved=0
</where>
WHERE solved=0 and setting_time BETWEEN #{beginTime} AND #{endTime}
</select>
</mapper>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册