|
|
|
@ -1,10 +1,37 @@
@@ -1,10 +1,37 @@
|
|
|
|
|
package com.cloud.kicc.commonbiz.service.impl; |
|
|
|
|
|
|
|
|
|
import cn.hutool.core.date.LocalDateTimeUtil; |
|
|
|
|
import cn.hutool.core.util.ObjectUtil; |
|
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
|
import cn.hutool.extra.servlet.ServletUtil; |
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
|
import com.cloud.kicc.common.core.api.R; |
|
|
|
|
import com.cloud.kicc.common.core.constant.SecurityConstants; |
|
|
|
|
import com.cloud.kicc.common.core.exception.CheckedException; |
|
|
|
|
import com.cloud.kicc.commonbiz.api.entity.Message; |
|
|
|
|
import com.cloud.kicc.commonbiz.api.entity.PushApplication; |
|
|
|
|
import com.cloud.kicc.commonbiz.api.entity.UserPush; |
|
|
|
|
import com.cloud.kicc.commonbiz.mapper.PushApplicationMapper; |
|
|
|
|
import com.cloud.kicc.commonbiz.service.IPushApplicationService; |
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
|
import com.cloud.kicc.commonbiz.service.UserPushService; |
|
|
|
|
import com.cloud.kicc.commonbiz.util.PushClientUtil; |
|
|
|
|
import com.cloud.kicc.system.api.entity.User; |
|
|
|
|
import com.cloud.kicc.system.api.feign.RemoteUserService; |
|
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
|
import okhttp3.OkHttpClient; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
import org.springframework.web.context.request.RequestContextHolder; |
|
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes; |
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
import java.time.format.DateTimeFormatter; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Objects; |
|
|
|
|
import java.util.Optional; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* <p> |
|
|
|
@ -15,6 +42,74 @@ import org.springframework.stereotype.Service;
@@ -15,6 +42,74 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
* @since 2023-02-16 |
|
|
|
|
*/ |
|
|
|
|
@Service |
|
|
|
|
@RequiredArgsConstructor |
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
|
public class PushApplicationServiceImpl extends ServiceImpl<PushApplicationMapper, PushApplication> implements IPushApplicationService { |
|
|
|
|
|
|
|
|
|
private final OkHttpClient okHttpClient; |
|
|
|
|
private final UserPushService userPushService; |
|
|
|
|
private final RemoteUserService remoteUserService; |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void messageSend(Message message) { |
|
|
|
|
|
|
|
|
|
if (StrUtil.isBlank(message.getSendUserId()) || StrUtil.isBlank(message.getMessageSecret())) { |
|
|
|
|
throw new CheckedException("当前发送用户ID与推送应用密钥必填!"); |
|
|
|
|
} |
|
|
|
|
R<User> user = remoteUserService.selectByUserId(message.getSendUserId(), SecurityConstants.FROM_IN); |
|
|
|
|
|
|
|
|
|
// 第三方发送前较验数据
|
|
|
|
|
if (user.getData().getUserType().equals("9")) { |
|
|
|
|
PushApplication pushApplication = baseMapper.selectOne(Wrappers.<PushApplication>lambdaQuery() |
|
|
|
|
.eq(PushApplication::getMessageSecret, message.getMessageSecret()).eq(PushApplication::getStatus, "0")); |
|
|
|
|
pushApplication = Optional.of(pushApplication).orElseThrow(() -> new CheckedException("你当前没有权限发送消息,请联系管理员!")); |
|
|
|
|
HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); |
|
|
|
|
// 如果ip地址存在即验证发送方ip
|
|
|
|
|
if (StrUtil.isNotBlank(pushApplication.getIp()) && !StrUtil.contains(pushApplication.getIp(), ServletUtil.getClientIP(request))) { |
|
|
|
|
throw new CheckedException("你的ip地址不正确,请检查消息应用中设置的ip地址!"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
List<UserPush> pushList = userPushService.list(Wrappers.<UserPush>lambdaQuery().eq(UserPush::getUserId, message.getSendUserId())); |
|
|
|
|
if (ObjectUtil.isNotEmpty(pushList)) { |
|
|
|
|
// 设置默认数据
|
|
|
|
|
message.setDisplayType("notification"); |
|
|
|
|
message.setAliasType(PushClientUtil.uPushUserAliasType); |
|
|
|
|
message.setAlias(pushList.stream().map(item -> item.getPushId()).collect(Collectors.joining(","))); |
|
|
|
|
JSONObject custom = new JSONObject(); |
|
|
|
|
custom.put("sendUserId", message.getSendUserId()); |
|
|
|
|
message.setCustom(custom.toString()); |
|
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
|
jsonObject.put("production_mode", PushClientUtil.PRODUCTION_MODE); |
|
|
|
|
jsonObject.put("type", "customizedcast"); |
|
|
|
|
jsonObject.put("description", message.getRemarks()); |
|
|
|
|
jsonObject.put("alias_type", message.getAliasType()); |
|
|
|
|
jsonObject.put("alias", message.getAlias()); |
|
|
|
|
JSONObject payload = new JSONObject(); |
|
|
|
|
payload.put("display_type", message.getDisplayType()); |
|
|
|
|
JSONObject body = new JSONObject(); |
|
|
|
|
body.put("ticker", message.getTitle()); |
|
|
|
|
body.put("text", message.getText()); |
|
|
|
|
body.put("title", message.getTitle()); |
|
|
|
|
body.put("sound", message.getSound()); |
|
|
|
|
body.put("after_open", "go_custom"); |
|
|
|
|
body.put("custom", message.getCustom()); |
|
|
|
|
body.put("play_sound", message.getPlaySound()); |
|
|
|
|
body.put("play_vibrate", message.getPlayVibrate()); |
|
|
|
|
body.put("play_lights", message.getPlayLights()); |
|
|
|
|
payload.put("body", body); |
|
|
|
|
jsonObject.put("payload", payload); |
|
|
|
|
JSONObject policy = new JSONObject(); |
|
|
|
|
policy.put("expire_time", LocalDateTimeUtil.now().plusDays(3).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); |
|
|
|
|
policy.put("out_biz_no", LocalDateTimeUtil.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); |
|
|
|
|
jsonObject.put("policy", policy); |
|
|
|
|
try { |
|
|
|
|
PushClientUtil pushClientUtil = new PushClientUtil(okHttpClient); |
|
|
|
|
pushClientUtil.send(jsonObject); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|