From de3623064c3ff469f2acb88396352f9840196f3f Mon Sep 17 00:00:00 2001 From: js Date: Thu, 25 May 2023 22:56:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=BF=9E=E6=8E=A5=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle.kts | 1 + .../data/account/AccountManager.kt | 63 +++++++++++++++---- .../imshit/aircraftwar/gui/AccountActivity.kt | 4 +- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index b85fa32..1b7ed69 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -65,6 +65,7 @@ dependencies { implementation("androidx.navigation:navigation-fragment-ktx:2.5.3") implementation("androidx.navigation:navigation-ui-ktx:2.5.3") implementation("com.google.android.material:material:1.9.0") + implementation("com.squareup.okhttp3:okhttp:4.10.0") testImplementation("junit:junit:4.13.2") androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") diff --git a/app/src/main/java/net/imshit/aircraftwar/data/account/AccountManager.kt b/app/src/main/java/net/imshit/aircraftwar/data/account/AccountManager.kt index 02f2b7c..d64d1c8 100644 --- a/app/src/main/java/net/imshit/aircraftwar/data/account/AccountManager.kt +++ b/app/src/main/java/net/imshit/aircraftwar/data/account/AccountManager.kt @@ -6,33 +6,74 @@ import android.view.LayoutInflater import com.google.android.material.dialog.MaterialAlertDialogBuilder import net.imshit.aircraftwar.R import net.imshit.aircraftwar.databinding.DialogLoginBinding +import okhttp3.Call +import okhttp3.Callback +import okhttp3.FormBody +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.Response +import java.io.IOException import java.net.URL +import java.security.MessageDigest +import java.util.concurrent.TimeUnit object AccountManager { private const val SALT = "HITsz520" - private val REMOTE = URL("https://haxiaoshen.top/") + private val LOGIN_URL = URL("https://haxiaoshen.top/login") + private val REGISTER_URL = URL("https://haxiaoshen.top/register") - fun login(account: String, password: String, onSuccess: Runnable) { - // TODO - onSuccess.run() + private val httpClient by lazy { + OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS) + .writeTimeout(10, TimeUnit.SECONDS).readTimeout(10, TimeUnit.SECONDS) + .callTimeout(10, TimeUnit.SECONDS).build() } - fun register(account: String, password: String, onSuccess: Runnable) { - // TODO - login(account, password, onSuccess) + private fun encodePassword(password: String): String { + val salted = "$SALT$password$SALT" + val bytes = salted.toByteArray(Charsets.UTF_8) + val sha512 = MessageDigest.getInstance("SHA-512").digest(bytes) + return sha512.joinToString(separator = "") { "%02x".format(it) } } - fun showLoginDialog(context: Context, onSuccess: Runnable) { + fun login(account: String, password: String, onSuccess: Runnable, onFail: Runnable) { + val requestBody = + FormBody.Builder().add("user", account).add("password", encodePassword(password)) + .build() + val request = Request.Builder().url(LOGIN_URL).post(requestBody).build() + httpClient.newCall(request).enqueue(object : Callback { + override fun onFailure(call: Call, e: IOException) { + onFail.run() + } + + override fun onResponse(call: Call, response: Response) { + if (response.isSuccessful) { + // TODO + onSuccess.run() + } else { + onFail.run() + } + } + }) + } + + fun register(account: String, password: String, onSuccess: Runnable, onFail: Runnable) { + Thread { + // TODO + login(account, password, onSuccess, onFail) + }.start() + } + + fun showLoginDialog(context: Context, onSuccess: Runnable, onFail: Runnable) { with(MaterialAlertDialogBuilder(context)) { with(DialogLoginBinding.inflate(LayoutInflater.from(context), null, false)) { val dialogListener = DialogInterface.OnClickListener { _, which -> + val account = dlTietAc.text.toString() + val password = dlTietPw.text.toString() when (which) { DialogInterface.BUTTON_POSITIVE -> ::login DialogInterface.BUTTON_NEUTRAL -> ::register else -> null - }?.invoke( - dlTietAc.text.toString(), dlTietPw.text.toString(), onSuccess - ) + }?.invoke(account, password, onSuccess, onFail) } setView(root) setTitle(R.string.dialog_login_title) diff --git a/app/src/main/java/net/imshit/aircraftwar/gui/AccountActivity.kt b/app/src/main/java/net/imshit/aircraftwar/gui/AccountActivity.kt index bf4cc8a..dd0fc97 100644 --- a/app/src/main/java/net/imshit/aircraftwar/gui/AccountActivity.kt +++ b/app/src/main/java/net/imshit/aircraftwar/gui/AccountActivity.kt @@ -12,10 +12,10 @@ import net.imshit.aircraftwar.databinding.ActivityAccountBinding class AccountActivity : AppCompatActivity() { companion object { fun actionStart(context: Context) { - AccountManager.showLoginDialog(context) { + AccountManager.showLoginDialog(context, onSuccess = { context.startActivity(Intent(context, AccountActivity::class.java).apply { }) - } + }, onFail = {}) } }