ソースを参照

管乐迷优化需求

zouxuan 7 ヶ月 前
コミット
13fe64e2f0

+ 2 - 5
mec-application/src/main/java/com/ym/mec/web/controller/education/ImGroupNoticeController.java

@@ -34,11 +34,8 @@ public class ImGroupNoticeController extends BaseController {
     @RequestMapping("/add")
     public HttpResponseResult<Long> add(ImGroupNotice imGroupNotice ) {
         imGroupNotice.setOperatorId(sysUserService.getUserId().longValue());
-        if(imGroupNotice.isIsTop()){
-            //置顶取消
-            imGroupNoticeService.getDao().updateTop(imGroupNotice.getImGroupId(),false);
-        }
-        return succeed(imGroupNoticeService.insert(imGroupNotice));
+        imGroupNotice.setClientType("BACKEND");
+        return succeed(imGroupNoticeService.add(imGroupNotice));
     }
 
     @ApiOperation(value = "修改")

+ 23 - 1
mec-biz/src/main/java/com/ym/mec/biz/dal/entity/ImGroupNotice.java

@@ -1,5 +1,6 @@
 package com.ym.mec.biz.dal.entity;
 
+import com.baomidou.mybatisplus.annotation.TableField;
 import org.apache.commons.lang3.builder.ToStringBuilder;
 
 import com.ym.mec.common.entity.BaseEntity;
@@ -38,7 +39,28 @@ public class ImGroupNotice extends BaseEntity {
 	
 	/** 操作人 */
 	private Long operatorId;
-	
+
+	// IM消息ID
+	private String messageSeqId;
+
+	private String clientType;
+
+	public String getClientType() {
+		return clientType;
+	}
+
+	public void setClientType(String clientType) {
+		this.clientType = clientType;
+	}
+
+	public String getMessageSeqId() {
+		return messageSeqId;
+	}
+
+	public void setMessageSeqId(String messageSeqId) {
+		this.messageSeqId = messageSeqId;
+	}
+
 	public void setId(Long id){
 		this.id = id;
 	}

+ 34 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/enums/im/CK.java

@@ -0,0 +1,34 @@
+package com.ym.mec.biz.dal.enums.im;
+
+/**
+ * 常用定义
+ * Created by Eric.Shang on 2022/11/4.
+ */
+public interface CK {
+
+    // 手机号
+    String EXP_MOBILE_NUMBER = "^((13[0-9])|(14(0|[5-7]|9))|(15([0-3]|[5-9]))|(16(2|[5-8]))|(17[0-8])|(18[0-9])|(19([0-3]|[5-9])))\\d{8}$";
+    // 邮箱
+    String EXP_EMAIL = "^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$\n";
+    // 正型
+    String EXP_INT = "^\\d+$";
+    String EXP_JSON_A = "^\\[.?|.+\\]$";
+    String EXP_JSON_O = "^\\{.?|.+\\}$";
+
+    // 参数配置
+    String PARAM_SMS_MAX_TIMES = "sms_max_times";
+    // 参数配置
+    String PARAM_SMS_RECENT_MINUTES = "sms_recent_minutes";
+    // 密码前缀
+    String PASSWORD_PREFIX = "yyszkt";
+
+    // 时间格式化
+    String FORMAT_YYYY_MM_DD = "yyyyMMdd";
+
+    // IM用户ID格式
+    String IM_USER_ID_FORMAT = "{0}_{1}";
+    String IM_USER_LONG_ID_FORMAT = "{0}_{1}_{2}";
+    // 用户ID分隔符
+    String IM_USER_ID_SPLIT = "_";
+
+}

+ 107 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/enums/im/EClientType.java

@@ -0,0 +1,107 @@
+package com.ym.mec.biz.dal.enums.im;
+
+import com.baomidou.mybatisplus.annotation.EnumValue;
+import com.google.common.collect.Lists;
+import com.microsvc.toolkit.common.enums.ConverterEnum;
+import lombok.Getter;
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.List;
+
+/**
+ * 客户端类型
+ * Created by Eric.Shang on 2022/11/4.
+ */
+@Getter
+public enum EClientType implements ConverterEnum<String> {
+
+    BACKEND(1, "后端", "cooleshow-backend", "", "backend_login_locked"),
+    SCHOOL(2, "教务端", "cooleshow-school", "school_device_num", "school_login_locked"),
+    TEACHER(3, "老师端", "cooleshow-teacher", "teacher_device_num", "teacher_login_locked"),
+    STUDENT(4, "学生端", "cooleshow-student", "student_device_num", "student_login_locked"),
+    ;
+
+    private final int value;
+    private final String name;
+    private final String clientId;
+    private final String deviceParam;
+    private final String lockedParam;
+
+    @EnumValue
+    private final String code;
+
+    EClientType(int value, String name, String clientId, String param, String lockParam) {
+        this.value = value;
+        this.name = name;
+        this.clientId = clientId;
+        this.deviceParam = param;
+        this.lockedParam = lockParam;
+
+        this.code = this.name();
+    }
+
+    /**
+     * 客户端类型
+     * @param clientId 授权client_id
+     * @return EClientType
+     */
+    public static EClientType clientType(String clientId) {
+
+        if (StringUtils.isNoneBlank(clientId)) {
+
+            for (EClientType item : EClientType.values()) {
+
+                if (Lists.newArrayList(item.getClientId().split(",")).contains(clientId)) {
+
+                    return item;
+                }
+            }
+        }
+
+        return BACKEND;
+    }
+
+    public static EClientType clientName(String name) {
+
+        if (StringUtils.isNoneBlank(name)) {
+
+            for (EClientType item : EClientType.values()) {
+
+                if (Lists.newArrayList(item.name()).contains(name)) {
+
+                    return item;
+                }
+            }
+        }
+
+        return BACKEND;
+    }
+
+
+    /**
+     * 客户端对象比较
+     * @param code 客户端类型
+     * @return boolean
+     */
+    public boolean match(String code) {
+        return getCode().equals(code.toUpperCase());
+    }
+
+    /**
+     * jwtToken失效时间
+     * @param clientType 客户端类型
+     * @return boolean
+     */
+    public static boolean jwtTokenExpire(String clientType) {
+
+        List<EClientType> clientTypes = Lists.newArrayList(BACKEND, TEACHER);
+        for (EClientType item : clientTypes) {
+
+            // 需要设置token失效时间
+            if (item.getCode().equals(clientType)) {
+                return true;
+            }
+        }
+        return false;
+    }
+}

+ 137 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/wrapper/ImGroupNoticeWrapper.java

@@ -0,0 +1,137 @@
+package com.ym.mec.biz.dal.wrapper;
+
+import com.alibaba.fastjson.JSON;
+import com.microsvc.toolkit.common.response.paging.QueryInfo;
+import com.ym.mec.biz.dal.enums.im.EClientType;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.Serializable;
+import java.util.Date;
+import java.util.Optional;
+
+/**
+ * 群公告
+ * 2022-11-26 10:55:18
+ */
+@ApiModel(value = "ImGroupNoticeWrapper对象", description = "群公告查询对象")
+public class ImGroupNoticeWrapper {
+
+    @Data
+	@Builder
+    @NoArgsConstructor
+    @AllArgsConstructor
+    @ApiModel(" ImGroupNoticeQuery-群公告")
+    public static class ImGroupNoticeQuery implements QueryInfo {
+
+    	@ApiModelProperty("当前页")
+        private Integer page;
+
+        @ApiModelProperty("分页行数")
+        private Integer rows;
+
+        @ApiModelProperty("关键字匹配")
+		private String keyword;
+
+        @ApiModelProperty("群组ID")
+        private String groupId;
+
+        @ApiModelProperty("删除标记")
+        private Boolean delFlag;
+
+        public static ImGroupNoticeQuery from(String json) {
+            return JSON.parseObject(json, ImGroupNoticeQuery.class);
+        }
+
+        public Boolean getDelFlag() {
+            return Optional.ofNullable(delFlag).orElse(false);
+        }
+
+        public String getKeyword() {
+            return Optional.ofNullable(keyword).filter(StringUtils::isNotBlank).orElse(null);
+        }
+    }
+
+    @Data
+	@Builder
+    @NoArgsConstructor
+    @AllArgsConstructor
+	@ApiModel(" ImGroupNotice-群公告")
+    public static class ImGroupNotice {
+
+
+		@ApiModelProperty("主键")
+		private Long id;
+
+		@ApiModelProperty("群组ID")
+		private String groupId;
+
+		@ApiModelProperty("标题")
+		private String title;
+
+		@ApiModelProperty("内容")
+		private String content;
+
+		@ApiModelProperty("是否置顶")
+		private Boolean topFlag;
+
+		@ApiModelProperty("是否发送给新人")
+		private Boolean sentToNewMemberFlag;
+
+		@ApiModelProperty("操作人")
+		private Long operatorId;
+
+        @ApiModelProperty("操作用户身份")
+        private EClientType clientType;
+
+		@ApiModelProperty("是否删除1是0否;")
+		private Boolean delFlag;
+
+        @ApiModelProperty("IM消息ID")
+        private String messageSeqId;
+
+		@ApiModelProperty("修改时间")
+		private Date updateTime;
+
+		@ApiModelProperty("创建时间")
+		private Date createTime;
+
+        public static ImGroupNotice from(String json) {
+            return JSON.parseObject(json, ImGroupNotice.class);
+        }
+
+	}
+
+    @Data
+    @Builder
+    @NoArgsConstructor
+    @AllArgsConstructor
+    @ApiModel(" 自定义群公告")
+    public static class CustomGroupNotice implements Serializable {
+
+        @ApiModelProperty("业务ID")
+        private String businessID;
+
+        @ApiModelProperty("群公告ID")
+        private String msgId;
+
+        @ApiModelProperty("群公告标题")
+        private String msgTitle;
+
+        @ApiModelProperty("群公告内容")
+        private String msgContent;
+
+        public String jsonString() {
+            return JSON.toJSONString(this);
+        }
+
+        public String getBusinessID() {
+            return Optional.ofNullable(businessID).orElse("TC_GROUP_NOTICE");
+        }
+    }
+}

+ 2 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/ImGroupNoticeService.java

@@ -21,4 +21,6 @@ public interface ImGroupNoticeService extends BaseService<Long, ImGroupNotice> {
 	void modify(ImGroupNotice imGroupNotice);
 
 	ImGroupNoticeDao getDao();
+
+    Long add(ImGroupNotice imGroupNotice);
 }

+ 128 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/impl/ImGroupNoticeServiceImpl.java

@@ -1,29 +1,47 @@
 package com.ym.mec.biz.service.impl;
 
+import com.google.common.collect.Lists;
+import com.microsvc.toolkit.config.jwt.utils.JwtUserInfo;
+import com.microsvc.toolkit.middleware.im.ImPluginContext;
+import com.microsvc.toolkit.middleware.im.message.ETencentMessage;
+import com.microsvc.toolkit.middleware.im.message.MessageWrapper;
+import com.microsvc.toolkit.middleware.im.message.TencentRequest;
+import com.microsvc.toolkit.middleware.im.properties.ImConfigProperties;
 import com.ym.mec.biz.dal.dao.ImGroupNoticeDao;
 import com.ym.mec.biz.dal.dto.ImGroupNoticeDto;
 import com.ym.mec.biz.dal.entity.ImGroupNotice;
+import com.ym.mec.biz.dal.enums.im.CK;
+import com.ym.mec.biz.dal.enums.im.EClientType;
 import com.ym.mec.biz.dal.page.ImGroupNoticeQueryInfo;
+import com.ym.mec.biz.dal.wrapper.ImGroupNoticeWrapper;
 import com.ym.mec.biz.service.ImGroupNoticeService;
 import com.ym.mec.common.dal.BaseDAO;
 import com.ym.mec.common.exception.BizException;
 import com.ym.mec.common.page.PageInfo;
 import com.ym.mec.common.service.impl.BaseServiceImpl;
 import com.ym.mec.util.collection.MapUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 @Service
+@Slf4j
 public class ImGroupNoticeServiceImpl extends BaseServiceImpl<Long, ImGroupNotice>  implements ImGroupNoticeService {
 	
 	@Autowired
 	private ImGroupNoticeDao imGroupNoticeDao;
+	@Autowired
+	private ImPluginContext imPluginContext;
+	@Autowired
+	private ImConfigProperties imConfig;
 
 	@Override
 	public BaseDAO<Long, ImGroupNotice> getDAO() {
@@ -66,6 +84,17 @@ public class ImGroupNoticeServiceImpl extends BaseServiceImpl<Long, ImGroupNotic
 			imGroupNoticeDao.updateTop(imGroupNotice.getImGroupId(),false);
 		}
 		imGroupNoticeDao.update(imGroupNotice);
+		// 撤销群公告消息
+		revokeImGroupNoticeMessage(imGroupNotice);
+
+		// 发送群公告消息
+		String messageSeqId = sendGroupCustomNoticeMessage(convertToImGroupNotice(imGroupNotice));
+
+		// 更新群公告消息ID
+		if (StringUtils.isNotBlank(messageSeqId)) {
+			imGroupNotice.setMessageSeqId(messageSeqId);
+			this.getDao().update(imGroupNotice);
+		}
 	}
 
 	@Override
@@ -73,4 +102,103 @@ public class ImGroupNoticeServiceImpl extends BaseServiceImpl<Long, ImGroupNotic
 		return imGroupNoticeDao;
 	}
 
+    @Override
+    public Long add(ImGroupNotice imGroupNotice) {
+		if(imGroupNotice.isIsTop()){
+			//置顶取消
+			this.getDao().updateTop(imGroupNotice.getImGroupId(),false);
+		}
+		this.insert(imGroupNotice);
+		// 发送群公告消息
+		String messageSeqId = sendGroupCustomNoticeMessage(convertToImGroupNotice(imGroupNotice));
+		// 更新群公告消息ID
+		if (StringUtils.isNotBlank(messageSeqId)) {
+			imGroupNotice.setMessageSeqId(messageSeqId);
+			this.getDao().update(imGroupNotice);
+		}
+		return imGroupNotice.getId();
+    }
+
+	public ImGroupNoticeWrapper.ImGroupNotice convertToImGroupNotice(ImGroupNotice imGroupNotice) {
+		ImGroupNoticeWrapper.ImGroupNotice notice = ImGroupNoticeWrapper.ImGroupNotice.builder()
+				.id(imGroupNotice.getId())
+				.groupId(imGroupNotice.getImGroupId())
+				.title(imGroupNotice.getTitle())
+				.content(imGroupNotice.getContent())
+				.topFlag(imGroupNotice.isIsTop())
+				.sentToNewMemberFlag(true)
+				.operatorId(imGroupNotice.getOperatorId())
+				.clientType(EClientType.valueOf(imGroupNotice.getClientType()))
+				.delFlag(imGroupNotice.isDelFlag())
+				.build();
+		return notice;
+	}
+
+
+	/**
+	 * 发送群公告消息
+	 *
+	 * @param notice   ImGroupNoticeWrapper.ImGroupNotice
+	 * @return 消息ID
+	 */
+	private String sendGroupCustomNoticeMessage(ImGroupNoticeWrapper.ImGroupNotice notice) {
+		String messageSeqId;
+		// 发送群公告消息
+		ImGroupNoticeWrapper.CustomGroupNotice customGroupNotice = ImGroupNoticeWrapper.CustomGroupNotice.builder()
+				.businessID("TC_GROUP_NOTICE")
+				.msgId(String.valueOf(notice.getId()))
+				.msgTitle(notice.getTitle())
+				.msgContent(notice.getContent())
+				.build();
+
+		TencentRequest.MessageBody messageBody = TencentRequest.MessageBody.builder()
+				.msgType(ETencentMessage.TIMCustomElem.name())
+				.msgContent(TencentRequest.CustomMessageBody.builder()
+						.data(customGroupNotice.jsonString())
+						.desc("群公告")
+						.build())
+				.build();
+
+		try {
+			// 发送群公告消息
+			messageSeqId = imPluginContext.getPluginService().sendGroupMessage(MessageWrapper.GroupMessage.builder()
+					.senderId(getImUserId(notice.getOperatorId(), notice.getClientType()))
+					.groupId(notice.getGroupId())
+					.tencentMessage(messageBody)
+					.build());
+		} catch (Exception e) {
+			log.error("群公告消息发送失败, messageId={}, groupId={}, title={}, ", notice.getId(), notice.getGroupId(),
+					notice.getTitle(), e);
+			throw com.microsvc.toolkit.common.webportal.exception.BizException.from("群公告消息发送失败");
+		}
+		return messageSeqId;
+	}
+
+	public String getImUserId(Long userId, EClientType clientType) {
+		String imUserId;
+		imUserId = MessageFormat.format(CK.IM_USER_LONG_ID_FORMAT, imConfig.getAppPrefix(), userId.toString(),clientType.name());
+		return imUserId;
+	}
+
+	/**
+	 * 撤销群公告消息
+	 * @param imGroupNotice ImGroupNotice
+	 */
+	private void revokeImGroupNoticeMessage(ImGroupNotice imGroupNotice) {
+		try {
+			if (StringUtils.isNotBlank(imGroupNotice.getMessageSeqId())) {
+				imPluginContext.getPluginService().recallGroupMessage(MessageWrapper.RecallGroupMessage.builder()
+						.groupId(imGroupNotice.getImGroupId())
+						.reason("群公告消息撤销")
+						.messageSeqs(Lists.newArrayList(
+								TencentRequest.GroupMessageSeq.builder().messageSeq(Integer.parseInt(imGroupNotice.getMessageSeqId())).build()
+						))
+						.build());
+			}
+		} catch (Exception e) {
+			log.error("撤销群公告消息失败, messageId={}, groupId={}, title={}, ", imGroupNotice.getId(), imGroupNotice.getImGroupId(),
+					imGroupNotice.getTitle(), e);
+		}
+	}
+
 }

