diff --git a/src/main/java/com/onekeycall/videotablet/controller/ContactController.java b/src/main/java/com/onekeycall/videotablet/controller/ContactController.java index 392da27..eb33796 100644 --- a/src/main/java/com/onekeycall/videotablet/controller/ContactController.java +++ b/src/main/java/com/onekeycall/videotablet/controller/ContactController.java @@ -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 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(); } } diff --git a/src/main/java/com/onekeycall/videotablet/controller/DevicesController.java b/src/main/java/com/onekeycall/videotablet/controller/DevicesController.java index 12a81d1..48bb4b5 100644 --- a/src/main/java/com/onekeycall/videotablet/controller/DevicesController.java +++ b/src/main/java/com/onekeycall/videotablet/controller/DevicesController.java @@ -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,10 +27,13 @@ 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, - @RequestParam(value = "user_id") String userId, @RequestParam(value = "sn", required = false) String sn) { + 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 ")) { return Result.error().message("Invalid Authorization header"); @@ -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 contacts = contactService.findAllBySn(sn); + if (contacts == null || contacts.isEmpty()) { + return Result.notFound().message("contacts not found"); + } + return Result.ok().data("contacts", contacts); + } + } diff --git a/src/main/java/com/onekeycall/videotablet/converter/AesAttributeConverter.java b/src/main/java/com/onekeycall/videotablet/converter/AesAttributeConverter.java index cbd42a8..933c8b6 100644 --- a/src/main/java/com/onekeycall/videotablet/converter/AesAttributeConverter.java +++ b/src/main/java/com/onekeycall/videotablet/converter/AesAttributeConverter.java @@ -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 { @Override diff --git a/src/main/java/com/onekeycall/videotablet/entity/Contact.java b/src/main/java/com/onekeycall/videotablet/entity/Contact.java index f44797b..75d098f 100644 --- a/src/main/java/com/onekeycall/videotablet/entity/Contact.java +++ b/src/main/java/com/onekeycall/videotablet/entity/Contact.java @@ -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; diff --git a/src/main/java/com/onekeycall/videotablet/repository/ContactRepository.java b/src/main/java/com/onekeycall/videotablet/repository/ContactRepository.java new file mode 100644 index 0000000..ffc7045 --- /dev/null +++ b/src/main/java/com/onekeycall/videotablet/repository/ContactRepository.java @@ -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 { + 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 findAllByUserId(String userId); + List findAllBySn(String sn); + Contact findBySn(String sn); +} diff --git a/src/main/java/com/onekeycall/videotablet/service/ContactService.java b/src/main/java/com/onekeycall/videotablet/service/ContactService.java new file mode 100644 index 0000000..70fb218 --- /dev/null +++ b/src/main/java/com/onekeycall/videotablet/service/ContactService.java @@ -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 findAllBySn(String sn) { + return contactRepository.findAllBySn(sn); + } + + public void save(Contact contact) { + contactRepository.save(contact); + } + +}