@ -1,27 +1,19 @@
@@ -1,27 +1,19 @@
package com.cloud.kicc.commonbiz.service.impl ;
import cn.hutool.core.collection.CollectionUtil ;
import cn.hutool.core.date.LocalDateTimeUtil ;
import cn.hutool.core.util.BooleanUtil ;
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.* ;
import com.cloud.kicc.commonbiz.mapper.PushApplicationMapper ;
import com.cloud.kicc.commonbiz.service.* ;
import com.cloud.kicc.commonbiz.util.PushClientUtil ;
import com.cloud.kicc.system.api.entity.User ;
import com.cloud.kicc.system.api.feign.RemoteUserService ;
import io.lettuce.core.api.push.PushMessage ;
import lombok.RequiredArgsConstructor ;
import okhttp3.OkHttpClient ;
import org.springframework.security.core.userdetails.UsernameNotFoundException ;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;
import org.springframework.web.context.request.RequestContextHolder ;
@ -48,97 +40,102 @@ import java.util.stream.Collectors;
@@ -48,97 +40,102 @@ import java.util.stream.Collectors;
public class PushApplicationServiceImpl extends ServiceImpl < PushApplicationMapper , PushApplication > implements IPushApplicationService {
private final OkHttpClient okHttpClient ;
//private final IPushUserManageService iPushUserManageService;
//private final IPushMessageService iPushMessageService;
private final RemoteUserService remoteUser Service;
//private final IPushCustomTypeService iPushCustomTypeService;
private final IPushConcernFanService iPushConcernFanService ;
private final IPushBlacklistService iPushBlacklistService ;
private final IPushChatMessageService iPushChatMessage Service;
private final IPushConcernFanTypeService iPushConcernFanTypeService ;
@Override
public void messageSend ( PushChatMessage pushChatMessage ) {
/ * if ( StrUtil . isBlank ( pushChatMessage . getPushUserId ( ) ) | | StrUtil . isBlank ( pushChatMessage . getMessageSecret ( ) ) ) {
throw new CheckedException ( "当前发送方用户ID与推送应用密钥必填!" ) ;
} * /
R < User > result = remoteUserService . selectByUserId ( pushChatMessage . getPushTypeId ( ) , SecurityConstants . FROM_IN ) ;
if ( result = = null | | result . getData ( ) = = null ) {
throw new CheckedException ( "用户不存在" ) ;
public void enterpriseMessageSend ( PushChatMessage pushChatMessage ) {
if ( StrUtil . isBlank ( pushChatMessage . getUserId ( ) ) | | StrUtil . isBlank ( pushChatMessage . getMessageSecret ( ) ) ) {
throw new CheckedException ( "当前推送方用户ID与推送应用密钥必填!" ) ;
}
// 第三方发送前较验数据
if ( result . getData ( ) . getUserType ( ) . equals ( "9" ) ) {
PushApplication pushApplication = baseMapper . selectOne ( Wrappers . < PushApplication > lambdaQuery ( )
. eq ( PushApplication : : getMessageSecret , pushChatMessage . getMessageSecret ( ) ) . eq ( PushApplication : : getStatus , "0" ) ) ;
. eq ( PushApplication : : getMessageSecret , pushChatMessage . getMessageSecret ( ) )
. eq ( PushApplication : : getCreateById , pushChatMessage . getUserId ( ) )
. 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 . getIgnoreIp ( ) ) & & ! StrUtil . contains ( pushApplication . getIgnoreIp ( ) , ServletUtil . getClientIP ( request ) ) ) {
throw new CheckedException ( "你的ip地址不正确,请检查消息应用中设置的ip地址!" ) ;
}
}
// 查询当前用户设置的自定义快捷方式消息提醒属性
/ * PushCustomType pushCustomType = iPushCustomTypeService . getOne ( Wrappers . < PushCustomType > lambdaQuery ( )
. eq ( PushCustomType : : getId , pushChatMessage . getPushTypeId ( ) ) . eq ( PushCustomType : : getCreateById , pushChatMessage . getFromUserId ( ) ) ) ;
if ( ObjectUtil . isNotEmpty ( pushCustomType ) ) {
pushMessage . setPlaySound ( pushCustomType . getPlaySound ( ) ) ;
pushMessage . setPlayVibrate ( pushCustomType . getPlayVibrate ( ) ) ;
pushMessage . setPlayLights ( pushCustomType . getPlayLights ( ) ) ;
if ( StrUtil . isNotBlank ( pushCustomType . getCustomPlayFileName ( ) ) ) pushMessage . setCustomPlayFileName ( pushCustomType . getCustomPlayFileName ( ) ) ;
} * /
List < String > blacklist = CollectionUtil . newArrayList ( ) ; / * iPushPassListService . list ( Wrappers . < PushPassList > lambdaQuery ( )
. eq ( PushPassList : : getFromPushId , pushMessage . getFromUserId ( ) ) . eq ( PushPassList : : getType , "0" ) )
. stream ( ) . map ( item - > item . getToPushId ( ) ) . collect ( Collectors . toList ( ) ) ; * /
List < String > whitelist = CollectionUtil . newArrayList ( ) ; / * iPushPassListService . list ( Wrappers . < PushPassList > lambdaQuery ( )
. eq ( PushPassList : : getFromPushId , pushMessage . getFromUserId ( ) ) . eq ( PushPassList : : getType , "1" ) )
. stream ( ) . map ( item - > item . getToPushId ( ) ) . collect ( Collectors . toList ( ) ) ; * /
/ * List < PushUserManage > pushList = iPushUserManageService . list ( Wrappers . < PushUserManage > lambdaQuery ( )
. eq ( PushUserManage : : getFromUserId , pushMessage . getFromUserId ( ) ) . eq ( PushUserManage : : getStatus , "1" ) )
. stream ( ) . filter ( item - > ! blacklist . contains ( item . getToUserId ( ) ) | | ! whitelist . contains ( item . getToUserId ( ) ) ) . collect ( Collectors . toList ( ) ) ;
pushList . forEach ( item - > send ( pushMessage , item . getToUserId ( ) , item . getPlaySound ( ) , item . getPlayVibrate ( ) , item . getPlayLights ( ) ) ) ;
whitelist . forEach ( item - > {
pushMessage . setPlaySound ( "0" ) ;
pushMessage . setPlayVibrate ( "0" ) ;
pushMessage . setPlayLights ( "0" ) ;
send ( pushMessage , item , pushMessage . getPlaySound ( ) , pushMessage . getPlayVibrate ( ) , pushMessage . getPlayLights ( ) ) ;
} ) ; * /
// 配置推送用户
if ( StrUtil . isBlank ( pushChatMessage . getAlias ( ) ) ) {
List < PushConcernFan > pushConcernFanList = iPushConcernFanService . list ( Wrappers . < PushConcernFan > lambdaQuery ( ) . eq ( PushConcernFan : : getFanUserId , pushChatMessage . getUserId ( ) ) ) ;
pushChatMessage . setAlias ( pushConcernFanList . stream ( ) . map ( item - > item . getConcernUserId ( ) ) . collect ( Collectors . joining ( ) ) ) ;
}
// 过滤掉黑名单用户
List < PushBlacklist > pushBlacklists = iPushBlacklistService . list ( Wrappers . < PushBlacklist > lambdaQuery ( ) . eq ( PushBlacklist : : getFanUserId , pushChatMessage . getUserId ( ) ) ) ;
pushBlacklists . forEach ( item - > pushChatMessage . getAlias ( ) . replaceAll ( item . getConcernUserId ( ) + ",|$" , "" ) ) ;
// 查询推送用户设置的推送类型,如果参数中没有指定则采用默认的推送类型
List < PushConcernFanType > pushConcernFanTypeList = iPushConcernFanTypeService . list ( Wrappers . < PushConcernFanType > lambdaQuery ( )
. eq ( PushConcernFanType : : getFanUserId , pushChatMessage . getUserId ( ) )
. eq ( StrUtil . isNotBlank ( pushChatMessage . getPushTypeId ( ) ) , PushConcernFanType : : getTypeId , pushChatMessage . getPushTypeId ( ) )
. eq ( StrUtil . isBlank ( pushChatMessage . getPushTypeId ( ) ) , PushConcernFanType : : getDefaultType , "1" ) ) ;
private void send ( PushMessage pushMessage , String toUserId , String playSound , String playVibrate , String playLights ) {
/ * // 设置默认数据
pushMessage . setType ( "customizedcast" ) ;
pushMessage . setDisplayType ( "notification" ) ;
pushMessage . setAliasType ( PushClientUtil . uPushUserAliasType ) ;
pushMessage . setAlias ( toUserId ) ;
// 推送消息
StrUtil . split ( pushChatMessage . getAlias ( ) , "," ) . forEach ( concernUserId - > {
Optional < PushConcernFanType > pushConcernFanTypeOptional = pushConcernFanTypeList . stream ( ) . filter ( item - > item . getConcernUserId ( ) . equals ( concernUserId ) ) . findFirst ( ) ;
if ( pushConcernFanTypeOptional . isPresent ( ) ) {
PushConcernFanType pushConcernFanType = pushConcernFanTypeOptional . get ( ) ;
pushChatMessage . setPushTypeId ( pushConcernFanType . getTypeId ( ) ) ;
pushChatMessage . setPlayLights ( pushConcernFanType . getPlayLights ( ) ) ;
pushChatMessage . setPlaySound ( pushConcernFanType . getPlaySound ( ) ) ;
pushChatMessage . setPlayVibrate ( pushConcernFanType . getPlayVibrate ( ) ) ;
pushChatMessage . setPlayToText ( pushConcernFanType . getPlayToText ( ) ) ;
pushChatMessage . setSound ( pushConcernFanType . getOfflineRingtone ( ) ) ;
pushChatMessage . setOnlineRingtone ( pushConcernFanType . getOnlineRingtone ( ) ) ;
}
pushChatMessage . setType ( "customizedcast" ) ;
pushChatMessage . setDisplayType ( "notification" ) ;
pushChatMessage . setAliasType ( PushClientUtil . uPushUserAliasType ) ;
pushChatMessage . setAlias ( concernUserId ) ;
JSONObject custom = new JSONObject ( ) ;
custom . put ( "sendUserId" , pushMessage . getFromUserId ( ) ) ;
custom . put ( "customPlayFileName" , pushMessage . getCustomPlayFileName ( ) ) ;
custom . put ( "playSound" , BooleanUtil . toBoolean ( playSound ) ) ;
custom . put ( "playVibrate" , BooleanUtil . toBoolean ( playVibrate ) ) ;
custom . put ( "playLights" , BooleanUtil . toBoolean ( playLights ) ) ;
pushMessage . setCustom ( custom . toString ( ) ) ;
custom . put ( "fanUserId" , pushChatMessage . getUserId ( ) ) ;
custom . put ( "offlineRingtone" , pushChatMessage . getSound ( ) ) ;
custom . put ( "onlineRingtone" , pushChatMessage . getOnlineRingtone ( ) ) ;
custom . put ( "playToText" , BooleanUtil . toBoolean ( pushChatMessage . getPlayToText ( ) ) ) ;
custom . put ( "playSound" , BooleanUtil . toBoolean ( pushChatMessage . getPlaySound ( ) ) ) ;
custom . put ( "playVibrate" , BooleanUtil . toBoolean ( pushChatMessage . getPlayVibrate ( ) ) ) ;
custom . put ( "playLights" , BooleanUtil . toBoolean ( pushChatMessage . getPlayLights ( ) ) ) ;
pushChatMessage . setCustom ( custom . toString ( ) ) ;
JSONObject jsonObject = new JSONObject ( ) ;
jsonObject . put ( "production_mode" , PushClientUtil . PRODUCTION_MODE ) ;
jsonObject . put ( "type" , pushMessage . getType ( ) ) ;
jsonObject . put ( "description" , pushMessage . getRemarks ( ) ) ;
jsonObject . put ( "alias_type" , pushMessage . getAliasType ( ) ) ;
jsonObject . put ( "alias" , pushMessage . getAlias ( ) ) ;
jsonObject . put ( "type" , pushChat Message . getType ( ) ) ;
jsonObject . put ( "description" , pushChat Message . getRemarks ( ) ) ;
jsonObject . put ( "alias_type" , pushChat Message . getAliasType ( ) ) ;
jsonObject . put ( "alias" , pushChat Message . getAlias ( ) ) ;
JSONObject payload = new JSONObject ( ) ;
payload . put ( "display_type" , pushMessage . getDisplayType ( ) ) ;
payload . put ( "display_type" , pushChat Message . getDisplayType ( ) ) ;
JSONObject body = new JSONObject ( ) ;
body . put ( "ticker" , pushMessage . getTitle ( ) ) ;
body . put ( "text" , pushMessage . getText ( ) ) ;
body . put ( "title" , pushMessage . getTitle ( ) ) ;
body . put ( "sound" , pushMessage . getSound ( ) ) ;
body . put ( "ticker" , pushChat Message . getTitle ( ) ) ;
body . put ( "text" , pushChat Message . getText ( ) ) ;
body . put ( "title" , pushChat Message . getTitle ( ) ) ;
body . put ( "sound" , pushChat Message . getSound ( ) ) ;
body . put ( "after_open" , "go_custom" ) ;
body . put ( "custom" , pushMessage . getCustom ( ) ) ;
body . put ( "play_sound" , BooleanUtil . toBoolean ( playSound ) ? false : ! BooleanUtil . toBoolean ( pushMessage . getPlaySound ( ) ) ) ;
body . put ( "play_vibrate" , BooleanUtil . toBoolean ( playVibrate ) ? false : ! BooleanUtil . toBoolean ( pushMessage . getPlayVibrate ( ) ) ) ;
body . put ( "play_lights" , BooleanUtil . toBoolean ( playLights ) ? false : ! BooleanUtil . toBoolean ( pushMessage . getPlayLights ( ) ) ) ;
body . put ( "custom" , pushChatMessage . getCustom ( ) ) ;
if ( BooleanUtil . toBoolean ( pushChatMessage . getPlaySound ( ) ) ) {
body . put ( "play_sound" , true ) ;
}
if ( BooleanUtil . toBoolean ( pushChatMessage . getPlayVibrate ( ) ) ) {
body . put ( "play_vibrate" , true ) ;
}
if ( BooleanUtil . toBoolean ( pushChatMessage . getPlayLights ( ) ) ) {
body . put ( "play_lights" , true ) ;
}
payload . put ( "body" , body ) ;
jsonObject . put ( "payload" , payload ) ;
JSONObject policy = new JSONObject ( ) ;
@ -148,10 +145,12 @@ public class PushApplicationServiceImpl extends ServiceImpl<PushApplicationMappe
@@ -148,10 +145,12 @@ public class PushApplicationServiceImpl extends ServiceImpl<PushApplicationMappe
try {
PushClientUtil pushClientUtil = new PushClientUtil ( okHttpClient ) ;
pushClientUtil . send ( jsonObject ) ;
iPushMessageService . save ( pushMessage ) ;
iPushChat MessageService . save ( pushChat Message ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
} * /
}
} ) ;
}
}