73 lines
2.6 KiB
Kotlin
73 lines
2.6 KiB
Kotlin
package com.zjgsu.jianshu
|
|
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
import android.os.Bundle
|
|
import android.widget.Toast
|
|
import cn.bmob.v3.Bmob
|
|
import cn.bmob.v3.BmobQuery
|
|
import cn.bmob.v3.exception.BmobException
|
|
import cn.bmob.v3.listener.FindListener
|
|
import com.zjgsu.jianshu.Bmob.User_bmob
|
|
import kotlinx.android.synthetic.main.activity_login.*
|
|
|
|
class LoginActivity : AppCompatActivity() {
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
setContentView(R.layout.activity_login)
|
|
Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")
|
|
setClickListeners()
|
|
}
|
|
|
|
private fun setClickListeners() {
|
|
forget_password.setOnClickListener {
|
|
startActivity(Intent(this, FindpwdActivity::class.java))
|
|
finish()
|
|
}
|
|
|
|
bt_register.setOnClickListener {
|
|
startActivity(Intent(this, RegisterActivity::class.java))
|
|
finish()
|
|
}
|
|
|
|
bt_login.setOnClickListener {
|
|
handleLogin()
|
|
}
|
|
}
|
|
|
|
private fun handleLogin() {
|
|
val userAccount = et_blackOutName.text.toString()
|
|
val userPassword = et_UserPassword.text.toString()
|
|
if (userAccount.isEmpty() || userPassword.isEmpty()) {
|
|
Toast.makeText(this, "账号或密码不能为空白", Toast.LENGTH_SHORT).show()
|
|
return
|
|
}
|
|
|
|
val bmobQuery = BmobQuery<User_bmob>()
|
|
bmobQuery.findObjects(object : FindListener<User_bmob>() {
|
|
override fun done(users: List<User_bmob>, e: BmobException?) {
|
|
if (e == null) {
|
|
for (user in users) {
|
|
if (userAccount == user.account && userPassword == user.password) {
|
|
saveUserInfo(user.objectId)
|
|
startActivity(Intent(this@LoginActivity, MainActivity::class.java))
|
|
Toast.makeText(this@LoginActivity, "登陆成功!", Toast.LENGTH_SHORT).show()
|
|
finish()
|
|
return
|
|
}
|
|
}
|
|
Toast.makeText(this@LoginActivity, "账号或密码不正确", Toast.LENGTH_SHORT).show()
|
|
} else {
|
|
Toast.makeText(this@LoginActivity, "登录时发生错误: ${e.message}", Toast.LENGTH_SHORT).show()
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
private fun saveUserInfo(userId: String) {
|
|
getSharedPreferences("userinf", Context.MODE_PRIVATE).edit()
|
|
.putString("user_id", userId)
|
|
.apply()
|
|
}
|
|
} |