Param.java 12.9 KB
package com.glsx.pushsdk.common;

import android.annotation.SuppressLint;

import com.blankj.utilcode.util.LogUtils;
import com.glsx.pushsdk.manager.GLPushManager;
import com.glsx.pushsdk.model.GetOffLineIdItem;
import com.glsx.pushsdk.model.proto.MsgClient;
import com.glsx.pushsdk.model.proto.MsgClient.ClientGetMsgContextByMsgIdsReq;
import com.glsx.pushsdk.model.proto.MsgClient.ClientGetOfflineMsgReq;
import com.glsx.pushsdk.model.proto.MsgClient.ClientGetSdkVersionReq;
import com.glsx.pushsdk.model.proto.MsgClient.ClientHeartBeatReq;
import com.glsx.pushsdk.model.proto.MsgClient.ClientLoginReq;
import com.glsx.pushsdk.model.proto.MsgClient.ClientLogoutReq;
import com.glsx.pushsdk.model.proto.MsgClient.ClientUpAppUserFlagReq;
import com.glsx.pushsdk.model.proto.MsgClient.NotifyMsgToClientRsp;
import com.glsx.pushsdk.tools.ProtocolHandler;
import com.google.protobuf.ByteString;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 封装向业务服务器请求数据 感觉好坑呀,要死人,搞了半天才搞出第一个 夸平台数数据真蛋疼
 *
 * @author YFZX-yulg
 */
public class Param {
    public static final String TAG = "Param";

