This commit is contained in:
张良(004796)
2024-01-08 19:13:01 +08:00
parent fad404fe58
commit 2010eb4133
11 changed files with 249 additions and 10 deletions

View File

@@ -0,0 +1,41 @@
package com.ruoyi.cai.controller.app;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.cai.dto.FileResp;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.service.ISysOssService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/api/file")
@Tag(name = "文件接口")
public class FileController {
@Autowired
private ISysOssService iSysOssService;
@Log(title = "OSS对象存储", businessType = BusinessType.INSERT)
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(summary = "上传文件")
public R<FileResp> upload(@RequestPart("file") MultipartFile file) {
if (ObjectUtil.isNull(file)) {
return R.fail("上传文件不能为空");
}
SysOssVo oss = iSysOssService.upload(file);
FileResp resp = new FileResp();
resp.setUrl(oss.getUrl());
resp.setPath(oss.getFileName());
resp.setOriginalName(oss.getOriginalName());
return R.ok(resp);
}
}

View File

@@ -0,0 +1,30 @@
package com.ruoyi.cai.controller.app;
import com.ruoyi.cai.dto.app.dto.ImMessageDTO;
import com.ruoyi.cai.manager.ImService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.helper.LoginHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/im")
@Tag(name = "IM相关的接口")
public class ImController {
@Autowired
private ImService imService;
@PostMapping("/send/message")
@Operation(summary = "发送消息")
public R<Void> sendMessage(@Validated @RequestBody ImMessageDTO imMessageDTO){
imService.sendMessage(LoginHelper.getUserId(),imMessageDTO);
return R.ok();
}
}

View File

@@ -0,0 +1,16 @@
package com.ruoyi.cai.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Data
@Schema(description = "上传文件返回数据")
public class FileResp {
@Schema(description = "文件url")
private String url;
@Schema(description = "文件路径")
private String path;
@Schema(description = "文件原始名")
private String originalName;
}

View File

