Skip to content

Commit

Permalink
feat: 连接服务器
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim-shop committed May 25, 2023
1 parent 8a19aa9 commit de36230
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {})
}
}

Expand Down

0 comments on commit de36230

Please sign in to comment.