forked from xiongyuxing/tiku-backend.net
161 lines
4.5 KiB
C#
161 lines
4.5 KiB
C#
using System.ComponentModel;
|
||
using System.ComponentModel.DataAnnotations;
|
||
using Tiku.Application.Auth;
|
||
|
||
namespace Tiku.Api.Contracts;
|
||
|
||
/// <summary>
|
||
/// 手机号密码登录请求。
|
||
/// </summary>
|
||
public sealed class PasswordLoginDto
|
||
{
|
||
/// <summary>
|
||
/// 租户 ID。登录阶段允许客户端指定租户,登录后的业务接口以 JWT/session 解析出的当前租户为准。
|
||
/// </summary>
|
||
[Required]
|
||
[Description("租户 ID。登录阶段允许客户端指定租户,登录后的业务接口以 JWT/session 解析出的当前租户为准。")]
|
||
public Guid TenantId { get; set; }
|
||
|
||
/// <summary>
|
||
/// 手机号,建议前端提交规范化后的中国大陆手机号。
|
||
/// </summary>
|
||
[Required]
|
||
[StringLength(32)]
|
||
[Description("手机号,建议前端提交规范化后的中国大陆手机号。")]
|
||
public string Phone { get; set; } = string.Empty;
|
||
|
||
/// <summary>
|
||
/// 用户密码。
|
||
/// </summary>
|
||
[Required]
|
||
[StringLength(128, MinimumLength = 6)]
|
||
[Description("用户密码。")]
|
||
public string Password { get; set; } = string.Empty;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 短信验证码登录请求。
|
||
/// </summary>
|
||
public sealed class SmsLoginDto
|
||
{
|
||
/// <summary>
|
||
/// 租户 ID。
|
||
/// </summary>
|
||
[Required]
|
||
[Description("租户 ID。")]
|
||
public Guid TenantId { get; set; }
|
||
|
||
/// <summary>
|
||
/// 中国大陆手机号。
|
||
/// </summary>
|
||
[Required]
|
||
[StringLength(32)]
|
||
[Description("中国大陆手机号。")]
|
||
public string Phone { get; set; } = string.Empty;
|
||
|
||
/// <summary>
|
||
/// 收到的短信验证码。
|
||
/// </summary>
|
||
[Required]
|
||
[StringLength(12, MinimumLength = 4)]
|
||
[Description("收到的短信验证码。")]
|
||
public string Code { get; set; } = string.Empty;
|
||
}
|
||
|
||
/// <summary>
|
||
/// OAuth code 登录请求。
|
||
/// </summary>
|
||
public sealed class OAuthCodeDto
|
||
{
|
||
/// <summary>
|
||
/// 租户 ID。
|
||
/// </summary>
|
||
[Required]
|
||
[Description("租户 ID。")]
|
||
public Guid TenantId { get; set; }
|
||
|
||
/// <summary>
|
||
/// OAuth 平台返回的一次性授权 code。
|
||
/// </summary>
|
||
[Required]
|
||
[StringLength(512)]
|
||
[Description("OAuth 平台返回的一次性授权 code。")]
|
||
public string Code { get; set; } = string.Empty;
|
||
|
||
/// <summary>
|
||
/// 客户端可提供的公开用户资料,不允许包含 token/secret。当前后端先保留模板字段,后续按业务需要逐步使用。
|
||
/// </summary>
|
||
[Description("客户端可提供的公开用户资料,不允许包含 token/secret。当前后端先保留模板字段,后续按业务需要逐步使用。")]
|
||
public Dictionary<string, object?>? Profile { get; set; }
|
||
|
||
/// <summary>
|
||
/// 微信用户资料语言,例如 zh_CN。当前微信小程序登录不会使用该字段。
|
||
/// </summary>
|
||
[StringLength(20)]
|
||
[Description("微信用户资料语言,例如 zh_CN。当前微信小程序登录不会使用该字段。")]
|
||
public string? Lang { get; set; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// refresh token 请求。
|
||
/// </summary>
|
||
public sealed class RefreshSessionDto
|
||
{
|
||
/// <summary>
|
||
/// refresh token。服务端只存储哈希,明文只返回给客户端一次。
|
||
/// </summary>
|
||
[Required]
|
||
[StringLength(2048)]
|
||
[Description("refresh token。服务端只存储哈希,明文只返回给客户端一次。")]
|
||
public string RefreshToken { get; set; } = string.Empty;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 登录成功后的用户、租户成员和令牌信息。
|
||
/// </summary>
|
||
public sealed class AuthenticatedUserDto
|
||
{
|
||
/// <summary>
|
||
/// 用户 ID。
|
||
/// </summary>
|
||
public Guid UserId { get; init; }
|
||
|
||
/// <summary>
|
||
/// 手机号。
|
||
/// </summary>
|
||
public string? Phone { get; init; }
|
||
|
||
/// <summary>
|
||
/// 邮箱。
|
||
/// </summary>
|
||
public string? Email { get; init; }
|
||
|
||
/// <summary>
|
||
/// 用户显示名称。
|
||
/// </summary>
|
||
public string? Name { get; init; }
|
||
|
||
/// <summary>
|
||
/// 当前登录租户成员摘要。
|
||
/// </summary>
|
||
public TenantMembershipSummary Tenant { get; init; } = default!;
|
||
|
||
/// <summary>
|
||
/// access token 和 refresh token。
|
||
/// </summary>
|
||
public AuthTokenPair Tokens { get; init; } = default!;
|
||
|
||
public static AuthenticatedUserDto FromApplication(AuthenticatedUser user)
|
||
{
|
||
return new AuthenticatedUserDto
|
||
{
|
||
UserId = user.UserId,
|
||
Phone = user.Phone,
|
||
Email = user.Email,
|
||
Name = user.Name,
|
||
Tenant = user.Tenant,
|
||
Tokens = user.Tokens
|
||
};
|
||
}
|
||
}
|