chat.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <template>
  2. <view class="chat-container">
  3. <scroll-view refresher-enabled :refresher-triggered="isRefreshing" @refresherrefresh="onCustomRefresh"
  4. class="scroll-view-class" @scrolltolower="scrolltolower" scroll-y>
  5. <view class="chat-main">
  6. <view v-if="list && list.length > 0">
  7. <uni-swipe-action>
  8. <uni-swipe-action-item v-for="item in list" :key="item.code" :right-options="[
  9. {
  10. text: '删除',
  11. style: {
  12. backgroundColor: '#ff4949',
  13. width: '80px'
  14. }
  15. }
  16. ]" @click="handleDelete(item)">
  17. <template #default>
  18. <view class="chat-item" @click="onClick(item)">
  19. <img v-if="item.conversationAvatar" :src="item.conversationAvatar" alt=""
  20. class="chat-img" />
  21. <img src="/static/serverImg/mine/user.png" alt="" class="chat-img" v-else />
  22. <view class="chat-box">
  23. <view class="chat-top">
  24. <text class="chat-name">{{ item.conversationType === '1' ? '系统消息' :
  25. type?item.volunteerName:item.userName }}</text>
  26. <text class="chat-time">{{ handlerData(item.newestMsgTime ||
  27. item.createTime) }}</text>
  28. </view>
  29. <view class="chat-bottom">
  30. <text class="chat-text">
  31. {{ item.msgType === '2' ? '[图片]' : item.newestMsgContent || '[暂无消息]' }}
  32. </text>
  33. <view class="chat-num"
  34. v-if="item.msgUnreadCount && item.msgUnreadCount > 0">
  35. {{ item.msgUnreadCount }}
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. </template>
  41. </uni-swipe-action-item>
  42. </uni-swipe-action>
  43. </view>
  44. <view v-else>
  45. <NoneView value="您还没有相关消息" />
  46. </view>
  47. </view>
  48. </scroll-view>
  49. <custom-tab-bar page="chat" />
  50. </view>
  51. </template>
  52. <script setup>
  53. import CustomTabBar from '@/components/CustomTabBar/index.vue'
  54. import { ref, computed, reactive } from 'vue'
  55. import { getAccountChangeList, getVolunteerChangeList } from "@/api/mine";
  56. import { onShow } from '@dcloudio/uni-app';
  57. import { useDict } from '@/utils/dict.js';
  58. import NoneView from '@/components/NoneView/index.vue'
  59. import dayjs from 'dayjs/esm/index'
  60. import { getList, conversationRemove } from '@/api/conversation.js';
  61. const { } = useDict();
  62. const userType = uni.getStorageSync('userType') //读取本地存储
  63. console.log("TCL: userType", userType)
  64. const list = ref([])
  65. const type = computed(() => {
  66. return userType === 1
  67. })
  68. const isRefreshing = ref(false)
  69. const loadmoreInfo = ref({
  70. status: 'loadmore',
  71. loadingText: '努力加载中...',
  72. loadmoreText: '点击加载更多~',
  73. nomoreText: '您没有更多消息~'
  74. })
  75. const pages = ref({
  76. current: 1,
  77. pageSize: 10,
  78. total: 0,
  79. })
  80. const isToday = (date) => {
  81. return dayjs(date).isSame(dayjs(), 'day');
  82. };
  83. const isYesterday = (date) => {
  84. return dayjs(date).isSame(dayjs().subtract(1, 'day'), 'day');
  85. };
  86. const handlerData = (dates) => {
  87. const date = dayjs(dates);
  88. if (isToday(dates)) {
  89. return date.format('HH:MM');;
  90. } else if (isYesterday(dates)) {
  91. return '昨天';
  92. } else {
  93. return date.format('YY/MM/DD'); // 或者其他格式如 'YYYY年MM月DD日'
  94. }
  95. }
  96. const onClick = (record) => {
  97. console.log("TCL: onClick -> record", record)
  98. uni.navigateTo({
  99. url: `/pages_orderuser/pages/talk/pages/index/index?conversationRecordId=${record.conversationRecordId}`
  100. });
  101. }
  102. const scrolltolower = () => {
  103. init('bottom')
  104. };
  105. const onCustomRefresh = () => {
  106. isRefreshing.value = true;
  107. pages.value.current = 1;
  108. init('top')
  109. };
  110. const handleDelete = (item) => {
  111. uni.showModal({
  112. title: '提示',
  113. content: '确定要删除该聊天吗?',
  114. success: async (res) => {
  115. if (res.confirm) {
  116. // 调用删除接口
  117. try {
  118. await conversationRemove({
  119. conversationRecordId: item.conversationRecordId,
  120. system: userType === 1 ? '1' : '2'
  121. }); // 替换为实际接口
  122. uni.showToast({ title: '删除成功' });
  123. init('top'); // 刷新列表
  124. } catch (err) {
  125. uni.showToast({ title: '删除失败', icon: 'none' });
  126. }
  127. }
  128. }
  129. });
  130. };
  131. const init = async (type) => {
  132. try {
  133. if (type === 'bottom') {
  134. if (list.value.length < pages.value.total) {
  135. loadmoreInfo.value.status = 'loading';
  136. pages.value.current++;
  137. } else {
  138. loadmoreInfo.value.status = 'nomore';
  139. return;
  140. }
  141. } else {
  142. uni.showLoading({
  143. title: '数据加载中...',
  144. })
  145. }
  146. const res = await getList({
  147. // pageNum: pages.value.current,
  148. // pageSize: pages.value.pageSize,
  149. system: userType === 1 ? '1' : '2'
  150. });
  151. list.value = type === 'top' ? res.rows : [...list.value, ...res.rows];
  152. pages.value.total = res.total;
  153. } catch (error) {
  154. console.log('error', error);
  155. uni.showToast({
  156. title: error.msg,
  157. icon: 'error',
  158. });
  159. } finally {
  160. if (type === 'top') {
  161. isRefreshing.value = false;
  162. uni.hideLoading();
  163. }
  164. if (list.value.length === pages.value.total) {
  165. loadmoreInfo.value.status = 'nomore';
  166. }
  167. }
  168. }
  169. const totalInit = async () => {
  170. // try {
  171. // const listApi = userType === 1 ? getTotalMoney : getVolunteerTotalMoney;
  172. // const res = await listApi({});
  173. // data.value.totalEarning = res.data.totalEarning;
  174. // data.value.totalExpend = res.data.totalExpend;
  175. // } catch (error) {
  176. // console.log('error', error);
  177. // uni.showToast({
  178. // title: error.msg,
  179. // icon: 'error',
  180. // });
  181. // }
  182. }
  183. onShow(() => {
  184. init('top');
  185. // totalInit();
  186. })
  187. </script>
  188. <style lang="scss" scoped>
  189. .chat-container {
  190. height: 100vh;
  191. .scroll-view-class {
  192. height: 100vh;
  193. // background: rgba(245, 245, 245, 1);
  194. }
  195. .chat-item {
  196. padding: 16rpx;
  197. display: flex;
  198. border-bottom: 1px solid rgba(238, 238, 238, 1);
  199. .chat-img {
  200. width: 96rpx;
  201. height: 96rpx;
  202. border-radius: 16rpx;
  203. margin-right: 12rpx;
  204. }
  205. .chat-box {
  206. // border-bottom: 1px solid rgba(238, 238, 238, 1);
  207. flex: 1;
  208. // padding-bottom: 16rpx;
  209. .chat-top {
  210. display: flex;
  211. align-items: center;
  212. justify-content: space-between;
  213. .chat-name {
  214. font-size: 32rpx;
  215. font-weight: 400;
  216. line-height: 48rpx;
  217. color: rgba(0, 0, 0, 1);
  218. }
  219. .chat-time {
  220. font-size: 28rpx;
  221. font-weight: 400;
  222. line-height: 40rpx;
  223. color: rgba(75, 85, 99, 1);
  224. }
  225. }
  226. .chat-bottom {
  227. display: flex;
  228. justify-content: space-between;
  229. .chat-text {
  230. font-size: 28rpx;
  231. font-weight: 400;
  232. line-height: 40rpx;
  233. color: rgba(102, 102, 102, 1);
  234. width: 250px;
  235. white-space: nowrap;
  236. /* 禁止换行 */
  237. overflow: hidden;
  238. /* 隐藏溢出内容 */
  239. text-overflow: ellipsis;
  240. /* 溢出部分显示省略号 */
  241. }
  242. .chat-num {
  243. background: rgba(239, 68, 68, 1);
  244. color: #fff;
  245. display: flex;
  246. align-items: center;
  247. justify-content: center;
  248. width: 40rpx;
  249. height: 40rpx;
  250. border-radius: 40rpx;
  251. line-height: 40rpx;
  252. font-size: 24rpx;
  253. }
  254. }
  255. }
  256. }
  257. }
  258. .chat-item {
  259. transition: transform 0.2s ease;
  260. }
  261. </style>