@ -1,6 +1,7 @@
@@ -1,6 +1,7 @@
package com.cloud.kicc.commonbiz.service.impl ;
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 ;
@ -10,20 +11,15 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -10,20 +11,15 @@ 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.PushApplication ;
import com.cloud.kicc.commonbiz.api.entity.PushCustomType ;
import com.cloud.kicc.commonbiz.api.entity.PushMessage ;
import com.cloud.kicc.commonbiz.api.entity.PushUserManage ;
import com.cloud.kicc.commonbiz.api.entity.* ;
import com.cloud.kicc.commonbiz.mapper.PushApplicationMapper ;
import com.cloud.kicc.commonbiz.service.IPushApplicationService ;
import com.cloud.kicc.commonbiz.service.IPushCustomTypeService ;
import com.cloud.kicc.commonbiz.service.IPushMessageService ;
import com.cloud.kicc.commonbiz.service.IPushUserManageService ;
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 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 ;
@ -54,17 +50,19 @@ public class PushApplicationServiceImpl extends ServiceImpl<PushApplicationMappe
@@ -54,17 +50,19 @@ public class PushApplicationServiceImpl extends ServiceImpl<PushApplicationMappe
private final IPushMessageService iPushMessageService ;
private final RemoteUserService remoteUserService ;
private final IPushCustomTypeService iPushCustomTypeService ;
private final IPushPassListService iPushPassListService ;
@Override
public void messageSend ( PushMessage pushMessage ) {
if ( StrUtil . isBlank ( pushMessage . getSendUserId ( ) ) | | StrUtil . isBlank ( pushMessage . getMessageSecret ( ) ) ) {
throw new CheckedException ( "当前发送用户ID与推送应用密钥必填!" ) ;
if ( StrUtil . isBlank ( pushMessage . getFromUserId ( ) ) | | StrUtil . isBlank ( pushMessage . getMessageSecret ( ) ) ) {
throw new CheckedException ( "当前发送方用户ID与推送应用密钥必填!" ) ;
}
R < User > result = remoteUserService . selectByUserId ( pushMessage . getFromUserId ( ) , SecurityConstants . FROM_IN ) ;
if ( result = = null | | result . getData ( ) = = null ) {
throw new UsernameNotFoundException ( "用户不存在" ) ;
}
R < User > user = remoteUserService . selectByUserId ( pushMessage . getSendUserId ( ) , SecurityConstants . FROM_IN ) ;
// 第三方发送前较验数据
if ( use r. getData ( ) . getUserType ( ) . equals ( "9" ) ) {
if ( result . getData ( ) . getUserType ( ) . equals ( "9" ) ) {
PushApplication pushApplication = baseMapper . selectOne ( Wrappers . < PushApplication > lambdaQuery ( )
. eq ( PushApplication : : getMessageSecret , pushMessage . getMessageSecret ( ) ) . eq ( PushApplication : : getStatus , "0" ) ) ;
pushApplication = Optional . of ( pushApplication ) . orElseThrow ( ( ) - > new CheckedException ( "你当前没有权限发送消息,请联系管理员!" ) ) ;
@ -77,57 +75,79 @@ public class PushApplicationServiceImpl extends ServiceImpl<PushApplicationMappe
@@ -77,57 +75,79 @@ public class PushApplicationServiceImpl extends ServiceImpl<PushApplicationMappe
// 查询当前用户设置的自定义快捷方式消息提醒属性
PushCustomType pushCustomType = iPushCustomTypeService . getOne ( Wrappers . < PushCustomType > lambdaQuery ( )
. eq ( PushCustomType : : getId , pushMessage . getCustomTypeId ( ) ) . eq ( PushCustomType : : getCreateById , pushMessage . getSend UserId ( ) ) ) ;
. eq ( PushCustomType : : getId , pushMessage . getCustomTypeId ( ) ) . eq ( PushCustomType : : getCreateById , pushMessage . getFrom UserId ( ) ) ) ;
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 ( ) ) ;
if ( StrUtil . isNotBlank ( pushCustomType . getCustomPlayFileName ( ) ) ) pushMessage . setCustomPlayFileName ( pushCustomType . getCustomPlayFileName ( ) ) ;
}
List < String > blacklist = 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 = 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 : : getCreateById , pushMessage . getSendUserId ( ) ) . eq ( PushUserManage : : getStatus , "1" ) ) ;
if ( ObjectUtil . isNotEmpty ( pushList ) ) {
// 设置默认数据
pushMessage . setDisplayType ( "notification" ) ;
pushMessage . setAliasType ( PushClientUtil . uPushUserAliasType ) ;
pushMessage . setAlias ( pushList . stream ( ) . map ( item - > item . getToUserId ( ) ) . collect ( Collectors . joining ( "," ) ) ) ;
JSONObject custom = new JSONObject ( ) ;
custom . put ( "sendUserId" , pushMessage . getSendUserId ( ) ) ;
custom . put ( "customPlayFileName" , pushMessage . getCustomPlayFileName ( ) ) ;
pushMessage . setCustom ( custom . toString ( ) ) ;
JSONObject jsonObject = new JSONObject ( ) ;
jsonObject . put ( "production_mode" , PushClientUtil . PRODUCTION_MODE ) ;
jsonObject . put ( "type" , "customizedcast" ) ;
jsonObject . put ( "description" , pushMessage . getRemarks ( ) ) ;
jsonObject . put ( "alias_type" , pushMessage . getAliasType ( ) ) ;
jsonObject . put ( "alias" , pushMessage . getAlias ( ) ) ;
JSONObject payload = new JSONObject ( ) ;
payload . put ( "display_type" , pushMessage . 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 ( "after_open" , "go_custom" ) ;
body . put ( "custom" , pushMessage . getCustom ( ) ) ;
body . put ( "play_sound" , pushMessage . getPlaySound ( ) ) ;
body . put ( "play_vibrate" , pushMessage . getPlayVibrate ( ) ) ;
body . put ( "play_lights" , pushMessage . 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 ) ;
iPushMessageService . save ( pushMessage ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
. 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 ( ) ) ;
} ) ;
}
private void send ( PushMessage pushMessage , String toUserId , String playSound , String playVibrate , String playLights ) {
// 设置默认数据
pushMessage . setDisplayType ( "notification" ) ;
pushMessage . setAliasType ( PushClientUtil . uPushUserAliasType ) ;
pushMessage . setAlias ( toUserId ) ;
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 ( ) ) ;
JSONObject jsonObject = new JSONObject ( ) ;
jsonObject . put ( "production_mode" , PushClientUtil . PRODUCTION_MODE ) ;
jsonObject . put ( "type" , "customizedcast" ) ;
jsonObject . put ( "description" , pushMessage . getRemarks ( ) ) ;
jsonObject . put ( "alias_type" , pushMessage . getAliasType ( ) ) ;
jsonObject . put ( "alias" , pushMessage . getAlias ( ) ) ;
JSONObject payload = new JSONObject ( ) ;
payload . put ( "display_type" , pushMessage . 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 ( "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 ( ) ) ) ;
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 ) ;
iPushMessageService . save ( pushMessage ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}