chatting.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. <template>
  2. <view class="container">
  3. <view id="topnav" class="topnav flex-start" :style="{'height':`${topbarOffsetHeight-statusBarHeight}px`,'padding-top':`${statusBarHeight}px`}">
  4. <view class="nav-item flex-center" @click="back">
  5. <image :src="`${assetsUrl}back.png`" mode="widthFix" class="nav-img"></image>
  6. </view>
  7. <view class="nav-center flex-center">
  8. <view class="nav-text font32 fw600">
  9. {{userInfo.nick}}
  10. </view>
  11. <view class="nav-tip font20 fw400" >
  12. <text></text>{{onlineState}} · {{userInfo.distance}}
  13. </view>
  14. </view>
  15. <view class="nav-item"></view>
  16. </view>
  17. <view class="message-list" :style="{'height': `${scrollHeight}px`,'margin-top':`${topbarOffsetHeight}px`}">
  18. <TUI-message-list id="message-list" ref="messageList" :conversation="conversation" :scrollHeight="scrollHeight" :topbarOffsetHeight="topbarOffsetHeight" />
  19. </view>
  20. <view class="talk-box" id="talk-box" @touchmove="prevent()">
  21. <view class="input-box flex-between">
  22. <input type="text" class="input" confirm-type="send" placeholder="请输入消息…" :adjust-position="false" v-model="inputText" placeholder-style="color:#7D7DA4 ;font-size:24rpx" @confirm="sendTextMessage">
  23. <view class="input-btn font22 fw400" @click="sendTextMessage">
  24. 发送
  25. </view>
  26. </view>
  27. <view class="action-box flex-between">
  28. <image :src="`${assetsUrl}talk-voice.png`" mode="aspectFit" class="act-img" @click="showActionPanel(0)"></image>
  29. <image :src="`${assetsUrl}talk-pic.png`" mode="aspectFit" class="act-img" @click="showActionPanel(1)"></image>
  30. <image :src="`${assetsUrl}talk-video.png`" mode="aspectFit" class="act-img" @click="showActionPanel(2)"></image>
  31. <image :src="`${assetsUrl}talk-emo.png`" mode="aspectFit" class="act-img" @click="showActionPanel(3)"></image>
  32. </view>
  33. <view class="action-panel" :style="{'padding-bottom':`${showActionIndex===-1?'68rpx':'20rpx'}`}">
  34. <view class="voice-panel flex-center" v-if="showActionIndex===0">
  35. <view class="voice-text font22 fw400">
  36. {{voiceText}}
  37. </view>
  38. <image :src="`${assetsUrl}talk-voice-${isRecording?'delete':'open'}.png`" mode="aspectFill" class="voice-img" @longpress="handleLongPress" @touchmove="handleTouchMove" @touchend="handleTouchEnd"></image>
  39. </view>
  40. <view class="emoji-box" v-if="showActionIndex===3">
  41. <TUI-Emoji @enterEmoji="appendMessage"></TUI-Emoji>
  42. </view>
  43. </view>
  44. </view>
  45. <view v-if="videoPlay" class="container-box" @tap.stop="stopVideoHander">
  46. <video
  47. v-if="videoPlay"
  48. class="video-message"
  49. :src="videoMessage.payload.videoUrl||videoMessage.videoUrl"
  50. :poster="videoMessage.payload.thumbUrl||videoMessage.videoCover"
  51. object-fit="cover"
  52. error="videoError"
  53. autoplay="true"
  54. direction="0"
  55. show-fullscreen-btn="false"
  56. :style="{'width':`${videoMessage.payload.thumbWidth||videoMessage.width}px`,'height':`${videoMessage.payload.thumbHeight||videoMessage.height}px`}"
  57. />
  58. </view>
  59. </view>
  60. </template>
  61. <script>
  62. import TUIEmoji from '../../components/tui-chat/message-elements/emoji/index';
  63. import TUIMessageList from '../../components/tui-chat/message-list/index';
  64. export default {
  65. components: {
  66. TUIMessageList,
  67. TUIEmoji
  68. },
  69. data() {
  70. return {
  71. assetsUrl:this.$util.assetsUrl,
  72. scrollHeight:0,
  73. topNavHeight:0,
  74. userInfo:null,
  75. messageList:[],
  76. message:null,//消息实例
  77. onlineState:'',
  78. scrollRefreshing:false,
  79. scrollTriggered:true,
  80. conversation:'',
  81. conversationID: '',
  82. inputText:'',
  83. showSendBtn:false,
  84. showActionIndex:-1,
  85. recorderManager:null,
  86. voiceText:'按住说话,松手发送',
  87. recordTimeTotal:60,//最大长度,30秒
  88. isRecording:false,
  89. canSend:false,
  90. startPoint:0,
  91. videoPlay: false,
  92. videoMessage: {},
  93. }
  94. },
  95. computed: {
  96. statusBarHeight() {
  97. return this.$store.state.statusBarHeight;
  98. },
  99. topbarOffsetHeight() {
  100. return this.$store.state.topbarOffsetHeight;
  101. }
  102. },
  103. created() {
  104. uni.$on('videoPlayerHandler', value => {
  105. this.videoPlay = value.isPlay;
  106. this.videoMessage = value.message;
  107. });
  108. uni.authorize({
  109. scope:'scope.record',
  110. success:()=>{
  111. this.startRecordManage();
  112. },
  113. fail:err=>{
  114. uni.showModal({
  115. content: '检测到您没打开录音功能权限,是否去设置打开?',
  116. confirmText: "确认",
  117. cancelText: '取消',
  118. success:res=>{
  119. if(res.confirm){
  120. uni.openSetting({
  121. success:ress=>{
  122. if(ress.authSetting){
  123. this.startRecordManage();
  124. }
  125. }
  126. })
  127. }
  128. }
  129. })
  130. }
  131. })
  132. },
  133. onLoad(options) {
  134. this.conversationID=options.conversationid;
  135. uni.$TUIKit.setMessageRead({
  136. conversationID:this.conversationID
  137. })
  138. uni.$TUIKit.getConversationProfile(this.conversationID).then(res => {
  139. const { conversation } = res.data;
  140. this.conversation = conversation;
  141. console.log(this.conversation)
  142. });
  143. let pages=getCurrentPages();
  144. let prePage=pages[pages.length-2];
  145. this.userInfo=prePage.$vm.userInfo;
  146. },
  147. mounted() {
  148. if(this.userInfo.lastActiveTime===0||this.userInfo.online){
  149. this.onlineState='在线';
  150. }
  151. else if(this.userInfo.lastActiveTime<30&&!this.userInfo.online){
  152. this.onlineState='刚刚';
  153. }
  154. else{
  155. this.onlineState='离线';
  156. }
  157. setTimeout(()=>{
  158. this.computedScollviewHeight();
  159. },500)
  160. },
  161. methods: {
  162. back(){
  163. uni.navigateBack({
  164. delta:1
  165. })
  166. },
  167. prevent(){
  168. return false;
  169. },
  170. stopVideoHander() {
  171. this.videoPlay = false;
  172. },
  173. /**
  174. * 计算scroll高度
  175. */
  176. computedScollviewHeight() {
  177. let query = uni.createSelectorQuery().in(this);
  178. let heightLeaf =0;
  179. query.selectAll('#topnav,#talk-box').boundingClientRect(data => {
  180. data.forEach(item=>{
  181. heightLeaf+=item.height;
  182. })
  183. }).exec(() => {
  184. let sysInfo = uni.getSystemInfoSync();
  185. this.scrollHeight = sysInfo.windowHeight - heightLeaf;
  186. this.$refs.messageList.scrollToButtom();
  187. });
  188. },
  189. /**
  190. * 推荐下拉刷新、加载更多
  191. */
  192. scrollRefresh(){
  193. if (this.scrollRefreshing)
  194. {
  195. return;
  196. }
  197. this.scrollRefreshing = true;
  198. setTimeout(() => {
  199. this.scrollTriggered = false;
  200. this.scrollRefreshing = false;
  201. }, 1000);
  202. this.isLoadPreData=true;
  203. },
  204. scrollPulling(e) {},
  205. scrollRestore() {this.scrollTriggered = true;},
  206. scrollAbort() {},
  207. scrollToBottom(){},
  208. showActionPanel(index){
  209. if(this.showActionIndex===index){
  210. this.showActionIndex=-1;
  211. setTimeout(()=>{
  212. this.computedScollviewHeight();
  213. },50)
  214. return;
  215. }
  216. switch(index){
  217. case 0:
  218. this.showActionIndex=index;
  219. setTimeout(()=>{
  220. this.computedScollviewHeight();
  221. },50)
  222. break;
  223. case 1:
  224. this.showActionIndex=-1;
  225. uni.chooseImage({
  226. count: 1,
  227. sizeType: ['original', 'compressed'],
  228. sourceType: ['album', 'camera'],
  229. success:res=>{
  230. console.log(res)
  231. const message = uni.$TUIKit.createImageMessage({
  232. to: String(this.userInfo.id),
  233. conversationType: this.conversation.type,
  234. payload: {
  235. file: res
  236. },
  237. onProgress:event=>{}
  238. });
  239. this.$sendTIMMessage(message);
  240. }
  241. })
  242. break;
  243. case 2:
  244. uni.chooseVideo({
  245. sourceType: ['album', 'camera'],
  246. maxDuration: 60,
  247. camera: 'back',
  248. success:res=>{
  249. const message = uni.$TUIKit.createVideoMessage({
  250. to: String(this.userInfo.id),
  251. conversationType: this.conversation.type,
  252. payload: {
  253. file: res
  254. },
  255. onProgress:event=>{}
  256. });
  257. this.$sendTIMMessage(message);
  258. }
  259. })
  260. break;
  261. case 3:
  262. this.showActionIndex=index;
  263. setTimeout(()=>{
  264. this.computedScollviewHeight();
  265. },50)
  266. break;
  267. }
  268. },
  269. appendMessage(e) {
  270. this.inputText+=e.detail.message;
  271. },
  272. startRecordManage(){
  273. // 加载声音录制管理器
  274. this.recorderManager = uni.getRecorderManager();
  275. console.log(this.recorderManager)
  276. this.recorderManager.onStop(res => {
  277. console.log(res)
  278. clearInterval(this.recordTimer);
  279. // 兼容 uniapp 打包app,duration 和 fileSize 需要用户自己补充
  280. // 文件大小 = (音频码率) x 时间长度(单位:秒) / 8
  281. let msg = {
  282. duration: res.duration ? res.duration : this.recordTime * 1000,
  283. tempFilePath: res.tempFilePath,
  284. fileSize: res.fileSize ? res.fileSize : ((48 * this.recordTime) / 8) * 1024
  285. };
  286. uni.hideLoading();
  287. // 兼容 uniapp 语音消息没有duration
  288. if (this.canSend) {
  289. if (msg.duration < 1000) {
  290. uni.showToast({
  291. title: '录音时间太短',
  292. icon: 'none'
  293. });
  294. } else {
  295. // res.tempFilePath 存储录音文件的临时路径
  296. const message = uni.$TUIKit.createAudioMessage({
  297. to: String(this.userInfo.id),
  298. conversationType: this.conversation.type,
  299. payload: {
  300. file: msg
  301. }
  302. });
  303. this.$sendTIMMessage(message);
  304. }
  305. }
  306. this.startPoint=0;
  307. this.isRecording=false;
  308. this.canSend=true;
  309. this.voiceText='按住说话,松手发送';
  310. });
  311. this.recorderManager.onError(err=>{
  312. console.log(err)
  313. })
  314. },
  315. handleLongPress(e) {
  316. uni.vibrateShort();
  317. this.recorderManager.start({
  318. duration: 60000,
  319. // 录音的时长,单位 ms,最大值 600000(10 分钟)
  320. sampleRate: 44100,
  321. // 采样率
  322. numberOfChannels: 1,
  323. // 录音通道数
  324. encodeBitRate: 192000,
  325. // 编码码率
  326. format: 'aac' // 音频格式,选择此格式创建的音频消息,可以在即时通信 IM 全平台(Android、iOS、微信小程序和Web)互通
  327. });
  328. this.startPoint=e.touches[0];
  329. this.voiceText='正在录音,上划可取消';
  330. this.isRecording=true;
  331. this.recordTime=0;
  332. this.recordTimer = setInterval(() => {
  333. this.recordTime++;
  334. if(this.recorTime>=this.recordTimeTotal){
  335. this.recorderManager.stop();
  336. clearInterval(this.recordTimer);
  337. this.recordTimer=null;
  338. }
  339. }, 1000);
  340. },
  341. // 录音时的手势上划移动距离对应文案变化
  342. handleTouchMove(e) {
  343. if (this.isRecording) {
  344. if (this.startPoint.clientY - e.touches[e.touches.length - 1].clientY > 100) {
  345. this.voiceText='松开手指,取消发送';
  346. this.canSend=false;
  347. } else if (this.startPoint.clientY - e.touches[e.touches.length - 1].clientY > 20) {
  348. this.voiceText='上划可取消';
  349. this.canSend=true;
  350. } else {
  351. this.voiceText='抬起停止';
  352. this.canSend=true;
  353. }
  354. }
  355. },
  356. // 手指离开页面滑动
  357. handleTouchEnd() {
  358. this.isRecording=false;
  359. this.voiceText='按住说话,松手发送';
  360. uni.hideLoading();
  361. this.recorderManager.stop();
  362. },
  363. sendTextMessage() {
  364. const to = String(this.userInfo.id);
  365. const text =this.inputText;
  366. const message = uni.$TUIKit.createTextMessage({
  367. to,
  368. conversationType: this.conversation.type,
  369. payload: {
  370. text
  371. }
  372. });
  373. this.inputText='';
  374. this.$sendTIMMessage(message);
  375. },
  376. $sendTIMMessage(message) {
  377. uni.$TUIKit.sendMessage(message).then((res) => {
  378. this.$EventBus.$emit('sendMessage', message)
  379. this.$refs.messageList.scrollToButtom();
  380. }).catch((error) => {
  381. uni.showToast({
  382. title:'发送消息失败',
  383. icon:"none"
  384. })
  385. })
  386. },
  387. }
  388. }
  389. </script>
  390. <style lang="scss" scoped>
  391. .container{
  392. width: 100vw;
  393. height: 100vh;
  394. background-color: $bgcolor1;
  395. position: relative;
  396. overflow: hidden;
  397. .topnav {
  398. padding: 0 10rpx;
  399. position: fixed;
  400. top: 0;
  401. left: 0;
  402. width: 100vw;
  403. z-index: 100;
  404. background-color: $bgcolor1;
  405. .nav-item{
  406. width: 40rpx;
  407. height: 40rpx;
  408. margin-left: 16rpx;
  409. .nav-img{
  410. width: 40rpx;
  411. height: 40rpx;
  412. }
  413. }
  414. .nav-center{
  415. flex: 1;
  416. flex-direction: column;
  417. .nav-text{
  418. color: $fontcolor5;
  419. height: 40rpx;
  420. text-align: center;
  421. }
  422. .nav-tip{
  423. color:$fontcolor2;
  424. }
  425. }
  426. }
  427. .talk-box{
  428. position: fixed;
  429. bottom: 0;
  430. left: 0;
  431. width: 100vw;
  432. padding-top: 16rpx;
  433. background-color: $bgcolor1;
  434. .input-box{
  435. margin: 0rpx 32rpx;
  436. height: 80rpx;
  437. border-radius: 80rpx;
  438. background-color: $bgcolor4;
  439. padding: 0 24rpx;
  440. .input{
  441. width: 100%;
  442. height: 100%;
  443. color: #ffffff;
  444. font-size: 22rpx;
  445. }
  446. .input-btn{
  447. background-color: $primary;
  448. color: #ffffff;
  449. border-radius: 16rpx;
  450. height: 56rpx;
  451. line-height: 56rpx;
  452. width: 120rpx;
  453. text-align: center;
  454. }
  455. }
  456. .action-box{
  457. padding: 0rpx 0rpx 20rpx 0rpx;
  458. margin: 0 90rpx;
  459. margin-top: 36rpx;
  460. .act-img{
  461. width: 56rpx;
  462. height: 56rpx;
  463. }
  464. }
  465. .action-panel{
  466. width: 100vw;
  467. .voice-panel{
  468. height: 400rpx;
  469. flex-direction: column;
  470. .voice-text{
  471. color: #7D7DA4;
  472. text-align: center;
  473. }
  474. .voice-img{
  475. width: 200rpx;
  476. height: 200rpx;
  477. margin-top: 40rpx;
  478. }
  479. }
  480. }
  481. .emoji-box{
  482. padding: 0 11rpx;
  483. box-sizing: border-box;
  484. height: 400rpx;
  485. flex-wrap: wrap;
  486. transition: height .3s;
  487. .emoji-item{
  488. width: 64rpx;
  489. height: 64rpx;
  490. padding: 20rpx;
  491. .emoji-img{
  492. width: 64rpx;
  493. height: 64rpx;
  494. }
  495. }
  496. }
  497. }
  498. .container-box {
  499. position: fixed;
  500. display: flex;
  501. justify-content: center;
  502. align-items: center;
  503. left: 0;
  504. right: 0;
  505. bottom: 0;
  506. top: 0;
  507. background-color: rgba(0, 0, 0, 0.5);
  508. .video-message {
  509. width: 90vw;
  510. height: auto;
  511. }
  512. }
  513. }
  514. </style>