index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. <template>
  2. <view>
  3. <view class="TUI-message-input-container">
  4. <view class="TUI-commom-function">
  5. <view v-for="(item, index) in commonFunction" :key="index" class="TUI-commom-function-item" :data-function="item" @tap="handleCommonFunctions">
  6. {{ item.name }}
  7. </view>
  8. </view>
  9. <view class="TUI-message-input">
  10. <image class="TUI-icon" @tap="switchAudio" :src="isAudio ? '/static/assets/keyboard.svg' : '/static/assets/audio.svg'"></image>
  11. <view v-if="!isAudio" class="TUI-message-input-main">
  12. <input
  13. class="TUI-message-input-area"
  14. :adjust-position="true"
  15. cursor-spacing="20"
  16. v-model="inputText"
  17. @input="onInputValueChange"
  18. maxlength="140"
  19. type="text"
  20. placeholder-class="input-placeholder"
  21. placeholder="请输入内容"
  22. @focus="inputBindFocus"
  23. @blur="inputBindBlur"
  24. />
  25. </view>
  26. <view
  27. v-else
  28. class="TUI-message-input-main"
  29. @longpress="handleLongPress"
  30. @touchmove="handleTouchMove"
  31. @touchend="handleTouchEnd"
  32. style="display: flex; justify-content: center; font-size: 32rpx; font-family: PingFangSC-Regular;"
  33. >
  34. <text>{{ text }}</text>
  35. </view>
  36. <view class="TUI-message-input-functions" hover-class="none">
  37. <image class="TUI-icon" @tap="handleEmoji" src="/static/assets/face-emoji.svg"></image>
  38. <view v-if="!sendMessageBtn" @tap="handleExtensions"><image class="TUI-icon" src="/static/assets/more.svg"></image></view>
  39. <view v-else class="TUI-sendMessage-btn" @tap="sendTextMessage">发送</view>
  40. </view>
  41. </view>
  42. <view v-if="displayFlag === 'emoji'" class="TUI-Emoji-area"><TUI-Emoji @enterEmoji="appendMessage"></TUI-Emoji></view>
  43. <view v-if="displayFlag === 'extension'" class="TUI-Extensions">
  44. <!-- TODO: 这里功能还没实现 -->
  45. <!-- <camera device-position="back" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera>-->
  46. <view class="TUI-Extension-slot" @tap="handleSendPicture">
  47. <image class="TUI-Extension-icon" src="/static/assets/take-photo.svg"></image>
  48. <view class="TUI-Extension-slot-name">拍摄照片</view>
  49. </view>
  50. <view class="TUI-Extension-slot" @tap="handleSendImage">
  51. <image class="TUI-Extension-icon" src="/static/assets/send-img.svg"></image>
  52. <view class="TUI-Extension-slot-name">发送图片</view>
  53. </view>
  54. <view class="TUI-Extension-slot" @tap="handleShootVideo">
  55. <image class="TUI-Extension-icon" src="/static/assets/take-video.svg"></image>
  56. <view class="TUI-Extension-slot-name">拍摄视频</view>
  57. </view>
  58. <view class="TUI-Extension-slot" @tap="handleSendVideo">
  59. <image class="TUI-Extension-icon" src="/static/assets/send-video.svg"></image>
  60. <view class="TUI-Extension-slot-name">发送视频</view>
  61. </view>
  62. <view class="TUI-Extension-slot" @tap="handleServiceEvaluation">
  63. <image class="TUI-Extension-icon" src="/static/assets/service-assess.svg"></image>
  64. <view class="TUI-Extension-slot-name">服务评价</view>
  65. </view>
  66. <!-- <view class="TUI-Extension-slot" @tap="handleSendOrder">
  67. <image class="TUI-Extension-icon" src="/static/assets/send-order.svg"></image>
  68. <view class="TUI-Extension-slot-name">发送订单</view>
  69. </view> -->
  70. </view>
  71. <TUI-Common-Words class="tui-cards" :display="displayCommonWords" @sendMessage="$handleSendTextMessage" @close="$handleCloseCards"></TUI-Common-Words>
  72. <TUI-Order-List class="tui-cards" :display="displayOrderList" @sendCustomMessage="$handleSendCustomMessage" @close="$handleCloseCards"></TUI-Order-List>
  73. <TUI-Service-Evaluation
  74. class="tui-cards"
  75. :display="displayServiceEvaluation"
  76. @sendCustomMessage="$handleSendCustomMessage"
  77. @close="$handleCloseCards"
  78. ></TUI-Service-Evaluation>
  79. </view>
  80. <view class="record-modal" v-if="popupToggle" @longpress="handleLongPress" @touchmove="handleTouchMove" @touchend="handleTouchEnd">
  81. <view class="wrapper"><view class="modal-loading"></view></view>
  82. <view class="modal-title">{{ title }}</view>
  83. </view>
  84. </view>
  85. </template>
  86. <script>
  87. import TUIEmoji from '../message-elements/emoji/index';
  88. import TUICommonWords from '../message-private/common-words/index';
  89. import TUIOrderList from '../message-private/order-list/index';
  90. import TUIServiceEvaluation from '../message-private/service-evaluation/index';
  91. export default {
  92. data() {
  93. return {
  94. firstSendMessage: true,
  95. inputText: '',
  96. extensionArea: false,
  97. sendMessageBtn: false,
  98. displayFlag: '',
  99. isAudio: false,
  100. bottomVal: 0,
  101. startPoint: 0,
  102. popupToggle: false,
  103. isRecording: false,
  104. canSend: true,
  105. text: '按住说话',
  106. title: ' ',
  107. notShow: false,
  108. isShow: true,
  109. recordTime: 0,
  110. recordTimer: null,
  111. commonFunction: [
  112. {
  113. name: '常用语',
  114. key: '0'
  115. },
  116. // {
  117. // name: '发送订单',
  118. // key: '1'
  119. // },
  120. {
  121. name: '服务评价',
  122. key: '2'
  123. }
  124. ],
  125. displayServiceEvaluation: false,
  126. displayCommonWords: false,
  127. displayOrderList: false
  128. };
  129. },
  130. components: {
  131. TUIEmoji,
  132. TUICommonWords,
  133. TUIOrderList,
  134. TUIServiceEvaluation
  135. },
  136. props: {
  137. conversation: {
  138. type: Object,
  139. default: () => {}
  140. },
  141. toUser: {
  142. type: String,
  143. default: () => {}
  144. }
  145. },
  146. watch: {
  147. conversation: {
  148. handler: function(newVal) {
  149. // todo 值会被改变
  150. // this.setData({
  151. // conversation: newVal
  152. // });
  153. },
  154. immediate: true,
  155. deep: true
  156. }
  157. },
  158. beforeMount() {
  159. var that=this;
  160. // 加载声音录制管理器
  161. this.recorderManager = uni.getRecorderManager();
  162. this.recorderManager.onStop(res => {
  163. clearInterval(this.recordTimer);
  164. // 兼容 uniapp 打包app,duration 和 fileSize 需要用户自己补充
  165. // 文件大小 = (音频码率) x 时间长度(单位:秒) / 8
  166. let msg = {
  167. duration: res.duration ? res.duration : this.recordTime * 1000,
  168. tempFilePath: res.tempFilePath,
  169. fileSize: res.fileSize ? res.fileSize : ((48 * this.recordTime) / 8) * 1024
  170. };
  171. uni.hideLoading();
  172. // 兼容 uniapp 语音消息没有duration
  173. if (this.canSend) {
  174. if (msg.duration < 1000) {
  175. uni.showToast({
  176. title: '录音时间太短',
  177. icon: 'none'
  178. });
  179. } else {
  180. var orderId=uni.getStorageSync('orderId');
  181. // res.tempFilePath 存储录音文件的临时路径
  182. const message = uni.$TUIKit.createAudioMessage({
  183. to: this.getToAccount(),
  184. conversationType: this.conversation.type,
  185. payload: {
  186. file: msg
  187. },
  188. cloudCustomData: 'orderId='+orderId
  189. });
  190. this.$sendTIMMessage(message);
  191. }
  192. }
  193. that.setData({
  194. startPoint: 0,
  195. popupToggle: false,
  196. isRecording: false,
  197. canSend: true,
  198. title: ' ',
  199. text: '按住说话'
  200. });
  201. });
  202. },
  203. methods: {
  204. switchAudio() {
  205. this.isAudio= !this.isAudio;
  206. this.text='按住说话';
  207. },
  208. handleLongPress(e) {
  209. this.recorderManager.start({
  210. duration: 60000,
  211. // 录音的时长,单位 ms,最大值 600000(10 分钟)
  212. sampleRate: 44100,
  213. // 采样率
  214. numberOfChannels: 1,
  215. // 录音通道数
  216. encodeBitRate: 192000,
  217. // 编码码率
  218. format: 'aac' // 音频格式,选择此格式创建的音频消息,可以在即时通信 IM 全平台(Android、iOS、微信小程序和Web)互通
  219. });
  220. this.setData({
  221. startPoint: e.touches[0],
  222. title: '正在录音',
  223. // isRecording : true,
  224. // canSend: true,
  225. notShow: true,
  226. isShow: false,
  227. isRecording: true,
  228. popupToggle: true,
  229. recordTime: 0
  230. });
  231. this.recordTimer = setInterval(() => {
  232. this.recordTime++;
  233. }, 1000);
  234. },
  235. // 录音时的手势上划移动距离对应文案变化
  236. handleTouchMove(e) {
  237. if (this.isRecording) {
  238. if (this.startPoint.clientY - e.touches[e.touches.length - 1].clientY > 100) {
  239. this.setData({
  240. text: '抬起停止',
  241. title: '松开手指,取消发送',
  242. canSend: false
  243. });
  244. } else if (this.startPoint.clientY - e.touches[e.touches.length - 1].clientY > 20) {
  245. this.setData({
  246. text: '抬起停止',
  247. title: '上划可取消',
  248. canSend: true
  249. });
  250. } else {
  251. this.setData({
  252. text: '抬起停止',
  253. title: '正在录音',
  254. canSend: true
  255. });
  256. }
  257. }
  258. },
  259. // 手指离开页面滑动
  260. handleTouchEnd() {
  261. this.setData({
  262. isRecording: false,
  263. popupToggle: false
  264. });
  265. uni.hideLoading();
  266. this.recorderManager.stop();
  267. },
  268. handleEmoji() {
  269. let targetFlag = 'emoji';
  270. if (this.displayFlag === 'emoji') {
  271. targetFlag = '';
  272. }
  273. this.displayFlag=targetFlag;
  274. },
  275. handleExtensions() {
  276. let targetFlag = 'extension';
  277. if (this.displayFlag === 'extension') {
  278. targetFlag = '';
  279. }
  280. this.displayFlag=targetFlag;
  281. },
  282. error(e) {
  283. console.log(e.detail);
  284. },
  285. handleSendPicture() {
  286. this.sendImageMessage('camera');
  287. },
  288. handleSendImage() {
  289. this.sendImageMessage('album');
  290. },
  291. sendImageMessage(type) {
  292. uni.chooseImage({
  293. sourceType: [type],
  294. count: 1,
  295. success: res => {
  296. if (res) {
  297. var orderId=uni.getStorageSync('orderId');
  298. const message = uni.$TUIKit.createImageMessage({
  299. to: this.getToAccount(),
  300. conversationType: this.conversation.type,
  301. payload: {
  302. file: res
  303. },
  304. cloudCustomData: 'orderId='+orderId,
  305. onProgress: percent => {
  306. message.percent = percent;
  307. }
  308. });
  309. this.$sendTIMMessage(message);
  310. }
  311. }
  312. });
  313. },
  314. handleShootVideo() {
  315. this.sendVideoMessage('camera');
  316. },
  317. handleSendVideo() {
  318. this.sendVideoMessage('album');
  319. },
  320. sendVideoMessage(type) {
  321. uni.chooseVideo({
  322. sourceType: [type],
  323. // 来源相册或者拍摄
  324. maxDuration: 60,
  325. // 设置最长时间60s
  326. camera: 'back',
  327. // 后置摄像头
  328. success: res => {
  329. if (res) {
  330. var orderId=uni.getStorageSync('orderId');
  331. const message = uni.$TUIKit.createVideoMessage({
  332. to: this.getToAccount(),
  333. conversationType: this.conversation.type,
  334. payload: {
  335. file: res
  336. },
  337. cloudCustomData: 'orderId='+orderId,
  338. onProgress: percent => {
  339. message.percent = percent;
  340. }
  341. });
  342. this.$sendTIMMessage(message);
  343. }
  344. }
  345. });
  346. },
  347. handleCommonFunctions(e) {
  348. switch (e.target.dataset.function.key) {
  349. case '0':
  350. this.setData({
  351. displayCommonWords: true
  352. });
  353. break;
  354. // case '1':
  355. // this.setData({
  356. // displayOrderList: true
  357. // });
  358. // break;
  359. case '2':
  360. this.setData({
  361. displayServiceEvaluation: true
  362. });
  363. break;
  364. default:
  365. break;
  366. }
  367. },
  368. handleSendOrder() {
  369. this.setData({
  370. displayOrderList: true
  371. });
  372. },
  373. appendMessage(e) {
  374. this.setData({
  375. inputText: this.inputText + e.detail.message,
  376. sendMessageBtn: true
  377. });
  378. },
  379. getToAccount() {
  380. return this.toUser;
  381. },
  382. handleCalling(value) {
  383. // todo 目前支持单聊
  384. if (this.conversation.type === 'GROUP') {
  385. uni.showToast({
  386. title: '群聊暂不支持',
  387. icon: 'none'
  388. });
  389. return;
  390. }
  391. const { userID } = this.conversation.userProfile;
  392. // #ifdef APP-PLUS
  393. if(typeof(uni.$TUICalling) === 'undefined') {
  394. logger.error('请使用真机运行并且自定义基座调试,可能影响音视频功能~ 插件地址:https://ext.dcloud.net.cn/plugin?id=7097 , 调试地址:https://nativesupport.dcloud.net.cn/NativePlugin/use/use');
  395. uni.showToast({
  396. title: '请使用真机运行并且自定义基座调试,可能影响音视频功能~ ',
  397. icon: 'none',
  398. duration: 3000
  399. });
  400. } else {
  401. uni.$TUICalling.call(
  402. {
  403. userID: userID,
  404. type: value
  405. },
  406. res => {
  407. console.log(JSON.stringify(res));
  408. }
  409. );
  410. }
  411. // #endif
  412. // #ifdef MP-WEIXIN
  413. uni.showToast({
  414. title: '微信小程序暂不支持',
  415. icon: 'none'
  416. });
  417. // uni.$wxTUICalling.call({userID, type: value})
  418. // #endif
  419. },
  420. sendTextMessage(msg, flag) {
  421. var orderId=uni.getStorageSync('orderId');
  422. console.log(this.conversation.type)
  423. const to = this.getToAccount();
  424. const text = flag ? msg : this.inputText;
  425. const message = uni.$TUIKit.createTextMessage({
  426. to,
  427. conversationType: this.conversation.type,
  428. payload: {
  429. text:text
  430. },
  431. cloudCustomData: 'orderId='+orderId
  432. });
  433. this.setData({
  434. inputText: '',
  435. sendMessageBtn: false
  436. });
  437. this.$sendTIMMessage(message);
  438. },
  439. onInputValueChange(event) {
  440. if (event.detail.value) {
  441. this.setData({
  442. sendMessageBtn: true
  443. });
  444. } else {
  445. this.setData({
  446. sendMessageBtn: false
  447. });
  448. }
  449. },
  450. $handleSendTextMessage(event) {
  451. this.sendTextMessage(event.detail.message, true);
  452. this.setData({
  453. displayCommonWords: false
  454. });
  455. },
  456. $handleSendCustomMessage(e) {
  457. var orderId=uni.getStorageSync('orderId');
  458. const message = uni.$TUIKit.createCustomMessage({
  459. to: this.getToAccount(),
  460. conversationType: this.conversation.type,
  461. payload: e.detail.payload,
  462. cloudCustomData: 'orderId='+orderId
  463. });
  464. this.$sendTIMMessage(message);
  465. this.setData({
  466. displayOrderList: false
  467. });
  468. },
  469. $handleCloseCards(e) {
  470. switch (e.detail.key) {
  471. case '0':
  472. this.setData({
  473. displayCommonWords: false
  474. });
  475. break;
  476. case '1':
  477. this.setData({
  478. displayOrderList: false
  479. });
  480. break;
  481. case '2':
  482. this.setData({
  483. displayServiceEvaluation: false
  484. });
  485. break;
  486. default:
  487. break;
  488. }
  489. },
  490. $sendTIMMessage(message) {
  491. this.$emit('sendMessage', {
  492. detail: {
  493. message
  494. }
  495. });
  496. uni.$TUIKit.sendMessage(message).then((res) => {
  497. this.firstSendMessage = false
  498. }).catch((error) => {
  499. })
  500. this.setData({
  501. displayFlag: ''
  502. });
  503. },
  504. handleClose() {
  505. this.setData({
  506. displayFlag: ''
  507. });
  508. },
  509. handleServiceEvaluation() {
  510. this.setData({
  511. displayServiceEvaluation: true
  512. });
  513. },
  514. inputBindFocus() {
  515. console.log('占位:函数 inputBindFocus 未声明');
  516. },
  517. inputBindBlur() {
  518. console.log('占位:函数 inputBindBlur 未声明');
  519. }
  520. }
  521. };
  522. </script>
  523. <style>
  524. @import './index.css';
  525. </style>