+ 8 - 2
mec-biz/src/main/resources/config/mybatis/ImGroupNoticeMapper.xml

@@ -18,6 +18,7 @@
 		<result column="del_flag_" property="delFlag" />
 		<result column="operator_id_" property="operatorId" />
         <result column="tenant_id_" property="tenantId"/>
+        <result column="message_seq_id_" property="messageSeqId"/>
 	</resultMap>
 	
 	<resultMap type="com.ym.mec.biz.dal.dto.ImGroupNoticeDto" id="ImGroupNoticeDto" extends="ImGroupNotice">
@@ -38,8 +39,10 @@
 	
 	<!-- 向数据库增加一条记录 -->
 	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.ImGroupNotice" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
-		INSERT INTO im_group_notice (im_group_id_,title_,content_,is_top_,is_sent_to_new_member_,create_time_,update_time_,del_flag_,operator_id_,tenant_id_)
-		VALUES(#{imGroupId},#{title},#{content},#{isTop},#{isSentToNewMember},NOW(),NOW(),#{delFlag},#{operatorId},#{tenantId})
+		INSERT INTO im_group_notice (im_group_id_,title_,content_,is_top_,is_sent_to_new_member_,
+		                             create_time_,update_time_,del_flag_,operator_id_,tenant_id_,message_seq_id_)
+		VALUES(#{imGroupId},#{title},#{content},#{isTop},#{isSentToNewMember},
+		       NOW(),NOW(),#{delFlag},#{operatorId},#{tenantId},#{messageSeqId})
 	</insert>
 	
 	<!-- 根据主键查询一条记录 -->
@@ -48,6 +51,9 @@
 		<if test="delFlag != null">
 		del_flag_ = #{delFlag},
 		</if>
+		<if test="messageSeqId != null">
+			message_seq_id_ = #{messageSeqId},
+		</if>
 		<if test="operatorId != null">
 		operator_id_ = #{operatorId},
 		</if>

+ 1 - 1
pom.xml

@@ -25,7 +25,7 @@
 		<docker.host>http://127.0.0.1:2375</docker.host>
 		<docker.registry.repository>127.0.0.1:5000</docker.registry.repository>
 		<docker.maven.plugin.version>1.2.2</docker.maven.plugin.version>
-		<com.microsvc.toolkit.version>1.0.8</com.microsvc.toolkit.version>
+		<com.microsvc.toolkit.version>1.0.8-RC1</com.microsvc.toolkit.version>
 		<cbs.version>1.0.15</cbs.version>
 	</properties>