This commit is contained in:
parent
00f4c106a4
commit
4ae7a9d676
|
|
@ -421,6 +421,15 @@ class AndroidInterface(private val activity: MainActivity) {
|
||||||
try {
|
try {
|
||||||
activity.cancelTimeoutTimer()
|
activity.cancelTimeoutTimer()
|
||||||
activity.findViewById<android.view.View>(R.id.loading_layout)?.visibility = android.view.View.GONE
|
activity.findViewById<android.view.View>(R.id.loading_layout)?.visibility = android.view.View.GONE
|
||||||
|
|
||||||
|
// 正式界面模式下,页面加载完成后传递 token 给前端
|
||||||
|
val isOfficial = activity.isOfficialMode()
|
||||||
|
val token = activity.getEssToken()
|
||||||
|
Timber.tag("AndroidInterface").d("onPageFinished: isOfficial=$isOfficial, token=$token")
|
||||||
|
if (isOfficial && token.isNotEmpty()) {
|
||||||
|
com.stand.standapp.MainActivity.executeJs("if(window.setAppToken){window.setAppToken('$token');}")
|
||||||
|
Timber.tag("AndroidInterface").d("Called setAppToken with token")
|
||||||
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.tag("UI").e(e, "隐藏加载布局失败")
|
Timber.tag("UI").e(e, "隐藏加载布局失败")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ class LoginActivity : AppCompatActivity() {
|
||||||
val etPassword = findViewById<EditText>(R.id.et_password)
|
val etPassword = findViewById<EditText>(R.id.et_password)
|
||||||
val cbRemember = findViewById<CheckBox>(R.id.cb_remember)
|
val cbRemember = findViewById<CheckBox>(R.id.cb_remember)
|
||||||
val btnLogin = findViewById<Button>(R.id.btn_login)
|
val btnLogin = findViewById<Button>(R.id.btn_login)
|
||||||
|
val btnLoginOfficial = findViewById<Button>(R.id.btn_login_official)
|
||||||
val btnExit = findViewById<Button>(R.id.btn_exit)
|
val btnExit = findViewById<Button>(R.id.btn_exit)
|
||||||
val ivSettings = findViewById<ImageView>(R.id.iv_settings)
|
val ivSettings = findViewById<ImageView>(R.id.iv_settings)
|
||||||
val tvCopyright = findViewById<TextView>(R.id.tv_copyright)
|
val tvCopyright = findViewById<TextView>(R.id.tv_copyright)
|
||||||
|
|
@ -60,7 +61,20 @@ class LoginActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 执行真实接口登录
|
// 执行真实接口登录
|
||||||
performLogin(username, password, cbRemember.isChecked)
|
performLogin(username, password, cbRemember.isChecked, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3.1 正式界面登录按钮点击事件
|
||||||
|
btnLoginOfficial.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, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. 退出按钮
|
// 4. 退出按钮
|
||||||
|
|
@ -91,7 +105,7 @@ class LoginActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun performLogin(username: String, password: String, remember: Boolean) {
|
private fun performLogin(username: String, password: String, remember: Boolean, officialMode: Boolean) {
|
||||||
val serverUrl = AppConfig.getServerUrl(this)
|
val serverUrl = AppConfig.getServerUrl(this)
|
||||||
val loginUrl = "$serverUrl/api/auth/login"
|
val loginUrl = "$serverUrl/api/auth/login"
|
||||||
|
|
||||||
|
|
@ -137,14 +151,11 @@ class LoginActivity : AppCompatActivity() {
|
||||||
AppConfig.setSavedPassword(this@LoginActivity, "")
|
AppConfig.setSavedPassword(this@LoginActivity, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
Toast.makeText(this@LoginActivity, "登录成功", Toast.LENGTH_SHORT).show()
|
// 提取 token
|
||||||
|
val data = json.optJSONObject("data")
|
||||||
|
val essToken = data?.optString("access_token", "") ?: ""
|
||||||
|
|
||||||
// // 0. 先将账号信息写入 cfg.dat,避免 native 读取时报 zpoc_init_cfg 13
|
Toast.makeText(this@LoginActivity, "登录成功", Toast.LENGTH_SHORT).show()
|
||||||
// try {
|
|
||||||
// com.stand.standapp.MyApplication.writeCfgDat("220.154.131.231", username, "123456")
|
|
||||||
// } catch (e: Exception) {
|
|
||||||
// Timber.tag("PTT").e(e, "writeCfgDat error")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 1. PTT 引擎强制重置后登录 (销毁旧 session,确保干净状态)
|
// 1. PTT 引擎强制重置后登录 (销毁旧 session,确保干净状态)
|
||||||
try {
|
try {
|
||||||
|
|
@ -176,7 +187,11 @@ class LoginActivity : AppCompatActivity() {
|
||||||
// 3. 初始化打印机 (异步)
|
// 3. 初始化打印机 (异步)
|
||||||
PrinterManager.init(this@LoginActivity)
|
PrinterManager.init(this@LoginActivity)
|
||||||
|
|
||||||
startActivity(Intent(this@LoginActivity, MainActivity::class.java))
|
// 4. 启动 MainActivity,传递登录模式和 token
|
||||||
|
val intent = Intent(this@LoginActivity, MainActivity::class.java)
|
||||||
|
intent.putExtra("official_mode", officialMode)
|
||||||
|
intent.putExtra("ess_token", essToken)
|
||||||
|
startActivity(intent)
|
||||||
finish()
|
finish()
|
||||||
} else {
|
} else {
|
||||||
val msg = json.optString("msg", "登录失败")
|
val msg = json.optString("msg", "登录失败")
|
||||||
|
|
|
||||||
|
|
@ -124,6 +124,9 @@ class MainActivity : AppCompatActivity() {
|
||||||
private var canGoBack = false
|
private var canGoBack = false
|
||||||
private lateinit var geckoView: GeckoView
|
private lateinit var geckoView: GeckoView
|
||||||
|
|
||||||
|
private var officialMode = false
|
||||||
|
private var essToken = ""
|
||||||
|
|
||||||
private val mHandler = android.os.Handler(android.os.Looper.getMainLooper())
|
private val mHandler = android.os.Handler(android.os.Looper.getMainLooper())
|
||||||
private val TIMEOUT_MS = 10000L
|
private val TIMEOUT_MS = 10000L
|
||||||
|
|
||||||
|
|
@ -135,6 +138,11 @@ class MainActivity : AppCompatActivity() {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
instance = this
|
instance = this
|
||||||
showPendingDialogIfNeeded()
|
showPendingDialogIfNeeded()
|
||||||
|
|
||||||
|
// 读取登录模式和 token
|
||||||
|
officialMode = intent.getBooleanExtra("official_mode", false)
|
||||||
|
essToken = intent.getStringExtra("ess_token") ?: ""
|
||||||
|
|
||||||
try {
|
try {
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
|
||||||
|
|
@ -167,8 +175,12 @@ class MainActivity : AppCompatActivity() {
|
||||||
Timber.tag("MainActivity").d("WebExtension installed: ${extension?.metaData?.name}")
|
Timber.tag("MainActivity").d("WebExtension installed: ${extension?.metaData?.name}")
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
val serverUrl = AppConfig.getServerUrl(this)
|
val serverUrl = AppConfig.getServerUrl(this)
|
||||||
val finalUrl = "$serverUrl/example/print_demo.html?t=${System.currentTimeMillis()}"
|
val finalUrl = if (officialMode) {
|
||||||
Timber.tag("MainActivity").d("Loading URL: $finalUrl")
|
"$serverUrl/sysdispatch/home?t=${System.currentTimeMillis()}"
|
||||||
|
} else {
|
||||||
|
"$serverUrl/example/print_demo.html?t=${System.currentTimeMillis()}"
|
||||||
|
}
|
||||||
|
Timber.tag("MainActivity").d("Loading URL: $finalUrl (officialMode=$officialMode)")
|
||||||
session.loadUri(finalUrl)
|
session.loadUri(finalUrl)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -236,6 +248,14 @@ class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
override fun onPageStop(session: GeckoSession, success: Boolean) {
|
override fun onPageStop(session: GeckoSession, success: Boolean) {
|
||||||
Timber.tag("MainActivity").d("Page stopped, success: $success")
|
Timber.tag("MainActivity").d("Page stopped, success: $success")
|
||||||
|
// 正式界面模式下,页面加载完成后传递 token 给前端
|
||||||
|
// if (success && officialMode && essToken.isNotEmpty()) {
|
||||||
|
// // 延迟 500ms 确保 JS 上下文就绪
|
||||||
|
// mHandler.postDelayed({
|
||||||
|
// executeJs("if(window.setAppToken){window.setAppToken('$essToken');}")
|
||||||
|
// Timber.tag("MainActivity").d("Called setAppToken via onPageStop")
|
||||||
|
// }, 500)
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -439,6 +459,10 @@ class MainActivity : AppCompatActivity() {
|
||||||
mHandler.removeCallbacks(timeoutRunnable)
|
mHandler.removeCallbacks(timeoutRunnable)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun isOfficialMode(): Boolean = officialMode
|
||||||
|
|
||||||
|
fun getEssToken(): String = essToken
|
||||||
|
|
||||||
private fun showErrorPage() {
|
private fun showErrorPage() {
|
||||||
findViewById<android.view.View>(R.id.loading_layout)?.visibility = android.view.View.GONE
|
findViewById<android.view.View>(R.id.loading_layout)?.visibility = android.view.View.GONE
|
||||||
findViewById<android.view.View>(R.id.error_layout)?.visibility = android.view.View.VISIBLE
|
findViewById<android.view.View>(R.id.error_layout)?.visibility = android.view.View.VISIBLE
|
||||||
|
|
|
||||||
|
|
@ -123,12 +123,22 @@
|
||||||
android:id="@+id/btn_login"
|
android:id="@+id/btn_login"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="55dp"
|
android:layout_height="55dp"
|
||||||
android:layout_marginBottom="15dp"
|
android:layout_marginBottom="10dp"
|
||||||
android:background="@drawable/shape_button_login"
|
android:background="@drawable/shape_button_login"
|
||||||
android:text="登录"
|
android:text="登录"
|
||||||
android:textColor="#FFFFFF"
|
android:textColor="#FFFFFF"
|
||||||
android:textSize="18sp" />
|
android:textSize="18sp" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btn_login_official"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="55dp"
|
||||||
|
android:layout_marginBottom="15dp"
|
||||||
|
android:background="@drawable/shape_button_login"
|
||||||
|
android:text="正式界面登录"
|
||||||
|
android:textColor="#FFFFFF"
|
||||||
|
android:textSize="18sp" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/btn_exit"
|
android:id="@+id/btn_exit"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue