@@ -9,39 +9,61 @@ import android.webkit.WebResourceRequest
99import android.webkit.WebSettings
1010import android.webkit.WebView
1111import android.webkit.WebViewClient
12+ import android.widget.Toast
1213import androidx.appcompat.app.AppCompatActivity
1314import com.gardilily.onedottongji.R
1415import com.gardilily.onedottongji.tools.tongjiapi.TongjiApi
1516import com.google.android.material.elevation.SurfaceColors
17+ import kotlin.concurrent.thread
1618
1719/* *
1820 *
21+ * Intent 传入:
22+ * scope: String 需要授权的项目。之间用空格分开。
1923 *
2024 * activity 信息返回格式:
2125 * oauthResult: Array<String?>(error, code)
2226 * err 非空,表示登录失败。
2327 * code 非空,可用 code 换结果。
2428 */
25- class TongjiOAuth : AppCompatActivity () {
29+ class TongjiOAuth : OneTJActivityBase (
30+ hasTitleBar = false ,
31+ withSpinning = true
32+ ) {
2633
2734 companion object {
28- const val RESULT_KEY = " oauthResult"
35+ /* *
36+ * 需要授权的选项。
37+ * String
38+ */
39+ const val INTENT_PARAM_SCOPE = " scope"
40+
41+ /* *
42+ * 授权错误信息。
43+ * String
44+ */
45+ const val RESULT_ERROR = " oauthError"
46+
47+ /* *
48+ * 授权得到的 code。
49+ * String
50+ */
51+ const val RESULT_AUTH_CODE = " oauthCode"
52+
2953 }
3054
3155 private lateinit var webView : WebView
3256
3357 override fun onCreate (savedInstanceState : Bundle ? ) {
3458 super .onCreate(savedInstanceState)
35-
3659 setContentView(R .layout.activity_tongji_oauth)
3760
38- val color = SurfaceColors .SURFACE_2 .getColor(this )
39- window.navigationBarColor = color
40- window.statusBarColor = color
41- title = " 同济大学开放平台 - 身份认证"
61+ stageSpinningProgressBar(findViewById(R .id.tongjiOAuth_rootContainer))
62+
63+ val scope = intent.getStringExtra(INTENT_PARAM_SCOPE )
4264
4365 initWebView()
44- loadOAuthPage()
66+ loadOAuthPage(scope )
4567
4668 }
4769
@@ -79,8 +101,34 @@ class TongjiOAuth : AppCompatActivity() {
79101 val code = uri.getQueryParameter(" code" )
80102 val error = uri.getQueryParameter(" error" )
81103
82- setResult(0 , Intent ().putExtra(RESULT_KEY , arrayOf(error, code)))
83- finish()
104+ val resIntent = Intent ()
105+ resIntent.putExtra(RESULT_AUTH_CODE , code)
106+ resIntent.putExtra(RESULT_ERROR , error)
107+
108+ code?.let {
109+
110+ thread {
111+ val res = TongjiApi .instance.code2token(it, this @TongjiOAuth)
112+
113+
114+ runOnUiThread {
115+ if (! res) {
116+
117+ Toast .makeText(
118+ this @TongjiOAuth,
119+ " 授权失败(code2token)" ,
120+ Toast .LENGTH_SHORT
121+ ).show()
122+
123+ resIntent.putExtra(RESULT_ERROR , " code2token returns false" )
124+ }
125+
126+ setResult(0 , resIntent)
127+ finish()
128+
129+ } // runOnUiThread {
130+ } // thread {
131+ } // code?.let {
84132
85133 return true // 防止白屏。
86134 }
@@ -92,7 +140,7 @@ class TongjiOAuth : AppCompatActivity() {
92140 webView.webChromeClient = object : WebChromeClient () {}
93141 }
94142
95- private fun loadOAuthPage () {
143+ private fun loadOAuthPage (scope : String? ) {
96144 // https://api.tongji.edu.cn/docs/#/architecture/authentication
97145 val urlBuilder = StringBuilder (" ${TongjiApi .BASE_URL } /keycloak/realms/OpenPlatform/protocol/openid-connect/auth" )
98146
@@ -101,15 +149,17 @@ class TongjiOAuth : AppCompatActivity() {
101149 urlBuilder.append(" &response_type=code" )
102150
103151 val scopeBuilder = StringBuilder ()
104- TongjiApi .SCOPE_LIST .forEach {
105- if (it != TongjiApi .SCOPE_LIST .first()) {
106- scopeBuilder.append(' ' )
107- }
152+ if (scope == null ) {
153+ TongjiApi .SCOPE_LIST .forEach {
154+ if (it != TongjiApi .SCOPE_LIST .first()) {
155+ scopeBuilder.append(' ' )
156+ }
108157
109- scopeBuilder.append(it)
158+ scopeBuilder.append(it)
159+ }
110160 }
111161
112- urlBuilder.append(" &scope=$scopeBuilder " )
162+ urlBuilder.append(" &scope=${scope ? : scopeBuilder} " )
113163
114164 webView.loadUrl(urlBuilder.toString())
115165
0 commit comments