新增验证码

This commit is contained in:
2024-10-06 20:08:57 +08:00
parent 40bbb8a0a0
commit 9168884edb
8 changed files with 412 additions and 39 deletions

View File

@@ -3,6 +3,7 @@ package com.aiosman.riderpro.data
import com.aiosman.riderpro.AppState
import com.aiosman.riderpro.data.api.ApiClient
import com.aiosman.riderpro.data.api.AppConfig
import com.aiosman.riderpro.data.api.CaptchaInfo
import com.aiosman.riderpro.data.api.ChangePasswordRequestBody
import com.aiosman.riderpro.data.api.GoogleRegisterRequestBody
import com.aiosman.riderpro.data.api.LoginUserRequestBody
@@ -266,8 +267,13 @@ interface AccountService {
* 使用用户名密码登录
* @param loginName 用户名
* @param password 密码
* @param captchaInfo 验证码信息
*/
suspend fun loginUserWithPassword(loginName: String, password: String): UserAuth
suspend fun loginUserWithPassword(
loginName: String,
password: String,
captchaInfo: CaptchaInfo? = null
): UserAuth
/**
* 使用google登录
@@ -383,8 +389,16 @@ class AccountServiceImpl : AccountService {
return UserAuth(body.id)
}
override suspend fun loginUserWithPassword(loginName: String, password: String): UserAuth {
val resp = ApiClient.api.login(LoginUserRequestBody(loginName, password))
override suspend fun loginUserWithPassword(
loginName: String,
password: String,
captchaInfo: CaptchaInfo?
): UserAuth {
val resp = ApiClient.api.login(LoginUserRequestBody(
username = loginName,
password = password,
captcha = captchaInfo,
))
if (!resp.isSuccessful) {
parseErrorResponse(resp.errorBody())?.let {
throw it.toServiceException()

View File

@@ -0,0 +1,45 @@
package com.aiosman.riderpro.data
import com.aiosman.riderpro.data.api.ApiClient
import com.aiosman.riderpro.data.api.CaptchaRequestBody
import com.aiosman.riderpro.data.api.CaptchaResponseBody
import com.aiosman.riderpro.data.api.CheckLoginCaptchaRequestBody
import com.aiosman.riderpro.data.api.GenerateLoginCaptchaRequestBody
interface CaptchaService {
suspend fun generateCaptcha(source: String): CaptchaResponseBody
suspend fun checkLoginCaptcha(username: String): Boolean
suspend fun generateLoginCaptcha(username: String): CaptchaResponseBody
}
class CaptchaServiceImpl : CaptchaService {
override suspend fun generateCaptcha(source: String): CaptchaResponseBody {
val resp = ApiClient.api.generateCaptcha(
CaptchaRequestBody(source)
)
val data = resp.body() ?: throw Exception("Failed to generate captcha")
return data.data.copy(
masterBase64 = data.data.masterBase64.replace("data:image/jpeg;base64,", ""),
thumbBase64 = data.data.thumbBase64.replace("data:image/png;base64,", "")
)
}
override suspend fun checkLoginCaptcha(username: String): Boolean {
val resp = ApiClient.api.checkLoginCaptcha(
CheckLoginCaptchaRequestBody(username)
)
return resp.body()?.data ?: true
}
override suspend fun generateLoginCaptcha(username: String): CaptchaResponseBody {
val resp = ApiClient.api.generateLoginCaptcha(
GenerateLoginCaptchaRequestBody(username)
)
val data = resp.body() ?: throw Exception("Failed to generate captcha")
return data.data.copy(
masterBase64 = data.data.masterBase64.replace("data:image/jpeg;base64,", ""),
thumbBase64 = data.data.thumbBase64.replace("data:image/png;base64,", "")
)
}
}

View File

@@ -38,6 +38,8 @@ data class LoginUserRequestBody(
val password: String? = null,
@SerializedName("googleId")
val googleId: String? = null,
@SerializedName("captcha")
val captcha: CaptchaInfo? = null,
)
data class GoogleRegisterRequestBody(
@@ -128,6 +130,69 @@ data class DictItem(
val desc: String,
)
data class CaptchaRequestBody(
@SerializedName("source")
val source: String,
)
data class CaptchaResponseBody(
@SerializedName("id")
val id: Int,
@SerializedName("thumb_base64")
val thumbBase64: String,
@SerializedName("master_base64")
val masterBase64: String,
@SerializedName("count")
val count: Int,
)
data class CheckLoginCaptchaRequestBody(
@SerializedName("username")
val username: String,
)
data class GenerateLoginCaptchaRequestBody(
@SerializedName("username")
val username: String,
)
//{
// "id":48,
// "dot": [
// {
// "index": 0,
// "x": 76,
// "y": 165
// },
// {
// "index": 1,
// "x": 144,
// "y": 21
// },
// {
// "index": 2,
// "x": 220,
// "y": 42
// },
// {
// "index": 3,
// "x": 10,
// "y": 10
// }
// ]
//}
data class DotPosition(
@SerializedName("index")
val index: Int,
@SerializedName("x")
val x: Int,
@SerializedName("y")
val y: Int,
)
data class CaptchaInfo(
@SerializedName("id")
val id: Int,
@SerializedName("dot")
val dot: List<DotPosition>
)
interface RiderProAPI {
@POST("register")
suspend fun register(@Body body: RegisterRequestBody): Response<Unit>
@@ -336,4 +401,20 @@ interface RiderProAPI {
suspend fun getDict(
@Query("key") key: String
): Response<DataContainer<DictItem>>
@POST("captcha/generate")
suspend fun generateCaptcha(
@Body body: CaptchaRequestBody
): Response<DataContainer<CaptchaResponseBody>>
@POST("login/needCaptcha")
suspend fun checkLoginCaptcha(
@Body body: CheckLoginCaptchaRequestBody
): Response<DataContainer<Boolean>>
@POST("captcha/login/generate")
suspend fun generateLoginCaptcha(
@Body body: GenerateLoginCaptchaRequestBody
): Response<DataContainer<CaptchaResponseBody>>
}