chatting.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. <template>
  2. <div class="container">
  3. <div ref="topnav" class="topnav flex-start">
  4. <div class="nav-item flex-center" @click="back">
  5. <img :src="`${assetsUrl}back.png`" mode="widthFix" class="nav-img"></img>
  6. </div>
  7. <div class="nav-center flex-center">
  8. <div class="nav-text font28 fw600">
  9. {{userInfo.nick}}
  10. </div>
  11. <div class="nav-tip font20 fw400" >
  12. <text></text>{{onlineState}} · {{userInfo.distance}}
  13. </div>
  14. </div>
  15. <div class="nav-item"></div>
  16. </div>
  17. <div class="message-list" ref="msglist" :style="{'height': `${scrollHeight}px`,'transform':`translateY(${(showActionIndex===0||showActionIndex===3)?-200:0}px)`}">
  18. <div class="no-message" v-if="isCompleted">没有更多啦</div>
  19. <div v-for="(item,index) in messageList" :key="index" class="t-message">
  20. <div v-if="conversation.type !== '@TIM#SYSTEM'" :id="item.ID">
  21. <div class="t-message-item">
  22. <!-- <TUI-TipMessage v-if="item.type === 'TIMGroupTipElem'" :message="item"></TUI-TipMessage> -->
  23. <div v-if="item.type !== 'TIMGroupTipElem'" :class="item.flow === 'out'||(item.flow === 'in'&&item.payload.data&&JSON.parse(item.payload.data).type===99) ? 't-self-message' : 't-recieve-message'">
  24. <img
  25. class="t-message-avatar"
  26. v-if="(item.flow === 'in'&&item.type !== 'TIMCustomElem')||(item.flow === 'in'&&(item.payload.data&&JSON.parse(item.payload.data).type!==99))"
  27. :src="item.avatar || 'https://sdk-web-1252463788.cos.ap-hongkong.myqcloud.com/component/TUIKit/assets/avatar_21.png'"
  28. ></img>
  29. <div class="read-receipts" v-if="conversation.type === 'C2C' && item.flow === 'out'">
  30. <div v-if="item.isPeerRead" class="font24">已读</div>
  31. <div v-else class="font24">未读</div>
  32. </div>
  33. <div>
  34. <TextMessage v-if="item.type === 'TIMTextElem'" :message="item" :isMine="item.flow === 'out'"></TextMessage>
  35. <ImageMessage v-if="item.type === 'TIMImageElem'" :message="item" :isMine="item.flow === 'out'"></ImageMessage>
  36. <VideoMessage v-if="item.type === 'TIMVideoFileElem'" :message="item" :isMine="item.flow === 'out'" @videoPlayerHandler="videoPlayerHandler"></VideoMessage>
  37. <AudioMessage v-if="item.type === 'TIMSoundElem'" :message="item" :isMine="item.flow === 'out'"></AudioMessage>
  38. </div>
  39. <img
  40. class="t-message-avatar"
  41. v-if="item.flow === 'out'"
  42. :src="item.avatar || 'https://sdk-web-1252463788.cos.ap-hongkong.myqcloud.com/component/TUIKit/assets/avatar_21.png'"
  43. ></img>
  44. <!-- <img
  45. class="t-message-avatar"
  46. v-if="item.flow === 'in'&&(item.payload.data&&JSON.parse(item.payload.data).type!==99)"
  47. :src="mineUserInfo.icon || 'https://sdk-web-1252463788.cos.ap-hongkong.myqcloud.com/component/TUIKit/assets/avatar_21.png'"
  48. ></image> -->
  49. </div>
  50. </div>
  51. </div>
  52. <div v-else :id="item.ID" :data-value="item.ID"><TUI-SystemMessage :message="item"></TUI-SystemMessage></div>
  53. </div>
  54. </div>
  55. <div class="talk-box" ref="talkbox" @touchmove="prevent()">
  56. <div class="input-box flex-between">
  57. <input type="text" class="input" confirm-type="send" placeholder="请输入消息…" v-model="inputText" placeholder-style="color:#7D7DA4 ;font-size:24rpx" @confirm="sendTextMessage">
  58. <div class="input-btn font22 fw400" @click="sendTextMessage">
  59. 发送
  60. </div>
  61. </div>
  62. <div class="action-box flex-between">
  63. <img :src="`${assetsUrl}talk-voice.png`" mode="aspectFit" class="act-img" @click="showActionPanel(0)"></img>
  64. <img :src="`${assetsUrl}talk-pic.png`" mode="aspectFit" class="act-img" @click="showActionPanel(1)"></img>
  65. <img :src="`${assetsUrl}talk-pos.png`" mode="aspectFit" class="act-img" @click="showActionPanel(2)"></img>
  66. <img :src="`${assetsUrl}talk-emo.png`" mode="aspectFit" class="act-img" @click="showActionPanel(3)"></img>
  67. </div>
  68. <div class="action-panel" :style="{'padding-bottom':`${safeArea?34:0}px`}">
  69. <div class="voice-panel flex-center":style="{'height':`${showActionIndex===0?'200px':'0px'}`}">
  70. <div class="voice-text font22 fw400">
  71. {{voiceText}}
  72. </div>
  73. <img :src="`${assetsUrl}talk-voice-${isRecording?'delete':'open'}.png`" mode="aspectFill" class="voice-img"@touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd"></img>
  74. </div>
  75. <div class="emoji-panel" :style="{'height':`${showActionIndex===3?'200px':'0px'}`}">
  76. <Emoji @enterEmoji="appendMessage"></Emoji>
  77. </div>
  78. </div>
  79. </div>
  80. <div v-if="videoPlay" class="container-box" @click.stop="stopVideoHander">
  81. <video
  82. v-if="videoPlay"
  83. class="video-message"
  84. :src="videoMessage.payload.videoUrl||videoMessage.videoUrl"
  85. error="videoError"
  86. autoplay="autoplay"
  87. controls="controls"/>
  88. <img :src="`${assetsUrl}popup-close-white.png`" class="close-video">
  89. </div>
  90. <input type="file" accept="image/png,image/jpg,image/jpeg,video/mp4" ref="uploadMedia" @change="fileUpload" class="hide-file">
  91. </div>
  92. </template>
  93. <script>
  94. import TextMessage from '../../components/Message/text-message/index.vue';
  95. import VideoMessage from '../../components/Message/video-message/index.vue';
  96. import AudioMessage from '../../components/Message/audio-message/index.vue';
  97. import ImageMessage from '../../components/Message/image-message/index.vue';
  98. import Emoji from '../../components/Emoji/emoji.vue'
  99. import {assetsUrl,hasSafeArea} from '../../util/index.js';
  100. import {getPolicy,computeSignature,getKey} from '@/util/oss.js';
  101. import Recorder from 'js-audio-recorder';
  102. export default {
  103. components: {
  104. Emoji,
  105. TextMessage,
  106. VideoMessage,
  107. AudioMessage,
  108. ImageMessage
  109. },
  110. data() {
  111. return {
  112. assetsUrl,
  113. safeArea:hasSafeArea(),
  114. scrollHeight:0,
  115. topbarOffsetHeight:60,
  116. topNavHeight:0,
  117. messageList:[],
  118. message:null,//消息实例
  119. onlineState:'',
  120. scrollRefreshing:false,
  121. scrollTriggered:true,
  122. conversation:'',
  123. conversationID: '',
  124. inputText:'',
  125. showSendBtn:false,
  126. showActionIndex:-1,
  127. recorder:null,
  128. voiceText:'按住说话,松手发送',
  129. recordTimeTotal:60,//最大长度,30秒
  130. isRecording:false,
  131. canSend:false,
  132. startPoint:0,
  133. videoPlay: false,
  134. videoMessage: {},
  135. isCompleted:false,//是否加载完毕所有消息
  136. }
  137. },
  138. watch:{
  139. conversation: {
  140. handler: function(newVal) {//发送消息不进行全部拉取
  141. if (newVal&&newVal.conversationID&&!this.isSend) {
  142. this.conversation=newVal;
  143. this.getMessageList(newVal);
  144. };
  145. },
  146. immediate: true,
  147. deep: true
  148. }
  149. },
  150. computed: {
  151. userInfo(){
  152. return JSON.parse(localStorage.getItem('otherInfo'))
  153. }
  154. },
  155. created(){
  156. this.recorder = new Recorder({
  157. sampleBits: 16, // 采样位数,支持 8 或 16,默认是16
  158. sampleRate: 16000, // 采样率,支持 11025、16000、22050、24000、44100、48000,根据浏览器默认值,我的chrome是48000
  159. numChannels: 1,
  160. });
  161. },
  162. mounted() {
  163. this.$on('videoPlayerHandler', value => {
  164. console.log(value);
  165. this.videoPlay = value.isPlay;
  166. this.videoMessage = value.message;
  167. });
  168. this.conversationID=this.$route.params.conversationid;
  169. console.log('conversationID:',this.conversationID)
  170. this.$TUIKit.setMessageRead({
  171. conversationID:this.conversationID
  172. })
  173. this.$TUIKit.getConversationProfile(this.conversationID).then(res => {
  174. const { conversation } = res.data;
  175. this.conversation = conversation;
  176. });
  177. this.$TUIKit.on(this.$TUIKitEvent.MESSAGE_RECEIVED, this.$onMessageReceived, this);
  178. this.$TUIKit.on(this.$TUIKitEvent.MESSAGE_READ_BY_PEER, this.$onMessageReadByPeer, this);
  179. this.getMessageList(this.conversation);
  180. if(this.userInfo.lastActiveTime===0||this.userInfo.online){
  181. this.onlineState='在线';
  182. }
  183. else if(this.userInfo.lastActiveTime<30&&!this.userInfo.online){
  184. this.onlineState='刚刚';
  185. }
  186. else{
  187. this.onlineState='离线';
  188. }
  189. },
  190. // destroyed() {
  191. // this.$TUIKit.off(this.$TUIKitEvent.MESSAGE_RECEIVED, this.$onMessageReceived);
  192. // },
  193. methods: {
  194. videoPlayerHandler(value){
  195. console.log(value);
  196. this.videoPlay = value.isPlay;
  197. this.videoMessage = value.message;
  198. },
  199. stopVideoHander() {
  200. console.log('yes')
  201. this.videoPlay = false;
  202. },
  203. back(){
  204. this.$router.back();
  205. },
  206. prevent(){
  207. return false;
  208. },
  209. /**
  210. * 计算scroll高度
  211. */
  212. computedScollviewHeight() {
  213. this.scrollHeight=window.screen.height-this.$refs.talkbox.offsetHeight-this.$refs.topnav.offsetHeight;
  214. this.scrollToBottom();
  215. },
  216. scrollToBottom(){
  217. this.$nextTick(()=>{
  218. this.$refs.msglist.scrollTop = 99999;
  219. })
  220. },
  221. /**
  222. * 推荐下拉刷新、加载更多
  223. */
  224. scrollRefresh(){
  225. if (this.scrollRefreshing)
  226. {
  227. return;
  228. }
  229. this.scrollRefreshing = true;
  230. setTimeout(() => {
  231. this.scrollTriggered = false;
  232. this.scrollRefreshing = false;
  233. }, 1000);
  234. this.isLoadPreData=true;
  235. },
  236. scrollRestore() {this.scrollTriggered = true;},
  237. showActionPanel(index){
  238. if(this.showActionIndex===index){
  239. this.showActionIndex=-1;
  240. }
  241. else{
  242. switch(index){
  243. case 0:
  244. this.showActionIndex=index;
  245. break;
  246. case 1:
  247. this.showActionIndex=-1;
  248. this.$refs.uploadMedia.click();
  249. break;
  250. case 2:
  251. break;
  252. case 3:
  253. this.showActionIndex=index;
  254. break;
  255. }
  256. }
  257. this.scrollToBottom();
  258. },
  259. fileUpload(e){
  260. console.log(e.target.files[0]);
  261. let file=e.target.files[0];
  262. if(file.type==='video/mp4'){
  263. const message = this.$TUIKit.createVideoMessage({
  264. to: String(this.userInfo.id),
  265. conversationType: this.conversation.type,
  266. payload: {
  267. file: file
  268. },
  269. onProgress:event=>{}
  270. });
  271. this.$sendTIMMessage(message);
  272. }
  273. else{
  274. const message = this.$TUIKit.createImageMessage({
  275. to: String(this.userInfo.id),
  276. conversationType: this.conversation.type,
  277. payload: {
  278. file: file
  279. },
  280. onProgress:event=>{}
  281. });
  282. this.$sendTIMMessage(message);
  283. }
  284. // this.$api.public.aliossToken({}).then(result=>{
  285. // const client=new this.$oss({
  286. // region: 'oss-cn-qingdao',
  287. // accessKeyId: result.data.accessKeyId,
  288. // accessKeySecret: result.data.accessKeySecret,
  289. // stsToken: result.data.securityToken,
  290. // bucket: 'zhenyanapp-gen',
  291. // refreshSTSToken: async () => {
  292. // const {data: res} = await this.$api.public.aliossToken({})
  293. // return {
  294. // accessKeyId: res.data.accessKeyId, // 自己账户的accessKeyId或临时秘钥
  295. // accessKeySecret: res.data.accessKeySecret, // 自己账户的accessKeySecret或临时秘钥
  296. // stsToken: res.data.securityToken, // 从STS服务获取的安全令牌(SecurityToken)。
  297. // }
  298. // },
  299. // refreshSTSTokenInterval: 3600 * 1000
  300. // });
  301. // client.put(getKey(this.ext),file).then(succ=>{
  302. // }).catch(err=>{
  303. // this.$toast.clear();
  304. // this.$toast('上传失败')
  305. // })
  306. // })
  307. },
  308. appendMessage(e) {
  309. this.inputText+=e.detail.message;
  310. },
  311. handleLongPress(e) {
  312. this.recorder.start().then(()=>{
  313. this.startPoint=e.touches[0];
  314. this.voiceText='正在录音,上划可取消';
  315. this.isRecording=true;
  316. this.recordTime=0;
  317. this.recordTimer = setInterval(() => {
  318. this.recordTime++;
  319. if(this.recorTime>=this.recordTimeTotal){
  320. this.recorder.stop();
  321. clearInterval(this.recordTimer);
  322. this.recordTimer=null;
  323. }
  324. }, 1000);
  325. }).catch(err=>{
  326. console.log(err);
  327. })
  328. },
  329. handleTouchStart(e){
  330. this.timeOutEvent = setTimeout(()=> {
  331. this.handleLongPress(e);
  332. }, 500);
  333. return false;
  334. },
  335. // 录音时的手势上划移动距离对应文案变化
  336. handleTouchMove(e) {
  337. if (this.isRecording) {
  338. if (this.startPoint.clientY - e.touches[e.touches.length - 1].clientY > 100) {
  339. this.voiceText='松开手指,取消发送';
  340. this.canSend=false;
  341. } else if (this.startPoint.clientY - e.touches[e.touches.length - 1].clientY > 20) {
  342. this.voiceText='上划可取消';
  343. this.canSend=true;
  344. } else {
  345. this.voiceText='抬起停止';
  346. this.canSend=true;
  347. }
  348. }
  349. },
  350. // 手指离开页面滑动
  351. handleTouchEnd() {
  352. this.isRecording=false;
  353. this.voiceText='按住说话,松手发送';
  354. let voiceFile=this.recorder.getPCMBlob();
  355. console.log(voiceFile)
  356. },
  357. sendTextMessage() {
  358. const to = String(this.userInfo.id);
  359. const text =this.inputText;
  360. const message = this.$TUIKit.createTextMessage({
  361. to,
  362. conversationType: this.conversation.type,
  363. payload: {
  364. text
  365. }
  366. });
  367. this.inputText='';
  368. this.$sendTIMMessage(message);
  369. },
  370. $sendTIMMessage(message) {
  371. this.$TUIKit.sendMessage(message).then((res) => {
  372. this.isSend=true;
  373. this.messageList.push(message);
  374. this.$nextTick(() => {
  375. this.$refs.msglist.scrollTop = this.$refs.msglist.scrollHeight;
  376. })
  377. }).catch((error) => {
  378. console.log(error)
  379. })
  380. },
  381. getMessageList(conversation) {
  382. console.log(conversation)
  383. if (!this.isCompleted) {
  384. this.$TUIKit
  385. .getMessageList({
  386. conversationID: this.conversationID,
  387. nextReqMessageID: this.nextReqMessageID,
  388. count: 15
  389. })
  390. .then(res => {
  391. const { messageList } = res.data; // 消息列表。
  392. console.log(res)
  393. this.nextReqMessageID = res.data.nextReqMessageID; // 用于续拉,分页续拉时需传入该字段。
  394. this.isCompleted = res.data.isCompleted; // 表示是否已经拉完所有消息。
  395. this.messageList = [...messageList, ...this.messageList];
  396. this.$handleMessageRender(this.messageList, messageList);
  397. this.computedScollviewHeight();
  398. });
  399. }
  400. },
  401. // 消息已读更新
  402. $onMessageReadByPeer() {
  403. this.messageList=this.messageList
  404. },
  405. // 收到的消息
  406. $onMessageReceived(value) {
  407. this.isSend=true;
  408. this.messageList.push(value.data[0]);
  409. this.scrollToButtom();
  410. },
  411. // 历史消息渲染
  412. $handleMessageRender(messageList) {
  413. if (messageList.length > 0) {
  414. this.messageList=messageList;
  415. setTimeout(()=>{
  416. this.$nextTick(() => {
  417. //进入页面滚动到底部
  418. });
  419. },500)
  420. }
  421. }
  422. }
  423. }
  424. </script>
  425. <style lang="scss" scoped>
  426. @import "../../public.scss";
  427. .hide-file{
  428. position: fixed;
  429. left: 1000px;
  430. top: 1000px;
  431. }
  432. .container{
  433. width: 100vw;
  434. height: 100vh;
  435. background-color: $bgcolor1;
  436. position: relative;
  437. overflow: hidden;
  438. .topnav {
  439. padding: 0 5px;
  440. position: fixed;
  441. height: 60px;
  442. top: 0;
  443. left: 0;
  444. width: 100vw;
  445. z-index: 100;
  446. background-color: $bgcolor1;
  447. .nav-item{
  448. width: 20px;
  449. height: 20px;
  450. padding:10px;
  451. margin-left: 16rpx;
  452. .nav-img{
  453. width: 20px;
  454. height: 20px;
  455. }
  456. }
  457. .nav-center{
  458. flex: 1;
  459. flex-direction: column;
  460. .nav-text{
  461. color: $fontcolor5;
  462. height: 20px;
  463. text-align: center;
  464. }
  465. .nav-tip{
  466. color:$fontcolor2;
  467. }
  468. }
  469. }
  470. .message-list{
  471. transition: height .3s;;
  472. padding-top: 60px;
  473. overflow: auto;
  474. }
  475. .talk-box{
  476. position: fixed;
  477. bottom: 0;
  478. left: 0;
  479. width: 100vw;
  480. padding-top: 8px;
  481. background-color: $bgcolor1;
  482. .input-box{
  483. margin: 0px 16px;
  484. height: 40px;
  485. border-radius: 40px;
  486. background-color: $bgcolor4;
  487. padding: 0 12px;
  488. .input{
  489. width: 100%;
  490. height:95%;
  491. color: #ffffff;
  492. font-size: 11px;
  493. background-color: $bgcolor4;
  494. }
  495. .input-btn{
  496. background-color: $primary;
  497. color: #ffffff;
  498. border-radius: 8px;
  499. height: 28px;
  500. line-height: 28px;
  501. width: 60px;
  502. text-align: center;
  503. }
  504. }
  505. .action-box{
  506. padding: 0px 0px 10px 0px;
  507. margin: 0 45px;
  508. margin-top: 18px;
  509. .act-img{
  510. width: 28px;
  511. height: 28px;
  512. }
  513. }
  514. .action-panel{
  515. width: 100vw;
  516. .voice-panel{
  517. overflow: hidden;
  518. transition: height .3s;
  519. flex-direction: column;
  520. .voice-text{
  521. color: #7D7DA4;
  522. text-align: center;
  523. }
  524. .voice-img{
  525. width: 100px;
  526. height: 100px;
  527. margin-top: 20px;
  528. }
  529. }
  530. .emoji-panel{
  531. transition: height .3s;
  532. }
  533. }
  534. }
  535. .container-box {
  536. position: fixed;
  537. left: 0;
  538. right: 0;
  539. bottom: 0;
  540. top: 0;
  541. width:100vw;
  542. height:100vh;
  543. z-index:3000;
  544. background-color: rgba(0, 0, 0, 0.5);
  545. .video-message {
  546. width: 100vw;
  547. height: 100vh;
  548. object-fit: cover;
  549. }
  550. .close-video{
  551. position: absolute;
  552. right: 30px;
  553. top: 30px;
  554. background-color: rgba(0, 0, 0, 0.2);
  555. width: 20px;
  556. height: 20px;
  557. border-radius: 50px;
  558. padding: 10px;
  559. }
  560. }
  561. }
  562. .t-message-item {
  563. /*max-width: 60vw;*/
  564. padding: 8px 0;
  565. }
  566. .t-recieve-message {
  567. display: flex;
  568. flex-direction: row;
  569. justify-items: flex-end;
  570. align-items:flex-start;
  571. width: 100vw;
  572. }
  573. .t-message-avatar {
  574. margin-left: 10px;
  575. margin-right: 6px;
  576. border-radius: 5px;
  577. width: 40px;
  578. height: 40px;
  579. }
  580. .t-self-message {
  581. display: flex;
  582. flex-direction: row;
  583. justify-content: flex-end;
  584. /*align-items: center;*/
  585. width: 100vw;
  586. }
  587. .t-self-message-body {
  588. display: flex;
  589. justify-content: flex-start;
  590. flex-wrap: wrap;
  591. outline: none;
  592. }
  593. .t-recieve-message-body {
  594. display: flex;
  595. justify-content: flex-start;
  596. flex-wrap: wrap;
  597. outline: none;
  598. /*background: #F8F8F8;*/
  599. border-radius: 2px 10px 10px 10px;
  600. margin-left: 4px;
  601. }
  602. .read-receipts {
  603. line-height: 42px;
  604. height: 42px;
  605. font-size: 12px;
  606. color: #6e7981;
  607. margin-right: 10px
  608. }
  609. .no-message {
  610. text-align: center;
  611. width: 100%;
  612. font-size: 12px;
  613. color: #a5b5c1;
  614. height: 30px;
  615. line-height: 30px;
  616. }
  617. </style>