| | |
| | | package com.ruoyi.web.controller.group; |
| | | |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.ruoyi.common.core.controller.BaseController; |
| | | import com.ruoyi.common.core.domain.AjaxResult; |
| | | import com.ruoyi.common.core.page.TableDataInfo; |
| | | import com.ruoyi.im.comm.Result; |
| | | import com.ruoyi.im.config.AddTeamMembersRequest; |
| | | import com.ruoyi.im.config.CreateTeamRequest; |
| | | import com.ruoyi.im.dto.CreateGroupDto; |
| | | import com.ruoyi.im.config.AssignmentRequest; |
| | | import com.ruoyi.im.dto.UpdateUserBusinessDto; |
| | | import com.ruoyi.im.service.NeteaseTeamService; |
| | | import com.ruoyi.system.domain.NeteaseTeam; |
| | | import com.ruoyi.system.domain.UserAccount; |
| | | import com.ruoyi.system.domain.vo.UserAccountVo; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.security.access.prepost.PreAuthorize; |
| | | 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; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | | import java.nio.file.attribute.PosixFilePermission; |
| | | import java.nio.file.attribute.PosixFilePermissions; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.UUID; |
| | | |
| | | @RestController |
| | | @RequestMapping("/im/group") |
| | |
| | | @Autowired |
| | | NeteaseTeamService neteaseGroupService; |
| | | |
| | | @Value("${file.upload-dir}") |
| | | private String uploadDir; |
| | | |
| | | @Value("${file.upload-prefix}") |
| | | private String prefix; |
| | | |
| | | /** |
| | | * 获取群组列表 |
| | | */ |
| | | // @PreAuthorize("@ss.hasPermi('im:group:list')") |
| | | @GetMapping("/list") |
| | | public TableDataInfo list(@PathVariable(value = "keyword", required = false) String keyword) |
| | | { |
| | | // 创建查询条件包装器 |
| | | LambdaQueryWrapper<NeteaseTeam> queryWrapper = new LambdaQueryWrapper<>(); |
| | | |
| | | // 只有当 keyword 不为空时才添加 OR 条件 |
| | | if (ObjectUtil.isNotEmpty(keyword)) { |
| | | queryWrapper.and(wrapper -> wrapper |
| | | .eq(NeteaseTeam::getTid, keyword) |
| | | .or() |
| | | .eq(NeteaseTeam::getOwnerAccountId, keyword) |
| | | .or() |
| | | .eq(NeteaseTeam::getName,keyword) |
| | | ); |
| | | } |
| | | |
| | | // 默认按创建时间倒序 |
| | | queryWrapper.orderByDesc(NeteaseTeam::getCreateTime); |
| | | startPage(); |
| | | List<NeteaseTeam> list = neteaseGroupService.list(queryWrapper); |
| | | return getDataTable(list); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 创建群组 |
| | | */ |
| | | @PostMapping("/create") |
| | | public AjaxResult createGroup(@Validated @RequestBody CreateTeamRequest request) { |
| | | public AjaxResult createGroup(CreateTeamRequest request) { |
| | | return neteaseGroupService.createGroup(request); |
| | | } |
| | | |
| | | /** |
| | | * 更新群组 |
| | | */ |
| | | @PostMapping("/updateCreate") |
| | | public AjaxResult updateCreate(CreateTeamRequest request) { |
| | | return neteaseGroupService.updateCreate(request); |
| | | } |
| | | |
| | | /** |
| | | * 拉人进去 |
| | | */ |
| | | @PostMapping("/inviteTeamMembers") |
| | | public AjaxResult inviteTeamMembers( AddTeamMembersRequest request) { |
| | | return neteaseGroupService.inviteTeamMembers(request); |
| | | } |
| | | |
| | | /** |
| | | * 转让群主 |
| | | */ |
| | | @PostMapping("/assignment") |
| | | public AjaxResult assignment(AssignmentRequest request) { |
| | | return neteaseGroupService.assignment(request); |
| | | } |
| | | |
| | | /** |
| | | * 解散 |
| | | */ |
| | | @GetMapping("/dismiss") |
| | | public AjaxResult dismiss(@RequestParam(value = "groupId") Integer groupId) { |
| | | return neteaseGroupService.dismiss(groupId); |
| | | } |
| | | |
| | | |
| | | @PostMapping("/upload") |
| | | public Result uploadFile(@RequestParam("file") MultipartFile file) { |
| | | try { |
| | | |
| | | // 1. 验证文件类型 |
| | | String contentType = file.getContentType(); |
| | | if (contentType == null || |
| | | (!contentType.equals("image/jpeg") && |
| | | !contentType.equals("image/png") && |
| | | !contentType.equals("image/gif"))) { |
| | | |
| | | return Result.error("只支持JPEG、PNG和GIF格式的图片!"); |
| | | } |
| | | |
| | | // 确保上传目录存在 |
| | | File directory = new File(uploadDir); |
| | | if (!directory.exists()) { |
| | | directory.mkdirs(); |
| | | // 设置目录权限为755 (rwxr-xr-x) |
| | | setPermissions(directory, "rwxr-xr-x"); |
| | | } |
| | | |
| | | // 生成唯一文件名 |
| | | String fileName = UUID.randomUUID().toString() + "_" + file.getOriginalFilename(); |
| | | Path filePath = Paths.get(uploadDir, fileName); |
| | | |
| | | // 保存文件 |
| | | Files.write(filePath, file.getBytes()); |
| | | |
| | | // 设置文件权限为644 (rw-r--r--) |
| | | setPermissions(filePath.toFile(), "rw-r--r--"); |
| | | |
| | | // 5. 调用网易云信API更新头像 |
| | | fileName = prefix+"/"+fileName; |
| | | return Result.success("文件上传成功",fileName); |
| | | } catch (IOException e) { |
| | | return Result.error("文件上传失败"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error("设置文件权限失败!"); |
| | | } |
| | | return Result.success(); |
| | | } |
| | | |
| | | /** |
| | | * 设置文件/目录权限 |
| | | * @param file 文件或目录对象 |
| | | * @param perm 权限字符串,如 "rw-r--r--" |
| | | */ |
| | | private void setPermissions(File file, String perm) throws IOException { |
| | | // 获取当前操作系统 |
| | | String os = System.getProperty("os.name").toLowerCase(); |
| | | |
| | | // 只有在Unix/Linux系统上才能设置POSIX权限 |
| | | if (os.contains("nix") || os.contains("nux") || os.contains("mac")) { |
| | | Set<PosixFilePermission> permissions = PosixFilePermissions.fromString(perm); |
| | | Files.setPosixFilePermissions(file.toPath(), permissions); |
| | | } else { |
| | | // Windows系统不支持POSIX权限,可以设置基本权限 |
| | | file.setReadable(true, false); // 所有用户可读 |
| | | if (perm.startsWith("rw")) { |
| | | file.setWritable(true, false); // 所有用户可写(如果权限字符串以rw开头) |
| | | } else { |
| | | file.setWritable(false, false); |
| | | } |
| | | |
| | | // 如果是目录且包含执行权限,设置可执行 |
| | | if (file.isDirectory() && perm.contains("x")) { |
| | | file.setExecutable(true, false); |
| | | } |
| | | } |
| | | } |
| | | } |