user_models.dart 5.41 KB
import 'package:json_annotation/json_annotation.dart';

part 'user_models.g.dart';

// ─── Requests(手写 toJson,无 fromJson)────────────────────────────────────

class LoginRequest {
  const LoginRequest({
    this.loginType = 'verification_code',
    required this.telephone,
    required this.verificationCode,
  });

  final String loginType;
  final String telephone;
  final String verificationCode;

  Map<String, dynamic> toJson() => {
        'login_type': loginType,
        'telephone': telephone,
        'verification_code': verificationCode,
      };
}

class RegisterRequest {
  const RegisterRequest({
    required this.telephone,
    required this.verificationCode,
  });

  final String telephone;
  final String verificationCode;

  Map<String, dynamic> toJson() => {
        'telephone': telephone,
        'verification_code': verificationCode,
      };
}

class VerifyCodeRequest {
  const VerifyCodeRequest({required this.telephone, required this.scene});

  final String telephone;
  final int scene;

  factory VerifyCodeRequest.login(String telephone) =>
      VerifyCodeRequest(telephone: telephone, scene: 1);

  factory VerifyCodeRequest.changeTelephone(String telephone) =>
      VerifyCodeRequest(telephone: telephone, scene: 2);

  Map<String, dynamic> toJson() => {
        'telephone': telephone,
        'scene': scene,
      };
}

class BindPartnerRequest {
  const BindPartnerRequest({required this.bindCode});

  final String bindCode;

  Map<String, dynamic> toJson() => {'pair_code': bindCode};
}

class PushTokenRequest {
  const PushTokenRequest({
    required this.pushToken,
    required this.deviceInfo,
    required this.pushPlatform,
  });

  final String pushToken;
  final String deviceInfo;
  final int pushPlatform;

  Map<String, dynamic> toJson() => {
        'cid': pushToken,
        'device_info': deviceInfo,
        'push_platform': pushPlatform,
      };
}

class UserInfoUpdateRequest {
  const UserInfoUpdateRequest({this.avatar, this.nickname, this.persona});

  final String? avatar;
  final String? nickname;
  final int? persona;

  Map<String, dynamic> toJson() {
    final json = <String, dynamic>{};
    if (avatar != null) json['avatar'] = avatar;
    if (nickname != null) json['nickname'] = nickname;
    if (persona != null) json['persona'] = persona;
    return json;
  }
}

// ─── Responses(json_annotation 代码生成)──────────────────────────────────

@JsonSerializable()
class UserAccessToken {
  const UserAccessToken({this.accessToken, this.expireTime});

  @JsonKey(name: 'access_token')
  final String? accessToken;
  @JsonKey(name: 'expire_time')
  final int? expireTime;

  factory UserAccessToken.fromJson(Map<String, dynamic> json) =>
      _$UserAccessTokenFromJson(json);
  Map<String, dynamic> toJson() => _$UserAccessTokenToJson(this);
}

@JsonSerializable()
class LoginResponse {
  const LoginResponse({this.isNewUser, this.accessTokenInfo, this.id});

  @JsonKey(name: 'is_new_user')
  final bool? isNewUser;
  @JsonKey(name: 'token_info')
  final UserAccessToken? accessTokenInfo;
  final int? id;

  String get accessToken => accessTokenInfo?.accessToken ?? '';

  factory LoginResponse.fromJson(Map<String, dynamic> json) =>
      _$LoginResponseFromJson(json);
  Map<String, dynamic> toJson() => _$LoginResponseToJson(this);
}

@JsonSerializable()
class RegisterResponse {
  const RegisterResponse({this.accessTokenInfo});

  @JsonKey(name: 'token_info')
  final UserAccessToken? accessTokenInfo;

  String get accessToken => accessTokenInfo?.accessToken ?? '';

  factory RegisterResponse.fromJson(Map<String, dynamic> json) =>
      _$RegisterResponseFromJson(json);
  Map<String, dynamic> toJson() => _$RegisterResponseToJson(this);
}

@JsonSerializable()
class UserInfoResponse {
  const UserInfoResponse({
    this.id,
    this.pairCode,
    this.pairId,
    this.telephone,
    this.nickname,
    this.avatar,
    this.persona,
    this.status,
    this.lastLoginTime,
    this.createTime,
    this.isBot,
  });

  final int? id;
  @JsonKey(name: 'pair_code')
  final String? pairCode;
  @JsonKey(name: 'pair_id')
  final int? pairId;
  final String? telephone;
  final String? nickname;
  final String? avatar;
  final int? persona;
  final int? status;
  @JsonKey(name: 'last_login_time')
  final int? lastLoginTime;
  @JsonKey(name: 'create_time')
  final int? createTime;
  @JsonKey(name: 'is_bot')
  final int? isBot;

  factory UserInfoResponse.fromJson(Map<String, dynamic> json) =>
      _$UserInfoResponseFromJson(json);
  Map<String, dynamic> toJson() => _$UserInfoResponseToJson(this);
}

@JsonSerializable()
class BoundUserInfoResponse {
  const BoundUserInfoResponse({this.userInfo, this.partnerUserInfo});

  @JsonKey(name: 'user_info')
  final UserInfoResponse? userInfo;
  @JsonKey(name: 'pair_user_info')
  final UserInfoResponse? partnerUserInfo;

  factory BoundUserInfoResponse.fromJson(Map<String, dynamic> json) =>
      _$BoundUserInfoResponseFromJson(json);
  Map<String, dynamic> toJson() => _$BoundUserInfoResponseToJson(this);
}

@JsonSerializable()
class RongcloudTokenResponse {
  const RongcloudTokenResponse({this.token});

  final String? token;

  factory RongcloudTokenResponse.fromJson(Map<String, dynamic> json) =>
      _$RongcloudTokenResponseFromJson(json);
  Map<String, dynamic> toJson() => _$RongcloudTokenResponseToJson(this);
}