Commit 18325979 authored by jiangjiantao's avatar jiangjiantao

dev

parent 3f1608b2
......@@ -2,6 +2,6 @@ package com.miya.fastcashier.beans
import com.google.gson.annotations.SerializedName
data class LoginRequest(@SerializedName("userName") val userName: String,
data class LoginRequest(@SerializedName("username") val username: String,
@SerializedName("password") val password: String,
@SerializedName("serialNo") val serialNo: String)
package com.miya.fastcashier.service
package com.miya.fastcashier.net
data class BaseResult<T>(val code: String, val msg: String, val data: T)
\ No newline at end of file
package com.miya.fastcashier.service
import android.util.Log
import com.miya.fastcashier.beans.SelfCashierAccountInfo
import com.miya.fastcashier.beans.LoginRequest;
import com.miya.fastcashier.net.BaseResult
import com.miya.fastcashier.net.MiyaHttpLoggingInterceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
......@@ -24,10 +27,12 @@ interface LoginService {
fun getApi(): LoginService {
if (null == service) {
val httpLoggingInterceptor =
HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BASIC }
MiyaHttpLoggingInterceptor {
Log.e("####", it)
}.apply { level = MiyaHttpLoggingInterceptor.Level.BODY }
val client = OkHttpClient.Builder()
// .addInterceptor(httpLoggingInterceptor)
.addInterceptor(httpLoggingInterceptor)
.build()
val retrofit = Retrofit.Builder()
......@@ -41,6 +46,7 @@ interface LoginService {
return service!!
}
}
}
\ No newline at end of file
......@@ -36,12 +36,16 @@ class LoginActivity : AppCompatActivity() {
.get(LoginViewModel::class.java)
loginViewModel.loginLiveData.observe(this) { result ->
val selfCashierAccountInfo = result.getOrNull()
if (null == selfCashierAccountInfo) {
Log.e("######","失败")
result.getOrElse {
Toast.makeText(this,it.message, Toast.LENGTH_SHORT).show()
return@observe
}
Log.e("######",JSON.toJSONString(selfCashierAccountInfo))
result.getOrNull().also {
Toast.makeText(this,JSON.toJSONString(it), Toast.LENGTH_SHORT).show()
}
}
binding.login.setOnClickListener{
......
package com.miya.fastcashier.viewmodel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.miya.fastcashier.beans.LoginRequest
import com.miya.fastcashier.beans.SelfCashierAccountInfo
import com.miya.fastcashier.service.BaseResult
import com.miya.fastcashier.net.BaseResult
import com.miya.fastcashier.service.LoginService
import kotlinx.coroutines.launch
import java.lang.RuntimeException
class LoginViewModel : ViewModel() {
val loginLiveData: MutableLiveData<Result<BaseResult<SelfCashierAccountInfo>>> = MutableLiveData()
val loginLiveData: MutableLiveData<Result<SelfCashierAccountInfo>> = MutableLiveData()
fun login(userName:String,passWord:String) {
viewModelScope.launch {
val result = try {
// 网络返回成功
Result.success(LoginService.getApi().login(LoginRequest(userName,passWord,"")))
Result.success(errorHandle(LoginService.getApi().login(LoginRequest(userName,passWord,""))))
} catch (e: Exception) {
// 网络返回失败
Result.failure(e)
}
// 注意这里是主线程,直接用setValue()即可
......@@ -29,4 +26,11 @@ class LoginViewModel : ViewModel() {
}
}
private fun <T> errorHandle(result: BaseResult<T>): T {
if(result.code != "200"){
throw RuntimeException(result.msg)
}
return result.data;
}
}
\ No newline at end of file
......@@ -19,6 +19,7 @@
android:layout_marginTop="96dp"
android:layout_marginEnd="24dp"
android:hint="@string/prompt_email"
android:text="miyago"
android:inputType="textEmailAddress"
android:selectAllOnFocus="true"
app:layout_constraintEnd_toEndOf="parent"
......@@ -34,6 +35,7 @@
android:layout_marginEnd="24dp"
android:hint="@string/prompt_password"
android:imeActionLabel="@string/action_sign_in_short"
android:text="123456"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:selectAllOnFocus="true"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment