GoodsMsg

@MessageTag(value = "App:YXGoodsMsg", flag = MessageTag.ISCOUNTED | MessageTag.ISPERSISTED)
@DestructionTag(destructionFlag = DestructionTag.FLAG_COUNT_DOWN_WHEN_CLICK)
public class GoodsMsg extends MessageContent implements Parcelable {

    // 自定义属性
//    图片:商品SPU主图;
//    价格:返回SPU最低价格;
//    标题:返回商品标题,最多显示27个汉字(两排),超出部分显示为“…”;
//    标签:返回商品定义标签,最多显示1排,超出部分不显示;
//    商铺信息:返回店铺Logo和店铺名称
    private String goodsCoverUrl;
    private String goodsPriceMin;
    private String goodsName;
    private List goodsTag;
    private String shopLogo;
    private String shopName;
    private String goodsId;
    private String shopId;

    private static final String TAG = GoodsMsg.class.getSimpleName();

    public GoodsMsg() {
    }

    public static final Creator CREATOR = new Creator() {

        @Override
        public GoodsMsg createFromParcel(Parcel in) {
            return new GoodsMsg(in);
        }

        @Override
        public GoodsMsg[] newArray(int size) {
            return new GoodsMsg[size];
        }
    };

    // 快速构建消息对象方法
    public static GoodsMsg obtain() {
        return new GoodsMsg();
    }

    /**
     * 创建 CustomMessage(byte[] data) 带有 byte[] 的构造方法用于解析消息内容.
     */
    public GoodsMsg(byte[] data) {
        if (data == null) {
            RLog.e(TAG, "data is null ");
            return;
        }

        String jsonStr = null;
        try {
            jsonStr = new String(data, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            RLog.e(TAG, "UnsupportedEncodingException ", e);
        }

        if (jsonStr == null) {
            RLog.e(TAG, "jsonStr is null ");
            return;
        }

        try {
            JSONObject jsonObj = new JSONObject(jsonStr);

            // 消息携带用户信息时, 自定义消息需添加下面代码
            if (jsonObj.has("user")) {
                setUserInfo(parseJsonToUserInfo(jsonObj.getJSONObject("user")));
            }

            // 用于群组聊天, 消息携带 @ 人信息时, 自定义消息需添加下面代码
            if (jsonObj.has("mentionedInfo")) {
                setMentionedInfo(parseJsonToMentionInfo(jsonObj.getJSONObject("mentionedInfo")));
            }

            // ...
            // 自定义消息, 定义的字段
            // ...
            if (jsonObj.has("goodsCoverUrl")) {
                setGoodsCoverUrl(jsonObj.optString("goodsCoverUrl"));
            }

            if (jsonObj.has("goodsPriceMin")) {
                setGoodsPriceMin(jsonObj.optString("goodsPriceMin"));
            }

            if (jsonObj.has("goodsName")) {
                setGoodsName(jsonObj.optString("goodsName"));
            }

            if (jsonObj.has("shopLogo")) {
                setShopLogo(jsonObj.optString("shopLogo"));
            }

            if (jsonObj.has("shopName")) {
                setShopName(jsonObj.optString("shopName"));
            }

            if (jsonObj.has("goodsId")) {
                setGoodsId(jsonObj.optString("goodsId"));
            }

            if (jsonObj.has("shopId")) {
                setShopId(jsonObj.optString("shopId"));
            }

            if (jsonObj.has("goodsTag")) {
                JSONArray array = jsonObj.optJSONArray("goodsTag");
                List tags = new ArrayList<>();
                for (int i = 0; i < array.length(); i++) {
                    tags.add(array.getString(i));
                }
                setGoodsTag(tags);
            }

        } catch (JSONException e) {
            RLog.e(TAG, "JSONException " + e.getMessage());
        }

    }

    /**
     * 将本地消息对象序列化为消息数据。
     *
     * @return 消息数据。
     */
    @Override
    public byte[] encode() {
        JSONObject jsonObj = new JSONObject();
        try {

            // 消息携带用户信息时, 自定义消息需添加下面代码
            if (getJSONUserInfo() != null) {
                jsonObj.putOpt("user", getJSONUserInfo());
            }

            // 用于群组聊天, 消息携带 @ 人信息时, 自定义消息需添加下面代码
            if (getJsonMentionInfo() != null) {
                jsonObj.putOpt("mentionedInfo", getJsonMentionInfo());
            }

            // ...
            // 自定义消息, 定义的字段.
            // ...
            jsonObj.put("goodsCoverUrl", getGoodsCoverUrl());
            jsonObj.put("goodsPriceMin", getGoodsPriceMin());
            jsonObj.put("goodsName", getGoodsName());
            jsonObj.put("goodsTag", getGoodsTag());
            jsonObj.put("shopLogo", getShopLogo());
            jsonObj.put("shopName", getShopName());
            jsonObj.put("goodsId", getGoodsId());
            jsonObj.put("shopId", getShopId());

        } catch (JSONException e) {
            RLog.e(TAG, "JSONException " + e.getMessage());
        }

        try {
            return jsonObj.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            RLog.e(TAG, "UnsupportedEncodingException ", e);
        }
        return null;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        ParcelUtils.writeToParcel(dest, getGoodsCoverUrl());
        ParcelUtils.writeToParcel(dest, getGoodsPriceMin());
        ParcelUtils.writeToParcel(dest, getGoodsName());
        ParcelUtils.writeToParcel(dest, getGoodsId());
        ParcelUtils.writeToParcel(dest, getShopId());
        ParcelUtils.writeToParcel(dest, getShopLogo());
        ParcelUtils.writeToParcel(dest, getShopName());
        ParcelUtils.writeListToParcel(dest, getGoodsTag());
    }

    public GoodsMsg(Parcel in) {
        setGoodsCoverUrl(ParcelUtils.readFromParcel(in));
        setGoodsPriceMin(ParcelUtils.readFromParcel(in));
        setGoodsName(ParcelUtils.readFromParcel(in));
        setGoodsId(ParcelUtils.readFromParcel(in));
        setShopId(ParcelUtils.readFromParcel(in));
        setShopLogo(ParcelUtils.readFromParcel(in));
        setShopName(ParcelUtils.readFromParcel(in));
        setGoodsTag(ParcelUtils.readListFromParcel(in, String.class));
    }

    public String getGoodsCoverUrl() {
        return goodsCoverUrl;
    }

    public void setGoodsCoverUrl(String goodsCoverUrl) {
        this.goodsCoverUrl = goodsCoverUrl;
    }

    public String getGoodsPriceMin() {
        return goodsPriceMin;
    }

    public void setGoodsPriceMin(String goodsPriceMin) {
        this.goodsPriceMin = goodsPriceMin;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public List getGoodsTag() {
        return goodsTag;
    }

    public void setGoodsTag(List goodsTag) {
        this.goodsTag = goodsTag;
    }

    public String getShopLogo() {
        return shopLogo;
    }

    public void setShopLogo(String shopLogo) {
        this.shopLogo = shopLogo;
    }

    public String getShopName() {
        return shopName;
    }

    public void setShopName(String shopName) {
        this.shopName = shopName;
    }

    public String getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(String goodsId) {
        this.goodsId = goodsId;
    }

    public String getShopId() {
        return shopId;
    }

    public void setShopId(String shopId) {
        this.shopId = shopId;
    }
}

你可能感兴趣的:(GoodsMsg)