-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
xieyy
committed
Jan 22, 2025
1 parent
c6ba9d3
commit 5024ec9
Showing
13 changed files
with
445 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
common_component/src/main/java/com/xyoye/common_component/config/DevelopConfigTable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.xyoye.common_component.config | ||
|
||
import com.xyoye.mmkv_annotation.MMKVFiled | ||
import com.xyoye.mmkv_annotation.MMKVKotlinClass | ||
|
||
/** | ||
* author: [email protected] | ||
* time : 2025/1/22 | ||
* desc : 开发者配置表 | ||
*/ | ||
|
||
@MMKVKotlinClass(className = "DevelopConfig") | ||
object DevelopConfigTable { | ||
|
||
// AppId | ||
@MMKVFiled | ||
const val appId = "" | ||
|
||
// App Secret | ||
@MMKVFiled | ||
const val appSecret = "" | ||
|
||
// 是否已自动显示认证弹窗 | ||
@MMKVFiled | ||
const val isAutoShowAuthDialog = false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...rc/main/java/com/xyoye/common_component/network/helper/DeveloperCertificateInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.xyoye.common_component.network.helper | ||
|
||
import com.xyoye.common_component.config.DevelopConfig | ||
import com.xyoye.common_component.utils.SecurityHelper | ||
import okhttp3.Interceptor | ||
import okhttp3.Response | ||
|
||
/** | ||
* author: [email protected] | ||
* time : 2025/1/22 | ||
* desc : 开发者凭证拦截器 | ||
*/ | ||
class DeveloperCertificateInterceptor : Interceptor { | ||
companion object { | ||
const val HEADER_APP_ID = "X-AppId" | ||
const val HEADER_APP_SECRET = "X-AppSecret" | ||
} | ||
|
||
|
||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val oldRequest = chain.request() | ||
|
||
// 官方应用,不做处理 | ||
if (SecurityHelper.getInstance().isOfficialApplication) { | ||
return chain.proceed(oldRequest) | ||
} | ||
|
||
// 请求自带凭证,不做处理 | ||
val requestAppId = oldRequest.header(HEADER_APP_ID) | ||
val requestAppSecret = oldRequest.header(HEADER_APP_SECRET) | ||
if (requestAppId?.isNotEmpty() == true && requestAppSecret?.isNotEmpty() == true) { | ||
return chain.proceed(oldRequest) | ||
} | ||
|
||
// 未配置凭证,不做处理 | ||
val appId = DevelopConfig.getAppId() | ||
val appSecret = DevelopConfig.getAppSecret() | ||
if (appId.isNullOrEmpty() || appSecret.isNullOrEmpty()) { | ||
return chain.proceed(oldRequest) | ||
} | ||
|
||
// 添加凭证 | ||
return chain.proceed( | ||
oldRequest.newBuilder() | ||
.header(HEADER_APP_ID, appId) | ||
.header(HEADER_APP_SECRET, appSecret).build() | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...component/src/main/java/com/xyoye/user_component/ui/dialog/DeveloperAuthenticateDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package com.xyoye.user_component.ui.dialog | ||
|
||
import android.app.Activity | ||
import com.xyoye.common_component.base.BaseActivity | ||
import com.xyoye.common_component.config.DevelopConfig | ||
import com.xyoye.common_component.extension.startUrlActivity | ||
import com.xyoye.common_component.network.repository.UserRepository | ||
import com.xyoye.common_component.utils.SupervisorScope | ||
import com.xyoye.common_component.weight.ToastCenter | ||
import com.xyoye.common_component.weight.dialog.BaseBottomDialog | ||
import com.xyoye.user_component.R | ||
import com.xyoye.user_component.databinding.DialogDeveloperAuthenticateBinding | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
|
||
/** | ||
* author: [email protected] | ||
* time : 2025/1/22 | ||
* desc : | ||
*/ | ||
|
||
class DeveloperAuthenticateDialog( | ||
private val activity: Activity, | ||
private val onAuthenticate: () -> Unit | ||
) : BaseBottomDialog<DialogDeveloperAuthenticateBinding>(activity) { | ||
|
||
private lateinit var binding: DialogDeveloperAuthenticateBinding | ||
|
||
override fun getChildLayoutId(): Int { | ||
return R.layout.dialog_developer_authenticate | ||
} | ||
|
||
override fun initView(binding: DialogDeveloperAuthenticateBinding) { | ||
this.binding = binding | ||
|
||
setTitle("开发者认证") | ||
|
||
binding.inputAppId.setText(DevelopConfig.getAppId()) | ||
binding.inputAppSecret.setText(DevelopConfig.getAppSecret()) | ||
|
||
setNegativeText("忽略") | ||
setNegativeListener { | ||
dismiss() | ||
} | ||
|
||
setPositiveListener { | ||
checkAuthenticate() | ||
} | ||
|
||
binding.tvReadDocument.setOnClickListener { | ||
activity.startUrlActivity("https://github.com/kaedei/dandanplay-libraryindex/blob/master/api/OpenPlatform.md") | ||
} | ||
} | ||
|
||
/** | ||
* 开发者认证 | ||
*/ | ||
private fun checkAuthenticate() { | ||
val appId = binding.inputAppId.text.toString() | ||
val appSecret = binding.inputAppSecret.text.toString() | ||
|
||
if (appId.isEmpty() || appSecret.isEmpty()) { | ||
ToastCenter.showWarning("请输入AppId和AppSecret") | ||
return | ||
} | ||
|
||
SupervisorScope.IO.launch { | ||
loading(true) | ||
val result = UserRepository.checkAuthenticate(appId, appSecret).getOrNull() | ||
loading(false) | ||
if (result != null && result.code() == 200) { | ||
authenticateSuccess(appId, appSecret) | ||
return@launch | ||
} | ||
|
||
if (result?.code() == 403) { | ||
ToastCenter.showError("认证失败,凭证不被允许访问") | ||
} else { | ||
ToastCenter.showError("认证过程中发生意外,请稍后重试") | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 显示/隐藏加载框 | ||
*/ | ||
private suspend fun loading(show: Boolean) { | ||
if (activity is BaseActivity<*, *>) { | ||
withContext(Dispatchers.Main) { | ||
if (show) { | ||
activity.showLoading() | ||
} else { | ||
activity.hideLoading() | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 认证成功 | ||
*/ | ||
private fun authenticateSuccess(appId: String, appSecret: String) { | ||
DevelopConfig.putAppId(appId) | ||
DevelopConfig.putAppSecret(appSecret) | ||
ToastCenter.showSuccess("认证成功") | ||
onAuthenticate.invoke() | ||
dismiss() | ||
} | ||
} |
Oops, something went wrong.