LoginActivity.kt登录界面省去短信的校验,直接登录获取服务器数据
package com.example.takeout.ui.activity
import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.os.SystemClock
import android.text.TextUtils
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import cn.smssdk.EventHandler
import cn.smssdk.SMSSDK
import com.example.takeout.R
import com.example.takeout.presenter.LoginActivityPresenter
import com.heima.takeout.utils.SMSUtil
import com.mob.MobSDK
import kotlinx.android.synthetic.main.activity_login.*
import org.jetbrains.anko.toast
class LoginActivity : AppCompatActivity() {
//监听每一次提交验证码操作
val eh = object : EventHandler() {
override fun afterEvent(event: Int, result: Int, data: Any) {
if (result == SMSSDK.RESULT_COMPLETE) {
//回调完成
if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
//提交验证码成功
Log.d("LoginActivity", "提交验证码成功")
// //登录外卖服务器
// val phone = et_user_phone.text.toString().trim()
// loginActivityPresenter.loginByPhone(phone)
} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
//获取验证码成功
Log.d("LoginActivity", "获取验证码成功")
} else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
//返回支持发送验证码的国家列表
}
} else {
(data as Throwable).printStackTrace()
}
}
}
lateinit var loginActivityPresenter: LoginActivityPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
loginActivityPresenter = LoginActivityPresenter(this)
initListener()
MobSDK.submitPolicyGrantResult(true, null);
//注册短信回调
SMSSDK.registerEventHandler(eh)
}
private fun initListener() {
iv_user_back.setOnClickListener { finish() }
//获取验证码
tv_user_code.setOnClickListener {
val phone = et_user_phone.text.toString().trim()
//验证手机号码
if (SMSUtil.judgePhoneNums(this, phone)) {
// 请求验证码,其中country表示国家代码,如“86”;phone表示手机号码,如“13800138000”
SMSSDK.getVerificationCode("86", phone)
//开启倒计时
tv_user_code.isEnabled = false
Thread(CutDownTask()).start()
}
}
iv_login.setOnClickListener {
//提交验证码
val phone = et_user_phone.text.toString().trim()
val code = et_user_code.text.toString().trim()
//下面是短信验证码的校验,先屏蔽
// if (SMSUtil.judgePhoneNums(this, phone) && !TextUtils.isEmpty(code)) {
// // 提交验证码,其中的code表示验证码,如“1357”
// SMSSDK.submitVerificationCode("86", phone, code)
// }
//直接登录登录外卖服务器,省去短信验证码验证阶段
loginActivityPresenter.loginByPhone(phone)
}
}
companion object {
val TIME_MINUS = -1 //剩余时间的消息
val TIME_IS_OUT = 0 //60s超时
}
//创建一个handler来刷新UI
val handler = @SuppressLint("HandlerLeak")
object : Handler(){
override fun handleMessage(msg: Message?) {
super.handleMessage(msg)
when(msg!!.what){
TIME_MINUS -> tv_user_code.text = "剩余时间(${time})秒"
TIME_IS_OUT -> {
tv_user_code.isEnabled = true
tv_user_code.text = "点击重发"
time = 60
}
}
}
}
//倒计时的实现60s
var time = 60
inner class CutDownTask: Runnable {
override fun run() {
//倒计时循环
while (time>0){
//刷新剩余时间,当前子线程,使用handler
handler.sendEmptyMessage(TIME_MINUS)
SystemClock.sleep(999)
time --
}
handler.sendEmptyMessage(TIME_IS_OUT)
}
}
// 使用完EventHandler需注销,否则可能出现内存泄漏
override fun onDestroy() {
super.onDestroy()
SMSSDK.unregisterEventHandler(eh)
}
fun onLoginSuccess() {
finish()
toast("登录成功")
}
fun onLoginFailed(){
toast("登录失败")
}
}
LoginActivityPresenter.kt登录的网络请求逻辑
package com.example.takeout.presenter
import com.example.takeout.beans.User
import com.example.takeout.ui.activity.LoginActivity
import com.example.takeout.utils.TakeoutApp
import com.google.gson.Gson
//继承NetPresenter
class LoginActivityPresenter(val loginActivity: LoginActivity) : NetPresenter(){
//使用手机号码登录的业务
fun loginByPhone(phone:String) {
val homeCall = takeoutService.loginByPhone()
homeCall.enqueue(callback)
}
override fun parserJson(json: String) {
val user = Gson().fromJson(json, User::class.java)
if (user != null) {
//缓存到内存中
TakeoutApp.sUser = user
loginActivity.onLoginSuccess()
} else{
loginActivity.onLoginFailed()
}
}
}
UserFragment.kt登录成功以后用户展示界面的刷新
package com.example.takeout.ui.fragment
import android.app.Fragment
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import com.example.takeout.R
import com.example.takeout.ui.activity.LoginActivity
import com.example.takeout.utils.TakeoutApp
import kotlinx.android.synthetic.main.fragment_user.*
import org.jetbrains.anko.find
class UserFragment : Fragment() {
lateinit var ll_userinfo: LinearLayout
lateinit var ivLogin:ImageView
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val userview = View.inflate(activity, R.layout.fragment_user, null)
ll_userinfo = userview.find<LinearLayout>(R.id.ll_userinfo)
ivLogin = userview.find<ImageView>(R.id.login)
ivLogin.setOnClickListener {
val intent = Intent(activity, LoginActivity::class.java)
activity.startActivity(intent)
}
return userview
}
override fun onStart() {
super.onStart()
//展示登录成功后的ui效果
val user = TakeoutApp.sUser
if (user.id == -1) {
//未登录
ll_userinfo.visibility = View.GONE
ivLogin.visibility = View.VISIBLE
} else {
ivLogin.visibility = View.GONE
ll_userinfo.visibility = View.VISIBLE
username.text = "欢迎您,${user.name}"
phone.text = user.phone
}
}
}
TakeoutApp.kt保存登录的数据
package com.example.takeout.utils
import android.app.Application
import com.example.takeout.beans.User
class TakeoutApp : Application() {
companion object {
var sUser: User = User()
lateinit var sInstance: TakeoutApp
}
//应用程序的入口
override fun onCreate() {
super.onCreate()
sInstance = this
sUser.id = -1 //未登录用户id=-1
}
}
User.kt用户的bean数据,对bean的数据进行初始化
package com.example.takeout.beans
import java.io.Serializable
data class User(
var id: Int = 0,
var name: String? = null,
var balance: Float = 0.toFloat(),
var discount: Int = 0,
var integral: Int = 0,
var phone: String? = null
) : Serializable {
// var id: Long = 0
// var pic: String? = null
// var name: String? = null
//
// var score: String? = null
// var sale: String? = null
// var ensure: String? = null
//
// var invoice: String? = null
// var sendPrice: String? = null
// var deliveryFee: String? = null
//
// var recentVisit: String? = null
// var distance: String? = null
// var time: String? = null
//
// var icon: String? = null
//
// var activityList: ArrayList<ActivityInfo>? = null
}
登录成功以后的展示界面如下: