auth_service.go 1022 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package services
  2. import (
  3. "go-msa-auth/internal/models"
  4. "time"
  5. )
  6. // AuthRequest 客户端请求结构
  7. type AuthRequest struct {
  8. UUID string `json:"uuid" binding:"required"`
  9. DeviceID string `json:"deviceId" binding:"required"`
  10. }
  11. // AuthResponse 服务端响应结构
  12. type AuthResponse struct {
  13. IsLegal bool `json:"isLegal"`
  14. ServerTime string `json:"serverTime"`
  15. }
  16. // HandleAuth 处理授权逻辑
  17. func HandleAuth(req AuthRequest) AuthResponse {
  18. authCode, err := models.FindAuthCode(req.UUID)
  19. if err != nil {
  20. // UUID 不存在
  21. return AuthResponse{
  22. IsLegal: false,
  23. ServerTime: time.Now().Format(time.RFC3339),
  24. }
  25. }
  26. if authCode.DeviceID == "" {
  27. // 如果 DeviceID 未绑定,绑定当前 DeviceID
  28. authCode.DeviceID = req.DeviceID
  29. models.SaveAuthCode(authCode)
  30. return AuthResponse{
  31. IsLegal: true,
  32. ServerTime: time.Now().Format(time.RFC3339),
  33. }
  34. }
  35. // 如果 DeviceID 已绑定
  36. return AuthResponse{
  37. IsLegal: false,
  38. ServerTime: time.Now().Format(time.RFC3339),
  39. }
  40. }