|
|
@@ -0,0 +1,110 @@
|
|
|
+package org.springblade.salesPart.util;
|
|
|
+
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.springblade.core.tool.utils.CollectionUtil;
|
|
|
+import org.springblade.salesPart.enums.FinalMap;
|
|
|
+import org.springblade.salesPart.push.dto.AppMsgPushDto;
|
|
|
+import org.springblade.salesPart.push.entity.UserPushCid;
|
|
|
+import org.springblade.salesPart.push.mapper.UserPushCidMapper;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author Rain
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@AllArgsConstructor
|
|
|
+@Slf4j
|
|
|
+public class PushUtil {
|
|
|
+
|
|
|
+ private final UserPushCidMapper userPushCidMapper;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送单用户推送
|
|
|
+ *
|
|
|
+ * @param userId 用户系统id
|
|
|
+ * @param pushDto 推送内容
|
|
|
+ * @return 消息
|
|
|
+ */
|
|
|
+ public String sendAppTitleMsgByOne(Long userId, AppMsgPushDto pushDto) {
|
|
|
+ if (Objects.isNull(userId) || Objects.isNull(pushDto)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ UserPushCid userPush = userPushCidMapper.selectOne(new LambdaQueryWrapper<UserPushCid>().eq(UserPushCid::getUserId, userId).eq(UserPushCid::getStatus, 1).orderByDesc(UserPushCid::getLastActiveTime).last("limit 1"));
|
|
|
+ if (Objects.isNull(userPush)) {
|
|
|
+ log.error("用户id:{}没有推送id", userId);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ pushDto.setCId(userPush.getCid());
|
|
|
+ pushDto.setRequestId(IdUtil.getSnowflakeNextIdStr());
|
|
|
+ try {
|
|
|
+ //创建一个获取连接客户端的工具
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+ //创建Post请求
|
|
|
+ HttpPost httpPost = new HttpPost(FinalMap.APP_PUSH_URL);
|
|
|
+ //添加请求头
|
|
|
+ httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
|
|
|
+ //封装请求参数,将map集合转换成json格式
|
|
|
+ StringEntity entity = new StringEntity(JSONObject.toJSONString(pushDto), "UTF-8");
|
|
|
+ //将封装的参数添加到Post请求中
|
|
|
+ httpPost.setEntity(entity);
|
|
|
+ //执行请求
|
|
|
+ CloseableHttpResponse response = httpClient.execute(httpPost);
|
|
|
+ //获取响应的实体
|
|
|
+ HttpEntity responseEntity = response.getEntity();
|
|
|
+ //转化成字符串
|
|
|
+ return EntityUtils.toString(responseEntity);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String sendAppTitleMsgByList(List<Long> userIds, AppMsgPushDto pushDto) {
|
|
|
+ if (CollectionUtil.isEmpty(userIds) || Objects.isNull(pushDto)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ List<UserPushCid> userPushCidList = userPushCidMapper.selectList(new LambdaQueryWrapper<UserPushCid>().in(UserPushCid::getUserId, userIds).eq(UserPushCid::getStatus, 1));
|
|
|
+ if (CollectionUtil.isEmpty(userPushCidList)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ pushDto.setCId(userPushCidList.stream().map(UserPushCid::getCid).collect(Collectors.joining(",")));
|
|
|
+ pushDto.setRequestId(IdUtil.getSnowflakeNextIdStr());
|
|
|
+ try {
|
|
|
+ //创建一个获取连接客户端的工具
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+ //创建Post请求
|
|
|
+ HttpPost httpPost = new HttpPost(FinalMap.APP_PUSH_URL);
|
|
|
+ //添加请求头
|
|
|
+ httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
|
|
|
+ //封装请求参数,将map集合转换成json格式
|
|
|
+ StringEntity entity = new StringEntity(JSONObject.toJSONString(pushDto), "UTF-8");
|
|
|
+ //将封装的参数添加到Post请求中
|
|
|
+ httpPost.setEntity(entity);
|
|
|
+ //执行请求
|
|
|
+ CloseableHttpResponse response = httpClient.execute(httpPost);
|
|
|
+ //获取响应的实体
|
|
|
+ HttpEntity responseEntity = response.getEntity();
|
|
|
+ //转化成字符串
|
|
|
+ return EntityUtils.toString(responseEntity);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|