增加添加和获取联系人接口,AesAttributeConverter中autoApply = false取消全局加密

This commit is contained in:
2025-08-26 19:07:26 +08:00
parent 49572f46d7
commit bab1db37e3
6 changed files with 129 additions and 7 deletions

View File

@@ -1,9 +1,12 @@
package com.onekeycall.videotablet.controller; 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.Contact;
import com.onekeycall.videotablet.entity.DeviceInfo; import com.onekeycall.videotablet.entity.DeviceInfo;
import com.onekeycall.videotablet.entity.User; import com.onekeycall.videotablet.entity.User;
import com.onekeycall.videotablet.result.Result; import com.onekeycall.videotablet.result.Result;
import com.onekeycall.videotablet.service.ContactService;
import com.onekeycall.videotablet.service.DeviceSnService; import com.onekeycall.videotablet.service.DeviceSnService;
import com.onekeycall.videotablet.service.UserService; import com.onekeycall.videotablet.service.UserService;
import com.onekeycall.videotablet.utils.JwtUtil; import com.onekeycall.videotablet.utils.JwtUtil;
@@ -13,6 +16,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@RestController @RestController
@RequestMapping("/contact") @RequestMapping("/contact")
@@ -23,6 +27,8 @@ public class ContactController {
private UserService userService; private UserService userService;
@Autowired @Autowired
private DeviceSnService deviceSnService; private DeviceSnService deviceSnService;
@Autowired
private ContactService contactService;
@Autowired @Autowired
private RedisTemplate<String, Object> redisTemplate; private RedisTemplate<String, Object> redisTemplate;
@@ -33,7 +39,9 @@ public class ContactController {
public Result userAddContact( public Result userAddContact(
@RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId, @RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId,
@RequestParam(value = "user_id") String userId, @RequestParam(value = "sn") String sn, @RequestParam(value = "user_id") String userId, @RequestParam(value = "sn") String sn,
@Valid @RequestBody Contact contact) { @RequestPart(value = "file", required = false) MultipartFile file,
@RequestPart("contact_json") String jsonData
) throws JsonProcessingException {
if (!authHeader.startsWith("Bearer ")) { if (!authHeader.startsWith("Bearer ")) {
return Result.error().message("Invalid Authorization header"); return Result.error().message("Invalid Authorization header");
@@ -57,6 +65,17 @@ public class ContactController {
return Result.error().message("device not belong to user"); 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(); return Result.ok();
} }
} }

View File

@@ -4,6 +4,7 @@ import com.onekeycall.videotablet.entity.Contact;
import com.onekeycall.videotablet.entity.DeviceInfo; import com.onekeycall.videotablet.entity.DeviceInfo;
import com.onekeycall.videotablet.entity.DeviceLocation; import com.onekeycall.videotablet.entity.DeviceLocation;
import com.onekeycall.videotablet.result.Result; import com.onekeycall.videotablet.result.Result;
import com.onekeycall.videotablet.service.ContactService;
import com.onekeycall.videotablet.service.DeviceLocationService; import com.onekeycall.videotablet.service.DeviceLocationService;
import com.onekeycall.videotablet.service.DeviceSnService; import com.onekeycall.videotablet.service.DeviceSnService;
import com.onekeycall.videotablet.utils.JwtUtil; import com.onekeycall.videotablet.utils.JwtUtil;
@@ -26,9 +27,12 @@ public class DevicesController {
private DeviceSnService deviceSnService; private DeviceSnService deviceSnService;
@Autowired @Autowired
private DeviceLocationService deviceLocationService; private DeviceLocationService deviceLocationService;
@Autowired
private ContactService contactService;
@GetMapping("/get_sn_list") @GetMapping("/get_sn_list")
public Result register(@RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId, public Result register(
@RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId,
@RequestParam(value = "user_id") String userId, @RequestParam(value = "sn", required = false) String sn) { @RequestParam(value = "user_id") String userId, @RequestParam(value = "sn", required = false) String sn) {
// 1. 校验 Authorization 头 // 1. 校验 Authorization 头
if (!authHeader.startsWith("Bearer ")) { if (!authHeader.startsWith("Bearer ")) {
@@ -98,4 +102,35 @@ public class DevicesController {
} }
return Result.ok(); return Result.ok();
} }
@GetMapping("/get_contacts")
public Result getContacts(
@RequestHeader("Device-Token") String deviceToken, @RequestHeader("Device-ID") String deviceId,
@RequestHeader("Device-Sig") String deviceSig, @RequestParam(value = "sn") String sn
) {
if (!jwtUtil.validateDeviceToken(deviceToken, deviceId, sn)) {
return Result.error().message("Invalid token");
}
DeviceInfo deviceInfo = deviceSnService.findBySn(sn);
if (deviceInfo == null) {
return Result.notFound().message("sn not found");
}
if (!deviceInfo.getBindSig().equals(deviceSig)) {
return Result.error().message("device sig not match");
}
if (TextUtils.isEmpty(deviceInfo.getBindPhone())) {
return Result.error().message("sn not bind");
}
List<Contact> contacts = contactService.findAllBySn(sn);
if (contacts == null || contacts.isEmpty()) {
return Result.notFound().message("contacts not found");
}
return Result.ok().data("contacts", contacts);
}
} }

View File

@@ -5,7 +5,7 @@ import com.onekeycall.videotablet.utils.AESUtil;
import jakarta.persistence.AttributeConverter; import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter; import jakarta.persistence.Converter;
@Converter(autoApply = true) @Converter(autoApply = false)// 全局自动应用开关
public class AesAttributeConverter implements AttributeConverter<String, String> { public class AesAttributeConverter implements AttributeConverter<String, String> {
@Override @Override

View File

@@ -1,5 +1,6 @@
package com.onekeycall.videotablet.entity; package com.onekeycall.videotablet.entity;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.persistence.*; import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import lombok.Data; import lombok.Data;
@@ -18,8 +19,9 @@ public class Contact {
private String name; private String name;
@NotBlank(message = "手机号不能为空") @NotBlank(message = "手机号不能为空")
@Column @JsonProperty("phone_number")
private String phone_number; @Column(name = "phone_number")
private String phoneNumber;
@Column @Column
private String avatar; private String avatar;

View File

@@ -0,0 +1,18 @@
package com.onekeycall.videotablet.repository;
import com.onekeycall.videotablet.entity.Contact;
import com.onekeycall.videotablet.entity.DeviceLocation;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface ContactRepository extends JpaRepository<Contact, Long> {
boolean existsByPhoneNumber(String phone);
boolean existsByPhoneNumberAndSn(String phone, String sn);
Contact findByPhoneNumber(String phone);
Contact findByPhoneNumberAndUserId(String phone, String userId);
Contact findByUserId(String userId);
List<Contact> findAllByUserId(String userId);
List<Contact> findAllBySn(String sn);
Contact findBySn(String sn);
}

View File

@@ -0,0 +1,48 @@
package com.onekeycall.videotablet.service;
import com.onekeycall.videotablet.entity.Contact;
import com.onekeycall.videotablet.entity.DeviceLocation;
import com.onekeycall.videotablet.repository.ContactRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ContactService {
private final ContactRepository contactRepository;
@Autowired
public ContactService(ContactRepository deviceSnRepository) {
this.contactRepository = deviceSnRepository;
}
public boolean isExistByPhoneNumber(String phoneNumber) {
return contactRepository.existsByPhoneNumber(phoneNumber);
}
public boolean isExistByPhoneNumberAndSn(String phoneNumber, String sn) {
return contactRepository.existsByPhoneNumberAndSn(phoneNumber, sn);
}
public Contact findByPhoneNumber(String phoneNumber) {
return contactRepository.findByPhoneNumber(phoneNumber);
}
public Contact findByPhoneNumberAndUserId(String phoneNumber, String userId) {
return contactRepository.findByPhoneNumberAndUserId(phoneNumber, userId);
}
public Contact findByUserId(String userId) {
return contactRepository.findByUserId(userId);
}
public List<Contact> findAllBySn(String sn) {
return contactRepository.findAllBySn(sn);
}
public void save(Contact contact) {
contactRepository.save(contact);
}
}