18034727885
2026-04-14 370014cbb9ec1ef64e7e42bb4f9a12671461d8e4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package com.xw.yunwei.ui.user.login
 
import android.annotation.SuppressLint
import android.graphics.Color
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import android.webkit.WebSettings
import android.webkit.WebView
import android.widget.Button
import androidx.core.os.bundleOf
import androidx.databinding.DataBindingUtil
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.blankj.utilcode.util.SpanUtils
import com.google.android.material.tabs.TabLayout
import com.xw.common.Constants
import com.xw.common.R
import com.xw.common.base.BaseActivity
import com.xw.common.utils.BindUtils.getResColor
import com.xw.common.utils.KeyPreferences
import com.xw.common.utils.MMKVManager
import com.xw.yunwei.BR
import com.xw.yunwei.MyApp
import com.xw.yunwei.databinding.ActivityLoginBinding
import com.xw.yunwei.databinding.DialogProtocolBinding
import com.xw.yunwei.ui.MainActivity
import com.xw.yunwei.ui.work.car.detail.StandardDescriptionActivity
 
class LoginActivity : BaseActivity<ActivityLoginBinding, LoginVM>() {
    override fun getBindingVariable() = BR.viewModel
 
    override fun init() {
        super.init()
 
        if (Constants.token.isNotEmpty()) {
            startActivity(MainActivity::class.java)
            finish()
        } else {
            MyApp.myApp.unbindPushAccount()
 
            val tab1 = binding.tabLayout.newTab().apply { text = "验证码登录" }
            val tab2 = binding.tabLayout.newTab().apply { text = "密码登录" }
 
            binding.tabLayout.addTab(tab1, true)
            binding.tabLayout.addTab(tab2, false)
 
            val protocol =
                SpanUtils().append("我已阅读并同意")
                    .setForegroundColor(getResColor(R.color.gray_9))
                    .append("《小鹏Robotaxi自动驾驶出行运维隐私政策》").setBold()
                    .setForegroundColor(getResColor(R.color.blue_5))
                    .setClickSpan(object : ClickableSpan() {
                        override fun onClick(widget: View) {
//                            showProtocolDialog(1)
                            startActivity(
                                StandardDescriptionActivity::class.java,
                                bundleOf("type" to 8, "webType" to 1)
                            )
                        }
 
                        override fun updateDrawState(ds: TextPaint) {}
                    })
                    .append("《第三方共享信息清单》").setBold()
                    .setForegroundColor(getResColor(R.color.blue_5))
                    .setClickSpan(object : ClickableSpan() {
                        override fun onClick(widget: View) {
//                            showProtocolDialog(2)
                            startActivity(
                                StandardDescriptionActivity::class.java,
                                bundleOf("type" to 9, "webType" to 1)
                            )
                        }
 
                        override fun updateDrawState(ds: TextPaint) {}
                    })
                    .append("《个人信息收集清单》").setBold()
                    .setForegroundColor(getResColor(R.color.blue_5))
                    .setClickSpan(object : ClickableSpan() {
                        override fun onClick(widget: View) {
                            startActivity(
                                StandardDescriptionActivity::class.java,
                                bundleOf("type" to 10, "webType" to 1)
                            )
                        }
 
                        override fun updateDrawState(ds: TextPaint) {}
                    })
                    .append("《权限调用》").setBold()
                    .setForegroundColor(getResColor(R.color.blue_5))
                    .setClickSpan(object : ClickableSpan() {
                        override fun onClick(widget: View) {
                            startActivity(
                                StandardDescriptionActivity::class.java,
                                bundleOf("type" to 11, "webType" to 1)
                            )
                        }
 
                        override fun updateDrawState(ds: TextPaint) {}
                    })
                    .create()
 
            binding.protocol.text = protocol
            binding.protocol.highlightColor = Color.TRANSPARENT
            binding.protocol.movementMethod = LinkMovementMethod.getInstance()
        }
 
    }
 
