项目修改与整理
This commit is contained in:
@ -34,6 +34,7 @@ import okhttp3.ResponseBody;
|
||||
import rx.Observable;
|
||||
import rx.Subscriber;
|
||||
import rx.android.schedulers.AndroidSchedulers;
|
||||
import rx.functions.Action1;
|
||||
import rx.functions.Func1;
|
||||
import rx.schedulers.Schedulers;
|
||||
|
||||
@ -90,62 +91,56 @@ public class TokenUtils {
|
||||
}
|
||||
|
||||
// 获取用户token
|
||||
public static synchronized Observable<String> getToken(final Context context, final boolean isCheck) {
|
||||
return Observable.create(new Observable.OnSubscribe<String>() {
|
||||
@Override
|
||||
public void call(Subscriber<? super String> subscriber) {
|
||||
String token = null;
|
||||
if (isCheck) {
|
||||
SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
|
||||
token = sp.getString("token", null);
|
||||
if (token != null) {
|
||||
long expire = sp.getLong("token_expire", 0) * 1000 - 10 * 1000;
|
||||
long time = Utils.getTime(context);
|
||||
// 判断token是否过期
|
||||
if (time >= expire) {
|
||||
// token已过期
|
||||
token = null;
|
||||
public static synchronized Observable<String> getToken(final Context context, boolean isCheck) {
|
||||
String token = null;
|
||||
if (isCheck) {
|
||||
SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
|
||||
token = sp.getString("token", null);
|
||||
if (token != null) {
|
||||
long expire = sp.getLong("token_expire", 0) * 1000 - 10 * 1000;
|
||||
long time = Utils.getTime(context);
|
||||
// 判断token是否过期
|
||||
if (time >= expire) {
|
||||
// token已过期
|
||||
token = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (token == null) {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("device_id", getDeviceId(context));
|
||||
return RetrofitManager.getUser()
|
||||
.postLogin(RequestBody.create(MediaType.parse("application/json"), new JSONObject(params).toString()))
|
||||
.flatMap(new Func1<ResponseBody, Observable<String>>() {
|
||||
@Override
|
||||
public Observable<String> call(ResponseBody responseBody) {
|
||||
String value = null;
|
||||
try {
|
||||
SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
|
||||
Editor editor = sp.edit();
|
||||
JSONObject response = new JSONObject(responseBody.string());
|
||||
editor.putString("user_name", response.getString("name"));
|
||||
editor.putString("user_icon", response.getString("icon"));
|
||||
response = response.getJSONObject("token");
|
||||
editor.putString("token", response.getString("value"));
|
||||
editor.putLong("token_expire", response.getLong("expire"));
|
||||
editor.apply();
|
||||
// 服务器返回的token和本地已存的token相同,更新本地时间
|
||||
getTime(context);
|
||||
value = response.getString("value");
|
||||
} catch (IOException | JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return Observable.just(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
subscriber.onNext(token);
|
||||
subscriber.onCompleted();
|
||||
}
|
||||
}).flatMap(new Func1<String, Observable<String>>() {
|
||||
@Override
|
||||
public Observable<String> call(String token) {
|
||||
if (token == null) {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("device_id", getDeviceId(context));
|
||||
return RetrofitManager.getUser()
|
||||
.postLogin(RequestBody.create(MediaType.parse("application/json"), params.toString()))
|
||||
.flatMap(new Func1<ResponseBody, Observable<String>>() {
|
||||
@Override
|
||||
public Observable<String> call(ResponseBody responseBody) {
|
||||
String value = null;
|
||||
try {
|
||||
SharedPreferences sp = context.getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
|
||||
Editor editor = sp.edit();
|
||||
JSONObject response = new JSONObject(responseBody.string());
|
||||
editor.putString("user_name", response.getString("name"));
|
||||
editor.putString("user_icon", response.getString("icon"));
|
||||
response = response.getJSONObject("token");
|
||||
editor.putString("token", response.getString("value"));
|
||||
editor.putLong("token_expire", response.getLong("expire"));
|
||||
editor.apply();
|
||||
// 服务器返回的token和本地已存的token相同,更新本地时间
|
||||
getTime(context);
|
||||
value = response.getString("value");
|
||||
} catch (IOException | JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return Observable.just(value);
|
||||
}
|
||||
});
|
||||
}
|
||||
return Observable.just(token);
|
||||
}
|
||||
});
|
||||
}).onErrorResumeNext(new Func1<Throwable, Observable<? extends String>>() {
|
||||
@Override
|
||||
public Observable<? extends String> call(Throwable throwable) {
|
||||
return Observable.empty();
|
||||
}
|
||||
});
|
||||
}
|
||||
return Observable.just(token);
|
||||
}
|
||||
|
||||
// 检查设备信息是否已经上传完整
|
||||
|
||||
Reference in New Issue
Block a user