index.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { Text, View } from "@tarojs/components";
  2. import { useLoad, setNavigationBarTitle, navigateTo } from "@tarojs/taro";
  3. import { timeFormat } from "../../../utils";
  4. import { Image } from "@nutui/nutui-react-taro";
  5. import { Message } from "@nutui/icons-react-taro";
  6. import { useEffect, useState } from "react";
  7. import CommentInput from "../../../components/comment-list/comment-input";
  8. import {
  9. getNotificationListApi,
  10. markAsReadByTypeApi,
  11. } from "../../../api/notifications";
  12. const MessageList = () => {
  13. const [showCommentInput, setShowCommentInput] = useState(false);
  14. const [notificationList, setNotificationList] = useState<any[]>([]);
  15. const [CommentPostId, setCommentPostId] = useState(0);
  16. const [CommentParentCommentId, setCommentParentCommentId] = useState(0);
  17. const [type, setType] = useState("");
  18. const [typeId, setTypeId] = useState("0");
  19. useLoad((options) => {
  20. setType(options.type);
  21. switch (options.type) {
  22. case "like":
  23. setTypeId("4,5");
  24. handleReadNotification("4,5");
  25. setNavigationBarTitle({
  26. title: "收到的点赞",
  27. });
  28. break;
  29. case "comment":
  30. setTypeId("1,2,3");
  31. handleReadNotification("1,2,3");
  32. setNavigationBarTitle({
  33. title: "评论和@",
  34. });
  35. break;
  36. case "follow":
  37. setTypeId("6,7");
  38. handleReadNotification("6,7");
  39. setNavigationBarTitle({
  40. title: "关注和分享",
  41. });
  42. break;
  43. default:
  44. break;
  45. }
  46. });
  47. // 标记消息类型通知为已读
  48. const handleReadNotification = async (type_id) => {
  49. markAsReadByTypeApi({ type_id });
  50. };
  51. const getNotificationList = async () => {
  52. const res: any = await getNotificationListApi({
  53. type_id: typeId,
  54. page: 1,
  55. page_size: 10,
  56. });
  57. if (res.code === 200) {
  58. setNotificationList(res.data.list);
  59. }
  60. };
  61. useEffect(() => {
  62. getNotificationList();
  63. }, [typeId]);
  64. return (
  65. <View className="bg-[#f8f8f8]">
  66. {notificationList.map((item) => {
  67. return (
  68. <View className="p-[10px] text-[14px] flex mb-[2px] bg-[#fff]">
  69. <View className="w-[50px] h-[50px] bg-[#000] rounded-[50%] overflow-hidden">
  70. <Image
  71. width={50}
  72. height={50}
  73. src="https://storage.360buyimg.com/jdc-article/NutUItaro34.jpg"
  74. mode="aspectFill"
  75. />
  76. </View>
  77. <View className="flex-1 ml-[10px]">
  78. <View className="text-[14px] font-[800] mb-[3px]">
  79. {item.nickname || item.username}
  80. </View>
  81. <View className="text-[12px] text-[#949494] mb-[3px]">
  82. {timeFormat(item.created_at)}
  83. </View>
  84. <View className="text-[14px] text-[#3d3d3d]">
  85. {item.type_id === 4 ? "点赞了你的动态" : ""}
  86. {item.type_id === 5 ? "点赞了你的评论" : ""}
  87. {item.type_id === 2 ? "评论了你的动态" : ""}
  88. {item.type_id === 3 ? item.reply_comment_info.content : ""}
  89. {item.type_id === 1
  90. ? `${
  91. item.source_type === "post" ? "在动态中" : "在评论中"
  92. }刚刚@了你 `
  93. : ""}
  94. {item.type_id === 6 ? "关注了你" : ""}
  95. {item.type_id === 7 ? "分享了你的动态" : ""}
  96. </View>
  97. <View
  98. className="text-[#858585] text-[14px] pl-[5px] mt-[10px]"
  99. style={{ borderLeft: "4rpx solid #e2e2e2" }}
  100. >
  101. {item.source_type === "comment" ? item.sourse_info.content : ""}
  102. {item.source_type === "post" && item.type_id === 2
  103. ? item.reply_comment_info.content
  104. : ""}
  105. </View>
  106. {/* 回复/评论 */}
  107. {item.type_id === 2 ||
  108. item.type_id === 3 ||
  109. (item.type_id === 1 && item.source_type === "comment") ? (
  110. <View
  111. className="flex mt-[10px]"
  112. onClick={() => {
  113. setShowCommentInput(true);
  114. setCommentPostId(item.source_post_info.id);
  115. setCommentParentCommentId(item.reply_comment_info.id);
  116. }}
  117. >
  118. <View className="pt-[5px] pb-[5px] pl-[10px] pr-[10px] bg-[#f8f8f8] flex items-center rounded-[20px]">
  119. <Message size={16} color="#949494" />
  120. <Text className="text-[12px] ml-[5px]">回复</Text>
  121. </View>
  122. </View>
  123. ) : (
  124. <></>
  125. )}
  126. </View>
  127. {item.source_post_info ? (
  128. <View
  129. className="text-[12px] text-[#333]"
  130. onClick={() => {
  131. navigateTo({
  132. url: `/pages/detail/index?id=${item.source_post_info.id}`,
  133. });
  134. }}
  135. >
  136. <Image
  137. width={50}
  138. height={50}
  139. src={
  140. item.source_post_info.media_url
  141. ? item.source_post_info.media_url.split(",")[0]
  142. : ""
  143. }
  144. mode="aspectFill"
  145. />
  146. </View>
  147. ) : (
  148. <></>
  149. )}
  150. </View>
  151. );
  152. })}
  153. {showCommentInput ? (
  154. <CommentInput
  155. postId={CommentPostId}
  156. parentCommentId={CommentParentCommentId}
  157. onClose={() => setShowCommentInput(false)}
  158. onOK={() => {
  159. setShowCommentInput(false);
  160. getNotificationList();
  161. }}
  162. />
  163. ) : null}
  164. </View>
  165. );
  166. };
  167. export default MessageList;