message-bubble.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <template>
  2. <div :class="containerClassNameList">
  3. <!-- todo 统一组件处理-->
  4. <div
  5. class="message-bubble-main-content"
  6. :class="[message.flow === 'in' ? '' : 'reverse']"
  7. >
  8. <Avatar useSkeletonAnimation :url="message.avatar || ''" />
  9. <main class="message-body" @click.stop>
  10. <div
  11. v-if="message.flow === 'in' && message.conversationType === 'GROUP'"
  12. class="message-body-nick-name"
  13. >
  14. {{ props.content.showName }}
  15. </div>
  16. <div
  17. :class="[
  18. 'message-body-main',
  19. message.flow === 'out' && 'message-body-main-reverse',
  20. ]"
  21. >
  22. <div
  23. :class="[
  24. 'blink',
  25. 'message-body-content',
  26. message.flow === 'out' ? 'content-out' : 'content-in',
  27. message.hasRiskContent && 'content-has-risk',
  28. isNoPadding ? 'content-no-padding' : '',
  29. isNoPadding && isBlink ? 'blink-shadow' : '',
  30. !isNoPadding && isBlink ? 'blink-content' : '',
  31. ]"
  32. >
  33. <div class="content-main">
  34. <img
  35. v-if="
  36. (message.type === TYPES.MSG_IMAGE ||
  37. message.type === TYPES.MSG_VIDEO) &&
  38. message.hasRiskContent
  39. "
  40. :class="[
  41. 'message-risk-replace',
  42. !isPC && 'message-risk-replace-h5',
  43. ]"
  44. :src="riskImageReplaceUrl"
  45. />
  46. <template v-else>
  47. <slot />
  48. </template>
  49. </div>
  50. <!-- 敏感信息失败提示 -->
  51. <div v-if="message.hasRiskContent" class="content-has-risk-tips">
  52. {{ riskContentText }}
  53. </div>
  54. </div>
  55. <!-- 发送失败 -->
  56. <div
  57. v-if="message.status === 'fail' || message.hasRiskContent"
  58. class="message-label fail"
  59. @click="resendMessage()"
  60. >
  61. !
  62. </div>
  63. <!-- 加载图标 -->
  64. <Icon
  65. v-if="
  66. message.status === 'unSend' &&
  67. needLoadingIconMessageType.includes(message.type)
  68. "
  69. class="message-label loading-circle"
  70. :file="loadingIcon"
  71. :width="'15px'"
  72. :height="'15px'"
  73. />
  74. <!-- 已读 & 未读 -->
  75. <ReadStatus
  76. class="message-label align-self-bottom"
  77. :message="shallowCopyMessage(message)"
  78. @openReadUserPanel="openReadUserPanel"
  79. />
  80. </div>
  81. </main>
  82. </div>
  83. <!-- message extra area -->
  84. <div class="message-bubble-extra-content">
  85. <!-- extra: message translation -->
  86. <MessageTranslate
  87. :class="message.flow === 'out' ? 'reverse' : 'flex-row'"
  88. :message="message"
  89. />
  90. <!-- extra: message convert voice to text -->
  91. <MessageConvert
  92. :class="message.flow === 'out' ? 'reverse' : 'flex-row'"
  93. :message="message"
  94. />
  95. <!-- extra: message quote -->
  96. <MessageQuote
  97. :class="message.flow === 'out' ? 'reverse' : 'flex-row'"
  98. :message="message"
  99. @blinkMessage="blinkMessage"
  100. @scrollTo="scrollTo"
  101. />
  102. </div>
  103. </div>
  104. </template>
  105. <script lang="ts" setup>
  106. import { computed, toRefs } from "../../../../adapter-vue";
  107. import TUIChatEngine, {
  108. TUITranslateService,
  109. IMessageModel,
  110. } from "@tencentcloud/chat-uikit-engine";
  111. import Icon from "../../../common/Icon.vue";
  112. import ReadStatus from "./read-status/index.vue";
  113. import MessageQuote from "./message-quote/index.vue";
  114. import Avatar from "../../../common/Avatar/index.vue";
  115. import MessageTranslate from "./message-translate/index.vue";
  116. import MessageConvert from "./message-convert/index.vue";
  117. import loadingIcon from "../../../../assets/icon/loading.png";
  118. import { shallowCopyMessage } from "../../utils/utils";
  119. import { isPC } from "../../../../utils/env";
  120. interface IProps {
  121. messageItem: IMessageModel;
  122. content?: any;
  123. blinkMessageIDList?: string[];
  124. classNameList?: string[];
  125. }
  126. interface IEmits {
  127. (e: "resendMessage"): void;
  128. (e: "blinkMessage", messageID: string): void;
  129. (
  130. e: "setReadReceiptPanelVisible",
  131. visible: boolean,
  132. message?: IMessageModel
  133. ): void;
  134. // 下面的方法是 uniapp 专属
  135. (e: "scrollTo", scrollHeight: number): void;
  136. }
  137. const emits = defineEmits<IEmits>();
  138. const props = withDefaults(defineProps<IProps>(), {
  139. messageItem: () => ({} as IMessageModel),
  140. content: () => ({}),
  141. blinkMessageIDList: () => [],
  142. classNameList: () => [],
  143. });
  144. const TYPES = TUIChatEngine.TYPES;
  145. const riskImageReplaceUrl =
  146. "https://web.sdk.qcloud.com/component/TUIKit/assets/has_risk_default.png";
  147. const needLoadingIconMessageType = [
  148. TYPES.MSG_LOCATION,
  149. TYPES.MSG_TEXT,
  150. TYPES.MSG_CUSTOM,
  151. TYPES.MSG_MERGER,
  152. TYPES.MSG_FACE,
  153. ];
  154. const { blinkMessageIDList, messageItem: message } = toRefs(props);
  155. const containerClassNameList = computed(() => {
  156. return ["message-bubble", ...props.classNameList];
  157. });
  158. const isNoPadding = computed(() => {
  159. return [TYPES.MSG_IMAGE, TYPES.MSG_VIDEO].includes(message.value.type);
  160. });
  161. const riskContentText = computed<string>(() => {
  162. let content = TUITranslateService.t("TUIChat.涉及敏感内容") + ", ";
  163. if (message.value.flow === "out") {
  164. content += TUITranslateService.t("TUIChat.发送失败");
  165. } else {
  166. content += TUITranslateService.t(
  167. message.value.type === TYPES.MSG_AUDIO
  168. ? "TUIChat.无法收听"
  169. : "TUIChat.无法查看"
  170. );
  171. }
  172. return content;
  173. });
  174. const isBlink = computed(() => {
  175. if (message.value?.ID) {
  176. return blinkMessageIDList?.value?.includes(message.value.ID);
  177. }
  178. return false;
  179. });
  180. function resendMessage() {
  181. if (!message.value?.hasRiskContent) {
  182. emits("resendMessage");
  183. }
  184. }
  185. function blinkMessage(messageID: string) {
  186. emits("blinkMessage", messageID);
  187. }
  188. function scrollTo(scrollHeight: number) {
  189. emits("scrollTo", scrollHeight);
  190. }
  191. function openReadUserPanel() {
  192. emits("setReadReceiptPanelVisible", true, message.value);
  193. }
  194. </script>
  195. <style lang="scss" scoped>
  196. .flex-row {
  197. display: flex;
  198. }
  199. .reverse {
  200. display: flex;
  201. flex-direction: row-reverse;
  202. justify-content: flex-start;
  203. }
  204. .message-bubble {
  205. width: 100%;
  206. box-sizing: border-box;
  207. display: flex;
  208. flex-direction: column;
  209. padding: 0 20px 25px;
  210. user-select: none;
  211. -webkit-touch-callout: none; /* 系统默认菜单被禁用 */
  212. -webkit-user-select: none; /* webkit浏览器 */
  213. -khtml-user-select: none; /* 早期浏览器 */
  214. -moz-user-select: none; /* 火狐 */
  215. -ms-user-select: none; /* IE10 */
  216. .message-bubble-main-content {
  217. display: flex;
  218. .message-avatar {
  219. display: block;
  220. width: 36px;
  221. height: 36px;
  222. border-radius: 5px;
  223. flex: 0 0 auto;
  224. }
  225. .message-body {
  226. display: flex;
  227. flex: 0 1 auto;
  228. flex-direction: column;
  229. align-items: flex-start;
  230. margin: 0 8px;
  231. max-width: calc(100% - 54px);
  232. .message-body-nick-name {
  233. margin-bottom: 4px;
  234. font-size: 12px;
  235. color: #999;
  236. max-width: 150px;
  237. overflow: hidden;
  238. text-overflow: ellipsis;
  239. white-space: nowrap;
  240. }
  241. .message-body-main {
  242. max-width: 100%;
  243. display: flex;
  244. flex-direction: row;
  245. min-width: 0;
  246. box-sizing: border-box;
  247. &-reverse {
  248. flex-direction: row-reverse;
  249. }
  250. .message-body-content {
  251. display: flex;
  252. flex-direction: column;
  253. min-width: 0;
  254. box-sizing: border-box;
  255. padding: 10px;
  256. font-size: 14px;
  257. color: #000;
  258. letter-spacing: 0;
  259. word-wrap: break-word;
  260. word-break: break-all;
  261. position: relative;
  262. .content-main {
  263. box-sizing: border-box;
  264. display: flex;
  265. flex-direction: column;
  266. flex-shrink: 0;
  267. align-content: flex-start;
  268. border: 0 solid black;
  269. margin: 0;
  270. padding: 0;
  271. min-width: 0;
  272. .message-risk-replace {
  273. width: 130px;
  274. height: 130px;
  275. }
  276. }
  277. .content-has-risk-tips {
  278. font-size: 12px;
  279. color: #fa5151;
  280. font-family: PingFangSC-Regular;
  281. margin-top: 5px;
  282. border-top: 1px solid #e5c7c7;
  283. padding-top: 5px;
  284. }
  285. }
  286. .content-in {
  287. background: #fbfbfb;
  288. border-radius: 0 10px 10px;
  289. }
  290. .content-out {
  291. background: #5570ff;
  292. border-radius: 10px 0 10px 10px;
  293. color: #fff;
  294. }
  295. .content-no-padding {
  296. padding: 0;
  297. background: transparent;
  298. border-radius: 10px;
  299. overflow: hidden;
  300. }
  301. .content-no-padding.content-has-risk {
  302. padding: 12px;
  303. }
  304. .content-has-risk {
  305. background: rgba(250, 81, 81, 0.16);
  306. }
  307. .blink-shadow {
  308. @keyframes shadow-blink {
  309. 50% {
  310. box-shadow: rgba(255, 156, 25, 1) 0 0 10px 0;
  311. }
  312. }
  313. box-shadow: rgba(255, 156, 25, 0) 0 0 10px 0;
  314. animation: shadow-blink 1s linear 3;
  315. }
  316. .blink-content {
  317. @keyframes reference-blink {
  318. 50% {
  319. background-color: #ff9c19;
  320. }
  321. }
  322. animation: reference-blink 1s linear 3;
  323. }
  324. .message-label {
  325. align-self: flex-end;
  326. font-family: PingFangSC-Regular;
  327. font-size: 12px;
  328. color: #b6b8ba;
  329. word-break: keep-all;
  330. flex: 0 0 auto;
  331. margin: 0 8px;
  332. &.fail {
  333. width: 15px;
  334. height: 15px;
  335. border-radius: 15px;
  336. background: red;
  337. color: #fff;
  338. display: flex;
  339. justify-content: center;
  340. align-items: center;
  341. cursor: pointer;
  342. }
  343. &.loading-circle {
  344. opacity: 0;
  345. animation: circle-loading 2s linear 1s infinite;
  346. }
  347. @keyframes circle-loading {
  348. 0% {
  349. transform: rotate(0);
  350. opacity: 1;
  351. }
  352. 100% {
  353. opacity: 1;
  354. transform: rotate(360deg);
  355. }
  356. }
  357. }
  358. .align-self-bottom {
  359. align-self: flex-end;
  360. }
  361. }
  362. }
  363. }
  364. .message-bubble-extra-content {
  365. display: flex;
  366. flex-direction: column;
  367. }
  368. }
  369. </style>