    /**
     * 终端类型0:ios 1:android
     *
     * @return
     */
/*	public static RequestParams getAccessIport() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("type", "1");
		return getParams(map);
	}*/
    public static Map<String, String> getAccessIport() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("type", "1");
        return map;
    }

    /**
     * 获取Param
     * @param map
     * @return
     */
	/*public static RequestParams getParams(Map<String, String> map) {
		RequestParams params = new RequestParams();
		Iterator<String> key = map.keySet().iterator();
		while (key.hasNext()) {
			String key_s = key.next();
			params.addQueryStringParameter(key_s, map.get(key_s));
		}

		return params;
	}*/

    /**
     * 数组拼接
     *
     * @param first
     * @param rest
     * @return
     */
    @SuppressLint("NewApi")
    public static byte[] concatAll(byte[] first, byte[]... rest) {
        int totalLength = first.length;
        for (byte[] array : rest) {
            totalLength += array.length;
        }
        byte[] result = Arrays.copyOf(first, totalLength);
        int offset = first.length;
        for (byte[] array : rest) {
            System.arraycopy(array, 0, result, offset, array.length);
            offset += array.length;
        }
        return result;
    }

    /**
     * 登录验证,设置请求参数
     *
     * @return
     */
    public static byte[] buildLoginData() {
        byte[] allData = null;

        try {

            MsgClient.ClientLoginReq.Builder loginReqBuilder = MsgClient.ClientLoginReq
                    .newBuilder();

            loginReqBuilder.setChAppKey(ByteString.copyFrom(GLPushManager
                    .getInstance().getAppKey().getBytes()));// appkey
            loginReqBuilder.setChannelSecret(ByteString.copyFrom(GLPushManager
                    .getInstance().getChannelSecret().getBytes()));// 通道密钥
            loginReqBuilder.setChImei(ByteString.copyFrom(GLPushManager
                    .getInstance().getImei().getBytes()));// imei
            loginReqBuilder.setNType(ByteString.copyFrom(GLPushManager
                    .getInstance().getTypy().getBytes()));// 终端类型0:ios
            // 1:android
            loginReqBuilder.setChSdkVno(ByteString.copyFrom(GLPushManager
                    .getInstance().getSdkVersion().getBytes()));// SDK当前版本号

            loginReqBuilder.setChIosDevToken(ByteString.copyFrom(GLPushManager
                    .getInstance().getChIosDevToken().getBytes()));// SDK当前版本号

            loginReqBuilder.setChAppUserFlag(ByteString.copyFrom(GLPushManager
                    .getInstance().getAppUserId().getBytes()));// 第三方用户账号,
            // 如果没有账号体系的,
            // 直接填写终端唯一标识,如IMEI等
            loginReqBuilder.setChAppVersion(ByteString.copyFrom(GLPushManager
                    .getInstance().getAppVersion().getBytes()));// 应用版本号

            ClientLoginReq loginReq = loginReqBuilder.build();

            allData = loginReq.toByteArray();// 转换的数据

        } catch (Exception e) {
            LogUtils.i("TAG", "e = " + e);
        }
        return ProtocolHandler.getProtocolHead(allData, CmdContants.LOGIN_SEND,
                CmdContants.FLAGS_F2);

    }

    /**
     * 心跳包数据封装
     *
     * @return
     */
    public static Object buildHeartBeatData() {
        byte[] allData = null;
        try {
            MsgClient.ClientHeartBeatReq.Builder heartBeatReqBuilder = MsgClient.ClientHeartBeatReq
                    .newBuilder();
            heartBeatReqBuilder.setChGlsxKey(ByteString.copyFrom(GLPushManager
                    .getInstance().getmLoginResult().getGlsxkey().getBytes()));
            ClientHeartBeatReq heartReq = heartBeatReqBuilder.build();

            allData = heartReq.toByteArray();// 转换的数据
        } catch (Exception e) {
            LogUtils.i("TAG", "e = " + e);
        }

        return ProtocolHandler.getProtocolHead(allData, CmdContants.HEART_SEND,
                CmdContants.FLAGS_F2);

    }

    /**
     * 获取离线数据封装
     *
     * @return
     * @author YFZX-yulg 2016-11-15
     */
    public static Object buildOffLineData() {
        byte[] allData = null;
        try {
            MsgClient.ClientGetOfflineMsgReq.Builder getOffLineReqBuilder = MsgClient.ClientGetOfflineMsgReq
                    .newBuilder();

            getOffLineReqBuilder.setChGlsxKey(ByteString.copyFrom(GLPushManager
                    .getInstance().getmLoginResult().getGlsxkey().getBytes()));

            ClientGetOfflineMsgReq req = getOffLineReqBuilder.build();

            allData = req.toByteArray();// 转换的数据
        } catch (Exception e) {
            LogUtils.i("TAG", "e = " + e);
        }

        LogUtils.i("ylg", "请求获取离线数据");

        return ProtocolHandler.getProtocolHead(allData,
                CmdContants.GET_OFF_LINE_SEND, CmdContants.FLAGS_F2);
    }

    /**
     * 根据联系数据ID获取详情封装
     *
     * @author YFZX-yulg 2016-11-15
     */
    public static Object buildOffLineDetailData(List<GetOffLineIdItem> mListMsgId) {
        byte[] allData = null;
        try {
            MsgClient.ClientGetMsgContextByMsgIdsReq.Builder reqBuilder =
                    MsgClient.ClientGetMsgContextByMsgIdsReq.newBuilder();

            reqBuilder.setChGlsxKey(ByteString.copyFrom(GLPushManager.getInstance()
                    .getmLoginResult().getGlsxkey().getBytes()));

            for (int i = 0; i < mListMsgId.size(); i++) {
                MsgClient.MsgId.Builder msgIdBuilder = MsgClient.MsgId.newBuilder();
                msgIdBuilder.setChMsgId(ByteString.copyFrom(mListMsgId.get(i).getMsgId().getBytes()));
                reqBuilder.addChMsgIds(i, msgIdBuilder);// 加载消息ID集合
            }
            ClientGetMsgContextByMsgIdsReq req = reqBuilder.build();
            allData = req.toByteArray();// 转换的数据
        } catch (Exception e) {
            LogUtils.i("TAG", "e = " + e);
        }

        LogUtils.i("ylg", "请求根据联系数据ID获取详情封装");

        return ProtocolHandler.getProtocolHead(allData,
                CmdContants.GET_OFF_LINE_DETAIL_MESSAGE_SEND,
                CmdContants.FLAGS_F2);
    }

    /**
     * 请求退出登录
     *
     * @return
     */
    public static Object buildExitData() {
        // TODO Auto-generated method stub
        byte[] allData = null;
        try {
            MsgClient.ClientLogoutReq.Builder reqBuilder = MsgClient.ClientLogoutReq
                    .newBuilder();

            reqBuilder.setChGlsxKey(ByteString.copyFrom(GLPushManager
                    .getInstance().getmLoginResult().getGlsxkey().getBytes()));

            ClientLogoutReq req = reqBuilder.build();

            allData = req.toByteArray();// 转换的数据
        } catch (Exception e) {
            LogUtils.i("TAG", "e = " + e);
        }

        LogUtils.i("ylg", "请求退出登录");
        return ProtocolHandler.getProtocolHead(allData, CmdContants.EXIT_SEND,
                CmdContants.FLAGS_F2);
    }

    /**
     * 获取SDK版本
     *
     * @param flag 0:按照终端类型获取最新版本信息 1按照版本号查询版本信息
     * @param type 终端类型 0:ios,1:android
     * @return
     */
    public static Object buildSendSdkVersion(int flag, int type) {
        // TODO Auto-generated method stub
        byte[] allData = null;
        try {
            MsgClient.ClientGetSdkVersionReq.Builder reqBuilder = MsgClient.ClientGetSdkVersionReq
                    .newBuilder();

            reqBuilder.setChSdkVno(ByteString.copyFrom(GLPushManager
                    .getInstance().getSdkVersion().getBytes()));
            reqBuilder.setNFlag(flag);
            reqBuilder.setNType(type);

            ClientGetSdkVersionReq req = reqBuilder.build();

            allData = req.toByteArray();// 转换的数据
        } catch (Exception e) {
            LogUtils.i("TAG", "e = " + e);
        }
        LogUtils.i("ylg", "请求获取SDK版本");
        return ProtocolHandler.getProtocolHead(allData,
                CmdContants.GET_VERSION_SEND, CmdContants.FLAGS_F2);
    }

    /**
     * 回调服务器推送获取请求
     *
     * @return
     */
    public static Object buildSendData(short seq, String msgId,
                                       String currentTime) {
        // TODO Auto-generated method stub
        byte[] allData = null;
        try {
            MsgClient.NotifyMsgToClientRsp.Builder reqBuilder = MsgClient.NotifyMsgToClientRsp
                    .newBuilder();

            reqBuilder.setChGlsxKey(ByteString.copyFrom(GLPushManager
                    .getInstance().getmLoginResult().getGlsxkey().getBytes()));
            reqBuilder.setChErrDesc(ByteString.copyFrom("".getBytes()));
            reqBuilder.setNRetCode(0);
            reqBuilder.setChRvTime(ByteString.copyFrom(currentTime.getBytes()));
            reqBuilder.setChMsgId(ByteString.copyFrom(msgId.getBytes()));

            NotifyMsgToClientRsp req = reqBuilder.build();

            allData = req.toByteArray();// 转换的数据
        } catch (Exception e) {
            LogUtils.i("TAG", "e = " + e);
        }

        return ProtocolHandler
                .getProtocolHead(allData, CmdContants.SERVER_PUSH_MESSAGE_BACK,
                        CmdContants.FLAGS_F2, seq);
    }

    /**
     * 重新设置userId
     *
     * @return
     */
    public static Object buildSetUserIdData() {
        // TODO Auto-generated method stub
        byte[] allData = null;
        try {
            MsgClient.ClientUpAppUserFlagReq.Builder reqBuilder = MsgClient.ClientUpAppUserFlagReq
                    .newBuilder();

            reqBuilder.setChGlsxKey(ByteString.copyFrom(GLPushManager
                    .getInstance().getmLoginResult().getGlsxkey().getBytes()));
            reqBuilder.setChAppUserFlag(ByteString.copyFrom(GLPushManager
                    .getInstance().getAppUserId().getBytes()));

            ClientUpAppUserFlagReq req = reqBuilder.build();

            allData = req.toByteArray();// 转换的数据
        } catch (Exception e) {
            LogUtils.i("TAG", "e = " + e);
        }

        LogUtils.i("ylg", "重新设置userId");
        return ProtocolHandler.getProtocolHead(allData,
                CmdContants.SET_USERID_SEND, CmdContants.FLAGS_F2);
    }


    /**
     * 终端点击消息通知
     *
     * @return
     * @author YFZX-yulg 2016-11-15
     */
    public static Object buildSetMsgId(
            List<GetOffLineIdItem> mListMsgId) {
        byte[] allData = null;
        try {
            MsgClient.ClientGetMsgContextByMsgIdsReq.Builder reqBuilder = MsgClient.ClientGetMsgContextByMsgIdsReq
                    .newBuilder();

            reqBuilder.setChGlsxKey(ByteString.copyFrom(GLPushManager
                    .getInstance().getmLoginResult().getGlsxkey().getBytes()));

            for (int i = 0; i < mListMsgId.size(); i++) {
                MsgClient.MsgId.Builder msgIdBuilder = MsgClient.MsgId
                        .newBuilder();
                msgIdBuilder.setChMsgId(ByteString.copyFrom(mListMsgId.get(i)
                        .getMsgId().getBytes()));
                reqBuilder.addChMsgIds(i, msgIdBuilder);// 加载消息ID集合
            }

            ClientGetMsgContextByMsgIdsReq req = reqBuilder.build();

            allData = req.toByteArray();// 转换的数据
        } catch (Exception e) {
            LogUtils.i("TAG", "e = " + e);
        }

        LogUtils.i("ylg", "请求终端点击消息通知");

        return ProtocolHandler.getProtocolHead(allData,
                CmdContants.CLICK_MSG_SEND,
                CmdContants.FLAGS_F2);
    }


}