添加clientid和content-languag
This commit is contained in:
@@ -2,14 +2,25 @@ package com.example.defenseapplication
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import bean.LoginBean
|
||||
import com.blankj.utilcode.util.Utils
|
||||
import com.example.defenseapplication.bean.BaseInfo
|
||||
import com.example.defenseapplication.utils.ApiConfig
|
||||
import com.example.defenseapplication.utils.LanguageUtil
|
||||
import com.example.defenseapplication.utils.UserinfoUtil
|
||||
import com.google.gson.Gson
|
||||
|
||||
class MyApplication : Application() {
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
Utils.init(this) // 初始化 AndroidUtilCode
|
||||
|
||||
var token = UserinfoUtil.getUserInfo()?.access_token;
|
||||
if (token != null) {
|
||||
ApiConfig.authToken = token
|
||||
};
|
||||
LanguageHeaderManager.init(this)
|
||||
}
|
||||
|
||||
override fun attachBaseContext(base: Context) {
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
package bean
|
||||
|
||||
data class LoginBean(
|
||||
val headPic: String,
|
||||
val nickName: String,
|
||||
val sex: Int,
|
||||
val signature: String,
|
||||
val token: String,
|
||||
val userId: Int
|
||||
val access_token: String,
|
||||
val client_id: String,
|
||||
val expire_in: Int,
|
||||
val openid: Any,
|
||||
val refresh_expire_in: Any,
|
||||
val refresh_token: Any,
|
||||
val role: String,
|
||||
val scope: Any
|
||||
)
|
||||
data class LoginRequest(
|
||||
val clientId: String = "428a8310cd442757ae699df5d894f051",
|
||||
@@ -34,4 +35,9 @@ data class ForgotRequest(
|
||||
val phonenumber: String,
|
||||
val smsCode: String,
|
||||
val password: String
|
||||
)
|
||||
data class LoginResponse(
|
||||
val clientid: String,
|
||||
val content_language: String,
|
||||
|
||||
)
|
||||
@@ -14,6 +14,8 @@ import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import bean.LoginRequest
|
||||
import bean.LoginResponse
|
||||
import com.example.defenseapplication.R
|
||||
import com.example.defenseapplication.adapter.DeviceAdapter
|
||||
import com.example.defenseapplication.adapter.DeviceUi
|
||||
@@ -108,6 +110,11 @@ class HomeMenuFragment : Fragment() {
|
||||
private fun fetchFamilyList() {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
val loginRequest = LoginResponse(
|
||||
clientid = "428a8310cd442757ae699df5d894f051",
|
||||
content_language = "en_US",
|
||||
)
|
||||
|
||||
val response = RetrofitNetwork.getNetworkService().getFamilyList()
|
||||
withContext(Dispatchers.Main) {
|
||||
if (response.code == 200 && response.data != null) {
|
||||
|
||||
@@ -22,7 +22,7 @@ object ApiConfig {
|
||||
private const val TAG = "ApiConfig"
|
||||
private val gson = Gson()
|
||||
@Volatile
|
||||
private var authToken: String = ""
|
||||
public var authToken: String = ""
|
||||
|
||||
fun updateAuthToken(token: String) {
|
||||
authToken = token
|
||||
@@ -162,9 +162,12 @@ object ApiConfig {
|
||||
fun createHeadersInterceptor() = Interceptor { chain ->
|
||||
val requestBuilder = chain.request().newBuilder()
|
||||
val token = authToken
|
||||
|
||||
val langHeader = LanguageHeaderManager.getLanguageHeaderValue()
|
||||
if (token.isNotBlank()) {
|
||||
Log.d("ApiConfig", buildAuthorizationValue(token))
|
||||
requestBuilder.header("Authorization", buildAuthorizationValue(token))
|
||||
requestBuilder.header("clientid", "428a8310cd442757ae699df5d894f051")
|
||||
requestBuilder.header("content-language", langHeader)
|
||||
}
|
||||
|
||||
requestBuilder
|
||||
|
||||
@@ -38,7 +38,7 @@ interface ApiService {
|
||||
suspend fun forgotPassword(@Body forgotRequest: ForgotRequest): GetCodeBean<Any>
|
||||
|
||||
/**
|
||||
* 获取我所在家庭列表
|
||||
* 获取家庭列表
|
||||
*/
|
||||
@GET("/app/selectFamily")
|
||||
suspend fun getFamilyList(): GetCodeBean<List<FamilyBean>>
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import android.content.Context
|
||||
import androidx.core.content.edit
|
||||
|
||||
enum class AppLanguage(val code: String) {
|
||||
ENGLISH("en_US"),
|
||||
SIMPLIFIED_CHINESE("zh_CN"),
|
||||
TRADITIONAL_CHINESE("zh_TW")
|
||||
}
|
||||
//语言枚举与管理器
|
||||
object LanguageHeaderManager {
|
||||
private const val PREF_NAME = "app_lang_pref"
|
||||
private const val KEY_LANGUAGE = "selected_language"
|
||||
|
||||
private var currentLanguage: AppLanguage = AppLanguage.ENGLISH
|
||||
|
||||
fun init(context: Context) {
|
||||
val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||
val langCode = prefs.getString(KEY_LANGUAGE, AppLanguage.ENGLISH.code)
|
||||
currentLanguage = AppLanguage.values().find { it.code == langCode } ?: AppLanguage.ENGLISH
|
||||
}
|
||||
|
||||
fun getCurrentLanguage(): AppLanguage = currentLanguage
|
||||
|
||||
fun switchLanguage(context: Context, language: AppLanguage) {
|
||||
currentLanguage = language
|
||||
// 持久化
|
||||
context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit {
|
||||
putString(KEY_LANGUAGE, language.code)
|
||||
apply()
|
||||
}
|
||||
// 可选:更新应用 Locale、重启 Activity 等
|
||||
}
|
||||
|
||||
fun getLanguageHeaderValue(): String = currentLanguage.code
|
||||
}
|
||||
@@ -12,6 +12,7 @@ object UserinfoUtil {
|
||||
|
||||
fun saveUserInfo(userinfo: LoginBean?) {
|
||||
userinfo ?: return
|
||||
ApiConfig.updateAuthToken(userinfo.access_token);
|
||||
val json = gson.toJson(userinfo)
|
||||
SPUtils.getInstance().put(KEY_USER, json)
|
||||
}
|
||||
|
||||
@@ -51,6 +51,11 @@
|
||||
android:src="@drawable/triangle" />
|
||||
</LinearLayout>
|
||||
|
||||
<TextView
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"/>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
@@ -138,7 +143,6 @@
|
||||
android:clipToPadding="false"
|
||||
android:nestedScrollingEnabled="false"
|
||||
android:paddingBottom="16dp"
|
||||
tools:itemCount="3"
|
||||
tools:listitem="@layout/item_device" />
|
||||
</LinearLayout>
|
||||
|
||||
@@ -168,7 +172,6 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:nestedScrollingEnabled="false"
|
||||
tools:itemCount="3"
|
||||
tools:listitem="@layout/item_family" />
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
@@ -8,4 +8,5 @@
|
||||
android:paddingHorizontal="14dp"
|
||||
android:paddingVertical="12dp"
|
||||
android:textColor="@color/black"
|
||||
android:text="张三"
|
||||
android:textSize="16sp" />
|
||||
|
||||
Reference in New Issue
Block a user