增加添加和绑定sn,增加阿里云推送

This commit is contained in:
hc
2025-08-10 20:13:45 +08:00
parent 301b23e6e6
commit 6eed13b07d
13 changed files with 721 additions and 44 deletions

View File

@@ -6,10 +6,14 @@ import com.onekeycall.videotablet.result.Result;
import com.onekeycall.videotablet.service.DeviceSnService;
import com.onekeycall.videotablet.service.UserService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
@RestController
@RequestMapping("/public")
public class BindSnController {
@@ -50,7 +54,62 @@ public class BindSnController {
}
try {
String randomString = RandomStringUtils.randomAlphanumeric(32);
PushUtils.aliyunAsyncPush(randomString, userPhone, sn);
return Result.ok().message("send message success");
} catch (Exception e) {
e.printStackTrace();
return Result.error().message(e.getMessage());
}
return Result.ok();
}
@PostMapping("/device_bind")
public Result deviceBind(
@RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId,
@RequestParam(value = "user_id") String userId, @RequestParam(value = "sn") String sn,
@RequestParam(value = "verify_key") String verifyKey) {
// 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");
}
User user = userService.getUserByUserId(userId);
if (user == null) {
return Result.error().message("user not found");
}
String userPhone = user.getPhone();
// 3. 校验 sn 是否存在
DeviceInfo oldDeviceInfo = deviceSnService.findBySn(sn);
if (oldDeviceInfo == null) {
return Result.error().message("sn not found");
}
if (!TextUtils.isEmpty(oldDeviceInfo.getBindPhone())) {
return Result.error().message("sn already bind");
}
oldDeviceInfo.setBindPhone(userPhone);
oldDeviceInfo.setBindTime(new Date());
oldDeviceInfo.setSn(sn);
deviceSnService.save(oldDeviceInfo);
try {
PushUtils.aliyunAsyncPush(verifyKey, userPhone, sn);
return Result.ok().message("bind success");
} catch (Exception e) {
e.printStackTrace();
return Result.error().message(e.getMessage());
}
}
}

View File

@@ -116,11 +116,10 @@ public class LoginController {
tokenMap.put("user_id", user.getUserId());
tokenMap.put("has_password", user.isHasPassword());
tokenMap.put("token", tokenPair.toMap());
redisTemplate.delete(phone);
return Result.ok().data(tokenMap);
} catch (RuntimeException e) {
return Result.error().message(e.getMessage());
} finally {
redisTemplate.delete(phone);
}
} else {
return Result.error().message("verify key is expired");
@@ -150,6 +149,7 @@ public class LoginController {
tokenMap.put("user_id", user.getUserId());
tokenMap.put("has_password", user.isHasPassword());
tokenMap.put("token", tokenPair.toMap());
redisTemplate.delete(phone);
return Result.ok().data(tokenMap);
} catch (RuntimeException e) {
return Result.error().message(e.getMessage());

View File

@@ -0,0 +1,57 @@
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.JwtUtil;
import com.onekeycall.videotablet.utils.PushUtils;
import com.onekeycall.videotablet.utils.TextUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
@RestController
@RequestMapping("/public")
public class ManageSnController {
@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());
deviceSnService.save(deviceInfo);
return Result.ok();
}
}

View File

@@ -0,0 +1,49 @@
package com.onekeycall.videotablet.controller;
import com.onekeycall.videotablet.entity.DeviceInfo;
import com.onekeycall.videotablet.result.Result;
import com.onekeycall.videotablet.service.DeviceSnService;
import com.onekeycall.videotablet.service.UserService;
import com.onekeycall.videotablet.tencent.trtc.TLSSigAPIv2;
import com.onekeycall.videotablet.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/public")
public class TencentTrtcController {
@Autowired
private JwtUtil jwtUtil;
@Autowired
private UserService userService;
@Autowired
private DeviceSnService deviceSnService;
@GetMapping("/get_trtc_sig")
public Result getTrtcSig(
@RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId,
@RequestParam(value = "user_id") String userId
) {
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");
}
TLSSigAPIv2 tlsSigAPIv2 = new TLSSigAPIv2(1600100994, "ccc0d591fe50bd9c05df7e3182256eb43fd83d1127ae7dc01a3d256dd80f3ae2");
String sig = tlsSigAPIv2.genUserSig(userId);
Map<String, Object> map = new HashMap<>();
map.put("sig", sig);
map.put("expire", Instant.now().getEpochSecond() + TLSSigAPIv2.EXPIRETIME);
return Result.ok().data(map);
}
}