增加添加和绑定sn,增加阿里云推送
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user