401跳转登录界面
This commit is contained in:
@@ -15,6 +15,7 @@ import bean.LoginRequest
|
||||
import com.ckkj.defense_device.R
|
||||
import com.ckkj.defense_device.databinding.ActivityLoginBinding
|
||||
import com.ckkj.defense_device.home.HomeActivity
|
||||
import com.ckkj.defense_device.utils.ApiConfig
|
||||
import com.ckkj.defense_device.utils.RetrofitNetwork
|
||||
import com.ckkj.defense_device.utils.UserinfoUtil
|
||||
import com.gyf.immersionbar.ImmersionBar
|
||||
@@ -32,6 +33,7 @@ class LoginActivity : AppCompatActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
ApiConfig.resetAuthRedirectFlag()
|
||||
|
||||
binding = ActivityLoginBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
package com.ckkj.defense_device.utils
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Intent
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import com.blankj.utilcode.util.Utils
|
||||
import com.ckkj.defense_device.bean.BaseInfo
|
||||
import com.ckkj.defense_device.login.LoginActivity
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.JsonParser
|
||||
import okhttp3.Interceptor
|
||||
@@ -24,10 +30,17 @@ object ApiConfig {
|
||||
@Volatile
|
||||
public var authToken: String = ""
|
||||
|
||||
@Volatile
|
||||
private var isRedirectingToLogin = false
|
||||
|
||||
fun updateAuthToken(token: String) {
|
||||
authToken = token
|
||||
}
|
||||
|
||||
fun resetAuthRedirectFlag() {
|
||||
isRedirectingToLogin = false
|
||||
}
|
||||
|
||||
fun hasAuthToken(): Boolean {
|
||||
return authToken.isNotBlank()
|
||||
}
|
||||
@@ -116,8 +129,8 @@ object ApiConfig {
|
||||
val businessCode = if (jsonObject.has("code")) jsonObject.get("code").asInt else 0
|
||||
|
||||
if (httpCode == 401 || httpCode == 424 || businessCode == 401) {
|
||||
clearUserLoginInfo()
|
||||
val msg = if (jsonObject.has("msg")) jsonObject.get("msg").asString else "登录已过期,请重新登录"
|
||||
redirectToLogin(msg)
|
||||
return@Interceptor buildFakeErrorResponse(request, msg, code = if (businessCode != 0) businessCode else httpCode)
|
||||
}
|
||||
|
||||
@@ -179,12 +192,36 @@ object ApiConfig {
|
||||
private fun buildAuthorizationValue(token: String): String {
|
||||
return if (token.startsWith("Bearer ")) token else "Bearer $token"
|
||||
}
|
||||
//401跳转登录界面
|
||||
private fun redirectToLogin(msg: String) {
|
||||
Handler(Looper.getMainLooper()).post {
|
||||
if (isRedirectingToLogin) return@post
|
||||
val current = ActivityManager.getCurrentActivity()
|
||||
if (current is LoginActivity) return@post
|
||||
|
||||
isRedirectingToLogin = true
|
||||
clearUserLoginInfo()
|
||||
|
||||
val context = current ?: Utils.getApp()
|
||||
ActivityManager.finishAllActivities()
|
||||
|
||||
val intent = Intent(context, LoginActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||
}
|
||||
context.startActivity(intent)
|
||||
|
||||
if (msg.isNotBlank()) {
|
||||
Toast.makeText(Utils.getApp(), msg, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun clearUserLoginInfo() {
|
||||
try {
|
||||
authToken = ""
|
||||
UserinfoUtil.clearUserInfo()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
authToken = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user