Commit 09ffc425 authored by mengcuiguang2's avatar mengcuiguang2

优化response

parent 95295eb1
......@@ -67,13 +67,9 @@ android {
buildConfigField "String", "MainIp", DEBUG_URL
//混淆
// minifyEnabled false
// zipAlignEnabled false
// shrinkResources false//打开
minifyEnabled true
zipAlignEnabled true
shrinkResources true//打开
minifyEnabled false
zipAlignEnabled false
shrinkResources false//打开
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.debug
......
......@@ -59,9 +59,10 @@
<meta-data
android:name="Mob-AppSecret"
android:value="${SHARE_SECRET}" />
<!--百度地图开放平台 -->
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="SD6luBGzUFSVEZ3n9Xjk3xTsvDFmuRCi" /><!--百度地图开放平台 -->
android:value="SD6luBGzUFSVEZ3n9Xjk3xTsvDFmuRCi" />
<!--百度地图开放平台 service-->
<service android:name="com.baidu.location.f"
android:enabled="true"
......
package com.mints.street.api
import com.mints.street.bean.BaseResponse
import com.mints.street.bean.UserBean
import com.mints.street.netwrok.common.HttpManager
import io.reactivex.Observable
import me.goldze.mvvmhabit.http.BaseResponse
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.POST
......
package me.goldze.mvvmhabit.http;
package com.mints.street.bean;
import androidx.annotation.Nullable;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
/**
* Created by goldze on 2017/5/10.
* 该类仅供参考,实际业务返回的固定字段, 根据需求来定义,
*/
public class BaseResponse<T> {
public class BaseResponse<T> implements Serializable {
private int status;
private String message;
......
......@@ -12,13 +12,12 @@ class UserBean : Serializable {
val consumer: ConsumerBean? = null
inner class ConsumerBean : Serializable {
val head: String = ""
val openid: String = ""
val nickname: String = ""
val mobile: String = ""
val real_name: String = ""
val idcode: String = ""
val pk_id // 用户id
: Long = 0
val isForever: Boolean = false
val expireTime : Long = 0
val pk_id : Long = 0
}
}
......@@ -2,6 +2,7 @@ package com.mints.street.login
import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.mints.street.bean.BaseResponse
import com.mints.street.bean.UserBean
import com.mints.street.common.DeviceInfo
import com.mints.street.manager.UserManager
......@@ -9,7 +10,6 @@ import com.mints.street.model.ApiModel
import com.mints.street.netwrok.base.HttpSubscribeImpl
import com.mints.street.utils.DeviceUuidFactory
import me.goldze.mvvmhabit.base.BaseViewModel
import me.goldze.mvvmhabit.http.BaseResponse
import me.goldze.mvvmhabit.utils.KLog
import me.goldze.mvvmhabit.utils.ToastUtils
import java.util.HashMap
......
......@@ -33,11 +33,6 @@ class UserManager {
*/
private const val MOBILE = "mobile"
/**
* 姓名
*/
private const val REAL_NAME = "realName"
/**
* 微信名称
*/
......@@ -53,6 +48,16 @@ class UserManager {
*/
private const val WX_OPENID = "wx_openid"
/**
* 是否购买会员
*/
private const val IS_FOREVER = "is_forever"
/**
* 会员是否永久
*/
private const val EXPIRETIME = "expiretime"
val INSTANCE: UserManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
UserManager()
}
......@@ -83,11 +88,11 @@ class UserManager {
MMKV.mmkvWithID(MOBILE).encode(MOBILE, mobile)
}
MMKV.mmkvWithID(WX_NAME).encode(WX_NAME, user.nickname.toString())
MMKV.mmkvWithID(WX_HEADER).encode(WX_HEADER, user.head)
MMKV.mmkvWithID(IS_FOREVER).encode(IS_FOREVER, user.isForever)
MMKV.mmkvWithID(EXPIRETIME).encode(EXPIRETIME, user.expireTime)
if (!TextUtils.isEmpty(openid)) {
MMKV.mmkvWithID(WX_OPENID).encode(WX_OPENID, openid)
}
MMKV.mmkvWithID(REAL_NAME).encode(REAL_NAME, user.real_name)
}
}
......@@ -133,23 +138,6 @@ class UserManager {
return kv.decodeString(CODE_ID, "")
}
/**
* 获取真实姓名
*/
fun getRealName(): String {
val kv = MMKV.mmkvWithID(REAL_NAME)
return kv.decodeString(REAL_NAME, "")
}
/**
* 设置真实姓名
*
* @param name
*/
fun setRealName(name: String?) {
val kv = MMKV.mmkvWithID(REAL_NAME)
kv.encode(REAL_NAME, name)
}
fun getWxName(): String {
val kv = MMKV.mmkvWithID(WX_NAME)
......@@ -166,34 +154,26 @@ class UserManager {
return kv.decodeString(WX_OPENID, "")
}
fun setWxName(wxName: String?) {
val kv = MMKV.mmkvWithID(WX_NAME)
kv.encode(WX_NAME, wxName)
fun getIsforever(): Boolean {
val kv = MMKV.mmkvWithID(IS_FOREVER)
return kv.decodeBool(IS_FOREVER, false)
}
fun setWxHeader(wxHeader: String?) {
val kv = MMKV.mmkvWithID(WX_HEADER)
kv.encode(WX_HEADER, wxHeader)
fun getExpiretime(): Long {
val kv = MMKV.mmkvWithID(EXPIRETIME)
return kv.decodeLong(EXPIRETIME, 0)
}
/**
* mobile
*
* @param mobile
*/
fun setMobile(mobile: String?) {
val kv = MMKV.mmkvWithID(MOBILE)
kv.encode(MOBILE, mobile)
}
fun userLogout() {
MMKV.mmkvWithID(USER_ID).remove(USER_ID)
MMKV.mmkvWithID(CODE_ID).remove(CODE_ID)
MMKV.mmkvWithID(TOKEN_ID).remove(TOKEN_ID)
MMKV.mmkvWithID(REAL_NAME).remove(REAL_NAME)
MMKV.mmkvWithID(WX_OPENID).remove(WX_OPENID)
MMKV.mmkvWithID(WX_NAME).remove(WX_NAME)
MMKV.mmkvWithID(WX_HEADER).remove(WX_HEADER)
MMKV.mmkvWithID(IS_FOREVER).remove(IS_FOREVER)
MMKV.mmkvWithID(EXPIRETIME).remove(EXPIRETIME)
}
}
\ No newline at end of file
package com.mints.street.model
import com.mints.street.api.MainApi
import com.mints.street.bean.BaseResponse
import com.mints.street.bean.UserBean
import com.mints.street.netwrok.common.HttpManager
import com.trello.rxlifecycle2.LifecycleProvider
import io.reactivex.Observable
import me.goldze.mvvmhabit.http.BaseResponse
import retrofit2.Response
/**
......
package com.mints.street.netwrok.base
import com.mints.street.bean.BaseResponse
import me.goldze.mvvmhabit._enum.ViewStatus
import me.goldze.mvvmhabit.base.BaseViewModel
import me.goldze.mvvmhabit.http.BaseResponse
import me.goldze.mvvmhabit.http.ExceptionHandle
import me.goldze.mvvmhabit.http.ResponseThrowable
import me.goldze.mvvmhabit.utils.ToastUtils
......
package com.mints.street.netwrok.base;
import com.mints.street.bean.BaseResponse;
import io.reactivex.disposables.Disposable;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.http.ResponseThrowable;
import retrofit2.Response;
......
package com.mints.street.netwrok.base
import com.mints.street.bean.BaseResponse
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import me.goldze.mvvmhabit.base.BaseViewModel
import me.goldze.mvvmhabit.http.BaseResponse
import retrofit2.Response
/**
......
package com.mints.street.netwrok.common;
import com.google.gson.Gson;
import com.mints.street.bean.BaseResponse;
import com.mints.street.manager.UserManager;
import java.io.IOException;
......@@ -12,7 +13,6 @@ import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import me.goldze.mvvmhabit.http.BaseResponse;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
......
package com.mints.street.netwrok.common;
import com.mints.street.bean.BaseResponse;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.functions.Function;
import me.goldze.mvvmhabit.http.BaseResponse;
import retrofit2.Response;
/**
......
......@@ -3,10 +3,10 @@ package com.mints.street.netwrok.common;
import android.annotation.SuppressLint;
import com.fry.base.global.Constants;
import com.mints.street.bean.BaseResponse;
import com.mints.street.netwrok.base.HttpConfiguation;
import com.mints.street.netwrok.base.IHttpResponseListener;
import com.mints.street.netwrok.base.RetrofitClient;
import com.mints.street.utils.encry.AESUtils;
import com.trello.rxlifecycle2.LifecycleProvider;
import io.reactivex.Observable;
......@@ -17,7 +17,6 @@ import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.http.ResponseThrowable;
import me.goldze.mvvmhabit.utils.RxUtils;
import retrofit2.Response;
......@@ -79,60 +78,4 @@ public class HttpManager {
.compose(RxUtils.exceptionTransformer())
.compose(HttpErrorProcess.businessExceptionTransformer());
}
@SuppressLint("CheckResult")
public <B extends BaseResponse, T extends Response<B>>
void execute(LifecycleProvider life, final Observable<T> observable, IHttpResponseListener<B> listener) {
Observable<T> newObservable = Observable.just(observable)
.observeOn(Schedulers.io())
.flatMap((Function<Observable<T>, ObservableSource<T>>) tObservable -> {
// ServerTimeHelper.requestServerTime();
return observable;
})
.observeOn(AndroidSchedulers.mainThread());
if (life != null) {
newObservable = newObservable.compose(RxUtils.bindToLifecycle(life));
}
newObservable
.compose(RxUtils.schedulersTransformer())
.compose(RxUtils.exceptionTransformer())
.compose(HttpErrorProcess.businessExceptionTransformer())
.doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
if (listener != null) {
listener.onStart(disposable);
}
}
})
.doFinally(new Action() {
@Override
public void run() throws Exception {
if (listener != null) {
listener.onCompleted();
}
}
})
.subscribe(t -> {
if (listener != null) {
// listener.onCompleted();
if (!listener.onParseResponse(t)) {
if (t.body() != null && t.body().isOk()) {
listener.onBusinessSuccess(t.body());
} else {
listener.onBusinessFail(t.body());
}
}
}
}, throwable -> {
if (listener != null && throwable instanceof ResponseThrowable) {
listener.onFailed((ResponseThrowable) throwable);
}
throwable.printStackTrace();
}, () -> {
});
}
}
package me.goldze.mvvmhabit.http;
import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.AppManager;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.goldze.mvvmhabit.utils.Utils;
/**
* Created by goldze on 2017/5/10.
* 统一的Code封装处理。该类仅供参考,实际业务逻辑, 根据需求来定义,
*/
public abstract class ApiDisposableObserver<T> extends DisposableObserver<T> {
public abstract void onResult(T t);
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
KLog.e(e.getMessage());
if (e instanceof ResponseThrowable) {
ResponseThrowable rError = (ResponseThrowable) e;
ToastUtils.showShort(rError.getMessage());
return;
}
//其他全部甩锅网络异常
ToastUtils.showShort("网络异常");
}
@Override
public void onStart() {
super.onStart();
ToastUtils.showShort("http is start");
// if NetworkAvailable no ! must to call onCompleted
if (!NetworkUtil.isNetworkAvailable(Utils.getContext())) {
ToastUtils.showShort("无网络,读取缓存数据");
onComplete();
}
}
@Override
public void onNext(Object o) {
BaseResponse baseResponse = (BaseResponse) o;
switch (baseResponse.getstatus()) {
case CodeRule.CODE_200:
//请求成功, 正确的操作方式
onResult((T) baseResponse.getResult());
break;
case CodeRule.CODE_220:
// 请求成功, 正确的操作方式, 并消息提示
onResult((T) baseResponse.getResult());
break;
case CodeRule.CODE_300:
//请求失败,不打印Message
KLog.e("请求失败");
ToastUtils.showShort("错误代码:", baseResponse.getstatus());
break;
case CodeRule.CODE_330:
//请求失败,打印Message
ToastUtils.showShort(baseResponse.getMessage());
break;
case CodeRule.CODE_500:
//服务器内部异常
ToastUtils.showShort("错误代码:", baseResponse.getstatus());
break;
case CodeRule.CODE_503:
//参数为空
KLog.e("参数为空");
break;
case CodeRule.CODE_502:
//没有数据
KLog.e("没有数据");
break;
case CodeRule.CODE_510:
//无效的Token,提示跳入登录页
ToastUtils.showShort("token已过期,请重新登录");
//关闭所有页面
AppManager.getAppManager().finishAllActivity();
//跳入登录界面
//*****该类仅供参考,实际业务Code, 根据需求来定义,******//
break;
case CodeRule.CODE_530:
ToastUtils.showShort("请先登录");
break;
case CodeRule.CODE_551:
ToastUtils.showShort("错误代码:", baseResponse.getstatus());
break;
default:
ToastUtils.showShort("错误代码:", baseResponse.getstatus());
break;
}
}
public static final class CodeRule {
//请求成功, 正确的操作方式
static final int CODE_200 = 200;
//请求成功, 消息提示
static final int CODE_220 = 220;
//请求失败,不打印Message
static final int CODE_300 = 300;
//请求失败,打印Message
static final int CODE_330 = 330;
//服务器内部异常
static final int CODE_500 = 500;
//参数为空
static final int CODE_503 = 503;
//没有数据
static final int CODE_502 = 502;
//无效的Token
static final int CODE_510 = 510;
//未登录
static final int CODE_530 = 530;
//请求的操作异常终止:未知的页面类型
static final int CODE_551 = 551;
}
}
\ No newline at end of file
//package me.goldze.mvvmhabit.http;
//
//
//import io.reactivex.observers.DisposableObserver;
//import me.goldze.mvvmhabit.base.AppManager;
//import me.goldze.mvvmhabit.utils.KLog;
//import me.goldze.mvvmhabit.utils.ToastUtils;
//import me.goldze.mvvmhabit.utils.Utils;
//
///**
// * Created by goldze on 2017/5/10.
// * 统一的Code封装处理。该类仅供参考,实际业务逻辑, 根据需求来定义,
// */
//
//public abstract class ApiDisposableObserver<T> extends DisposableObserver<T> {
// public abstract void onResult(T t);
//
// @Override
// public void onComplete() {
//
// }
//
// @Override
// public void onError(Throwable e) {
// e.printStackTrace();
// KLog.e(e.getMessage());
// if (e instanceof ResponseThrowable) {
// ResponseThrowable rError = (ResponseThrowable) e;
// ToastUtils.showShort(rError.getMessage());
// return;
// }
// //其他全部甩锅网络异常
// ToastUtils.showShort("网络异常");
// }
//
// @Override
// public void onStart() {
// super.onStart();
// ToastUtils.showShort("http is start");
// // if NetworkAvailable no ! must to call onCompleted
// if (!NetworkUtil.isNetworkAvailable(Utils.getContext())) {
// ToastUtils.showShort("无网络,读取缓存数据");
// onComplete();
// }
// }
//
// @Override
// public void onNext(Object o) {
// BaseResponse baseResponse = (BaseResponse) o;
// switch (baseResponse.getstatus()) {
// case CodeRule.CODE_200:
// //请求成功, 正确的操作方式
// onResult((T) baseResponse.getResult());
// break;
// case CodeRule.CODE_220:
// // 请求成功, 正确的操作方式, 并消息提示
// onResult((T) baseResponse.getResult());
// break;
// case CodeRule.CODE_300:
// //请求失败,不打印Message
// KLog.e("请求失败");
// ToastUtils.showShort("错误代码:", baseResponse.getstatus());
// break;
// case CodeRule.CODE_330:
// //请求失败,打印Message
// ToastUtils.showShort(baseResponse.getMessage());
// break;
// case CodeRule.CODE_500:
// //服务器内部异常
// ToastUtils.showShort("错误代码:", baseResponse.getstatus());
// break;
// case CodeRule.CODE_503:
// //参数为空
// KLog.e("参数为空");
// break;
// case CodeRule.CODE_502:
// //没有数据
// KLog.e("没有数据");
// break;
// case CodeRule.CODE_510:
// //无效的Token,提示跳入登录页
// ToastUtils.showShort("token已过期,请重新登录");
// //关闭所有页面
// AppManager.getAppManager().finishAllActivity();
// //跳入登录界面
// //*****该类仅供参考,实际业务Code, 根据需求来定义,******//
// break;
// case CodeRule.CODE_530:
// ToastUtils.showShort("请先登录");
// break;
// case CodeRule.CODE_551:
// ToastUtils.showShort("错误代码:", baseResponse.getstatus());
// break;
// default:
// ToastUtils.showShort("错误代码:", baseResponse.getstatus());
// break;
// }
// }
//
// public static final class CodeRule {
// //请求成功, 正确的操作方式
// static final int CODE_200 = 200;
// //请求成功, 消息提示
// static final int CODE_220 = 220;
// //请求失败,不打印Message
// static final int CODE_300 = 300;
// //请求失败,打印Message
// static final int CODE_330 = 330;
// //服务器内部异常
// static final int CODE_500 = 500;
// //参数为空
// static final int CODE_503 = 503;
// //没有数据
// static final int CODE_502 = 502;
// //无效的Token
// static final int CODE_510 = 510;
// //未登录
// static final int CODE_530 = 530;
// //请求的操作异常终止:未知的页面类型
// static final int CODE_551 = 551;
// }
//}
\ No newline at end of file
package me.goldze.mvvmhabit.http;
import java.util.List;
/**
* Created by goldze on 2017/5/10.
* 该类仅供参考,实际业务返回的固定字段, 根据需求来定义,
*/
public class BaseArrayResponse<T> extends BaseResponse<List<T>>{
@Override
public List<T> getResult() {
return result;
}
@Override
public void setResult(List<T> result) {
this.result = result;
}
}
//package me.goldze.mvvmhabit.http;
//
//import java.util.List;
//
///**
// * Created by goldze on 2017/5/10.
// * 该类仅供参考,实际业务返回的固定字段, 根据需求来定义,
// */
//public class BaseArrayResponse<T> extends BaseResponse<List<T>>{
//
//
// @Override
// public List<T> getResult() {
// return result;
// }
//
// @Override
// public void setResult(List<T> result) {
// this.result = result;
// }
//
//}
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