Files
OneKeyCallVideoTablet/src/main/java/com/onekeycall/videotablet/controller/ContactController.java

82 lines
3.0 KiB
Java

package com.onekeycall.videotablet.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
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.ContactService;
import com.onekeycall.videotablet.service.DeviceSnService;
import com.onekeycall.videotablet.service.UserService;
import com.onekeycall.videotablet.utils.JwtUtil;
import jakarta.validation.Valid;
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.*;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/contact")
public class ContactController {
@Autowired
private JwtUtil jwtUtil;
@Autowired
private UserService userService;
@Autowired
private DeviceSnService deviceSnService;
@Autowired
private ContactService contactService;
@Autowired
private RedisTemplate<String, Object> 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,
@RequestPart(value = "file", required = false) MultipartFile file,
@RequestPart("contact_json") String jsonData
) throws JsonProcessingException {
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");
}
ObjectMapper mapper = new ObjectMapper();
Contact contact = mapper.readValue(jsonData, Contact.class);
if (contactService.isExistByPhoneNumberAndSn(contact.getPhoneNumber(), sn)) {
return Result.error().message("contact already exist");
}
contact.setUserId(userId);
contact.setSn(sn);
contactService.save(contact);
return Result.ok();
}
}