package com.wd.workoffice.retrofit;

import org.greenrobot.eventbus.EventBus;

import flexible.xd.android_base.model.event.LoginEvent;
import flexible.xd.android_base.network.rtfhttp.observer.ISubscriber;
import io.reactivex.Observer;
import retrofit2.HttpException;

public abstract class WorkObserver<T> extends flexible.xd.android_base.network.rtfhttp.observer.BaseObserver<T> implements Observer<T>, ISubscriber<T> {
    @Override
    public void network401() {
        EventBus.getDefault().post(new LoginEvent());
    }

    @Override
    public void onError(Throwable e) {
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            if (httpException.code() == 401) {//一般用于判断登录状态
                network401();
            } else if (httpException.code() == 403) {
                onFail("没有权限处理");
            } else {
                onFail("服务器异常，请稍后再试");
            }
        } else {
            onFail("获取失败，请检查网络状态");
        }
    }
}
