增加添加和获取联系人接口,AesAttributeConverter中autoApply = false取消全局加密
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
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;
|
||||
@@ -13,6 +16,7 @@ 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")
|
||||
@@ -23,6 +27,8 @@ public class ContactController {
|
||||
private UserService userService;
|
||||
@Autowired
|
||||
private DeviceSnService deviceSnService;
|
||||
@Autowired
|
||||
private ContactService contactService;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
@@ -33,7 +39,9 @@ public class ContactController {
|
||||
public Result userAddContact(
|
||||
@RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId,
|
||||
@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 ")) {
|
||||
return Result.error().message("Invalid Authorization header");
|
||||
@@ -57,6 +65,17 @@ public class ContactController {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.onekeycall.videotablet.entity.Contact;
|
||||
import com.onekeycall.videotablet.entity.DeviceInfo;
|
||||
import com.onekeycall.videotablet.entity.DeviceLocation;
|
||||
import com.onekeycall.videotablet.result.Result;
|
||||
import com.onekeycall.videotablet.service.ContactService;
|
||||
import com.onekeycall.videotablet.service.DeviceLocationService;
|
||||
import com.onekeycall.videotablet.service.DeviceSnService;
|
||||
import com.onekeycall.videotablet.utils.JwtUtil;
|
||||
@@ -26,9 +27,12 @@ public class DevicesController {
|
||||
private DeviceSnService deviceSnService;
|
||||
@Autowired
|
||||
private DeviceLocationService deviceLocationService;
|
||||
@Autowired
|
||||
private ContactService contactService;
|
||||
|
||||
@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) {
|
||||
// 1. 校验 Authorization 头
|
||||
if (!authHeader.startsWith("Bearer ")) {
|
||||
@@ -87,7 +91,7 @@ public class DevicesController {
|
||||
return Result.error().message("sn not bind");
|
||||
}
|
||||
if (deviceLocationService.isExist(sn)) {
|
||||
DeviceLocation deviceLocationDB=deviceLocationService.getDeviceLocation(sn);
|
||||
DeviceLocation deviceLocationDB = deviceLocationService.getDeviceLocation(sn);
|
||||
deviceLocation.setId(deviceLocationDB.getId());
|
||||
deviceLocation.setUpdateTime(new Date(System.currentTimeMillis()));
|
||||
deviceLocationService.save(deviceLocation);
|
||||
@@ -98,4 +102,35 @@ public class DevicesController {
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.onekeycall.videotablet.utils.AESUtil;
|
||||
import jakarta.persistence.AttributeConverter;
|
||||
import jakarta.persistence.Converter;
|
||||
|
||||
@Converter(autoApply = true)
|
||||
@Converter(autoApply = false)// 全局自动应用开关
|
||||
public class AesAttributeConverter implements AttributeConverter<String, String> {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.onekeycall.videotablet.entity;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
@@ -18,8 +19,9 @@ public class Contact {
|
||||
private String name;
|
||||
|
||||
@NotBlank(message = "手机号不能为空")
|
||||
@Column
|
||||
private String phone_number;
|
||||
@JsonProperty("phone_number")
|
||||
@Column(name = "phone_number")
|
||||
private String phoneNumber;
|
||||
|
||||
@Column
|
||||
private String avatar;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user