Commit 18325979 authored by jiangjiantao's avatar jiangjiantao

dev

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