refactor: 优化前端认证服务

- 优化AuthService认证服务
- 更新AuthModels数据模型
- 优化SmsCodeInput短信验证码输入组件
This commit is contained in:
iammm0
2026-01-27 14:30:35 +08:00
parent 8e98456dae
commit b13877dcb7
3 changed files with 23 additions and 16 deletions

View File

@@ -52,12 +52,13 @@ class AuthService {
phone: String, phone: String,
password: String, password: String,
nickname: String, nickname: String,
email: String? = null email: String? = null,
agreedToTerms: Boolean
): Result<TokenResponse> { ): Result<TokenResponse> {
return try { return try {
val response = client.post("$AUTH_BASE/register") { val response = client.post("$AUTH_BASE/register") {
contentType(ContentType.Application.Json) contentType(ContentType.Application.Json)
setBody(RegisterRequest(phone, password, nickname, email)) setBody(RegisterRequest(phone, password, nickname, email, agreedToTerms))
} }
when (response.status) { when (response.status) {
@@ -102,11 +103,11 @@ class AuthService {
/** /**
* 用户登录 * 用户登录
*/ */
suspend fun login(phone: String, password: String): Result<TokenResponse> { suspend fun login(phone: String, password: String, agreedToTerms: Boolean): Result<TokenResponse> {
return try { return try {
val response = client.post("$AUTH_BASE/login") { val response = client.post("$AUTH_BASE/login") {
contentType(ContentType.Application.Json) contentType(ContentType.Application.Json)
setBody(LoginRequest(phone, password)) setBody(LoginRequest(phone, password, agreedToTerms))
} }
when (response.status) { when (response.status) {
@@ -327,11 +328,11 @@ class AuthService {
/** /**
* 验证码登录 * 验证码登录
*/ */
suspend fun loginWithSms(phone: String, code: String): Result<TokenResponse> { suspend fun loginWithSms(phone: String, code: String, agreedToTerms: Boolean): Result<TokenResponse> {
return try { return try {
val response = client.post("$AUTH_BASE/login/sms") { val response = client.post("$AUTH_BASE/login/sms") {
contentType(ContentType.Application.Json) contentType(ContentType.Application.Json)
setBody(SmsLoginRequest(phone, code)) setBody(SmsLoginRequest(phone, code, agreedToTerms))
} }
when (response.status) { when (response.status) {
@@ -364,12 +365,13 @@ class AuthService {
code: String, code: String,
password: String, password: String,
nickname: String, nickname: String,
email: String? = null email: String? = null,
agreedToTerms: Boolean
): Result<TokenResponse> { ): Result<TokenResponse> {
return try { return try {
val response = client.post("$AUTH_BASE/register/sms") { val response = client.post("$AUTH_BASE/register/sms") {
contentType(ContentType.Application.Json) contentType(ContentType.Application.Json)
setBody(SmsRegisterRequest(phone, code, password, nickname, email)) setBody(SmsRegisterRequest(phone, code, password, nickname, email, agreedToTerms))
} }
when (response.status) { when (response.status) {

View File

@@ -12,14 +12,16 @@ data class RegisterRequest(
val phone: String, val phone: String,
val password: String, val password: String,
val nickname: String, val nickname: String,
val email: String? = null val email: String? = null,
val agreed_to_terms: Boolean
) )
// 登录请求 // 登录请求
@Serializable @Serializable
data class LoginRequest( data class LoginRequest(
val phone: String, val phone: String,
val password: String val password: String,
val agreed_to_terms: Boolean
) )
// 刷新令牌请求 // 刷新令牌请求
@@ -91,7 +93,8 @@ data class SmsResponse(
@Serializable @Serializable
data class SmsLoginRequest( data class SmsLoginRequest(
val phone: String, val phone: String,
val code: String val code: String,
val agreed_to_terms: Boolean
) )
// 验证码注册请求 // 验证码注册请求
@@ -101,7 +104,8 @@ data class SmsRegisterRequest(
val code: String, val code: String,
val password: String, val password: String,
val nickname: String, val nickname: String,
val email: String? = null val email: String? = null,
val agreed_to_terms: Boolean
) )
// 重置密码请求 // 重置密码请求

View File

@@ -53,13 +53,14 @@ fun SmsCodeInput(
modifier = modifier, modifier = modifier,
decorationBox = { innerTextField -> decorationBox = { innerTextField ->
Row( Row(
horizontalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.wrapContentWidth(),
horizontalArrangement = Arrangement.spacedBy(6.dp),
verticalAlignment = Alignment.CenterVertically verticalAlignment = Alignment.CenterVertically
) { ) {
repeat(codeLength) { index -> repeat(codeLength) { index ->
Box( Box(
modifier = Modifier modifier = Modifier
.size(48.dp) .size(40.dp)
.border( .border(
width = 2.dp, width = 2.dp,
color = when { color = when {
@@ -68,14 +69,14 @@ fun SmsCodeInput(
index < code.length -> MaterialTheme.colorScheme.primary.copy(alpha = 0.5f) index < code.length -> MaterialTheme.colorScheme.primary.copy(alpha = 0.5f)
else -> MaterialTheme.colorScheme.outline else -> MaterialTheme.colorScheme.outline
}, },
shape = RoundedCornerShape(12.dp) shape = RoundedCornerShape(10.dp)
), ),
contentAlignment = Alignment.Center contentAlignment = Alignment.Center
) { ) {
Text( Text(
text = if (index < code.length) code[index].toString() else "", text = if (index < code.length) code[index].toString() else "",
style = TextStyle( style = TextStyle(
fontSize = 24.sp, fontSize = 20.sp,
fontWeight = FontWeight.Bold, fontWeight = FontWeight.Bold,
color = if (enabled) { color = if (enabled) {
MaterialTheme.colorScheme.onSurface MaterialTheme.colorScheme.onSurface