bug:修复消息不能正确判断位置

This commit is contained in:
xiao12feng8 2025-12-30 10:10:35 +08:00
parent 72e90f4e0e
commit 7d1896d9d2
5 changed files with 92 additions and 23 deletions

View File

@ -25,6 +25,7 @@ public class ChatMessage {
private long timestamp;
private boolean isSystemMessage;
private boolean isGiftMessage; // 是否是礼物消息
private boolean isOutgoing; // 是否是自己发送的消息
private MessageStatus status;
private String avatarUrl; // 发送者头像 URL后续从后端获取
@ -49,7 +50,8 @@ public class ChatMessage {
this.isGiftMessage = false;
this.messageType = MessageType.TEXT;
// 如果是自己发送的消息初始状态为发送中
this.status = "".equals(username) ? MessageStatus.SENDING : MessageStatus.SENT;
this.isOutgoing = "".equals(username);
this.status = this.isOutgoing ? MessageStatus.SENDING : MessageStatus.SENT;
}
// 系统消息构造函数
@ -86,6 +88,20 @@ public class ChatMessage {
this.isSystemMessage = isSystemMessage;
this.status = status;
this.messageType = MessageType.TEXT;
this.isOutgoing = "".equals(username);
}
// 完整构造函数从后端数据构造 isOutgoing 参数
public ChatMessage(String messageId, String username, String message, long timestamp,
boolean isSystemMessage, MessageStatus status, boolean isOutgoing) {
this.messageId = messageId;
this.username = username;
this.message = message;
this.timestamp = timestamp;
this.isSystemMessage = isSystemMessage;
this.status = status;
this.messageType = MessageType.TEXT;
this.isOutgoing = isOutgoing;
}
// 图片消息构造函数
@ -140,6 +156,14 @@ public class ChatMessage {
isGiftMessage = giftMessage;
}
public boolean isOutgoing() {
return isOutgoing;
}
public void setOutgoing(boolean outgoing) {
isOutgoing = outgoing;
}
public MessageStatus getStatus() {
return status;
}

View File

@ -181,7 +181,8 @@ public class ConversationActivity extends AppCompatActivity {
return;
}
String url = ApiConfig.getBaseUrl() + "/api/front/user/info";
// 使用正确的用户信息接口
String url = ApiConfig.getBaseUrl() + "/api/front/user";
Log.d(TAG, "获取用户信息: " + url);
Request request = new Request.Builder()
@ -205,14 +206,31 @@ public class ConversationActivity extends AppCompatActivity {
if (json.optInt("code", -1) == 200) {
JSONObject data = json.optJSONObject("data");
if (data != null) {
// 尝试多种方式获取用户ID
int uid = data.optInt("uid", 0);
if (uid == 0) {
uid = data.optInt("id", 0);
}
if (uid == 0) {
// 尝试从字符串解析
String uidStr = data.optString("uid", "");
if (!uidStr.isEmpty()) {
try {
uid = (int) Double.parseDouble(uidStr);
} catch (NumberFormatException e) {
Log.e(TAG, "解析uid失败: " + uidStr, e);
}
}
}
if (uid > 0) {
currentUserId = String.valueOf(uid);
// 保存到 AuthStore
AuthStore.setUserInfo(ConversationActivity.this, currentUserId, data.optString("nickname", ""));
AuthStore.setUserInfo(ConversationActivity.this, currentUserId, data.optString("nickname", data.optString("nikeName", "")));
Log.d(TAG, "从服务器获取到用户ID: " + currentUserId);
// 重新加载消息以正确显示
runOnUiThread(() -> loadMessagesFromServer());
} else {
Log.w(TAG, "无法从响应中获取用户ID: " + body);
}
}
}
@ -308,8 +326,28 @@ public class ConversationActivity extends AppCompatActivity {
boolean isMine = false;
if (myUserId != null && !myUserId.isEmpty() && senderId > 0) {
// 处理可能的浮点数格式 "1.0" vs "1"
try {
int myUid = (int) Double.parseDouble(myUserId);
isMine = (myUid == senderId);
} catch (NumberFormatException e) {
isMine = myUserId.equals(String.valueOf(senderId));
}
}
// 如果 senderId 0尝试从 senderId 字段获取
if (senderId == 0) {
senderId = item.optInt("senderId", 0);
if (myUserId != null && !myUserId.isEmpty() && senderId > 0) {
try {
int myUid = (int) Double.parseDouble(myUserId);
isMine = (myUid == senderId);
} catch (NumberFormatException e) {
isMine = myUserId.equals(String.valueOf(senderId));
}
}
}
Log.d(TAG, "消息判断: myUserId=" + myUserId + ", senderId=" + senderId + ", isMine=" + isMine);
String displayName = isMine ? "" : username;
@ -327,6 +365,7 @@ public class ConversationActivity extends AppCompatActivity {
}
ChatMessage chatMessage = new ChatMessage(messageId, displayName, message, timestamp, isSystem, msgStatus);
chatMessage.setOutgoing(isMine); // 关键设置消息方向确保自己发送的消息显示在右侧
chatMessage.setAvatarUrl(avatarUrl);
return chatMessage;
} catch (Exception e) {

View File

@ -70,7 +70,8 @@ public class ConversationMessagesAdapter extends ListAdapter<ChatMessage, Recycl
ChatMessage msg = getItem(position);
if (msg == null) return TYPE_TEXT_INCOMING;
boolean isOutgoing = "".equals(msg.getUsername());
// 优先使用 isOutgoing 字段如果没有设置则回退到检查用户名
boolean isOutgoing = msg.isOutgoing() || "".equals(msg.getUsername());
ChatMessage.MessageType type = msg.getMessageType();
if (type == null) type = ChatMessage.MessageType.TEXT;

View File

@ -31,11 +31,15 @@ import com.example.livestreaming.net.ApiService;
import com.example.livestreaming.net.AuthStore;
import com.example.livestreaming.net.CreateRechargeRequest;
import com.example.livestreaming.net.CreateRechargeResponse;
import com.example.livestreaming.net.GiftResponse;
import com.example.livestreaming.net.OrderPayRequest;
import com.example.livestreaming.net.OrderPayResultResponse;
import com.example.livestreaming.net.RechargeOptionResponse;
import com.example.livestreaming.net.Room;
import com.example.livestreaming.net.SendGiftRequest;
import com.example.livestreaming.net.SendGiftResponse;
import com.example.livestreaming.net.StreamConfig;
import com.example.livestreaming.net.UserBalanceResponse;
import com.example.livestreaming.ShareUtils;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
@ -44,7 +48,9 @@ import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import okhttp3.OkHttpClient;
@ -353,8 +359,9 @@ public class RoomDetailActivity extends AppCompatActivity {
stopOnlineReconnect();
// 构建WebSocket URL添加clientId参数
String clientId = AuthStore.getUserId(this) > 0 ?
String.valueOf(AuthStore.getUserId(this)) :
String userIdStr = AuthStore.getUserId(this);
String clientId = (userIdStr != null && !userIdStr.isEmpty()) ?
userIdStr :
"guest_" + System.currentTimeMillis();
String wsUrl = WS_ONLINE_BASE_URL + roomId + "?clientId=" + clientId;
@ -1149,7 +1156,7 @@ public class RoomDetailActivity extends AppCompatActivity {
}
android.util.Log.d("RoomDetail", "成功加载 " + availableGifts.size() + " 个礼物");
} else {
android.util.Log.w("RoomDetail", "加载礼物列表失败: " + apiResponse.getMsg());
android.util.Log.w("RoomDetail", "加载礼物列表失败: " + apiResponse.getMessage());
setDefaultGifts();
}
} else {
@ -1485,7 +1492,7 @@ public class RoomDetailActivity extends AppCompatActivity {
ApiService apiService = ApiClient.getService(this);
OrderPayRequest payRequest = new OrderPayRequest(orderId, payType, payChannel);
Call<ApiResponse<OrderPayResultResponse>> call = apiService.processPayment(payRequest);
Call<ApiResponse<OrderPayResultResponse>> call = apiService.payment(payRequest);
call.enqueue(new Callback<ApiResponse<OrderPayResultResponse>>() {
@Override
@ -1597,10 +1604,13 @@ public class RoomDetailActivity extends AppCompatActivity {
ApiService apiService = ApiClient.getService(getApplicationContext());
SendGiftRequest request = new SendGiftRequest();
request.setRoomId(Integer.parseInt(roomId));
request.setGiftId(Integer.parseInt(selectedGift.getId()));
request.setCount(count);
// 获取主播ID使用房间ID作为主播ID或者从房间信息中获取
Integer streamerId = Integer.parseInt(roomId);
SendGiftRequest request = new SendGiftRequest(
Integer.parseInt(selectedGift.getId()),
streamerId,
count
);
Call<ApiResponse<SendGiftResponse>> call = apiService.sendRoomGift(roomId, request);
@ -1635,7 +1645,7 @@ public class RoomDetailActivity extends AppCompatActivity {
giftCountText.setText("1");
} else {
Toast.makeText(RoomDetailActivity.this,
"赠送失败: " + apiResponse.getMsg(),
"赠送失败: " + apiResponse.getMessage(),
Toast.LENGTH_SHORT).show();
}
} else {
@ -1665,7 +1675,7 @@ public class RoomDetailActivity extends AppCompatActivity {
ApiService apiService = ApiClient.getService(getApplicationContext());
java.util.Map<String, Object> body = new java.util.HashMap<>();
body.put("streamerId", room.getStreamerId());
body.put("streamerId", roomId); // 使用房间ID作为主播ID
body.put("action", "follow");
Call<ApiResponse<Map<String, Object>>> call = apiService.followStreamer(body);
@ -1682,7 +1692,7 @@ public class RoomDetailActivity extends AppCompatActivity {
binding.followButton.setEnabled(false);
} else {
Toast.makeText(RoomDetailActivity.this,
"关注失败: " + apiResponse.getMsg(),
"关注失败: " + apiResponse.getMessage(),
Toast.LENGTH_SHORT).show();
}
} else {
@ -1731,7 +1741,7 @@ public class RoomDetailActivity extends AppCompatActivity {
fetchRoom();
} else {
Toast.makeText(RoomDetailActivity.this,
"开始直播失败: " + apiResponse.getMsg(),
"开始直播失败: " + apiResponse.getMessage(),
Toast.LENGTH_SHORT).show();
}
} else {
@ -1785,7 +1795,7 @@ public class RoomDetailActivity extends AppCompatActivity {
fetchRoom();
} else {
Toast.makeText(RoomDetailActivity.this,
"结束直播失败: " + apiResponse.getMsg(),
"结束直播失败: " + apiResponse.getMessage(),
Toast.LENGTH_SHORT).show();
}
} else {

View File

@ -373,10 +373,6 @@ public interface ApiService {
Call<ApiResponse<Boolean>> queryAliPayResult(@Query("orderNo") String orderNo);
@POST("api/front/pay/payment")
<<<<<<< HEAD
Call<ApiResponse<OrderPayResultResponse>> processPayment(@Body OrderPayRequest body);
=======
Call<ApiResponse<OrderPayResultResponse>> payment(@Body OrderPayRequest body);
// ==================== 分类管理 ====================
@ -405,5 +401,4 @@ public interface ApiService {
Call<ApiResponse<List<CategoryResponse>>> getChildCategories(
@Path("parentId") Integer parentId,
@Query("recursive") Boolean recursive);
>>>>>>> d8f9cc8959c6d8d0d49e022a1f05833053abb8be
}