ArticleDetailFetcher.java 10.2 KB

package com.people.webview.model;

import android.text.TextUtils;

import com.people.webview.vm.IArticleDetailDataListener;
import com.wd.base.log.Logger;
import com.wd.capability.network.BaseObserver;
import com.wd.capability.network.RetrofitClient;
import com.wd.capability.network.constant.ParameterConstant;
import com.wd.capability.network.fetcher.BaseDataFetcher;
import com.wd.capability.network.interceptor.LoggingInterceptor;
import com.wd.capability.network.response.BaseResponse;
import com.wd.common.api.RequestApi;
import com.wd.foundation.bean.custom.content.ContentBean;
import com.wd.foundation.bean.custom.content.ContentTypeConstant;
import com.wd.foundation.bean.response.NewsDetailBean;
import com.wd.foundation.wdkit.system.DeviceUtil;
import com.wd.foundation.wdkit.utils.SpUtils;
import com.wd.foundation.wdkitcore.tools.JsonUtils;
import com.wd.foundation.wdkitcore.tools.StringUtils;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * 图文数据fetcher
 *
 * @author xujiawei
 */
public class ArticleDetailFetcher extends BaseDataFetcher<RequestApi> {

    private IArticleDetailDataListener mDataListener;

    public ArticleDetailFetcher(IArticleDetailDataListener dataListener) {
        this.mDataListener = dataListener;
    }

