package com.onekeycall.videotablet.controller; 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.CXAESUtil; import com.onekeycall.videotablet.utils.JwtUtil; import com.onekeycall.videotablet.utils.PushUtils; import com.onekeycall.videotablet.utils.TextUtils; import org.apache.commons.lang3.RandomStringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.Date; import java.util.HashMap; import java.util.Map; @RestController @RequestMapping("/public") public class ManageSnController { Logger logger = LoggerFactory.getLogger(ManageSnController.class); @Autowired private JwtUtil jwtUtil; @Autowired private UserService userService; @Autowired private DeviceSnService deviceSnService; @PostMapping("/add_sn") public Result addSn( @RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId, @RequestParam(value = "user_id") String userId, @RequestParam(value = "sn") String sn) { // 1. 校验 Authorization 头 if (!authHeader.startsWith("Bearer ")) { return Result.error().message("Invalid Authorization header"); } String token = authHeader.substring(7); // 去掉 "Bearer " 前缀 // 2. 校验 Token if (!jwtUtil.validateAccessToken(userId, token, deviceId)) { return Result.error().message("Invalid token"); } // 3. 校验 sn 是否存在 DeviceInfo oldDeviceInfo = deviceSnService.findBySn(sn); if (oldDeviceInfo != null) { return Result.error().message("sn already exists"); } // 4. 新增 sn DeviceInfo deviceInfo = new DeviceInfo(); deviceInfo.setSn(sn); deviceInfo.setAddTime(new Date()); deviceInfo.setDeviceModel(deviceId); deviceSnService.save(deviceInfo); return Result.ok(); } @GetMapping("/decode_sn") public Result decodeSn( @RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId, @RequestParam(value = "user_id") String userId, @RequestParam(value = "encrypt_sn") String encryptSn) throws Exception { logger.info("Authorization = {}, Device-ID = {}, user_id = {}, encrypt_sn = {}", authHeader, deviceId, userId, encryptSn); // 1. 校验 Authorization 头 if (!authHeader.startsWith("Bearer ")) { return Result.error().message("Invalid Authorization header"); } String token = authHeader.substring(7); // 去掉 "Bearer " 前缀 // 2. 校验 Token if (!jwtUtil.validateAccessToken(userId, token, deviceId)) { return Result.error().message("Invalid token"); } // 3. 解密 sn String sn = CXAESUtil.decrypt(CXAESUtil.key, encryptSn); logger.info("sn = {}", sn); if (TextUtils.isEmpty(sn)) { return Result.error().message("sn decrypt failed"); } DeviceInfo deviceInfo = deviceSnService.findBySn(sn); if (deviceInfo == null) { return Result.error().message("sn not found"); } if (!TextUtils.isEmpty(deviceInfo.getBindPhone())) { return Result.ok().message("sn already bind"); } Map map = new HashMap<>(); map.put("sn", sn); return Result.ok().data(map); } }