Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
MiYaFastCashier
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
jiangjiantao
MiYaFastCashier
Commits
18325979
Commit
18325979
authored
Jan 12, 2022
by
jiangjiantao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dev
parent
3f1608b2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
330 additions
and
14 deletions
+330
-14
LoginRequest.kt
app/src/main/java/com/miya/fastcashier/beans/LoginRequest.kt
+1
-1
BaseResult.kt
app/src/main/java/com/miya/fastcashier/net/BaseResult.kt
+1
-1
MiyaHttpLoggingInterceptor.java
.../com/miya/fastcashier/net/MiyaHttpLoggingInterceptor.java
+300
-0
LoginService.kt
...rc/main/java/com/miya/fastcashier/service/LoginService.kt
+8
-2
LoginActivity.kt
.../main/java/com/miya/fastcashier/ui/login/LoginActivity.kt
+8
-4
LoginViewModel.kt
...ain/java/com/miya/fastcashier/viewmodel/LoginViewModel.kt
+10
-6
activity_login.xml
app/src/main/res/layout/activity_login.xml
+2
-0
No files found.
app/src/main/java/com/miya/fastcashier/beans/LoginRequest.kt
View file @
18325979
...
@@ -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
(
"user
Name"
)
val
userN
ame
:
String
,
data class
LoginRequest
(
@SerializedName
(
"user
name"
)
val
usern
ame
:
String
,
@SerializedName
(
"password"
)
val
password
:
String
,
@SerializedName
(
"password"
)
val
password
:
String
,
@SerializedName
(
"serialNo"
)
val
serialNo
:
String
)
@SerializedName
(
"serialNo"
)
val
serialNo
:
String
)
app/src/main/java/com/miya/fastcashier/
service
/BaseResult.kt
→
app/src/main/java/com/miya/fastcashier/
net
/BaseResult.kt
View file @
18325979
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
app/src/main/java/com/miya/fastcashier/net/MiyaHttpLoggingInterceptor.java
0 → 100644
View file @
18325979
This diff is collapsed.
Click to expand it.
app/src/main/java/com/miya/fastcashier/service/LoginService.kt
View file @
18325979
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
app/src/main/java/com/miya/fastcashier/ui/login/LoginActivity.kt
View file @
18325979
...
@@ -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
{
...
...
app/src/main/java/com/miya/fastcashier/viewmodel/LoginViewModel.kt
View file @
18325979
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
app/src/main/res/layout/activity_login.xml
View file @
18325979
...
@@ -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"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment