lmw
2023-05-12 f67802a41f9e01444d1115f34ecc6e1beb05fc3b
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
package com.fuban.user.ui.login
 
import android.app.Activity
import android.os.CountDownTimer
import cn.sinata.xldutils.utils.*
import com.fuban.user.R
import com.fuban.user.network.HttpManager
import com.fuban.user.network.request
import com.fuban.user.ui.TransparentStatusBarActivity
import com.fuban.user.utils.Const
import kotlinx.android.synthetic.main.activity_bind_phone.*
 
class BindPhoneActivity : TransparentStatusBarActivity() {
    override fun setContentView() = R.layout.activity_bind_phone
 
    private val timer by lazy {
        object : CountDownTimer(60000, 1000) {
            override fun onFinish() {
                tv_code.text = "获取验证码"
                tv_code.isEnabled = true
            }
 
            override fun onTick(millisUntilFinished: Long) {
                tv_code.text = "已发送${millisUntilFinished / 1000}s"
            }
        }
    }
 
    override fun initClick() {
        tv_code.setOnClickListener {
            val phone = et_phone.text.toString()
            if (!phone.isValidPhone()) {
                myToast("请输入正确的手机号码")
                return@setOnClickListener
            }
            tv_code.isEnabled = false
            HttpManager.sendSms(phone,1).request(this,success = { _, _ ->
                timer.start()
                myToast("发送成功")
            },error = {_,_->
                tv_code.isEnabled = true
            })
        }
 
        tv_action.setOnClickListener {
            val phone = et_phone.text.toString()
            if (!phone.isValidPhone()) {
                myToast("请输入正确的手机号码")
                return@setOnClickListener
            }
            val code = et_code.text.toString()
            if (code.isEmpty()) {
                myToast("请输入短信验证码")
                return@setOnClickListener
            }
            if (code.length<4) {
                myToast("请输入4位数验证码")
                return@setOnClickListener
            }
            HttpManager.bindingPhone(code, phone).request(this){_,data->
                data?.let {
                    myToast("绑定成功")
                    SPUtils.instance().put(Const.User.NO_PHONE, false).apply()
                    if (it.optString("appid").isNotEmpty()){
                        SPUtils.instance().put(Const.User.APP_ID, it.optString("appid"))
                            .put(Const.User.TOKEN, it.optString("token"))
                            .put(Const.User.USER_ID, it.optInt("id")).apply()
                    }
                    setResult(Activity.RESULT_OK)
                    finish()
                }
            }
        }
    }
 
    override fun initView() {
        title = "完善手机号"
    }
 
}