鉴权平板返回不同的code,增加用户获取联系人列表,未完成

This commit is contained in:
2025-09-11 14:19:39 +08:00
parent 252e3d9143
commit dd0d1907a7
3 changed files with 57 additions and 4 deletions

View File

@@ -83,7 +83,12 @@ public class LoginController {
User userDetails = (User) authentication.getPrincipal();
TokenPair tokenPair = jwtUtil.generateTokenPair(userDetails.getUserId(), deviceId);
// 4. 返回 Token
return Result.ok().data(Collections.singletonMap("token", tokenPair.toMap()));
Map<String, Object> tokenMap = new HashMap<>();
tokenMap.put("new_user", user.isNewUser());
tokenMap.put("user_id", user.getUserId());
tokenMap.put("has_password", user.isHasPassword());
tokenMap.put("token", tokenPair.toMap());
return Result.ok().data(tokenMap);
} catch (Exception e) {
e.printStackTrace();
return Result.error().message("登录失败:密码错误");

View File

@@ -10,6 +10,7 @@ import com.onekeycall.videotablet.service.ContactService;
import com.onekeycall.videotablet.service.DeviceSnService;
import com.onekeycall.videotablet.service.UserService;
import com.onekeycall.videotablet.utils.JwtUtil;
import com.onekeycall.videotablet.utils.TextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -17,6 +18,8 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@RestController
@RequestMapping("/user")
public class ContactController {
@@ -77,4 +80,38 @@ public class ContactController {
return Result.ok();
}
@GetMapping("/get_contacts")
public Result getContacts(
@RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId,
@RequestParam(value = "user_id") String userId, @RequestParam(value = "sn") String 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);
if (user == null) {
return Result.error().message("user not found");
}
DeviceInfo deviceInfo = deviceSnService.findBySn(sn);
if (deviceInfo == null) {
return Result.error().message("device not found");
}
if (!deviceInfo.getUserId().equals(userId)) {
return Result.error().message("device not belong to user");
}
List<Contact> contacts = contactService.findAllBySn(sn);
if (contacts == null || contacts.isEmpty()) {
return Result.notFound().message("contacts not found");
}
return Result.ok().data("contacts", contacts);
}
}