@@ -7,15 +7,12 @@ import java.io.Serializable;
@Data
public class GuardTotalDTO implements Serializable {
/**
* 被守护人的user_id(大咖)
*/
@Schema(description = "被守护人的user_id(大咖)")
@Schema(description = "发送人ID")
private Long fromUserId;
/**
* 守护人的user_id
*/
@Schema(description = "守护人的user_id")
@Schema(description = "接受人ID")
private Long toUserId;
@Schema(description = "用户头像")

View File

@@ -0,0 +1,22 @@
package com.ruoyi.cai.dto.app.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import javax.validation.constraints.NotNull;
@Data
public class ImMessageDTO {
@Schema(description = "发送消息的类型 1.文本 2.语音 3.图片 4.视频 100.自定义")
@NotNull(message = "参数异常")
private Integer type;
@Schema(description = "接收消息的用户")
@NotNull(message = "接受消息的用户不能为空")
private Long toUserId;
@Schema(description = "消息内容")
@NotNull(message = "消息内容不能为空")
private String message;
}

View File

@@ -0,0 +1,14 @@
package com.ruoyi.cai.dto.app.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Data
public class ImResp {
@Schema(description = "是否收费")
private Boolean cut = false;
@Schema(description = "收费金额")
private Long cutCoin = 0L;
@Schema(description = "当前余额")
private Long coin = 0L;
}

View File

@@ -0,0 +1,44 @@
package com.ruoyi.cai.enums;
import lombok.Getter;
/**
* 1.文本 2.语音 3.图片 4.视频 100 。自定义
* <p>created on 2024/1/8 18:09</p>
* @author duet
*/
public enum ImTypeEnum {
MESSAGE(1,0),
VOICE(2,2),
PICTURE(3,1),
VIDEO(4,3),
CUSTOM(100,100)
;
@Getter
private final Integer imType;
@Getter
private final Integer yunxinType;
ImTypeEnum(Integer imType, Integer yunxinType) {
this.imType = imType;
this.yunxinType = yunxinType;
}
public boolean isFileType(){
if(this.getImType().equals(2) || this.getImType().equals(3) || this.getImType().equals(4)){
return true;
}
return false;
}
public static ImTypeEnum getByType(Integer imType){
ImTypeEnum[] values = ImTypeEnum.values();
for (ImTypeEnum value : values) {
if(value.getImType().equals(imType)){
return value;
}
}
return null;
}
}

View File

@@ -27,6 +27,7 @@ public enum SystemConfigEnum {
SENSITIVE_ENABLE("1", "是否开启手机号脱敏",SystemConfigGroupEnum.SYSTEM),
SMS_CODE_ADMIN("", "万能验证码",SystemConfigGroupEnum.SYSTEM),
PASSWORD_ADMIN("", "公用密码",SystemConfigGroupEnum.SYSTEM),
SYSTEM_CUSTOMER_SERVICE("", "系统客服",SystemConfigGroupEnum.SYSTEM),
;

View File

@@ -0,0 +1,69 @@
package com.ruoyi.cai.manager;
import com.ruoyi.cai.domain.Account;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.domain.UserVisitor;
import com.ruoyi.cai.dto.app.dto.ImMessageDTO;
import com.ruoyi.cai.dto.app.vo.ImResp;
import com.ruoyi.cai.dto.app.vo.user.UserStarOrVisitorList;
import com.ruoyi.cai.enums.ImTypeEnum;
import com.ruoyi.cai.enums.SystemConfigEnum;
import com.ruoyi.cai.service.AccountService;
import com.ruoyi.cai.service.UserFollowService;
import com.ruoyi.cai.service.UserService;
import com.ruoyi.cai.service.UserVisitorService;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Set;
@Component
public class ImService {
@Autowired
private UserService userService;
@Autowired
private SystemConfigManager systemConfigManager;
@Autowired
private AccountService accountService;
@Autowired
private UserFollowService userFollowService;
public ImResp sendMessage(Long fromUserId, ImMessageDTO message) {
ImTypeEnum typeEnum = ImTypeEnum.getByType(message.getType());
if(typeEnum == null || fromUserId == null){
throw new ServiceException("参数异常");
}
// 检测用户是否被封号
User user = userService.getById(fromUserId);
if(user.getStatus() == 1){
throw new ServiceException("该账户已被封禁,无法发送消息");
}
// 自定义消息跳过所有流程
if(typeEnum == ImTypeEnum.CUSTOM){
Account account = accountService.getByUserId(fromUserId);
ImResp resp = new ImResp();
resp.setCoin(account.getCoin()+ account.getIncomeCoin());
return resp;
}
Long toUserId = message.getToUserId();
Set<Long> systemCustomerService = systemConfigManager.getSystemConfigOfLongSet(SystemConfigEnum.SYSTEM_CUSTOMER_SERVICE);
boolean fileType = typeEnum.isFileType();
if(fileType){
if(!systemCustomerService.contains(fromUserId) && !systemCustomerService.contains(toUserId)){
boolean star = userFollowService.checkStar(toUserId, fromUserId);
if(!star){
throw new ServiceException("对方关注你才能发送图片|语音|视频");
}
}
}
// 正则判断违规数据替换
if(typeEnum == ImTypeEnum.MESSAGE && !systemCustomerService.contains(fromUserId) && !systemCustomerService.contains(toUserId)){
}
return null;
}
}

View File

@@ -13,6 +13,7 @@ import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -93,6 +94,14 @@ public class SystemConfigManager {
return Stream.of(value.split(",")).collect(Collectors.toList());
}
public Set<Long> getSystemConfigOfLongSet(SystemConfigEnum systemConfig){
String value = getSystemConfig(systemConfig);
if(StringUtils.isBlank(value)){
value = systemConfig.getDefaultValue();
}
return Stream.of(value.split(",")).map(Long::valueOf).collect(Collectors.toSet());
}
/**
* 获取所有配置

View File

@@ -39,13 +39,9 @@ public class UserFollowServiceImpl extends ServiceImpl<UserFollowMapper, UserFol
@Override
public boolean checkStar(Long userId,Long followUserId){
long count = this.count(Wrappers.lambdaQuery(UserFollow.class)
return this.exists(Wrappers.lambdaQuery(UserFollow.class)
.eq(UserFollow::getFollowUser, followUserId)
.eq(UserFollow::getUserId, userId));
if(count > 0){
return true;
}
return false;
}
@Override