From 9b03e11919797ac0202df0093cf552ffc1ac0a02 Mon Sep 17 00:00:00 2001 From: tongtongstudio Date: Mon, 15 Sep 2025 09:37:34 +0800 Subject: [PATCH] =?UTF-8?q?bind=5Fsn=E6=8E=A5=E5=8F=A3=E7=A7=BB=E5=8A=A8?= =?UTF-8?q?=E5=88=B0user=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/sn/BindSnController.java | 52 ----------------- .../controller/user/UserController.java | 56 +++++++++++++++++++ 2 files changed, 56 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/onekeycall/videotablet/controller/sn/BindSnController.java b/src/main/java/com/onekeycall/videotablet/controller/sn/BindSnController.java index c00fde5..4a1dbda 100644 --- a/src/main/java/com/onekeycall/videotablet/controller/sn/BindSnController.java +++ b/src/main/java/com/onekeycall/videotablet/controller/sn/BindSnController.java @@ -37,59 +37,7 @@ public class BindSnController { Logger logger = LoggerFactory.getLogger(BindSnController.class); - /** - * 用户app发送绑定推送到手机 - * - * @param authHeader - * @param deviceId - * @param userId - * @param sn - * @return - */ - @PostMapping("/bind_sn") - public Result bindSn( - @RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId, - @RequestParam(value = "user_id") String userId, @RequestParam(value = "sn") String sn) { - logger.info("bindSn: authHeader={}, deviceId={}, userId={}, sn={}", authHeader, deviceId, userId, sn); - 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); - String userPhone = user.getPhone(); - - DeviceInfo deviceInfo = deviceSnService.findBySn(sn); - if (deviceInfo == null) { - return Result.error().message("sn not found"); - } - - if (!TextUtils.isEmpty(deviceInfo.getBindPhone())) { - return Result.error().message("sn already bind"); - } - - try { - String verifyKey = RandomStringUtils.randomAlphanumeric(32); - JsonObject params = new JsonObject(); - params.addProperty("verify_key", verifyKey); - params.addProperty("phone", userPhone); - params.addProperty("expire_time", System.currentTimeMillis() + 60 * 1000); - -// PushUtils.aliyunAsyncPush("1", params.toString(), sn); - DevicePushUtils.aliyunAsyncPush(PushIdConfig.BIND_DEVICE, sn, params.toString()); - redisTemplate.opsForValue().set(sn, verifyKey, 1, TimeUnit.MINUTES); - return Result.ok().message("send message success"); - } catch (Exception e) { - e.printStackTrace(); - return Result.error().message(e.getMessage()); - } - - } /** * 平板根据返回的数据绑定手机 diff --git a/src/main/java/com/onekeycall/videotablet/controller/user/UserController.java b/src/main/java/com/onekeycall/videotablet/controller/user/UserController.java index 3dcd8cb..3ac2c24 100644 --- a/src/main/java/com/onekeycall/videotablet/controller/user/UserController.java +++ b/src/main/java/com/onekeycall/videotablet/controller/user/UserController.java @@ -1,5 +1,6 @@ package com.onekeycall.videotablet.controller.user; +import com.google.gson.JsonObject; import com.onekeycall.videotablet.config.PushIdConfig; import com.onekeycall.videotablet.controller.pub.LoginController; import com.onekeycall.videotablet.dto.TokenPair; @@ -10,6 +11,7 @@ import com.onekeycall.videotablet.service.*; import com.onekeycall.videotablet.utils.DevicePushUtils; import com.onekeycall.videotablet.utils.JwtUtil; 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; @@ -22,6 +24,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; import java.util.function.Consumer; @RestController @@ -52,6 +55,59 @@ public class UserController { this.authenticationManager = authenticationManager; } + /** + * 用户app发送绑定推送到手机 + * + * @param authHeader + * @param deviceId + * @param userId + * @param sn + * @return + */ + @PostMapping("/bind_sn") + public Result bindSn( + @RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId, + @RequestParam(value = "user_id") String userId, @RequestParam(value = "sn") String sn) { + + logger.info("bindSn: authHeader={}, deviceId={}, userId={}, sn={}", authHeader, deviceId, userId, sn); + 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); + String userPhone = user.getPhone(); + + DeviceInfo deviceInfo = deviceSnService.findBySn(sn); + if (deviceInfo == null) { + return Result.error().message("sn not found"); + } + + if (!TextUtils.isEmpty(deviceInfo.getBindPhone())) { + return Result.error().message("sn already bind"); + } + + try { + String verifyKey = RandomStringUtils.randomAlphanumeric(32); + JsonObject params = new JsonObject(); + params.addProperty("verify_key", verifyKey); + params.addProperty("phone", userPhone); + params.addProperty("expire_time", System.currentTimeMillis() + 60 * 1000); + +// PushUtils.aliyunAsyncPush("1", params.toString(), sn); + DevicePushUtils.aliyunAsyncPush(PushIdConfig.BIND_DEVICE, sn, params.toString()); + redisTemplate.opsForValue().set(sn, verifyKey, 1, TimeUnit.MINUTES); + return Result.ok().message("send message success"); + } catch (Exception e) { + e.printStackTrace(); + return Result.error().message(e.getMessage()); + } + + } @PostMapping("/refresh_token") public Result refreshToken(