    public void requestDetail(String id,String relId,String relType) {
        Map<String, Object> params = new HashMap<>();
        //稿件ID不能为空
        if (StringUtils.isBlank(id)) {
            if (mDataListener != null) {
                mDataListener.onDetailDataError("");
            }
            return;
        }

        params.put(ParameterConstant.CONTENTID, id);

        if (StringUtils.isNotBlank(relId)) {
            params.put(ParameterConstant.CONTENT_REL_ID, relId);
        }
        if (StringUtils.isNotBlank(relType)) {
            params.put(ParameterConstant.CONTENT_REL_TYPE, relType);
        }
        Observable<ResponseBody> phoneCode = getRetrofit().getArticleNewsDetail(params);
        RetrofitClient.execute(phoneCode, new Observer<ResponseBody>() {
            @Override
            public void onSubscribe(@NotNull Disposable d) {

            }

            @Override
            public void onNext(@NotNull ResponseBody responseBody) {
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(responseBody.string());
                    if (mDataListener != null) {
                        mDataListener.onDetailDataSuccess(jsonObject.toString());
                    }
                } catch (Exception e) {
                    if (mDataListener != null) {
                        mDataListener.onDetailDataError(e.toString());
                    }
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (mDataListener != null) {
                    mDataListener.onDetailDataError(e.toString());
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }

    public void requestPageData(String method, String url, JSONObject parameters,String callbackId) {
        Observable<ResponseBody> phoneCode = null;
        if (method.equals("POST") || method.equals("post")) {
            phoneCode = getRetrofit().pageDataPost(url, getBody(parameters.toString()));
        } else {
            phoneCode = getRetrofit().pageDataGet(url, getMap(parameters));
        }
        RetrofitClient.execute(phoneCode, new Observer<ResponseBody>() {
            @Override
            public void onSubscribe(@NotNull Disposable d) {

            }

            @Override
            public void onNext(@NotNull ResponseBody responseBody) {
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(responseBody.string());
                    if (mDataListener != null) {
                        mDataListener.onPageDataSuccess(url,jsonObject.toString(),callbackId);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    if (mDataListener != null) {
                        mDataListener.onPageDataError(e.toString(),callbackId);
                    }
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (mDataListener != null) {
                    mDataListener.onPageDataError(e.toString(),callbackId);
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public RequestBody getBody(Object object) {
        String requestJson = JsonUtils.convertObjectToJson(object);
        Logger.t(LoggingInterceptor.LOG_TAG).d("---请求data---" + requestJson);
        MediaType mediaType = MediaType.Companion.parse(ParameterConstant.HEADER_JSON_TYPE);
        RequestBody body = RequestBody.Companion.create(requestJson, mediaType);
        return body;
    }

    public RequestBody getBody(String requestJson) {
        Logger.t(LoggingInterceptor.LOG_TAG).d("---请求data---" + requestJson);
        MediaType mediaType = MediaType.Companion.parse(ParameterConstant.HEADER_JSON_TYPE);
        RequestBody body = RequestBody.Companion.create(requestJson, mediaType);
        return body;
    }

    public Map<String, Object> getMap(JSONObject object) {
        try {
            Map<String, Object> objectMap = new HashMap<>();
            for (Iterator<String> it = object.keys(); it.hasNext(); ) {
                String key = it.next();
                Object value = object.get(key);
                if (value == null || value.toString().equals("null")) {
//                    objectMap.put(key,"");
                } else {
                    if (value instanceof JSONArray) {
                        JSONArray jsonArray = (JSONArray) value;
                        List<Object> objectList = new ArrayList<>();
                        for (int i = 0; i < jsonArray.length();i++) {
                            Object json = jsonArray.opt(i);
                            objectList.add(json);
                        }
                        objectMap.put(key, objectList);
                    } else {
                        objectMap.put(key, value);
                    }
                }
            }
            return objectMap;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 待处理关联关系
     * @param id
     * @param relType
     * @param relId
     */
    public void getNewsDetail(String id,String relType,String relId){
        Map<String, Object> params = new HashMap<>();
        //稿件ID不能为空
        if (StringUtils.isBlank(id)) {
            if (mDataListener != null) {
                mDataListener.onGetNewsDetailFailed("");
            }
            return;
        }
        params.put(ParameterConstant.CONTENTID, id);

        if (StringUtils.isNotBlank(relId)) {
            params.put(ParameterConstant.CONTENT_REL_ID, relId);
        }
        if (StringUtils.isNotBlank(relType)) {
            params.put(ParameterConstant.CONTENT_REL_TYPE, relType);
        }
        Observable<BaseResponse<List<NewsDetailBean>>> observable = getRetrofit().getOneNewsDetail(params);
        request(observable, new BaseObserver<List<NewsDetailBean>>() {
            @Override
            protected void dealSpecialCode(int code, String message) {
                if (mDataListener != null) {
                    mDataListener.onGetNewsDetailFailed(message);
                }
            }

            @Override
            protected void onSuccess(List<NewsDetailBean> newsDetailBeanList) {
                if (mDataListener != null) {
                    mDataListener.onGetNewsDetailSuccess(newsDetailBeanList);
                }
            }

            @Override
            public void _onError(String e) {
                if (mDataListener != null) {
                    mDataListener.onGetNewsDetailFailed(e);
                }
            }
        });
    }

    /**
     *  获取推荐列表
     * @param contentId     当前数据id
     * @param relId         当前内容关系id
     * @param channelId     频道id
     * @param recType       推荐类型
     */
    public void getRecList(String contentId,String relId,String channelId,int recType){
        Map<String, Object> params = new HashMap<>();
        params.put(ParameterConstant.IMEI, DeviceUtil.getDeviceId());
        String userId = SpUtils.getUserId();
        if (!TextUtils.isEmpty(userId)) {
            params.put(ParameterConstant.USER_ID, userId);
        }
        params.put(ParameterConstant.CONTENTID, contentId);
        params.put("relId", relId);
        //当前数据类型 1:视频,2:直播,5:专题,8:图文,9:组图,10:H5新闻
        params.put(ParameterConstant.CONTENT_TYPE, ContentTypeConstant.URL_TYPE_EIGHT);
        //推荐类型:1.详情推荐;2.搜索推荐
        params.put("recType", recType);
        //频道id
        params.put("channelId", channelId);
        Observable<BaseResponse<List<ContentBean>>> observable = getRetrofit().getRecList(getBody(params));
        request(observable, new BaseObserver<List<ContentBean>>() {
            @Override
            protected void dealSpecialCode(int code, String message) {
                if (mDataListener != null) {
                    mDataListener.onGetRecListFailed(message);
                }
            }

            @Override
            protected void onSuccess(List<ContentBean> operDataList) {
                if (mDataListener != null) {
                    mDataListener.onGetRecListSuccess(operDataList);
                }
            }

            @Override
            public void _onError(String e) {
                if (mDataListener != null) {
                    mDataListener.onGetRecListFailed("");
                }
            }
        });
    }

}