Преглед изворни кода

feat: 账户封禁功能完成

xsx пре 1 година
родитељ
комит
00228b826e

+ 13 - 1
im-platform/src/main/java/com/bx/implatform/entity/User.java

@@ -74,11 +74,23 @@ public class User extends Model<User> {
     @TableField("signature")
     private String signature;
     /**
-     * 密码(明文)
+     * 密码
      */
     @TableField("password")
     private String password;
 
+    /**
+     * 是否被封禁
+     */
+    @TableField("is_banned")
+    private Boolean isBanned;
+
+    /**
+     * 被封禁原因
+     */
+    @TableField("reason")
+    private String reason;
+
     /**
      * 最后登录时间
      */

+ 22 - 6
im-platform/src/main/java/com/bx/implatform/service/impl/UserServiceImpl.java

@@ -50,8 +50,12 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
     @Override
     public LoginVO login(LoginDTO dto) {
         User user = this.findUserByUserName(dto.getUserName());
-        if (null == user) {
-            throw new GlobalException(ResultCode.PROGRAM_ERROR, "用户不存在");
+        if (Objects.isNull(user)) {
+            throw new GlobalException("用户不存在");
+        }
+        if (user.getIsBanned()) {
+            String tip = String.format("您的账号因'%s'已被管理员封禁,请联系客服!",user.getReason());
+            throw new GlobalException(tip);
         }
         if (!passwordEncoder.matches(dto.getPassword(), user.getPassword())) {
             throw new GlobalException(ResultCode.PASSWOR_ERROR);
@@ -61,8 +65,10 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
         session.setUserId(user.getId());
         session.setTerminal(dto.getTerminal());
         String strJson = JSON.toJSONString(session);
-        String accessToken = JwtUtil.sign(user.getId(), strJson, jwtProperties.getAccessTokenExpireIn(), jwtProperties.getAccessTokenSecret());
-        String refreshToken = JwtUtil.sign(user.getId(), strJson, jwtProperties.getRefreshTokenExpireIn(), jwtProperties.getRefreshTokenSecret());
+        String accessToken = JwtUtil.sign(user.getId(), strJson, jwtProperties.getAccessTokenExpireIn(),
+            jwtProperties.getAccessTokenSecret());
+        String refreshToken = JwtUtil.sign(user.getId(), strJson, jwtProperties.getRefreshTokenExpireIn(),
+            jwtProperties.getRefreshTokenSecret());
         LoginVO vo = new LoginVO();
         vo.setAccessToken(accessToken);
         vo.setAccessTokenExpiresIn(jwtProperties.getAccessTokenExpireIn());
@@ -79,8 +85,18 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
         }
         String strJson = JwtUtil.getInfo(refreshToken);
         Long userId = JwtUtil.getUserId(refreshToken);
-        String accessToken = JwtUtil.sign(userId, strJson, jwtProperties.getAccessTokenExpireIn(), jwtProperties.getAccessTokenSecret());
-        String newRefreshToken = JwtUtil.sign(userId, strJson, jwtProperties.getRefreshTokenExpireIn(), jwtProperties.getRefreshTokenSecret());
+        User user = this.getById(userId);
+        if (Objects.isNull(user)) {
+            throw new GlobalException("用户不存在");
+        }
+        if (user.getIsBanned()) {
+            String tip = String.format("您的账号因'%s'被管理员封禁,请联系客服!",user.getReason());
+            throw new GlobalException(tip);
+        }
+        String accessToken =
+            JwtUtil.sign(userId, strJson, jwtProperties.getAccessTokenExpireIn(), jwtProperties.getAccessTokenSecret());
+        String newRefreshToken = JwtUtil.sign(userId, strJson, jwtProperties.getRefreshTokenExpireIn(),
+            jwtProperties.getRefreshTokenSecret());
         LoginVO vo = new LoginVO();
         vo.setAccessToken(accessToken);
         vo.setAccessTokenExpiresIn(jwtProperties.getAccessTokenExpireIn());

+ 1 - 0
im-ui/src/api/enums.js

@@ -12,6 +12,7 @@ const MESSAGE_TYPE = {
 	LOADING: 30,
 	ACT_RT_VOICE: 40,
 	ACT_RT_VIDEO: 41,
+	USER_BANNED: 50,
 	RTC_CALL_VOICE: 100,
 	RTC_CALL_VIDEO: 101,
 	RTC_ACCEPT: 102,

+ 17 - 0
im-ui/src/view/Home.vue

@@ -107,6 +107,9 @@
 						} else if (cmd == 4) {
 							// 插入群聊消息
 							this.handleGroupMessage(msgInfo);
+						} else if (cmd == 5){
+							// 处理系统消息
+							this.handleSystemMessage(msgInfo);
 						}
 					});
 					this.$wsApi.onClose((e) => {
@@ -246,6 +249,20 @@
 					this.playAudioTip();
 				}
 			},
+			handleSystemMessage(msg){
+				// 用户被封禁
+				
+				if (msg.type == this.$enums.MESSAGE_TYPE.USER_BANNED) {
+					this.$wsApi.close(3000);
+					this.$alert("您的账号已被管理员封禁,原因:"+ msg.content, "账号被封禁", {
+						confirmButtonText: '确定',
+						callback: action => {
+							this.onExit();
+						}
+					});
+					return;
+				}
+			},
 			onExit() {
 				this.$wsApi.close(3000);
 				sessionStorage.removeItem("accessToken");

+ 18 - 5
im-uniapp/App.vue

@@ -30,7 +30,7 @@
 				wsApi.connect(UNI_APP.WS_URL, loginInfo.accessToken);
 				wsApi.onConnect(() => {
 					// 重连成功提示
-					if(this.reconnecting){
+					if (this.reconnecting) {
 						this.reconnecting = false;
 						uni.showToast({
 							title: "已重新连接",
@@ -55,6 +55,9 @@
 					} else if (cmd == 4) {
 						// 群聊消息
 						this.handleGroupMessage(msgInfo);
+					} else if (cmd == 5) {
+						// 系统消息
+						this.handleSystemMessage(msgInfo);
 					}
 				});
 				wsApi.onClose((res) => {
@@ -189,7 +192,17 @@
 					// 插入群聊消息
 					this.insertGroupMessage(group, msg);
 				})
-
+			},
+			handleSystemMessage(msg) {
+				if (msg.type == enums.MESSAGE_TYPE.USER_BANNED) {
+					// 用户被封禁
+					wsApi.close(3099);
+					uni.showModal({
+						content: '您的账号已被管理员封禁,原因:' + msg.content,
+						showCancel: false,
+					})
+					this.exit();
+				}
 			},
 			insertGroupMessage(group, msg) {
 				// 群视频信令
@@ -268,7 +281,7 @@
 			},
 			exit() {
 				console.log("exit");
-				wsApi.close(1000);
+				wsApi.close(3099);
 				uni.removeStorageSync("loginInfo");
 				uni.reLaunch({
 					url: "/pages/login/login"
@@ -302,9 +315,9 @@
 					wsApi.reconnect(UNI_APP.WS_URL, loginInfo.accessToken);
 				}).catch(() => {
 					// 5s后重试
-					setTimeout(()=>{
+					setTimeout(() => {
 						this.reconnectWs();
-					},5000)
+					}, 5000)
 				})
 			},
 			reloadUserInfo() {

+ 1 - 0
im-uniapp/common/enums.js

@@ -13,6 +13,7 @@ const MESSAGE_TYPE = {
 	LOADING:30,
 	ACT_RT_VOICE:40,
 	ACT_RT_VIDEO:41,
+	USER_BANNED:50,
 	RTC_CALL_VOICE: 100,
 	RTC_CALL_VIDEO: 101,
 	RTC_ACCEPT: 102,