messageList.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. <template>
  2. <!-- style="padding:14px 0 4px;" -->
  3. <Theme>
  4. <view
  5. class="message-list__wrap"
  6. :style="{ height: props.height ? props.height : 'auto' }"
  7. >
  8. <scroll-view
  9. :style="{ height: '100%' }"
  10. scroll-y
  11. :scroll-top="
  12. scrollTopAfterListUpdated
  13. ? scrollTopAfterListUpdated
  14. : listNodeInfo
  15. ? listNodeInfo.height - props.tabbarHeight
  16. : 0
  17. "
  18. :scroll-with-animation="scrollTopAfterListUpdated <= 0 && true"
  19. :upper-threshold="1"
  20. :lower-threshold="props.lowerThreshold"
  21. class="message-list__scroll"
  22. @scroll="onScroll"
  23. @scrolltoupper="onScrolltoupper"
  24. @scrolltolower="onScrollLower"
  25. >
  26. <view class="list_box">
  27. <!-- 咨询卡片 -->
  28. <view
  29. style="
  30. padding: 4px 30rpx 0;
  31. position: sticky;
  32. top: -2px;
  33. left: 0;
  34. z-index: 10;
  35. background-color: #fafafa;
  36. "
  37. >
  38. <ConsultCard></ConsultCard>
  39. <!-- <view class="list_head" v-else>
  40. <view class="list_head-left">
  41. <image class="list_head-avatar" :src="avatar"></image>
  42. <view>
  43. <view class="list_head-nickname">{{ nickname }}</view>
  44. <view class="list_head-online">
  45. <text class="list_head-online-status"></text>
  46. <text>{{ $t("在线") }}</text>
  47. </view>
  48. </view>
  49. </view>
  50. <view class="list_head-right">
  51. <view
  52. class="list_head-right-btn"
  53. style="margin-right: 5px"
  54. @click="handleOpenModal(1)"
  55. >{{ $t("结束会话") }}
  56. </view>
  57. <view class="list_head-right-btn" @click="handleOpenModal(2)">{{
  58. $t("申请退款")
  59. }}</view>
  60. </view>
  61. </view> -->
  62. </view>
  63. <!-- loading start -->
  64. <view v-if="props.loading">
  65. <up-loadmore :status="'loading'" :loadingText="$t('正在加载')" />
  66. </view>
  67. <!-- loading end -->
  68. <!-- 消息列表 -->
  69. <view class="message_list">
  70. <template v-for="(item, i) in dataList" :key="i">
  71. <!-- 时间 -->
  72. <view
  73. class="message__item message_item__tips"
  74. v-if="handleTime(i, dataList)"
  75. >
  76. <view class="message__item-time">
  77. <text>{{ handleTime(i, dataList) }}</text>
  78. </view>
  79. </view>
  80. <!-- 系统关闭提示信息 -->
  81. <view
  82. class="message__item message_item__tips"
  83. v-if="item.msg.type == 'service_close'"
  84. >
  85. <view class="message__item-time">
  86. <text>{{ $t("会话已关闭") }}</text>
  87. </view>
  88. </view>
  89. <view
  90. class="message__item message_item__tips"
  91. v-else-if="item.msg.type == 'service_start'"
  92. >
  93. <TipsListCard :datas="item.msg.data"></TipsListCard>
  94. </view>
  95. <view
  96. class="message__item"
  97. :class="[item.send > 0 ? 'message__item-self' : '']"
  98. v-else
  99. >
  100. <!-- 头像 -->
  101. <view class="message__item-left">
  102. <image
  103. v-if="item.send > 0"
  104. class="message__item-avatar"
  105. :src="item.uimg || getuserInfo.userimg"
  106. mode=""
  107. ></image>
  108. <image
  109. v-else
  110. class="message__item-avatar"
  111. :src="item.simg"
  112. mode=""
  113. ></image>
  114. </view>
  115. <!-- 内容 -->
  116. <view class="message__item-right">
  117. <!-- 图片 -->
  118. <view v-if="item.msg.type == 'image'">
  119. <up-image
  120. :show-loading="true"
  121. :src="item.msg.url"
  122. width="240rpx"
  123. height="240rpx"
  124. :fade="false"
  125. @click="previewImage(1, [item.msg.url])"
  126. ></up-image>
  127. </view>
  128. <!-- 欢迎信息 -->
  129. <view
  130. style="padding: 20rpx 30rpx"
  131. v-else-if="item.msg.type == 'service_welcome'"
  132. >
  133. <text>{{ item.msg.body }}</text>
  134. </view>
  135. <!-- 文本 -->
  136. <view style="padding: 20rpx 30rpx" v-else>
  137. <text>{{ item.msg.text || "" }}</text>
  138. </view>
  139. </view>
  140. <view class="message__item-status">
  141. <up-loading-icon
  142. mode="circle"
  143. size="40rpx"
  144. v-if="item.loading"
  145. ></up-loading-icon>
  146. </view>
  147. </view>
  148. </template>
  149. </view>
  150. </view>
  151. </scroll-view>
  152. </view>
  153. <Popup
  154. ref="tipPopupRef"
  155. mode="center"
  156. class="message_list_tip_popup"
  157. title="提示"
  158. >
  159. <template #content>
  160. <view class="popup_content">
  161. <text>{{ $t(tipPopupContent) }}</text>
  162. </view>
  163. </template>
  164. <template #footer>
  165. <view class="popup__footer">
  166. <view class="popup__footer-btn" @click="onClose">{{
  167. $t("取消")
  168. }}</view>
  169. <view
  170. class="popup__footer-btn popup__footer-btn__confirm"
  171. @click="onConfirm"
  172. >
  173. <up-loading-icon
  174. size="30rpx"
  175. style="margin-right: 12rpx"
  176. v-if="isSubmitting"
  177. ></up-loading-icon>
  178. <text>{{ $t("确认") }}</text>
  179. </view>
  180. </view>
  181. </template>
  182. </Popup>
  183. </Theme>
  184. </template>
  185. <script setup>
  186. import {
  187. ref,
  188. getCurrentInstance,
  189. nextTick,
  190. onMounted,
  191. watch,
  192. computed,
  193. inject,
  194. } from "vue";
  195. import { t } from "@/locale";
  196. import { storeToRefs } from "pinia";
  197. import { onReady } from "@dcloudio/uni-app";
  198. import { query, TimeOut, format, Toast } from "@/utils";
  199. import Popup from "@/components/popup.vue";
  200. import ConsultCard from "./consultCard.vue";
  201. import QuestionCard from "./questionCard.vue";
  202. import TipsListCard from "./tipsListCard.vue";
  203. import { useUserStore, useMessageStore } from "@/store";
  204. import useTools from "../hooks/useTools";
  205. import { RONGIMCALL_PAY_RETURN } from "@/api";
  206. const props = defineProps({
  207. lowerThreshold: {
  208. type: Number,
  209. default: 50,
  210. },
  211. height: {
  212. type: String,
  213. default: "",
  214. },
  215. listHeight: {
  216. type: Number,
  217. default: 0,
  218. },
  219. tabbarHeight: {
  220. type: Number,
  221. default: 0,
  222. },
  223. dataList: {
  224. type: Array,
  225. default() {
  226. return [];
  227. },
  228. },
  229. loading: {
  230. type: Boolean,
  231. default: false,
  232. },
  233. });
  234. const emits = defineEmits(["on-scroll", "on-scroll-top", "on-scroll-bottom"]);
  235. const tipPopupRef = ref(null);
  236. const tipPopupContent = ref("");
  237. const popupFlag = ref(0);
  238. const isSubmitting = ref(false);
  239. const channelData = inject("channelData");
  240. const channelID = inject("channel");
  241. const payStatus = ref(0);
  242. const avatar = ref("");
  243. const nickname = ref("");
  244. const useUser = useUserStore();
  245. const { getuserInfo } = storeToRefs(useUser);
  246. const useMessage = useMessageStore();
  247. const { handleTime } = useTools();
  248. const scrollTopAfterListUpdated = ref(0);
  249. const _this = getCurrentInstance();
  250. const currentScrollTop = ref(0);
  251. const oldListNodeInfo = ref(null);
  252. const listNodeInfo = ref(null);
  253. const isInit = ref(true);
  254. const scrollTop = computed(() => {
  255. const listHeight = listNodeInfo.value ? listNodeInfo.value.height : 0;
  256. const boxHeight = props.listHeight;
  257. let scrollHeight = listHeight - boxHeight - props.tabbarHeight;
  258. return scrollHeight > 50 ? scrollHeight : 0;
  259. });
  260. watch(
  261. () => channelData.value,
  262. (newData) => {
  263. payStatus.value = newData ? newData.openInfo.status : 0;
  264. avatar.value = newData ? newData.openInfo.simg : "";
  265. nickname.value = newData ? newData.openInfo.sname : "";
  266. },
  267. {
  268. deep: true,
  269. }
  270. );
  271. watch(
  272. () => props.height,
  273. (newHeight) => {
  274. computeScrollHeight();
  275. }
  276. );
  277. watch(
  278. () => props.dataList.length,
  279. (newLen, oldLen) => {
  280. if (newLen - oldLen > 0 && isInit.value) {
  281. computeScrollHeight().then(() => {
  282. isInit.value = false;
  283. });
  284. }
  285. },
  286. { immediate: true }
  287. );
  288. watch(
  289. () => props.loading,
  290. (newState, oldState) => {
  291. nextTick(async () => {
  292. if (newState == false && oldState == true && oldListNodeInfo.value) {
  293. oldListNodeInfo.value = listNodeInfo.value;
  294. listNodeInfo.value = await query(".list_box", _this);
  295. const value =
  296. listNodeInfo.value.height -
  297. oldListNodeInfo.value?.height -
  298. props.tabbarHeight +
  299. currentScrollTop.value;
  300. scrollTopAfterListUpdated.value = value > 0 ? value : 1;
  301. console.log(
  302. "scrollTopAfterListUpdated.value",
  303. scrollTopAfterListUpdated.value
  304. );
  305. }
  306. });
  307. }
  308. );
  309. function updateInitState(state) {
  310. isInit.value = state == true ? state : false;
  311. }
  312. function handleOpenModal(flag) {
  313. popupFlag.value = flag;
  314. if (flag == 1) {
  315. tipPopupContent.value = "page.user_chat.tipPopupContentForCloseChat";
  316. } else if (flag == 2) {
  317. tipPopupContent.value = "page.user_chat.tipPopupContentForRefund";
  318. }
  319. tipPopupRef.value && tipPopupRef.value.open();
  320. }
  321. function onClose() {
  322. tipPopupRef.value && tipPopupRef.value.close();
  323. }
  324. function onConfirm() {
  325. if (isSubmitting.value) return;
  326. isSubmitting.value = true;
  327. if (popupFlag.value == 1) {
  328. useMessage.closeChannel().then((result) => {
  329. Toast(result.msg);
  330. useMessage.updateStatus(channelID.value, 0);
  331. isSubmitting.value = false;
  332. tipPopupRef.value && tipPopupRef.value.close();
  333. });
  334. } else if (popupFlag.value == 2) {
  335. RONGIMCALL_PAY_RETURN()
  336. .then((result) => {
  337. Toast(result.msg);
  338. isSubmitting.value = false;
  339. tipPopupRef.value && tipPopupRef.value.close();
  340. })
  341. .catch((err) => {
  342. Toast(err.msg);
  343. });
  344. }
  345. }
  346. function previewImage(index, urls) {
  347. uni.previewImage({
  348. current: index,
  349. urls: urls,
  350. success() {},
  351. });
  352. }
  353. function computeScrollHeight() {
  354. return new Promise((resolve, reject) => {
  355. nextTick(() => {
  356. let timer = TimeOut(async () => {
  357. oldListNodeInfo.value = listNodeInfo.value;
  358. listNodeInfo.value = null;
  359. listNodeInfo.value = await query(".list_box", _this);
  360. scrollTopAfterListUpdated.value = 0;
  361. resolve(listNodeInfo.value);
  362. }, 50);
  363. });
  364. });
  365. }
  366. // 滚动到顶部
  367. function onScrolltoupper() {
  368. emits("on-scroll-top");
  369. }
  370. function onScrollLower() {
  371. emits("on-scroll-bottom");
  372. }
  373. function onScroll(e) {
  374. currentScrollTop.value = e.detail.scrollTop;
  375. emits("on-scroll", e);
  376. }
  377. defineExpose({
  378. computeScrollHeight,
  379. updateInitState,
  380. });
  381. </script>
  382. <style scoped lang="less">
  383. .message-list__wrap {
  384. width: 100%;
  385. height: 100%;
  386. overflow: hidden;
  387. }
  388. .message-list__scroll {
  389. padding: 0 0rpx 0rpx;
  390. box-sizing: border-box;
  391. }
  392. .list_box {
  393. position: relative;
  394. }
  395. .list_head {
  396. width: 100%;
  397. padding: 20rpx 0;
  398. display: flex;
  399. justify-content: space-between;
  400. align-items: center;
  401. .list_head-left {
  402. display: flex;
  403. align-items: center;
  404. }
  405. .list_head-avatar {
  406. overflow: hidden;
  407. width: 80rpx;
  408. height: 80rpx;
  409. margin-right: 16rpx;
  410. border-radius: 50%;
  411. }
  412. .list_head-nickname {
  413. font-weight: 600;
  414. font-size: 30rpx;
  415. }
  416. .list_head-online {
  417. display: flex;
  418. align-items: center;
  419. font-size: 24rpx;
  420. margin-top: 3px;
  421. }
  422. .list_head-online-status {
  423. width: 20rpx;
  424. height: 20rpx;
  425. margin-right: 4px;
  426. background-color: #39b54a;
  427. border-radius: 50%;
  428. }
  429. .list_head-right {
  430. display: flex;
  431. }
  432. .list_head-right-btn {
  433. padding: 0 20rpx;
  434. height: 52rpx;
  435. background-color: var(--black);
  436. border-radius: 26rpx;
  437. color: var(--light);
  438. font-size: 24rpx;
  439. line-height: 52rpx;
  440. }
  441. }
  442. .message_list {
  443. margin-top: 30rpx;
  444. padding: 20rpx 30rpx 30rpx;
  445. .message__item {
  446. width: 100%;
  447. display: flex;
  448. align-items: flex-start;
  449. margin-bottom: 30rpx;
  450. }
  451. .message__item-self {
  452. display: flex;
  453. flex-direction: row-reverse;
  454. .message__item-right {
  455. margin-left: 0;
  456. margin-right: 16rpx;
  457. background-color: var(--black);
  458. color: #fff;
  459. }
  460. .message__item-status {
  461. margin-left: 0;
  462. margin-right: 12rpx;
  463. }
  464. }
  465. .message_item__tips {
  466. justify-content: center;
  467. margin-bottom: 24rpx;
  468. font-size: 24rpx;
  469. color: #767676;
  470. }
  471. .message__item-avatar {
  472. width: 80rpx;
  473. height: 80rpx;
  474. border-radius: 50%;
  475. }
  476. .message__item-right {
  477. overflow: hidden;
  478. max-width: calc(100% - (80rpx * 2) - 16rpx - 20rpx - 40rpx - 12rpx);
  479. margin-left: 16rpx;
  480. border-radius: 20rpx;
  481. background: #fafafa;
  482. // text-align: justify;
  483. // word-break: break-all;
  484. word-wrap: break-word;
  485. color: #3d3d3d;
  486. font-size: 28rpx;
  487. }
  488. .message__item-status {
  489. width: 40rpx;
  490. height: 40rpx;
  491. margin-left: 12rpx;
  492. }
  493. }
  494. </style>
  495. <style lang="less" scoped>
  496. .popup_content {
  497. text-align: center;
  498. line-height: 1.5;
  499. }
  500. .popup__footer {
  501. display: flex;
  502. justify-content: space-around;
  503. .popup__footer-btn {
  504. padding: 12rpx 30rpx;
  505. border: 1rpx solid #333;
  506. border-radius: 12rpx;
  507. }
  508. .popup__footer-btn__confirm {
  509. display: flex;
  510. align-items: center;
  511. background-color: var(--black);
  512. color: var(--light);
  513. }
  514. }
  515. .message_list_tip_popup :deep(.u-popup__content) {
  516. width: 85%;
  517. }
  518. .message_list_tip_popup :deep(.pop .conts),
  519. .message_list_tip_popup :deep(.pop .top) {
  520. justify-content: center;
  521. }
  522. </style>