package com.shayu.onetoone.manager; import com.shayu.onetoone.bean.MessageConsumeConfigBean; import com.shayu.onetoone.bean.SendConsumeBean; import com.shayu.onetoone.listener.OnSendMessageListener; import com.yunbao.common.http.base.HttpCallback; import com.yunbao.common.utils.ToastUtil; import java.util.HashMap; import java.util.Map; import java.util.UUID; public class SendMessageManager { private static final Map cache = new HashMap<>(); public static void sendMessageForGift(String toUid, String giftId, OnSendMessageListener listener) { SendData sendData = new SendData(toUid, 3, giftId, "礼物消息", "1", 1); send(sendData, listener); } public static void sendMessageForAudio(String toUid, OnSendMessageListener listener) { SendData sendData = new SendData(toUid, 1, "not", "录音消息", "1", 1); send(sendData, listener); } public static void sendMessageForText(String toUid, String text, OnSendMessageListener messageListener) { SendData sendData = new SendData(toUid, 1, "not", text, "1", 1); send(sendData, messageListener); } public static void checkVideoMessage(String toUid, String time, OnSendMessageListener listener) { SendData sendData = new SendData(toUid, 2, "not", "视频消息", time, 2, false ); send(sendData, listener); } public static void checkAudioMessage(String toUid, String time, OnSendMessageListener listener) { SendData sendData = new SendData(toUid, 5, "not", "音频消息", time, 2, false ); send(sendData, listener); } public static void pingVideoMessage(String toUid, String time, OnSendMessageListener listener) { SendData sendData = new SendData(toUid, 2, "not", "视频消息", time, 2, "after", false ); send(sendData, listener); } public static void pingAudioMessage(String toUid, String time, OnSendMessageListener listener) { SendData sendData = new SendData(toUid, 5, "not", "音频消息", time, 2, "after", false ); send(sendData, listener); } public static void matching(OnSendMessageListener listener) { SendData sendData = new SendData("not", 9, "not", "灵魂匹配", "1", 1, "after", false ); send(sendData, listener); } public static void chatUp(String toUid, OnSendMessageListener listener) { SendData sendData = new SendData(toUid, 4, "not", "搭讪消息", "1", 1, true ); send(sendData, listener); } private static void send(SendData sendData, OnSendMessageListener messageListener) { OTONetManager.getInstance(null) .sendMessage(sendData.toUid, sendData.type, sendData.giftId, sendData.content, sendData.time, sendData.online, sendData.method, new HttpCallback() { @Override public void onSuccess(SendConsumeBean consumeBean) { if (consumeBean.getCode() != 0) { if (consumeBean.getCode() == 500) { messageListener.onError(OnSendMessageListener.STATUS_NOT_PRICE, consumeBean.getMsg(), consumeBean); } else { messageListener.onError(OnSendMessageListener.STATUS_ERROR, consumeBean.getMsg(), consumeBean); } } else { String token = null; if (sendData.needToken) { token = UUID.randomUUID().toString(); cache.put(token, sendData); } messageListener.onSuccess(token, consumeBean); } } @Override public void onError(String error) { messageListener.onError(OnSendMessageListener.STATUS_ERROR, error); } } ); } public static void onCallSuccess(String token, OnSendMessageListener listener) { if (cache.containsKey(token)) { SendData sendData = cache.get(token); assert sendData != null; OTONetManager.getInstance(null) .sendMessage(sendData.toUid, sendData.type, sendData.giftId, sendData.content, sendData.time, sendData.online, "after", new HttpCallback() { @Override public void onSuccess(SendConsumeBean sd) { if (sd.getCode() != 0) { if (listener != null) { listener.onSuccess(sd.getMsg(), sd); } } else { cache.remove(token); if (listener != null) { listener.onSuccess(null, sd); } } } @Override public void onError(String error) { ToastUtil.show(error); System.err.println(error); if (listener != null) { listener.onError(OnSendMessageListener.STATUS_ERROR, error); } } } ); } else { if (listener != null) { listener.onError(OnSendMessageListener.STATUS_ERROR, "消息发送异常"); } } } public static void cancel(String token) { cache.remove(token); } public static class SendData { public boolean needToken = true; private String toUid; private int type; private String giftId; private String content; private String time; private int online; private String method = "before"; public SendData(String toUid, int type, String giftId, String content, String time, int online) { this.toUid = toUid; this.type = type; this.giftId = giftId; this.content = content; this.time = time; this.online = online; } public SendData(String toUid, int type, String giftId, String content, String time, int online, boolean needToken) { this.needToken = needToken; this.toUid = toUid; this.type = type; this.giftId = giftId; this.content = content; this.time = time; this.online = online; } public SendData(String toUid, int type, String giftId, String content, String time, int online, String method) { this.toUid = toUid; this.type = type; this.giftId = giftId; this.content = content; this.time = time; this.online = online; this.method = method; } public SendData(String toUid, int type, String giftId, String content, String time, int online, String method, boolean needToken) { this.needToken = needToken; this.toUid = toUid; this.type = type; this.giftId = giftId; this.content = content; this.time = time; this.online = online; this.method = method; } } }