From a3d5df25c09c1b73abc8043eecf61eec9afb982d Mon Sep 17 00:00:00 2001 From: tongtongstudio Date: Mon, 25 Aug 2025 21:42:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B7=BB=E5=8A=A0=E8=81=94?= =?UTF-8?q?=E7=B3=BB=E4=BA=BA=E6=8E=A5=E5=8F=A3=EF=BC=8C=E6=9C=AA=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ContactController.java | 61 +++++++++++++++++++ .../entity/{Contacts.java => Contact.java} | 3 +- 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/onekeycall/videotablet/controller/ContactController.java rename src/main/java/com/onekeycall/videotablet/entity/{Contacts.java => Contact.java} (92%) diff --git a/src/main/java/com/onekeycall/videotablet/controller/ContactController.java b/src/main/java/com/onekeycall/videotablet/controller/ContactController.java new file mode 100644 index 0000000..0f3d696 --- /dev/null +++ b/src/main/java/com/onekeycall/videotablet/controller/ContactController.java @@ -0,0 +1,61 @@ +package com.onekeycall.videotablet.controller; + +import com.onekeycall.videotablet.entity.Contact; +import com.onekeycall.videotablet.entity.DeviceInfo; +import com.onekeycall.videotablet.entity.User; +import com.onekeycall.videotablet.result.Result; +import com.onekeycall.videotablet.service.DeviceSnService; +import com.onekeycall.videotablet.service.UserService; +import com.onekeycall.videotablet.utils.JwtUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/contact") +public class ContactController { + @Autowired + private JwtUtil jwtUtil; + @Autowired + private UserService userService; + @Autowired + private DeviceSnService deviceSnService; + + @Autowired + private RedisTemplate redisTemplate; + + Logger logger = LoggerFactory.getLogger(ContactController.class); + + @PostMapping("/user_add_contact") + public Result userAddContact( + @RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId, + @RequestParam(value = "user_id") String userId, @RequestParam(value = "sn") String sn + , @RequestBody Contact contact) { + + if (!authHeader.startsWith("Bearer ")) { + return Result.error().message("Invalid Authorization header"); + } + String token = authHeader.substring(7); // 去掉 "Bearer " 前缀 + + if (!jwtUtil.validateAccessToken(userId, token, deviceId)) { + return Result.error().message("Invalid token"); + } + + User user = userService.getUserByUserId(userId); + if (user == null) { + return Result.error().message("user not found"); + } + DeviceInfo deviceInfo = deviceSnService.findBySn(sn); + if (deviceInfo == null) { + return Result.error().message("device not found"); + } + + if (!deviceInfo.getUserId().equals(userId)) { + return Result.error().message("device not belong to user"); + } + + return Result.ok(); + } +} diff --git a/src/main/java/com/onekeycall/videotablet/entity/Contacts.java b/src/main/java/com/onekeycall/videotablet/entity/Contact.java similarity index 92% rename from src/main/java/com/onekeycall/videotablet/entity/Contacts.java rename to src/main/java/com/onekeycall/videotablet/entity/Contact.java index acd269c..2bcdff4 100644 --- a/src/main/java/com/onekeycall/videotablet/entity/Contacts.java +++ b/src/main/java/com/onekeycall/videotablet/entity/Contact.java @@ -6,11 +6,12 @@ import lombok.Data; @Data @Entity @Table(name = "tablet_default_settings") -public class Contacts { +public class Contact { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id",unique = true, nullable = false) private Long id; + }