caolinxuan 2 weeks ago
parent
commit
a4802c960f

+ 24 - 0
src/main/java/com/zhentao/moment/controller/MonmentController.java

@@ -0,0 +1,24 @@
+package com.zhentao.moment.controller;
+
+import com.zhentao.moment.dto.MonmentDto;
+import com.zhentao.moment.service.UserMomentsService;
+import com.zhentao.vo.Result;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestHeader;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("monment")
+public class MonmentController {
+    @Autowired
+    private UserMomentsService userMomentsService;
+//    发布朋友圈
+    @RequestMapping("sendMonment")
+    public Result sendMonment(@RequestHeader String  token,@RequestBody MonmentDto monmentDto){
+        return userMomentsService.sendMonment(token,monmentDto);
+    }
+////    查看所有朋友圈
+//    @RequestMapping("getAllMonment")
+}

+ 14 - 0
src/main/java/com/zhentao/moment/dto/MonmentDto.java

@@ -0,0 +1,14 @@
+package com.zhentao.moment.dto;
+
+import lombok.Data;
+import org.springframework.web.multipart.MultipartFile;
+
+@Data
+public class MonmentDto {
+    private Long uid;
+    private Integer visible;//是否可见  1公开  2好友 3自己
+    private String content;//发布内容
+//    private MultipartFile urlImage;
+    private Integer contentType;//文章类型
+    private String location;
+}

+ 34 - 0
src/main/java/com/zhentao/moment/enums/ContentTypeEnum.java

@@ -0,0 +1,34 @@
+package com.zhentao.moment.enums;
+
+public enum ContentTypeEnum {
+    TEXT(1, "文本"),
+    IMAGE(2, "图片"),
+    VIDEO(3, "视频");
+    private final Integer code;
+    private final String desc;
+
+    ContentTypeEnum(Integer code, String desc) {
+        this.code = code;
+        this.desc = desc;
+    }
+
+    // 根据 code 获取枚举
+    public static ContentTypeEnum getByCode(Integer code) {
+        for (ContentTypeEnum type : values()) {
+            if (type.code.equals(code)) {
+                return type;
+            }
+        }
+        // 不存在时可抛异常或返回默认值,根据业务决定
+        throw new IllegalArgumentException("无效的 content_type: " + code);
+    }
+
+    // getter
+    public Integer getCode() {
+        return code;
+    }
+
+    public String getDesc() {
+        return desc;
+    }
+}

+ 18 - 0
src/main/java/com/zhentao/utils/SensitiveWordFilter.java

@@ -0,0 +1,18 @@
+package com.zhentao.utils;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+public class SensitiveWordFilter {
+    // 敏感词列表(可从数据库/配置文件加载)
+    private static final Set<String> SENSITIVE_WORDS = new HashSet<>(Arrays.asList("暴力", "抢劫","杀人"));
+
+    public static String filter(String content) {
+        for (String word : SENSITIVE_WORDS) {
+            // 简单替换,也可扩展为正则匹配、全角半角转换等
+            content = content.replace(word, "***");
+        }
+        return content;
+    }
+}

+ 78 - 0
src/main/java/com/zhentao/utils/SnowflakeUtil.java

@@ -0,0 +1,78 @@
+package com.zhentao.utils;
+
+/**
+ * @Date 2025/4/21 19:25
+ * @Author gln
+ **/
+
+public class SnowflakeUtil {
+    // 起始时间戳,可自定义,这里以 2020-01-01 00:00:00 为例
+    private static final long START_TIMESTAMP = 1577836800000L;
+
+    // 数据中心 ID 所占位数
+    private static final long DATA_CENTER_ID_BITS = 5L;
+    // 机器 ID 所占位数
+    private static final long WORKER_ID_BITS = 5L;
+    // 序列号所占位数
+    private static final long SEQUENCE_BITS = 12L;
+
+    // 数据中心 ID 最大值
+    private static final long MAX_DATA_CENTER_ID = -1L ^ (-1L << DATA_CENTER_ID_BITS);
+    // 机器 ID 最大值
+    private static final long MAX_WORKER_ID = -1L ^ (-1L << WORKER_ID_BITS);
+    // 序列号最大值
+    private static final long SEQUENCE_MASK = -1L ^ (-1L << SEQUENCE_BITS);
+
+    // 机器 ID 向左移位数
+    private static final long WORKER_ID_SHIFT = SEQUENCE_BITS;
+    // 数据中心 ID 向左移位数
+    private static final long DATA_CENTER_ID_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS;
+    // 时间戳向左移位数
+    private static final long TIMESTAMP_LEFT_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS + DATA_CENTER_ID_BITS;
+
+    // 固定数据中心 ID 和机器 ID
+    private static final long DATA_CENTER_ID = 1;
+    private static final long WORKER_ID = 1;
+
+    private static long sequence = 0L;
+    private static long lastTimestamp = -1L;
+
+    public static synchronized long nextId() {
+        long timestamp = System.currentTimeMillis();
+
+        if (timestamp < lastTimestamp) {
+            throw new RuntimeException("Clock moved backwards. Refusing to generate id for " + (lastTimestamp - timestamp) + " milliseconds");
+        }
+
+        if (timestamp == lastTimestamp) {
+            sequence = (sequence + 1) & SEQUENCE_MASK;
+            if (sequence == 0) {
+                timestamp = waitForNextMillis(lastTimestamp);
+            }
+        } else {
+            sequence = 0L;
+        }
+
+        lastTimestamp = timestamp;
+
+        return ((timestamp - START_TIMESTAMP) << TIMESTAMP_LEFT_SHIFT) |
+                (DATA_CENTER_ID << DATA_CENTER_ID_SHIFT) |
+                (WORKER_ID << WORKER_ID_SHIFT) |
+                sequence;
+    }
+
+    private static long waitForNextMillis(long lastTimestamp) {
+        long timestamp = System.currentTimeMillis();
+        while (timestamp <= lastTimestamp) {
+            timestamp = System.currentTimeMillis();
+        }
+        return timestamp;
+    }
+
+    public static void main(String[] args) {
+        for (int i = 0; i < 10; i++) {
+            System.out.println(nextId());
+        }
+    }
+}
+

+ 2 - 2
src/main/java/com/zhentao/vo/Result.java

@@ -26,10 +26,10 @@ public class Result {
         return result;
     }
 
-    public static Result error(Integer code,Object data){
+    public static Result error(Integer code,String msg){
         Result result=new Result();
         result.setCode(code);
-        result.setData(data);
+        result.setMsg(msg);
         return result;
     }