Commit 2ae50809 authored by 赵鹏翔's avatar 赵鹏翔

完成日志上传,配置请求拦截签名,配置环境区分

parent e30804f8
......@@ -28,6 +28,7 @@ android {
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildConfigField 'String', "CHANNEL", "\"$CHANNEL\""
buildConfigField "Boolean", "ISTEST", ISTEST.toString()
buildConfigField "String", "appType", "\"mpos\""
}
......
......@@ -13,12 +13,18 @@ import android.widget.TextView;
import android.widget.Toast;
import com.blankj.utilcode.util.EncodeUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.miya.fastcashier.BuildConfig;
import com.miya.fastcashier.R;
import com.miya.fastcashier.beans.SelfCashierTerminalConfig;
import com.miya.fastcashier.net.ApiConfig;
import com.miya.fastcashier.net.ApiRequest;
import com.miya.fastcashier.net.ApiService;
import com.miya.fastcashier.net.CommonCallback;
import com.miya.fastcashier.service.AccountService;
import com.miya.fastcashier.ui.BaseActivity;
import com.miya.fastcashier.utils.LogFileUtils;
import java.io.File;
import java.util.ArrayList;
......@@ -27,6 +33,10 @@ import java.util.List;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import es.dmoral.toasty.Toasty;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
public class LFilePickerActivity extends BaseActivity {
......@@ -254,50 +264,61 @@ public class LFilePickerActivity extends BaseActivity {
}
public void uploadFile(String path) {
// LogFileUtils.INSTANCE.setProhibitWrite(true);
// String equType = getEquType();
//
// File file = new File(path);
// RequestBody requestBody = RequestBody.create(MediaType.parse(""), file);
// MultipartBody.Part part = MultipartBody.Part.createFormData("logFile", file.getName(), requestBody);
// String ip = DataSourceIpUtils.getDataSourceIp(selfCashierTerminalConfig, NetWorkUrl.UPLOAD_LOG_FILE + "?equType=" + equType);
// if (!TextUtils.isEmpty(selfCashierTerminalConfig.getScoRuntimeConfig().getPlatformUrl())) {
// ip = selfCashierTerminalConfig.getScoRuntimeConfig().getPlatformUrl() + NetWorkUrl.UPLOAD_LOG_FILE;
// }
//
// LogUtils.e("ip = " + ip);
//
// //防止文件过大 导致OOM 30M
LogFileUtils.INSTANCE.setProhibitWrite(true);
String equType = getEquType();
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);
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){
// if (size > 34307335) {
// ToastUtils.showLong("日志文件过大");
// LogFileUtils.INSTANCE.setProhibitWrite(false);
// hidProgressDialog();
// return;
// }
//
// tryGenQrCodeMsg(file.getName());
//
// ApiClient.get().getFileApi().uploadFile(ip, part)
// .subscribeOn(RxExecutorsUtils.getScheduler())
// .observeOn(AndroidSchedulers.mainThread())
// .subscribe(new Action1<HttpResult>() {
// @Override
// public void call(HttpResult s) {
// hidProgressDialog();
// LogFileUtils.INSTANCE.setProhibitWrite(false);
// successToast = Toasty.success(LFilePickerActivity.this, "文件上传成功!");
// successToast.show();
// showQrImageIfNeeded();
// }
// }, new Action1<Throwable>() {
// @Override
// public void call(Throwable throwable) {
// hidProgressDialog();
// LogFileUtils.INSTANCE.setProhibitWrite(false);
// Toasty.error(LFilePickerActivity.this, "文件上传失败!").show();
// }
// });
tryGenQrCodeMsg(file.getName());
ApiRequest.Companion.getInstance().uploadFile(ip, part,
new CommonCallback<Object>() {
@Override
public void onSuccess(Object data) {
hidProgressDialog();
LogFileUtils.INSTANCE.setProhibitWrite(false);
successToast = Toasty.success(LFilePickerActivity.this, "文件上传成功!");
successToast.show();
showQrImageIfNeeded();
}
@Override
public void onFailure(int errorType, String errorCode, String errorMessage) {
hidProgressDialog();
LogFileUtils.INSTANCE.setProhibitWrite(false);
Toasty.error(LFilePickerActivity.this, "文件上传失败!").show();
}
});
}
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
......@@ -310,6 +331,7 @@ public class LFilePickerActivity extends BaseActivity {
//支付宝
equType = "ALIPAY_EQU";
} else if (BuildConfig.appType.equals("mpos")) {
//mpos
equType = "MPOS_EQU";
} else if (BuildConfig.appType.equals("miya")) {
//强制进入米雅页面
......@@ -324,7 +346,7 @@ public class LFilePickerActivity extends BaseActivity {
String hhMerchant = huihuaConfig.getHhMerchant();
String storeId = huihuaConfig.getStoreId();
String username = AccountService.INSTANCE.getUserName();
String qrMsg = String.format("%s/%s/%s/%s/%s", hhMerchant, storeId, username, getUploadEq(),fileName);
String qrMsg = String.format("%s/%s/%s/%s/%s", hhMerchant, storeId, username, getUploadEq(), fileName);
mQrCodeEncodeMsg = EncodeUtils.base64Encode2String(qrMsg.getBytes());
}
}
......@@ -346,7 +368,7 @@ public class LFilePickerActivity extends BaseActivity {
private String getUploadEq() {
String eq = "";
String equType = getEquType();
switch (equType){
switch (equType) {
case "WECHAT_EQU":
eq = "w_eq";
break;
......
......@@ -11,7 +11,7 @@ import com.miya.fastcashier.BuildConfig;
import com.miya.fastcashier.R;
import com.miya.fastcashier.beans.SelfCashierAccountInfo;
import com.miya.fastcashier.databinding.DialogSystemParameterBinding;
import com.miya.fastcashier.net.ApiRequest;
import com.miya.fastcashier.net.ApiConfig;
import com.miya.fastcashier.service.AccountService;
import com.miya.fastcashier.utils.BaseFunctionKt;
import com.miya.print.PrinterManager;
......@@ -44,7 +44,7 @@ public class SystemParameterDialog extends Dialog {
setInfo(viewBinding.tvCashier, accountInfo.getShopInfo().getOperatorId());
setInfo(viewBinding.tvVersionType, "fastCashier_" + BuildConfig.appType);
setInfo(viewBinding.tvEquipment, "sunmi_v2pro");
setInfo(viewBinding.tvServerUrl, ApiRequest.Companion.getBaseUrl());
setInfo(viewBinding.tvServerUrl, ApiConfig.getBaseUrl());
setInfo(viewBinding.tvWifiName, BaseFunctionKt.getWifyName(getContext()));
setInfo(viewBinding.tvNetIp, BaseFunctionKt.getNetIp(getContext()) == null ? "未知" : BaseFunctionKt.getNetIp(getContext()));
setInfo(viewBinding.tvPrintType, PrinterManager.getInstance().getPrinter() == null ?
......
package com.miya.fastcashier.net
import com.miya.fastcashier.BuildConfig
import com.miya.fastcashier.net.ApiConfig
object ApiConfig {
private const val BASE_URL = "https://hhms.miyapay.com/"
private const val BASE_URL_4_TEST = ""
@JvmStatic
val baseUrl: String
get() = if (BuildConfig.ISTEST) {
BASE_URL_4_TEST
} else BASE_URL
}
\ No newline at end of file
......@@ -3,22 +3,16 @@ package com.miya.fastcashier.net
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 com.miya.fastcashier.utils.manage.AccountPasswordManageKit
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Body
import retrofit2.http.POST
class ApiRequest private constructor() {
companion object {
private const val BASE_URL = "https://hhms.miyapay.com/"
private var apiService: ApiService? = null
private var sInstance: ApiRequest? = null
......@@ -39,10 +33,11 @@ class ApiRequest private constructor() {
val client = OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.addInterceptor(RequestSignInterceptor())
.build()
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.baseUrl(ApiConfig.baseUrl)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
......@@ -52,10 +47,6 @@ class ApiRequest private constructor() {
return apiService!!
}
fun getBaseUrl(): String {
return BASE_URL
}
}
fun login(
......@@ -64,4 +55,12 @@ class ApiRequest private constructor() {
) {
getApiService().login(loginRequest).enqueue(commonCallback)
}
fun uploadFile(
url: String,
part: MultipartBody.Part,
commonCallback: CommonCallback<Object>
) {
getApiService().uploadFile(url, part).enqueue(commonCallback)
}
}
\ No newline at end of file
......@@ -2,9 +2,9 @@ package com.miya.fastcashier.net
import com.miya.fastcashier.beans.LoginRequest
import com.miya.fastcashier.beans.SelfCashierAccountInfo
import okhttp3.MultipartBody
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.POST
import retrofit2.http.*
interface ApiService {
......@@ -12,9 +12,29 @@ interface ApiService {
companion object {
const val LOGIN: String = "verify/auth/token"
/**
* 上传日志文件
*/
var UPLOAD_LOG_FILE: String = "verify/r-log/upload-log-file"
fun wrapUrl(suffix: String): String {
return ApiConfig.baseUrl.plus(suffix)
}
}
@POST(LOGIN)
fun login(@Body loginRequestCall: LoginRequest): Call<BaseResult<SelfCashierAccountInfo>>
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>>
}
\ No newline at end of file
......@@ -3,7 +3,7 @@ package com.miya.fastcashier.net
import java.io.Serializable
data class BaseResult<T>(val code: String, val msg: String, val data: T) : Serializable {
data class BaseResponse<T>(val code: String, val msg: String, val data: T) : Serializable {
fun isSuccess(): Boolean {
return code.equals("200")
......
......@@ -19,7 +19,7 @@ import retrofit2.Response;
* 修改人:
* 修改时间:
*/
public abstract class CommonCallback<T> implements Callback<BaseResult<T>> {
public abstract class CommonCallback<T> implements Callback<BaseResponse<T>> {
private static final String RESPONSE_MSG_UNKNOW = "未知异常";
......@@ -38,7 +38,7 @@ public abstract class CommonCallback<T> implements Callback<BaseResult<T>> {
}
@Override
public void onResponse(Call<BaseResult<T>> call, Response<BaseResult<T>> response) {
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)) {
......@@ -60,7 +60,7 @@ public abstract class CommonCallback<T> implements Callback<BaseResult<T>> {
}
@Override
public void onFailure(Call<BaseResult<T>> call, Throwable t) {
public void onFailure(Call<BaseResponse<T>> call, Throwable t) {
String temp = t.getMessage();
Log.e("#### HttpLog", "请求失败:" + temp);
String errorMessage;
......
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);
}
}
......@@ -22,4 +22,6 @@ android.jetifier.blacklist=miya-print
android.useDeprecatedNdk=true
#区别品牌
CHANNEL=converse
\ No newline at end of file
CHANNEL=converse
#区别环境 true:是测试环境,仅供内部人员测试使用;false:非测试包,供外部人员、渠道推广、应用商店等使用
ISTEST=false
\ No newline at end of file
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