chat.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <template>
  2. <page-meta :page-font-size="fontValue+'px'" :root-font-size="fontValue+'px'"></page-meta>
  3. <view class="chat_content" @touchmove.stop.prevent>
  4. <!-- <view class="chat_content"> -->
  5. <cu-custom :isBack='true' >
  6. <template v-slot:content>
  7. <text v-if='chat' class="text-jiachu">{{ chat.name }}</text>
  8. <text v-if='chat && chat.type === ChatType.GROUP' class="text-jiachu">({{ count }}人)</text>
  9. </template>
  10. <template v-slot:right>
  11. <text class='cuIcon cuIcon-more' @tap='chatInfo'></text>
  12. </template>
  13. </cu-custom>
  14. <scroll-view :scroll-y='true' class='cu-chat' :style="'height: calc(100vh - '+customBar+'px - 3.125rem)'"
  15. @scroll='handleScroll' scroll-with-animation="true"
  16. :scroll-into-view='last' id='messageBox'>
  17. <checkbox-group @change="checkboxChange">
  18. <template v-for='(item) in messageList' :key="item.id">
  19. <load-map-user :userId="item.fromId" />
  20. <view v-if="item.messageType === MessageType.event" class="text-center">
  21. <MessageEvent :message="item"></MessageEvent>
  22. </view>
  23. <view v-else :id="`m-${item.id}`" >
  24. <view v-if='item.mine' class='cu-item self'>
  25. <view class='main' style='display: block;'>
  26. <view class='text-gray padding-bottom-sm' style="font-size: 15px;padding-bottom: 10px;">
  27. <view class='margin-right inline' style="margin-right: 15px;">
  28. <Time :time='item.timestamp'></Time>
  29. </view>
  30. <text style='color: black;'>{{ userMap.get(item.fromId)?.name }}</text>
  31. </view>
  32. <view class='content shadow break-word contentR'>
  33. <message-view :item='item'></message-view>
  34. </view>
  35. <quote-message :message="item" v-if="item.extend?.quoteMessage"/>
  36. <view
  37. v-if="chat && chat.type === ChatType.FRIEND && item.fromId === user?.id"
  38. class="read-tips" :class='!chat.lastReadTime || item.timestamp > chat.lastReadTime?"":"read"'
  39. >
  40. {{ !chat?.lastReadTime || item.timestamp > chat?.lastReadTime ? '未读' : '已读' }}
  41. </view>
  42. </view>
  43. <view>
  44. <vim-avatar :size="'small'" :img="userMap.get(item.fromId)?.avatar" :name="userMap.get(item.fromId)?.name" />
  45. </view>
  46. <view v-if="showMultipleCheck" class="padding-left">
  47. <checkbox :value="item" style="transform:scale(0.7)"/>
  48. </view>
  49. </view>
  50. <view v-if='!item.mine' class='cu-item' :id="`m-${item.id}`" >
  51. <view v-if="showMultipleCheck" class="padding-right">
  52. <checkbox :value="item" style="transform:scale(0.7)"/>
  53. </view>
  54. <view class='flex flex-direction'>
  55. <vim-avatar :size="'small'" :img="userMap.get(item.fromId)?.avatar" :name="userMap.get(item.fromId)?.name" />
  56. </view>
  57. <view class='main' style='display: block;'>
  58. <view class='text-gray padding-bottom-sm' style="font-size: 15px;padding-bottom: 10px;">
  59. <text style='color: black;'>{{ userMap.get(item.fromId)?.name }}</text>
  60. <view class='margin-left inline' style="margin-left: 15px;">
  61. <Time :time='item.timestamp'></Time>
  62. </view>
  63. </view>
  64. <view class='content shadow break-word contentL'>
  65. <message-view :item='item'></message-view>
  66. </view>
  67. <quote-message :message="item" v-if="item.extend?.quoteMessage"/>
  68. </view>
  69. </view>
  70. </view>
  71. </template>
  72. <view v-if='messageList' :id="'m-last'"></view>
  73. </checkbox-group>
  74. <view style="height: 60upx;"></view>
  75. </scroll-view>
  76. <view class='cu-bar foot input' v-if="isValidatedUser" :style="chatStore.quoteMessage?'min-height:5rem;'+'bottom:'+KeyboardHeight+'px;':''+'bottom:'+KeyboardHeight+'px;'">
  77. <!-- #ifdef APP-PLUS -->
  78. <view class='action' @tap='changeSendType'>
  79. <text class='text-grey'
  80. :class="{'cuIcon-sound':sendType === 'keyboard','cuIcon-keyboard':sendType === 'voice'}"></text>
  81. </view>
  82. <!-- #endif -->
  83. <view class="flex-sub" v-if="sendType === 'keyboard'" >
  84. <textarea class="textinput" auto-height="true"
  85. :always-embed='false'
  86. :adjust-position='false'
  87. cursor-spacing='30'
  88. confirm-hold='true'
  89. confirm-type='send'
  90. :focus='false'
  91. maxlength='300'
  92. v-model='messageContent'
  93. @confirm='mineSendBT'
  94. @input='handleShowAt'
  95. ></textarea>
  96. <!-- <input type="text" :always-embed='false' :adjust-position='false' cursor-spacing='30' confirm-hold='true'
  97. v-model='messageContent' confirm-type='send' class='solid-bottom' :focus='false' maxlength='300' @confirm='mineSendBT' @input='handleShowAt'/> -->
  98. <handle-quote-message v-if="chatStore.quoteMessage" :message="chatStore.quoteMessage"/>
  99. </view>
  100. <view v-if="sendType === 'keyboard'" class='action'>
  101. <text class='cuIcon-add text-grey' @tap='showFace=true'></text>
  102. </view>
  103. <!-- <button v-if="sendType === 'keyboard' && isValidatedUser" class='cu-btn bg-green shadow' @tap='mineSend'>发送</button> -->
  104. <voice v-if="sendType === 'voice'" @uploadCallback='uploadCallback'></voice>
  105. </view>
  106. <view class='cu-bar foot input' v-if="!isValidatedUser">
  107. <view class="text-center" style="flex: 1">已退群</view>
  108. </view>
  109. <view class='cu-modal bottom-modal' :class="showFace?'show':''" @tap='showFace=false'>
  110. <view class='cu-dialog'>
  111. <Faces @click='showFace = true' class='faces-box' @insertFace='insertFace'></Faces>
  112. <vim-upload v-if="chat" :chat-type="chat.type" :chat-id="chat.id" ></vim-upload>
  113. </view>
  114. </view>
  115. <view class='cu-modal bottom-modal' :class="showAt?'show':''" @tap.stop='handleHideAt'>
  116. <view class='cu-dialog'>
  117. <view>
  118. <!-- <input type="text" v-model="keyword" placeholder="请输入过滤条件" style="height: 80upx"/> -->
  119. </view>
  120. <view class='cu-list menu menu-margin at-box'>
  121. <view v-if='isMaster' class='cu-item' @tap="handleAtUser('所有人')">
  122. <view class='content'>
  123. <text class='text-grey'>所有人</text>
  124. </view>
  125. </view>
  126. <view class='cu-item' v-for='(user, index) in keywordFilter([...userMap.values()],keyword)' :key='index'
  127. @tap="handleAtUser(user.name)">
  128. <view class='content'>
  129. <text class='text-grey'>{{ user.name }}</text>
  130. </view>
  131. </view>
  132. </view>
  133. </view>
  134. </view>
  135. <multiple-forward v-if="showMultipleCheck"></multiple-forward>
  136. </view>
  137. </template>
  138. <script setup lang='ts'>
  139. import Auth from "@/api/Auth";
  140. import {useChatStore} from '@/store/chatStore'
  141. import {useUserStore} from '@/store/userStore'
  142. import {computed, getCurrentInstance, ref} from 'vue'
  143. import Time from '@/components/Time.vue'
  144. import MessageView from '@/components/MessageView.vue'
  145. import Voice from '@/components/Voice.vue'
  146. import Faces from '@/components/Faces.vue'
  147. import VimUpload from '@/components/VimUpload.vue'
  148. import type Message from '@/mode/Message'
  149. import MessageType from '@/utils/MessageType'
  150. import {onBackPress, onHide, onLoad, onShow, onUnload} from '@dcloudio/uni-app'
  151. import CuCustom from '@/colorui/components/cu-custom.vue'
  152. import ChatType from '@/utils/ChatType'
  153. import type ChatSimple from '@/mode/ChatSimple'
  154. import type Extend from '@/mode/Extend'
  155. import {useWsStore} from '@/store/WsStore'
  156. import {match} from "pinyin-pro";
  157. import {imageLoad, last} from "@/hooks/useMessageImageLoad";
  158. import {count, isValidatedUser, isMaster, loadUserOrGroup, users} from "@/hooks/useChatInit";
  159. import {storeToRefs} from "pinia";
  160. import QuoteMessage from "@/components/QuoteMessage.vue";
  161. import HandleQuoteMessage from "@/components/HandleQuoteMessage.vue";
  162. import MessageEvent from "@/components/messages/MessageEvent.vue";
  163. import MessageApi from "@/api/MessageApi";
  164. import VimAvatar from "@/components/VimAvatar.vue";
  165. import {useMessageStore} from "@/store/messageStore";
  166. import MultipleForward from "@/components/MultipleForward.vue";
  167. import LoadMapUser from "@/components/LoadMapUser.vue";
  168. //@ts-ignore
  169. const {proxy} = getCurrentInstance()
  170. const chatStore = useChatStore()
  171. const userStore = useUserStore()
  172. const wsStore = useWsStore()
  173. //当前聊天室id
  174. const id = ref()
  175. const sendType = ref('keyboard')
  176. const customBar = ref(proxy.customBar)
  177. //是否展示多选
  178. const {showMultipleCheck} = storeToRefs(useMessageStore())
  179. //是否展示表情
  180. const showFace = ref(false)
  181. //弹出键盘高度
  182. const KeyboardHeight = ref(10)
  183. const gundwez = ref('')
  184. const fontValue=ref(Auth.getfontSize());
  185. //是否展示@
  186. const showAt = ref(false)
  187. //是否展示@
  188. const showJp = ref(false)
  189. //输入框的内容
  190. const messageContent = ref('')
  191. //过滤关键词
  192. const keyword = ref('')
  193. //当前聊天
  194. const chat = computed(() => {
  195. if (chatStore.openChatId) {
  196. return chatStore.getChat(chatStore.openChatId)
  197. } else {
  198. return undefined
  199. }
  200. })
  201. //聊天对象,
  202. const {userMap} = storeToRefs(userStore)
  203. //消息列表
  204. const messageList = computed(() => {
  205. return chatStore.getChatMessage(id.value) ?? []
  206. })
  207. //当前用户
  208. const user = userStore.getUser()
  209. onBackPress(() => {
  210. chatStore.setOpenChatId('')
  211. })
  212. onUnload(() => {
  213. chatStore.setOpenChatId('')
  214. })
  215. onHide(() => {
  216. chatStore.setOpenChatId('')
  217. })
  218. const changeSendType = () => {
  219. sendType.value === 'keyboard' ? sendType.value = 'voice' : sendType.value = 'keyboard'
  220. }
  221. //多选
  222. const checkboxChange= (e:any) => {
  223. useMessageStore().setCheckList(e.detail.value)
  224. }
  225. //获取聊天室id
  226. onLoad((opt) => {
  227. uni.onKeyboardHeightChange(onKeyboardHeightChange);
  228. //console.log(user);
  229. id.value = opt?.id
  230. //console.log(id);
  231. chatStore.setOpenChatId(id.value)
  232. if (chat.value && ChatType.FRIEND === chat.value.type && user) {
  233. //获取读取消息的时间,这里要颠倒下,是查询对方的读取时间,不是自己的
  234. //所以聊天室chatId是自己,fromId是对方
  235. MessageApi.getReadTime(user.id, chat.value.id).then((res) => {
  236. //console.log(res);
  237. if (res.data) {
  238. chatStore.setCurrentChatLastReadTime(parseInt(res.data))
  239. }
  240. })
  241. }
  242. })
  243. const keywordFilter = (items: ChatSimple[], keyword: string): ChatSimple[] => {
  244. return items.filter((item) => {
  245. return !!(keyword.trim() === '' || match(item.name, keyword))
  246. })
  247. }
  248. const handleScroll = () => {
  249. last.value = '';
  250. }
  251. onShow(() => {
  252. chatStore.setOpenChatId(id.value)
  253. if(chat.value){
  254. if (user) {
  255. loadUserOrGroup(chat.value, user)
  256. }
  257. //延迟1秒发生已读回执消息
  258. setTimeout(function () {
  259. readMessage()
  260. }, 1000)
  261. //重写 callback 方法,收到的消息后滚动到最下面
  262. wsStore.callBack(() => {
  263. if (chat.value && chatStore.openChatId === chat.value.id) {
  264. imageLoad(messageList.value)
  265. }
  266. //console.log('1111111');
  267. })
  268. }
  269. })
  270. const onKeyboardHeightChange = (res:any) => {
  271. const { height, duration } = res;
  272. //console.log(res);
  273. gundwez.value = last.value;
  274. last.value='';
  275. // 键盘弹起
  276. if (height > 0) {
  277. //console.log('键盘弹起');
  278. KeyboardHeight.value=height;
  279. customBar.value = proxy.customBar+height;
  280. }// 键盘收回
  281. else {
  282. //console.log('键盘收回');
  283. KeyboardHeight.value=10;
  284. customBar.value = proxy.customBar;
  285. }
  286. setTimeout(() => {
  287. last.value = 'm-last';
  288. }, 350)
  289. //console.log(last);
  290. }
  291. /**
  292. * 展示聊天室信息
  293. */
  294. const chatInfo = () => {
  295. if(chat.value){
  296. if (ChatType.GROUP === chat.value.type) {
  297. uni.navigateTo({
  298. url: '/pages/group/group?id=' + chat.value.id
  299. })
  300. } else {
  301. uni.navigateTo({
  302. url: '/pages/pub/user?id=' + chat.value.id
  303. })
  304. }
  305. }
  306. }
  307. const uploadCallback = (extend: Extend, messageType: string) => {
  308. console.log(user,chat.value)
  309. if (user && chat.value) {
  310. //console.log('uploadCallback');
  311. let msg: Message = {
  312. messageType: messageType,
  313. chatId: chat.value.id,
  314. fromId: user.id,
  315. content: '',
  316. type: chat.value.type,
  317. extend: extend
  318. }
  319. wsStore.sendMessage(msg)
  320. }
  321. }
  322. const getAtUserIds = (str: string): Array<string> => {
  323. if (users.value) {
  324. const regex = /@\[([^\]]+)\]/g
  325. let match
  326. const usernames = new Array<string>()
  327. while ((match = regex.exec(str)) !== null) {
  328. usernames.push(match[1])
  329. }
  330. const ids = users.value
  331. .filter((user) => {
  332. return usernames.indexOf(user.name) > -1
  333. })
  334. .map((user) => {
  335. return user.id
  336. })
  337. return Array.from(new Set(ids))
  338. } else {
  339. return []
  340. }
  341. }
  342. const mineSend = (): void => {
  343. if (messageContent.value && messageContent.value.trim() !== '' && user && chat.value) {
  344. let msg: Message = {
  345. messageType: MessageType.text,
  346. chatId: chat.value.id,
  347. fromId: user.id,
  348. content: messageContent.value,
  349. type: chat.value.type
  350. }
  351. msg.extend = {
  352. atAll: isMaster.value && msg.content.includes('@[所有人]'),
  353. atUserIds: getAtUserIds(msg.content)
  354. }
  355. if (chatStore.quoteMessage) {
  356. msg.extend = {
  357. quoteMessage: chatStore.quoteMessage,
  358. }
  359. }
  360. wsStore.sendMessage(msg)
  361. messageContent.value = ''
  362. chatStore.setQuoteMessage(undefined);
  363. }
  364. }
  365. const mineSendBT = (e) =>{
  366. console.log('mineSendBT',chat.value);
  367. if (messageContent.value && messageContent.value.trim() !== '' && user && chat.value) {
  368. let msg: Message = {
  369. messageType: MessageType.text,
  370. chatId: chat.value.id,
  371. fromId: user.id,
  372. content: messageContent.value,
  373. type: chat.value.type
  374. }
  375. msg.extend = {
  376. atAll: isMaster.value && msg.content.includes('@[所有人]'),
  377. atUserIds: getAtUserIds(msg.content)
  378. }
  379. if (chatStore.quoteMessage) {
  380. msg.extend = {
  381. quoteMessage: chatStore.quoteMessage,
  382. }
  383. }
  384. console.log('mineSend',msg);
  385. wsStore.sendMessage(msg)
  386. messageContent.value = ''
  387. chatStore.setQuoteMessage(undefined);
  388. }
  389. }
  390. //添加表情
  391. const insertFace = (item: string) => {
  392. messageContent.value = `${messageContent.value}face${item}`
  393. showFace.value = false
  394. }
  395. const handleAtUser = (username: string) => {
  396. messageContent.value = `${messageContent.value}[${username}]`
  397. showAt.value = false
  398. }
  399. const inputfocus = (e) =>{
  400. //console.log('inputfocus',e.detail.height);
  401. showkeybar.value=e.detail.height;
  402. }
  403. /**
  404. * 显示@
  405. */
  406. const handleShowAt = () => {
  407. if (chat.value && chat.value.type === ChatType.GROUP && messageContent.value.endsWith('@')) {
  408. showAt.value = true
  409. sendType.value = '';
  410. }
  411. }
  412. /**
  413. * 隐藏@
  414. * @param e TouchEvent
  415. */
  416. const handleHideAt = (e: TouchEvent) => {
  417. e.stopPropagation()
  418. showAt.value = false
  419. sendType.value = 'keyboard';
  420. }
  421. /**
  422. * 用户点击消息列表某个聊天场景
  423. */
  424. const readMessage = () => {
  425. if (user && chat.value) {
  426. let read = {
  427. chatId: chat.value.id,
  428. userId: user.id,
  429. timestamp: new Date().getTime(),
  430. type: chat.value.type
  431. }
  432. wsStore.sendRead(read)
  433. }
  434. }
  435. </script>
  436. <style scoped>
  437. .chat_content {
  438. position: fixed;
  439. top: 0;
  440. bottom: 0;
  441. left: 0;
  442. right: 0;
  443. overflow: hidden;
  444. }
  445. .cu-chat {
  446. display: block;
  447. background-color: #FFF;
  448. border: solid #eee;;
  449. border-width: 1px 0px 0rpx 0px;
  450. }
  451. .faces-box {
  452. height: auto;
  453. }
  454. .inline {
  455. display: inline-block;
  456. }
  457. .at-box {
  458. height: 50vh;
  459. bottom: 0;
  460. overflow-y: scroll
  461. }
  462. .break-word {
  463. word-wrap: break-word;
  464. word-break: break-all;
  465. }
  466. .read-tips {
  467. font-size: 20upx;
  468. line-height: 48upx;
  469. }
  470. .read {
  471. color: #5fb878;
  472. }
  473. .textinput{
  474. margin-top: 10rpx;
  475. margin-bottom: 10rpx;
  476. padding-top: 10rpx;
  477. padding-bottom: 10rpx;
  478. width: 100%;
  479. border-radius: 6rpx;
  480. border: 1rpx solid darkgray;
  481. font-size: 32upx;
  482. }
  483. </style>