bind_sn接口移动到user下

This commit is contained in:
2025-09-15 09:37:34 +08:00
parent b3352a2864
commit 9b03e11919
2 changed files with 56 additions and 52 deletions

View File

@@ -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(