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
10b5b968
Commit
10b5b968
authored
May 16, 2022
by
赵鹏翔
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
网络请求相关代码整理
parent
4e50bc6e
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
175 additions
and
164 deletions
+175
-164
LFilePickerActivity.java
...in/java/com/miya/fastcashier/log/LFilePickerActivity.java
+3
-27
ApiConfig.kt
app/src/main/java/com/miya/fastcashier/net/ApiConfig.kt
+20
-1
ApiRequest.kt
app/src/main/java/com/miya/fastcashier/net/ApiRequest.kt
+1
-1
ApiService.kt
app/src/main/java/com/miya/fastcashier/net/ApiService.kt
+7
-9
BaseResponse.kt
app/src/main/java/com/miya/fastcashier/net/BaseResponse.kt
+3
-1
CommonCallback.java
...rc/main/java/com/miya/fastcashier/net/CommonCallback.java
+0
-85
CommonCallback.kt
app/src/main/java/com/miya/fastcashier/net/CommonCallback.kt
+100
-0
RequestSignInterceptor.java
...java/com/miya/fastcashier/net/RequestSignInterceptor.java
+0
-37
RequestSignInterceptor.kt
...n/java/com/miya/fastcashier/net/RequestSignInterceptor.kt
+39
-0
LoginViewModel.kt
...ain/java/com/miya/fastcashier/viewmodel/LoginViewModel.kt
+2
-3
No files found.
app/src/main/java/com/miya/fastcashier/log/LFilePickerActivity.java
View file @
10b5b968
...
...
@@ -59,7 +59,6 @@ public class LFilePickerActivity extends BaseActivity {
private
List
<
File
>
mListFiles
;
private
ArrayList
<
String
>
mListNumbers
=
new
ArrayList
<
String
>();
//存放选中条目的数据地址
private
String
mQrCodeEncodeMsg
;
private
byte
[]
dialogLock
=
new
byte
[
0
];
@Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
...
...
@@ -270,30 +269,18 @@ public class LFilePickerActivity extends BaseActivity {
File
file
=
new
File
(
path
);
RequestBody
requestBody
=
RequestBody
.
create
(
MediaType
.
parse
(
""
),
file
);
MultipartBody
.
Part
part
=
MultipartBody
.
Part
.
createFormData
(
"logFile"
,
file
.
getName
(),
requestBody
);
String
ip
=
getDataSourceIp
(
selfCashierTerminalConfig
,
ApiService
.
Companion
.
getUPLOAD_LOG_FILE
()
+
"?equType="
+
equType
);
String
ip
=
ApiConfig
.
INSTANCE
.
getDataSourceIp
(
selfCashierTerminalConfig
,
equType
);
if
(!
TextUtils
.
isEmpty
(
selfCashierTerminalConfig
.
getScoRuntimeConfig
().
getPlatformUrl
()))
{
ip
=
selfCashierTerminalConfig
.
getScoRuntimeConfig
().
getPlatformUrl
()
+
ApiService
.
Companion
.
getUPLOAD_LOG_FILE
();
}
LogUtils
.
e
(
"ip = "
+
ip
);
//防止文件过大 导致OOM 30M
// long size = file.length();
// if (size > 34307335) {
// ToastUtils.showLong("日志文件过大");
// LogFileUtils.INSTANCE.setProhibitWrite(false);
// hidProgressDialog();
// return;
// }
tryGenQrCodeMsg
(
file
.
getName
());
ApiRequest
.
Companion
.
getInstance
().
uploadFile
(
ip
,
part
,
new
CommonCallback
<
Object
>()
{
new
CommonCallback
<
String
>()
{
@Override
public
void
onSuccess
(
Object
data
)
{
public
void
onSuccess
(
String
data
)
{
hidProgressDialog
();
LogFileUtils
.
INSTANCE
.
setProhibitWrite
(
false
);
successToast
=
Toasty
.
success
(
LFilePickerActivity
.
this
,
"文件上传成功!"
);
...
...
@@ -310,17 +297,6 @@ public class LFilePickerActivity extends BaseActivity {
});
}
public
static
String
getDataSourceIp
(
@NonNull
SelfCashierTerminalConfig
selfCashierTerminalConfig
,
@NonNull
String
path
)
{
if
(
selfCashierTerminalConfig
==
null
||
selfCashierTerminalConfig
.
getScoRuntimeConfig
()
==
null
)
{
throw
new
RuntimeException
(
"DataSourceIpUtils SelfCashierTerminalConfig is null"
);
}
return
TextUtils
.
isEmpty
(
selfCashierTerminalConfig
.
getScoRuntimeConfig
().
getErpIp
())
?
ApiConfig
.
getBaseUrl
()
+
path
:
selfCashierTerminalConfig
.
getScoRuntimeConfig
().
getErpIp
()
+
path
;
}
@NonNull
private
String
getEquType
()
{
String
equType
=
""
;
...
...
app/src/main/java/com/miya/fastcashier/net/ApiConfig.kt
View file @
10b5b968
package
com.miya.fastcashier.net
import
android.text.TextUtils
import
com.miya.fastcashier.BuildConfig
import
com.miya.fastcashier.
net.Api
Config
import
com.miya.fastcashier.
beans.SelfCashierTerminal
Config
object
ApiConfig
{
private
const
val
BASE_URL
=
"https://hhms.miyapay.com/"
...
...
@@ -12,4 +13,22 @@ object ApiConfig {
get
()
=
if
(
BuildConfig
.
ISTEST
)
{
BASE_URL_4_TEST
}
else
BASE_URL
fun
getAuthorization
(
token
:
String
):
String
{
return
"bearer $token"
}
fun
getDataSourceIp
(
selfCashierTerminalConfig
:
SelfCashierTerminalConfig
,
equType
:
String
):
String
?
{
if
(
selfCashierTerminalConfig
?.
scoRuntimeConfig
==
null
)
{
throw
RuntimeException
(
"DataSourceIpUtils SelfCashierTerminalConfig is null"
)
}
val
path
=
"${ApiService.UPLOAD_LOG_FILE}?equType=$equType"
return
if
(
TextUtils
.
isEmpty
(
selfCashierTerminalConfig
.
scoRuntimeConfig
.
erpIp
))
(
baseUrl
+
path
)
else
selfCashierTerminalConfig
.
scoRuntimeConfig
.
erpIp
+
path
}
}
\ No newline at end of file
app/src/main/java/com/miya/fastcashier/net/ApiRequest.kt
View file @
10b5b968
...
...
@@ -59,7 +59,7 @@ class ApiRequest private constructor() {
fun
uploadFile
(
url
:
String
,
part
:
MultipartBody
.
Part
,
commonCallback
:
CommonCallback
<
Object
>
commonCallback
:
CommonCallback
<
String
>
)
{
getApiService
().
uploadFile
(
url
,
part
).
enqueue
(
commonCallback
)
}
...
...
app/src/main/java/com/miya/fastcashier/net/ApiService.kt
View file @
10b5b968
...
...
@@ -6,11 +6,16 @@ import okhttp3.MultipartBody
import
retrofit2.Call
import
retrofit2.http.*
/**
* 请求地址配置
*/
interface
ApiService
{
companion
object
{
/**
* 登录
*/
const
val
LOGIN
:
String
=
"verify/auth/token"
/**
...
...
@@ -24,17 +29,10 @@ interface ApiService {
}
}
@POST
(
LOGIN
)
fun
login
(
@Body
loginRequestCall
:
LoginRequest
):
Call
<
BaseResponse
<
SelfCashierAccountInfo
>>
/**
* 上传日志文件
*
* @param file
* @return
*/
@Multipart
@POST
fun
uploadFile
(
@Url
url
:
String
?,
@Part
part
:
MultipartBody
.
Part
):
Call
<
BaseResponse
<
Object
>>
fun
uploadFile
(
@Url
url
:
String
?,
@Part
part
:
MultipartBody
.
Part
):
Call
<
BaseResponse
<
String
>>
}
\ No newline at end of file
app/src/main/java/com/miya/fastcashier/net/BaseResponse.kt
View file @
10b5b968
...
...
@@ -2,7 +2,9 @@ package com.miya.fastcashier.net
import
java.io.Serializable
/**
* 接口请求后返回的基础response
*/
data class
BaseResponse
<
T
>(
val
code
:
String
,
val
msg
:
String
,
val
data
:
T
)
:
Serializable
{
fun
isSuccess
():
Boolean
{
...
...
app/src/main/java/com/miya/fastcashier/net/CommonCallback.java
deleted
100644 → 0
View file @
4e50bc6e
package
com
.
miya
.
fastcashier
.
net
;
import
android.util.Log
;
import
com.miya.fastcashier.BaseApplication
;
import
com.miya.fastcashier.R
;
import
com.miya.fastcashier.utils.BaseFunctionKt
;
import
java.net.SocketTimeoutException
;
import
retrofit2.Call
;
import
retrofit2.Callback
;
import
retrofit2.Response
;
/**
* 类描述:通用请求返回
* 创建人:zpxiang
* 创建时间:
* 修改人:
* 修改时间:
*/
public
abstract
class
CommonCallback
<
T
>
implements
Callback
<
BaseResponse
<
T
>>
{
private
static
final
String
RESPONSE_MSG_UNKNOW
=
"未知异常"
;
private
static
final
String
CALLBACK_SUCCEED_CODE
=
"200"
;
interface
ErrorType
{
int
ERROR_TYPE_CUSTOM
=
0
;
int
ERROR_TYPE_HTTP
=
1
;
int
ERROR_TYPE_EXCEPTION
=
2
;
}
interface
ResponseCode
{
String
RESPONSE_CODE_FAILED
=
String
.
valueOf
(-
1
);
// 返回数据失败
String
RESPONSE_CODE_NETWORK_DISCONNECT
=
String
.
valueOf
(-
2
);
// 无网络
String
RESPONSE_CODE_NETWORK_TIMEOUT
=
String
.
valueOf
(-
3
);
// 超时
}
@Override
public
void
onResponse
(
Call
<
BaseResponse
<
T
>>
call
,
Response
<
BaseResponse
<
T
>>
response
)
{
if
(
response
.
isSuccessful
()
&&
response
.
body
()
!=
null
)
{
String
responseCode
=
response
.
body
().
getCode
();
// 业务自定义Code
if
(
CALLBACK_SUCCEED_CODE
.
equals
(
responseCode
))
{
onSuccess
(
response
.
body
().
getData
());
}
else
{
onFailure
(
ErrorType
.
ERROR_TYPE_CUSTOM
,
responseCode
,
response
.
body
().
getError
());
}
}
else
if
(
response
.
errorBody
()
!=
null
)
{
onFailure
(
ErrorType
.
ERROR_TYPE_HTTP
,
String
.
valueOf
(
response
.
code
()),
response
.
errorBody
().
toString
());
}
else
{
onFailure
(
ErrorType
.
ERROR_TYPE_HTTP
,
ResponseCode
.
RESPONSE_CODE_FAILED
,
RESPONSE_MSG_UNKNOW
);
// 理论上不会存在此种情况
}
}
@Override
public
void
onFailure
(
Call
<
BaseResponse
<
T
>>
call
,
Throwable
t
)
{
String
temp
=
t
.
getMessage
();
Log
.
e
(
"#### HttpLog"
,
"请求失败:"
+
temp
);
String
errorMessage
;
if
(!
BaseFunctionKt
.
isNetworkConnected
(
BaseApplication
.
Companion
.
getApplication
()))
{
errorMessage
=
BaseApplication
.
Companion
.
getApplication
().
getString
(
R
.
string
.
common_prompt_network_error
);
onFailure
(
ErrorType
.
ERROR_TYPE_EXCEPTION
,
ResponseCode
.
RESPONSE_CODE_NETWORK_DISCONNECT
,
errorMessage
);
return
;
}
if
(
t
instanceof
SocketTimeoutException
)
{
errorMessage
=
BaseApplication
.
Companion
.
getApplication
().
getString
(
R
.
string
.
common_prompt_network_timeout
);
onFailure
(
ErrorType
.
ERROR_TYPE_EXCEPTION
,
ResponseCode
.
RESPONSE_CODE_NETWORK_TIMEOUT
,
errorMessage
);
}
else
{
errorMessage
=
BaseApplication
.
Companion
.
getApplication
().
getString
(
R
.
string
.
common_prompt_data_error
);
onFailure
(
ErrorType
.
ERROR_TYPE_EXCEPTION
,
ResponseCode
.
RESPONSE_CODE_FAILED
,
errorMessage
);
}
}
public
abstract
void
onSuccess
(
T
data
);
public
abstract
void
onFailure
(
int
errorType
,
String
errorCode
,
String
errorMessage
);
}
app/src/main/java/com/miya/fastcashier/net/CommonCallback.kt
0 → 100644
View file @
10b5b968
package
com.miya.fastcashier.net
import
android.util.Log
import
com.miya.fastcashier.BaseApplication.Companion.getApplication
import
com.miya.fastcashier.R
import
com.miya.fastcashier.utils.isNetworkConnected
import
retrofit2.Call
import
retrofit2.Callback
import
retrofit2.Response
import
java.net.SocketTimeoutException
/**
* 类描述:通用请求返回
* 创建人:zpxiang
* 创建时间:
* 修改人:
* 修改时间:
*/
abstract
class
CommonCallback
<
T
>
:
Callback
<
BaseResponse
<
T
>?>
{
companion
object
{
private
const
val
RESPONSE_MSG_UNKNOW
=
"未知异常"
private
const
val
CALLBACK_SUCCEED_CODE
=
"200"
}
internal
interface
ErrorType
{
companion
object
{
const
val
ERROR_TYPE_CUSTOM
=
0
const
val
ERROR_TYPE_HTTP
=
1
const
val
ERROR_TYPE_EXCEPTION
=
2
}
}
internal
interface
ResponseCode
{
companion
object
{
val
RESPONSE_CODE_FAILED
:
String
=
"-1"
// 返回数据失败
val
RESPONSE_CODE_NETWORK_DISCONNECT
:
String
=
"-2"
// 无网络
val
RESPONSE_CODE_NETWORK_TIMEOUT
:
String
=
"-3"
// 超时
}
}
override
fun
onResponse
(
call
:
Call
<
BaseResponse
<
T
>?>,
response
:
Response
<
BaseResponse
<
T
>?>)
{
if
(
response
.
isSuccessful
&&
response
.
body
()
!=
null
)
{
val
responseCode
=
response
.
body
()
!!
.
code
// 业务自定义Code
if
(
CALLBACK_SUCCEED_CODE
==
responseCode
)
{
onSuccess
(
response
.
body
()
!!
.
data
)
}
else
{
onFailure
(
ErrorType
.
ERROR_TYPE_CUSTOM
,
responseCode
,
response
.
body
()
!!
.
getError
()
)
}
}
else
if
(
response
.
errorBody
()
!=
null
)
{
onFailure
(
ErrorType
.
ERROR_TYPE_HTTP
,
response
.
code
().
toString
(),
response
.
errorBody
().
toString
()
)
}
else
{
onFailure
(
// 理论上不会存在此种情况
ErrorType
.
ERROR_TYPE_HTTP
,
ResponseCode
.
RESPONSE_CODE_FAILED
,
RESPONSE_MSG_UNKNOW
)
}
}
override
fun
onFailure
(
call
:
Call
<
BaseResponse
<
T
>?>,
t
:
Throwable
)
{
val
temp
=
t
.
message
Log
.
e
(
"#### HttpLog"
,
"请求失败:$temp"
)
val
errorMessage
:
String
if
(!
isNetworkConnected
(
getApplication
()))
{
errorMessage
=
getApplication
().
getString
(
R
.
string
.
common_prompt_network_error
)
onFailure
(
ErrorType
.
ERROR_TYPE_EXCEPTION
,
ResponseCode
.
RESPONSE_CODE_NETWORK_DISCONNECT
,
errorMessage
)
return
}
if
(
t
is
SocketTimeoutException
)
{
errorMessage
=
getApplication
().
getString
(
R
.
string
.
common_prompt_network_timeout
)
onFailure
(
ErrorType
.
ERROR_TYPE_EXCEPTION
,
ResponseCode
.
RESPONSE_CODE_NETWORK_TIMEOUT
,
errorMessage
)
}
else
{
errorMessage
=
getApplication
().
getString
(
R
.
string
.
common_prompt_data_error
)
onFailure
(
ErrorType
.
ERROR_TYPE_EXCEPTION
,
ResponseCode
.
RESPONSE_CODE_FAILED
,
errorMessage
)
}
}
abstract
fun
onSuccess
(
data
:
T
)
abstract
fun
onFailure
(
errorType
:
Int
,
errorCode
:
String
?,
errorMessage
:
String
?)
}
\ No newline at end of file
app/src/main/java/com/miya/fastcashier/net/RequestSignInterceptor.java
deleted
100644 → 0
View file @
4e50bc6e
package
com
.
miya
.
fastcashier
.
net
;
import
com.miya.fastcashier.beans.SelfCashierAccountInfo
;
import
com.miya.fastcashier.service.AccountService
;
import
java.io.IOException
;
import
okhttp3.Interceptor
;
import
okhttp3.Request
;
import
okhttp3.Response
;
/**
* 签名拦截器
* 添加请求头校验 AUTHORIZATION
*/
public
class
RequestSignInterceptor
implements
Interceptor
{
private
static
final
String
AUTHORIZATION
=
"Authorization"
;
@Override
public
Response
intercept
(
Chain
chain
)
throws
IOException
{
Request
original
=
chain
.
request
();
String
requestUrl
=
original
.
url
().
toString
();
Request
.
Builder
requestBuilder
=
original
.
newBuilder
();
SelfCashierAccountInfo
accountInfo
=
AccountService
.
INSTANCE
.
getAccountInfo
();
if
(
accountInfo
!=
null
&&
accountInfo
.
getAccessToken
()
!=
null
)
{
String
loginUrl
=
ApiService
.
Companion
.
wrapUrl
(
ApiService
.
LOGIN
);
if
(!
requestUrl
.
equals
(
loginUrl
))
{
requestBuilder
.
addHeader
(
AUTHORIZATION
,
"bearer "
+
accountInfo
.
getAccessToken
().
getAccessToken
());
}
}
Request
request
=
requestBuilder
.
build
();
return
chain
.
proceed
(
request
);
}
}
app/src/main/java/com/miya/fastcashier/net/RequestSignInterceptor.kt
0 → 100644
View file @
10b5b968
package
com.miya.fastcashier.net
import
com.miya.fastcashier.net.ApiConfig.getAuthorization
import
com.miya.fastcashier.net.ApiService.Companion.wrapUrl
import
com.miya.fastcashier.service.AccountService.getAccountInfo
import
okhttp3.Interceptor
import
okhttp3.Request
import
okhttp3.Response
import
java.io.IOException
/**
* 签名拦截器
* 添加请求头校验 AUTHORIZATION
*/
class
RequestSignInterceptor
:
Interceptor
{
companion
object
{
private
const
val
AUTHORIZATION
=
"Authorization"
}
@Throws
(
IOException
::
class
)
override
fun
intercept
(
chain
:
Interceptor
.
Chain
):
Response
{
val
original
:
Request
=
chain
.
request
()
val
requestUrl
=
original
.
url
.
toString
()
val
requestBuilder
:
Request
.
Builder
=
original
.
newBuilder
()
val
accountInfo
=
getAccountInfo
()
if
(
accountInfo
!=
null
&&
accountInfo
.
accessToken
!=
null
)
{
val
loginUrl
=
wrapUrl
(
ApiService
.
LOGIN
)
if
(
requestUrl
!=
loginUrl
)
{
requestBuilder
.
addHeader
(
AUTHORIZATION
,
getAuthorization
(
accountInfo
.
accessToken
.
accessToken
)
)
}
}
val
request
:
Request
=
requestBuilder
.
build
()
return
chain
.
proceed
(
request
)
}
}
\ No newline at end of file
app/src/main/java/com/miya/fastcashier/viewmodel/LoginViewModel.kt
View file @
10b5b968
...
...
@@ -31,12 +31,11 @@ class LoginViewModel : ViewModel() {
override
fun
onFailure
(
errorType
:
Int
,
errorCode
:
String
,
errorMessage
:
String
errorCode
:
String
?
,
errorMessage
:
String
?
)
{
loginLiveData
.
postValue
(
Result
.
failure
(
RuntimeException
(
errorMessage
)))
}
})
}
}
...
...
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