209 lines
8.2 KiB
Kotlin
209 lines
8.2 KiB
Kotlin
package com.stand.standapp
|
|
|
|
import android.content.Intent
|
|
import android.os.Bundle
|
|
import timber.log.Timber
|
|
import android.widget.*
|
|
import androidx.appcompat.app.AlertDialog
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
import okhttp3.*
|
|
import okhttp3.MediaType.Companion.toMediaType
|
|
import org.json.JSONObject
|
|
import java.io.IOException
|
|
|
|
class LoginActivity : AppCompatActivity() {
|
|
|
|
private val client = OkHttpClient()
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
setContentView(R.layout.activity_login)
|
|
|
|
val etUsername = findViewById<EditText>(R.id.et_username)
|
|
val etPassword = findViewById<EditText>(R.id.et_password)
|
|
val cbRemember = findViewById<CheckBox>(R.id.cb_remember)
|
|
val btnLogin = findViewById<Button>(R.id.btn_login)
|
|
val btnPttExample = findViewById<Button>(R.id.btn_ptt_example)
|
|
val btnPrinterTest = findViewById<Button>(R.id.btn_printer_test)
|
|
val ivSettings = findViewById<ImageView>(R.id.iv_settings)
|
|
|
|
// 1. 加载保存的状态和凭据
|
|
val isRemember = AppConfig.isRememberLogin(this)
|
|
cbRemember.isChecked = isRemember
|
|
if (isRemember) {
|
|
etUsername.setText(AppConfig.getSavedUsername(this))
|
|
etPassword.setText(AppConfig.getSavedPassword(this))
|
|
}
|
|
|
|
// 2. 服务器配置图标点击事件
|
|
ivSettings.setOnClickListener {
|
|
showServerConfigDialog()
|
|
}
|
|
|
|
// 3. 登录按钮点击事件
|
|
btnLogin.setOnClickListener {
|
|
val username = etUsername.text.toString().trim()
|
|
val password = etPassword.text.toString().trim()
|
|
|
|
if (username.isEmpty() || password.isEmpty()) {
|
|
Toast.makeText(this, "请输入账号和密码", Toast.LENGTH_SHORT).show()
|
|
return@setOnClickListener
|
|
}
|
|
|
|
// 执行真实接口登录
|
|
performLogin(username, password, cbRemember.isChecked)
|
|
}
|
|
|
|
// 4. PTT 示例按钮点击事件
|
|
btnPttExample.setOnClickListener {
|
|
try {
|
|
val intent = Intent(this, com.example.kingway.ptt.pttActivity::class.java)
|
|
startActivity(intent)
|
|
} catch (e: Exception) {
|
|
Toast.makeText(this, "打开 PTT 示例失败: ${e.message}", Toast.LENGTH_SHORT).show();
|
|
}
|
|
}
|
|
|
|
// 5. 打印测试按钮点击事件
|
|
btnPrinterTest.setOnClickListener {
|
|
try {
|
|
val intent = Intent(this, com.stand.standapp.printer.PrintTestActivity::class.java)
|
|
startActivity(intent)
|
|
} catch (e: Exception) {
|
|
Toast.makeText(this, "打开打印测试失败: ${e.message}", Toast.LENGTH_SHORT).show()
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 执行真实接口登录
|
|
*/
|
|
private fun performLogin(username: String, password: String, remember: Boolean) {
|
|
val serverUrl = AppConfig.getServerUrl(this)
|
|
val loginUrl = "$serverUrl/api/auth/login"
|
|
val bearerToken = "eyJraWQiOiJzLXYxIiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJzdWIiOiJhZG1pbiIsInR5cCI6ImFjY2VzcyIsImV4cCI6MTc3NjQwMTU2MSwianRpIjoiMjM0NjdiZGMtNTY0Zi00NmY0LWJhZTgtNzJhZmQ1MzhmYmZhIiwiZmdwIjoiY2FhNzYzOTI4YWRhYWZiYjgzMjE3NjExYmVjYTc3ZjMiLCJraWQiOiJzLXYxIn0.4Zf1eiKcUjGNoFylEzmZ2no1kJY-44CQ8QWmMA_UxvY"
|
|
|
|
val body = MultipartBody.Builder()
|
|
.setType(MultipartBody.FORM)
|
|
.addFormDataPart("username", username)
|
|
.addFormDataPart("password", password)
|
|
.build()
|
|
|
|
val request = Request.Builder()
|
|
.url(loginUrl)
|
|
.post(body)
|
|
.addHeader("Authorization", "Bearer $bearerToken")
|
|
.build()
|
|
|
|
val loadingDialog = AlertDialog.Builder(this)
|
|
.setMessage("正在登录...")
|
|
.setCancelable(false)
|
|
.create()
|
|
loadingDialog.show()
|
|
|
|
client.newCall(request).enqueue(object : Callback {
|
|
override fun onFailure(call: Call, e: IOException) {
|
|
runOnUiThread {
|
|
loadingDialog.dismiss()
|
|
Toast.makeText(this@LoginActivity, "网络异常: ${e.message}", Toast.LENGTH_SHORT).show()
|
|
}
|
|
}
|
|
|
|
override fun onResponse(call: Call, response: Response) {
|
|
val responseBody = response.body?.string()
|
|
runOnUiThread {
|
|
loadingDialog.dismiss()
|
|
try {
|
|
val json = JSONObject(responseBody ?: "")
|
|
val code = json.optInt("code", -1)
|
|
if (code == 0) {
|
|
// 登录成功
|
|
AppConfig.setRememberLogin(this@LoginActivity, remember)
|
|
if (remember) {
|
|
AppConfig.setSavedUsername(this@LoginActivity, username)
|
|
AppConfig.setSavedPassword(this@LoginActivity, password)
|
|
} else {
|
|
AppConfig.setSavedUsername(this@LoginActivity, "")
|
|
AppConfig.setSavedPassword(this@LoginActivity, "")
|
|
}
|
|
|
|
Toast.makeText(this@LoginActivity, "登录成功", Toast.LENGTH_SHORT).show()
|
|
startActivity(Intent(this@LoginActivity, MainActivity::class.java))
|
|
finish()
|
|
} else {
|
|
val msg = json.optString("msg", "登录失败")
|
|
Toast.makeText(this@LoginActivity, msg, Toast.LENGTH_SHORT).show()
|
|
}
|
|
} catch (e: Exception) {
|
|
Timber.tag("Login").e(e, "解析响应失败")
|
|
Toast.makeText(this@LoginActivity, "服务器响应异常", Toast.LENGTH_SHORT).show()
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
private fun showServerConfigDialog() {
|
|
val dialogView = layoutInflater.inflate(R.layout.dialog_settings, null)
|
|
val etServerUrl = dialogView.findViewById<EditText>(R.id.et_server_url)
|
|
val etSerialPort = dialogView.findViewById<EditText>(R.id.et_serial_port)
|
|
val btnReset = dialogView.findViewById<TextView>(R.id.btn_reset)
|
|
|
|
// 初始化数据
|
|
etServerUrl.setText(AppConfig.getServerUrl(this))
|
|
etSerialPort.setText(AppConfig.getSerialPort(this))
|
|
|
|
val dialog = AlertDialog.Builder(this, R.style.CustomDialogTheme)
|
|
.setView(dialogView)
|
|
.setCancelable(false)
|
|
.create()
|
|
|
|
btnReset.setOnClickListener {
|
|
etServerUrl.setText(AppConfig.DEFAULT_SERVER_URL)
|
|
etSerialPort.setText(AppConfig.DEFAULT_SERIAL_PORT)
|
|
Toast.makeText(this, "已重置数值 (待保存)", Toast.LENGTH_SHORT).show()
|
|
}
|
|
|
|
dialogView.findViewById<TextView>(R.id.btn_cancel).setOnClickListener {
|
|
dialog.dismiss()
|
|
}
|
|
|
|
dialogView.findViewById<TextView>(R.id.btn_save).setOnClickListener {
|
|
val serverUrl = etServerUrl.text.toString().trim()
|
|
val serialPort = etSerialPort.text.toString().trim()
|
|
|
|
if (serverUrl.isEmpty()) {
|
|
etServerUrl.error = "地址不能为空"
|
|
return@setOnClickListener
|
|
}
|
|
|
|
AppConfig.setServerUrl(this, serverUrl)
|
|
AppConfig.setSerialPort(this, serialPort)
|
|
|
|
Toast.makeText(this, "参数已生效", Toast.LENGTH_SHORT).show()
|
|
dialog.dismiss()
|
|
}
|
|
|
|
dialog.show()
|
|
|
|
// 强制设置对话框宽度
|
|
dialog.window?.let { window ->
|
|
val params = window.attributes
|
|
params.width = (resources.displayMetrics.widthPixels * 0.75).toInt() // 占据屏幕 75% 宽度
|
|
window.attributes = params
|
|
}
|
|
}
|
|
|
|
override fun onBackPressed() {
|
|
AlertDialog.Builder(this)
|
|
.setTitle("提示")
|
|
.setMessage("确定要退出应用吗?")
|
|
.setPositiveButton("确定") { _, _ ->
|
|
@Suppress("DEPRECATION")
|
|
super.onBackPressed()
|
|
}
|
|
.setNegativeButton("取消", null)
|
|
.show()
|
|
}
|
|
}
|