新增邀请码页面,新增添加好友功能
Change-Id: Ifa0a5c355ab3693eecfe919de06fa9bef8171695
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUserFollow.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUserFollow.java
index a5878b1..4831e79 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUserFollow.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUserFollow.java
@@ -1,4 +1,3 @@
-// 作者关注
package com.ruoyi.system.domain;
import com.ruoyi.common.annotation.Excel;
@@ -20,4 +19,7 @@
@Excel(name = "作者ID")
@NotNull(message = "作者ID不能为空")
private Long authorId;
+
+ // 用于接收前端传入的作者用户名
+ private String authorUsername;
}
\ No newline at end of file
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUserInvite.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUserInvite.java
new file mode 100644
index 0000000..3432587
--- /dev/null
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUserInvite.java
@@ -0,0 +1,15 @@
+package com.ruoyi.system.domain;
+
+import com.ruoyi.common.core.domain.BaseEntity;
+import lombok.Data;
+
+@Data
+public class SysUserInvite extends BaseEntity {
+ private static final long serialVersionUID = 1L;
+
+ private Long codeId;
+
+ private String code;
+
+ private Long userId;
+}
\ No newline at end of file
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserFollowMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserFollowMapper.java
index 77bca7e..521f26b 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserFollowMapper.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserFollowMapper.java
@@ -1,4 +1,3 @@
-// 作者关注 Mapper
package com.ruoyi.system.mapper;
import com.ruoyi.system.domain.SysUserFollow;
@@ -9,4 +8,5 @@
int deleteFollow(SysUserFollow follow);
List<SysUserFollow> selectFollowListByUserId(Long userId);
SysUserFollow selectFollow(SysUserFollow follow);
+ Long selectUserIdByUsername(String username);
}
\ No newline at end of file
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserInviteMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserInviteMapper.java
new file mode 100644
index 0000000..b0239ca
--- /dev/null
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserInviteMapper.java
@@ -0,0 +1,8 @@
+package com.ruoyi.system.mapper;
+
+import com.ruoyi.system.domain.SysUserInvite;
+
+public interface SysUserInviteMapper {
+ SysUserInvite selectInviteByUserId(Long userId);
+ SysUserInvite selectInviteByCode(String code);
+}
\ No newline at end of file
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserInviteService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserInviteService.java
new file mode 100644
index 0000000..d5b04f4
--- /dev/null
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserInviteService.java
@@ -0,0 +1,8 @@
+package com.ruoyi.system.service;
+
+import com.ruoyi.system.domain.SysUserInvite;
+
+public interface ISysUserInviteService {
+ SysUserInvite getInviteCode(Long userId);
+ SysUserInvite getUserByInviteCode(String code);
+}
\ No newline at end of file
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserFollowServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserFollowServiceImpl.java
index 9cec124..a12f985 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserFollowServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserFollowServiceImpl.java
@@ -1,9 +1,10 @@
-// 作者关注服务实现
package com.ruoyi.system.service.impl;
import com.ruoyi.system.domain.SysUserFollow;
+import com.ruoyi.system.domain.SysUserMessage;
import com.ruoyi.system.mapper.SysUserFollowMapper;
import com.ruoyi.system.service.ISysUserFollowService;
+import com.ruoyi.system.service.ISysUserMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@@ -13,17 +14,46 @@
@Autowired
private SysUserFollowMapper followMapper;
+ @Autowired
+ private ISysUserMessageService messageService;
+
@Override
public int followAuthor(SysUserFollow follow) {
+ // 根据 authorUsername 查询 authorId
+ Long authorId = followMapper.selectUserIdByUsername(follow.getAuthorUsername());
+ if (authorId == null) {
+ return -1; // 作者不存在
+ }
+ follow.setAuthorId(authorId);
+
+ // 检查是否已关注
if (followMapper.selectFollow(follow) != null) {
return 0; // 已经关注
}
- return followMapper.insertFollow(follow);
+
+ // 插入关注记录
+ int result = followMapper.insertFollow(follow);
+ if (result > 0) {
+ // 发送 "hello" 消息
+ SysUserMessage message = new SysUserMessage();
+ message.setSenderId(follow.getUserId());
+ message.setReceiverId(authorId);
+ message.setContent("hello");
+ messageService.sendMessage(message);
+ }
+ return result;
}
@Override
public int unfollowAuthor(SysUserFollow follow) {
- return followMapper.deleteFollow(follow);
+ // 根据 authorUsername 查询 authorId
+ Long authorId = followMapper.selectUserIdByUsername(follow.getAuthorUsername());
+ if (authorId == null) {
+ return -1; // 作者不存在
+ }
+ follow.setAuthorId(authorId);
+
+ return followMapper.deleteFollow(follow); // 返回删除的行数(0 或 1)
}
@Override
@@ -33,6 +63,13 @@
@Override
public boolean isFollowing(SysUserFollow follow) {
+ // 根据 authorUsername 查询 authorId
+ Long authorId = followMapper.selectUserIdByUsername(follow.getAuthorUsername());
+ if (authorId == null) {
+ return false; // 作者不存在
+ }
+ follow.setAuthorId(authorId);
+
return followMapper.selectFollow(follow) != null;
}
}
\ No newline at end of file
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserInviteServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserInviteServiceImpl.java
new file mode 100644
index 0000000..e499a5a
--- /dev/null
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserInviteServiceImpl.java
@@ -0,0 +1,23 @@
+package com.ruoyi.system.service.impl;
+
+import com.ruoyi.system.domain.SysUserInvite;
+import com.ruoyi.system.mapper.SysUserInviteMapper;
+import com.ruoyi.system.service.ISysUserInviteService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class SysUserInviteServiceImpl implements ISysUserInviteService {
+ @Autowired
+ private SysUserInviteMapper inviteMapper;
+
+ @Override
+ public SysUserInvite getInviteCode(Long userId) {
+ return inviteMapper.selectInviteByUserId(userId);
+ }
+
+ @Override
+ public SysUserInvite getUserByInviteCode(String code) {
+ return inviteMapper.selectInviteByCode(code);
+ }
+}
\ No newline at end of file
diff --git a/ruoyi-system/src/main/resources/mapper/system/SysUserFollowMapper.xml b/ruoyi-system/src/main/resources/mapper/system/SysUserFollowMapper.xml
index 6fb9169..bea8888 100644
--- a/ruoyi-system/src/main/resources/mapper/system/SysUserFollowMapper.xml
+++ b/ruoyi-system/src/main/resources/mapper/system/SysUserFollowMapper.xml
@@ -28,4 +28,10 @@
from sys_user_follow
where user_id = #{userId} and author_id = #{authorId}
</select>
+
+ <select id="selectUserIdByUsername" resultType="java.lang.Long">
+ select user_id
+ from sys_user
+ where user_name = #{username}
+ </select>
</mapper>
\ No newline at end of file
diff --git a/ruoyi-system/src/main/resources/mapper/system/SysUserInviteMapper.xml b/ruoyi-system/src/main/resources/mapper/system/SysUserInviteMapper.xml
new file mode 100644
index 0000000..82da470
--- /dev/null
+++ b/ruoyi-system/src/main/resources/mapper/system/SysUserInviteMapper.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.ruoyi.system.mapper.SysUserInviteMapper">
+ <resultMap id="SysUserInviteMap" type="com.ruoyi.system.domain.SysUserInvite">
+ <id property="codeId" column="code_id"/>
+ <result property="code" column="code"/>
+ <result property="userId" column="user_id"/>
+ </resultMap>
+
+ <select id="selectInviteByUserId" resultMap="SysUserInviteMap">
+ select code_id, code, user_id
+ from invite_codes
+ where user_id = #{userId}
+ </select>
+
+ <select id="selectInviteByCode" resultMap="SysUserInviteMap">
+ select code_id, code, user_id
+ from invite_codes
+ where code = #{code}
+ </select>
+</mapper>
\ No newline at end of file