    override fun setListener() {
        super.setListener()
        binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                tab?.let {
                    viewModel.tabIndex.value = when (it.text) {
                        "验证码登录" -> 0
                        "密码登录" -> 1
                        else -> 0
                    }
                }
            }
 
            override fun onTabUnselected(tab: TabLayout.Tab?) {}
            override fun onTabReselected(tab: TabLayout.Tab?) {}
        })
    }
 
    override fun setObservable() {
        super.setObservable()
        viewModel.protocolStatus.observe(this){
            MMKVManager.getInstance().saveBoolean(KeyPreferences.NEED_READ, !it)
        }
        viewModel.phone.observe(this){
            viewModel.canGetCode.value = it.length == 11
        }
    }
 
    // 标记两个协议是否都已读到底部
    private var userProtocolRead = false
    private var privacyProtocolRead = false
    private var currentTab = 1 // 当前选中标签
    fun showProtocolDialog(type: Int) {
        MaterialDialog(this).cornerRadius(15f).cancelable(true).show {
            customView(
                com.xw.yunwei.R.layout.dialog_protocol,
                noVerticalPadding = true,
                dialogWrapContent = true
            )
            DataBindingUtil.bind<DialogProtocolBinding>(getCustomView())?.let { bind ->
                // 初始禁用确认按钮
                bind.btnRight.isEnabled = false
                // bind.btnRight.alpha = 0.5f
                bind.btnRight.setTextColor(getResColor(R.color.gray_7))
 
                val tab1 = bind.protocolLayout.tabLayout.newTab().apply { text = "用户协议" }
                val tab2 = bind.protocolLayout.tabLayout.newTab().apply { text = "隐私条款" }
 
                bind.protocolLayout.tabLayout.addTab(tab1, type == 1)
                bind.protocolLayout.tabLayout.addTab(tab2, type == 2)
                bind.btnLeft.setOnClickListener { dismiss() }
                bind.btnRight.setOnClickListener {
                    viewModel.protocolStatus.value = true
                    dismiss()
                }
                initWeb(bind.protocolLayout.web,bind.btnRight)
 
                val getProtocol: (Int) -> Unit = { type ->
                    currentTab = type
                    viewModel.getProtocol(type) {
                        setContent(bind.protocolLayout.web, it, bind.btnRight)
                    }
                }
 
                getProtocol(type)
                bind.protocolLayout.tabLayout.addOnTabSelectedListener(object :
                    TabLayout.OnTabSelectedListener {
                    override fun onTabSelected(tab: TabLayout.Tab?) {
                        tab?.let {
                            when (it.text) {
                                "用户协议" -> getProtocol(1)
                                "隐私条款" -> getProtocol(2)
                                else -> {}
                            }
                        }
                    }
 
                    override fun onTabUnselected(tab: TabLayout.Tab?) {}
                    override fun onTabReselected(tab: TabLayout.Tab?) {}
                })
 
            }
        }
    }
 
    @SuppressLint("SetJavaScriptEnabled")
    private fun initWeb(webView: WebView, confirmBtn: Button) {
        //声明WebSettings子类
        val webSettings = webView.settings
        webSettings.domStorageEnabled = true //设置适应Html5
 
        //如果访问的页面中要与Javascript交互,则webview必须设置支持Javascript
        webSettings.javaScriptEnabled = true //允许js调用
        webSettings.allowFileAccess = true //在File域下,能够执行任意的JavaScript代码,同源策略跨域访问能够对私有目录文件进行访问等
        webSettings.allowContentAccess = true //是否允许在WebView中访问内容URL(Content Url),默认允许
 
        //缩放操作
        webSettings.setSupportZoom(true) //支持缩放,默认为true。是下面那个的前提。
        webSettings.builtInZoomControls = true //设置内置的缩放控件。若为false,则该WebView不可缩放
        webSettings.displayZoomControls = false //隐藏原生的缩放控件
 
        //其他细节操作
        webSettings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK //关闭webview中缓存
        webSettings.allowFileAccess = true //设置可以访问文件
        webSettings.javaScriptCanOpenWindowsAutomatically = true //支持通过JS打开新窗口
        webSettings.loadsImagesAutomatically = true //支持自动加载图片
        webSettings.defaultTextEncodingName = "utf-8" //设置编码格式
 
        // 关键:添加滚动监听
        webView.setOnScrollChangeListener { _, _, scrollY, _, oldScrollY ->
            // 只有向下滑动时才检测
            if (scrollY > oldScrollY) {
                checkScrollBottom(webView, confirmBtn)
            }
        }
    }
 
    @SuppressLint("SetJavaScriptEnabled")
    private fun setContent(webView: WebView, content: String?, confirmBtn: Button) {
        content?.let {
            webView.loadDataWithBaseURL(null, it, "text/html", "utf-8", null)
 
//            confirmBtn.text = if(currentTab == 1) "请阅读协议" else "请阅读条款"
            // 禁用按钮直到滚动到底
            confirmBtn.isEnabled = false
//            confirmBtn.alpha = 0.5f
            confirmBtn.setTextColor(getResColor(R.color.gray_7))
 
 
            // 页面加载完成后延迟检查(确保内容渲染完成)
            webView.postDelayed({
                checkScrollBottom(webView, confirmBtn)
            }, 500)
        }
    }
 
    /**
     * 检测是否滚动到底部
     */
    private fun checkScrollBottom(webView: WebView, confirmBtn: Button) {
        webView.evaluateJavascript(
            """
            (function() {
                var scrollHeight = Math.max(
                    document.body.scrollHeight, 
                    document.documentElement.scrollHeight,
                    document.body.offsetHeight, 
                    document.documentElement.offsetHeight,
                    document.body.clientHeight, 
                    document.documentElement.clientHeight
                );
                var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
                var clientHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0;
                
                // 计算距离底部距离,允许20px误差
                var distanceToBottom = scrollHeight - scrollTop - clientHeight;
                return distanceToBottom <= 20;
            })();
            """.trimIndent()
        ) { result ->
            val isBottom = result?.trim() == "true"
 
            if (isBottom) {
                // 标记当前协议已读
                when (currentTab) {
                    1 -> userProtocolRead = true
                    2 -> privacyProtocolRead = true
                }
                updateButtonState(confirmBtn)
            }
        }
    }
 
    /**
     * 更新按钮状态:两个协议都必须阅读完才能点击
     */
    private fun updateButtonState(confirmBtn: Button) {
        val allRead = userProtocolRead && privacyProtocolRead
 
        if (allRead && !confirmBtn.isEnabled) {
            confirmBtn.isEnabled = true
//            confirmBtn.alpha = 1.0f
            confirmBtn.setTextColor(getResColor(R.color.blue_5))
            confirmBtn.text = "同意并继续"
 
            // 可选:启用动画
            confirmBtn.animate()
                .scaleX(1.05f)
                .scaleY(1.05f)
                .setDuration(150)
                .withEndAction {
                    confirmBtn.animate()
                        .scaleX(1.0f)
                        .scaleY(1.0f)
                        .setDuration(150)
                        .start()
                }
                .start()
        } else if (!allRead) {
            confirmBtn.isEnabled = false
//            confirmBtn.alpha = 0.5f
            confirmBtn.setTextColor(getResColor(R.color.gray_7))
 
            // 提示还需阅读哪个协议
//            val hint = if(currentTab == 1){
//                when {
//                    !userProtocolRead -> "请阅读协议"
//                    !privacyProtocolRead -> "请阅读条款"
//                    else -> "请阅读协议"
//                }
//            }else{
//                when {
//                    !privacyProtocolRead -> "请阅读条款"
//                    !userProtocolRead -> "请阅读协议"
//                    else -> "请阅读协议"
//                }
//            }
//            confirmBtn.text = hint
        }
    }
}