index.vue 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. <template>
  2. <view>
  3. <!--prescription/inquiry/index.wxml-->
  4. <scroll-view :scroll-into-view="toView" :style="'height: ' + scroll_height + 'px;'" upper-threshold="100"
  5. scroll-y="true" enable-back-to-top="true" class="message-list">
  6. <!-- 每一行 -->
  7. <view v-for="(item, index) in messages" :key="index" class="row" :id="'row_' + index">
  8. <!-- 日期 -->
  9. <!-- <view class="datetime" wx:if="{{item.msgTime != ''}}">{{item.msgTime}}</view> -->
  10. <!-- 头像与内容文本 -->
  11. <view class="body" :style="'flex-flow: ' + (item.flow == 'in' ? 'row' : 'row-reverse')">
  12. <view class="avatar-container">
  13. <image v-if="item.flow=='in'" class="avatar"
  14. :src="friendAvatarUrl?friendAvatarUrl:'/prescription/image/ys.png'"></image>
  15. <image v-else class="avatar" :src="userData.avatar"></image>
  16. </view>
  17. <!-- 画对话框 -->
  18. <!-- <view class="triangle" style="{{item.flow == 'out' ? 'right: 60px; background: #7ECB4B' : 'left: 60px;'}}">
  19. </view> -->
  20. <view :class="item.flow == 'out' ? 'content' : 'contentl'"
  21. :style="item.flow == 'out' ? 'background: #7ECB4B' : ''">
  22. <view v-if="item.type === 'TIMTextElem'">{{item.payload.text}}</view>
  23. <image class="image-message" v-else-if="item.type === 'TIMImageElem'"
  24. :src="item.payload.imageInfoArray[1].url" @tap="previewImage"
  25. :data-src="item.payload.imageInfoArray[1].url"></image>
  26. <view v-else-if="item.type === 'TIMSoundElem'" :url="item.payload.url">
  27. <view class="box" @tap="openAudio" :data-eventid="'13_'+index"
  28. :data-time="item.payload.second" :data-comkey="item.payload.url">
  29. <image :src="'13_'+index==audioIndex?audioGif:audioPng" style="height:22px;width:22px"
  30. class="_image">
  31. </image>
  32. <!-- <view style="padding-left: 4px;" class="_div data-v-afeb3abc">
  33. {{item.payload.second<1?1:item.payload.second}}s
  34. </view> -->
  35. </view>
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. <view class="placeholder-view" :style="'height:' + (bottomBlankHeight + bottomShow?170:58) + 'rpx'"></view>
  41. <!-- <view class="placeholder-view" style="height:{{bottomBlankHeight + num>1?230:120}}rpx" wx:if="{{bottomShow}}"></view> -->
  42. </scroll-view>
  43. <view class="reply" :style="'bottom:' + reply_height + 'px;'">
  44. <view class="top" v-if="num>1" :style="'bottom:' + (reply_height+55) + 'px;'">
  45. <text class="num">对不起,医生忙不过来,您前面还有 <text class="sum">{{num}}</text> 人在排队中!</text>
  46. </view>
  47. <view class="top" v-else :style="'bottom:' + (reply_height+55) + 'px;'">
  48. <text class="num">医生正在开方中,请耐心等待...</text>
  49. </view>
  50. <!-- <view class="Audio">
  51. <image wx:if="{{opration==true}}" bindtap="Audio" src="../image/Audio.png"></image>
  52. <image wx:else bindtap="keyboard" src="../image/keyboard.png"></image>
  53. </view> -->
  54. <view class="opration-area">
  55. <input :adjust-position="false" @focus="bindfocus" @blur="blur" @confirm="sendMsg" v-if="opration==true"
  56. type="text" @input="getContent" :value="content"></input>
  57. <!-- <view wx:else class="voice-button {{touchBtn?'hoverBtn':''}}" bind:touchstart="startAudio"
  58. bind:touchend="onTouchEnd" bind:longpress="onLongpress" bind:touchmove="onTouchMove" >
  59. {{touchBtn?'松开 结束':'按住说话'}}
  60. </view> -->
  61. </view>
  62. <view :class="sendBtn==true?'send':'sendActive'" @tap="sendMsg">发送</view>
  63. <!-- <view class="add" bind:tap="moreClick">
  64. <image class="more" src="../image/more.png"></image>
  65. </view> -->
  66. <!-- <view class="send" bindtap="sendImg">相册</view>
  67. <view class="send" bindtap="startAudio">开始</view>
  68. <view class="send" bindtap="endAudio">结束</view> -->
  69. </view>
  70. <!-- <view class="more_box" hidden="{{moreShow}}">
  71. <view class="more_item" bindtap="sendImg">
  72. <view class="img_box">
  73. <image src="../image/picture.png"></image>
  74. </view>
  75. <view style="margin-top:10rpx;">
  76. <text>相册</text>
  77. </view>
  78. </view>
  79. </view> -->
  80. </view>
  81. </template>
  82. <script>
  83. const TIM = require("../imjs/tim-wx-sdk.js");
  84. const COS = require("../imjs/cos-wx-sdk-v5");
  85. const req = require("../../utils/request.js");
  86. var windowWidth = uni.getSystemInfoSync().windowWidth;
  87. var windowHeight = uni.getSystemInfoSync().windowHeight;
  88. var keyHeight = 0;
  89. var numberTime;
  90. var orderTime;
  91. let tim;
  92. const app = getApp();
  93. let recorderManager = uni.getRecorderManager(); // 录音部分参数 小程序文档
  94. // 录音部分参数 小程序文档
  95. const recordOptions = {
  96. duration: 60000,
  97. // 录音的时长,单位 ms,最大值 600000(10 分钟)
  98. sampleRate: 44100,
  99. // 采样率
  100. numberOfChannels: 1,
  101. // 录音通道数
  102. encodeBitRate: 192000,
  103. // 编码码率
  104. format: 'aac' // 音频格式,选择此格式创建的音频消息,可以在即时通信 IM 全平台(Android、iOS、微信小程序和Web)互通
  105. };
  106. /**
  107. * 处理历史消息
  108. */
  109. function handlerHistoryMsgs(result, that) {
  110. // console.log(result, that.messages);
  111. var historyMsgs = that.messages;
  112. result.forEach(item => {
  113. // console.log(item);
  114. if (item.payload.text == "__end__") {
  115. item.payload.text = "医生端结束在线问诊!";
  116. }
  117. historyMsgs.push(item);
  118. }); // historyMsgs.push(result[0])
  119. that.setData({
  120. messages: historyMsgs
  121. }); // 将某会话下所有未读消息已读上报
  122. let promise = tim.setMessageRead({
  123. conversationID: that.conversationID
  124. });
  125. promise.then(function(imResponse) { // 已读上报成功
  126. }).catch(function(imError) { // 已读上报失败
  127. });
  128. }
  129. export default {
  130. data() {
  131. return {
  132. bottomBlankHeight: app.globalData.isIPhoneX ? 68 : 0,
  133. friendId: '',
  134. friendName: '',
  135. friendAvatarUrl: '',
  136. messages: [],
  137. // 消息集合
  138. complete: 0,
  139. // 是否还有历史消息可以拉取,1 - 表示没有,0 - 表示有
  140. content: '',
  141. // 输入框的文本值
  142. lock: false,
  143. // 发送消息锁 true - 加锁状态 false - 解锁状态
  144. scroll_height: uni.getSystemInfoSync().windowHeight - 54,
  145. reply_height: 0,
  146. moreShow: true,
  147. userData: [],
  148. audioPng: "/prescription/static/prescription/image/audio-play.png",
  149. audioGif: "/prescription/static/prescription/image/audio-play.gif",
  150. audioState: true,
  151. /**
  152. * 历史消息消息集合(结构如下):
  153. * nextReqMessageID 用于续拉,分页续拉时需传入该字段。
  154. * isCompleted 表示是否已经拉完所有消息。
  155. */
  156. nextReqMessageID: "",
  157. isCompleted: "",
  158. isFirstGetList: true,
  159. audioContext: null,
  160. opration: true,
  161. touchBtn: false,
  162. recording: false,
  163. stopflag: false,
  164. cancelRecord: false,
  165. refreshTime: '',
  166. ScrollLoading: 0,
  167. audioIndex: null,
  168. sendBtn: true,
  169. conversationID: "",
  170. num: "",
  171. toView: "",
  172. stopFlag: false,
  173. bottomShow: false,
  174. titleShow: false,
  175. orderDetail: {},
  176. end: false,
  177. config: ''
  178. };
  179. },
  180. components: {},
  181. props: {},
  182. onLoad: function(options) {
  183. var that = this;
  184. that.product = true;
  185. that.config = JSON.parse(req.getStorage('configRes'));
  186. uni.hideNavigationBarLoading();
  187. // console.log(options);
  188. let opt = {
  189. SDKAppID: options.sdkAppID // 接入时需要将0替换为您的即时通信 IM 应用的 SDKAppID
  190. }; // 创建 SDK 实例,`TIM.create()`方法对于同一个 `SDKAppID` 只会返回同一份实例
  191. tim = TIM.create(opt); // SDK 实例通常用 tim 表示
  192. // 设置 SDK 日志输出级别,详细分级请参见 setLogLevel 接口的说明
  193. tim.setLogLevel(1); // 普通级别,日志量较多,接入时建议使用
  194. // tim.setLogLevel(1); // release 级别,SDK 输出关键信息,生产环境时建议使用
  195. // 注册 COS SDK 插件
  196. tim.registerPlugin({
  197. 'cos-wx-sdk': COS
  198. });
  199. this.query = options;
  200. that.messages = []; // 清空历史消息
  201. // tim.on(TIM.EVENT.SDK_READY,this.getMessageList)
  202. if (options.msg) {
  203. let msg = JSON.parse(options.msg);
  204. msg = msg.map(it => {
  205. it.payload = {
  206. text: it.msg
  207. }; // it.concat(...it[index],payload:{text:1})
  208. // console.log(it);
  209. return it;
  210. });
  211. this.setData({
  212. messages: msg
  213. });
  214. // console.log(options.msg.replace(/msg/g, 'payload'), JSON.parse(options.msg), msg);
  215. }
  216. this.getNumber();
  217. this.setData({
  218. friendId: JSON.parse(options.doctor).uuid,
  219. friendName: JSON.parse(options.doctor).name,
  220. friendAvatarUrl: JSON.parse(options.doctor).img,
  221. conversationID: 'C2C' + options.comunicationid
  222. });
  223. uni.setNavigationBarTitle({
  224. title: JSON.parse(options.doctor).name + '医生在线问诊'
  225. });
  226. var userData = req.getStorage('userInfo');
  227. let audioContext = uni.createInnerAudioContext();
  228. this.setData({
  229. userData,
  230. audioContext
  231. });
  232. // 将某会话下所有未读消息已读上报
  233. let promise = function(event) {
  234. tim.setMessageRead({
  235. conversationID: options.comunicationid
  236. });
  237. promise.then(function(imResponse) { // 已读上报成功
  238. }).catch(function(imError) { // 已读上报失败
  239. });
  240. };
  241. // console.log("=========================");
  242. this.out()
  243. let newlist = function() {
  244. setTimeout(function() {
  245. promise;
  246. }, 1000);
  247. };
  248. tim.on(TIM.EVENT.SDK_READY, newlist);
  249. let login = tim.login({
  250. userID: this.query.identifier,
  251. userSig: this.query.userSig
  252. });
  253. login.then(function(imResponse) {
  254. // console.log("login"); // 登录成功
  255. // console.log(imResponse); // 登录成功
  256. // console.log(imResponse.data.repeatLogin, "重复登录");
  257. if (imResponse.data.repeatLogin === true) {
  258. let promise = tim.logout();
  259. promise.then(function(imResponse) {
  260. // console.log(imResponse.data, "============"); // 登出成功
  261. }).catch(function(imError) {
  262. // console.warn('logout error:', imError);
  263. });
  264. let login = tim.login({
  265. userID: this.query.identifier,
  266. userSig: this.query.userSig
  267. }); // 标识账号已登录,本次登录操作为重复登录。v2.5.1 起支持
  268. // console.log(imResponse.data.errorInfo);
  269. }
  270. let time = function() {
  271. setTimeout(function() {
  272. that.sendMsg("__refresh__");
  273. }, 1000);
  274. };
  275. tim.on(TIM.EVENT.SDK_READY, time);
  276. }).catch(function(imError) {
  277. uni.showModal({
  278. title: '提示',
  279. cancelText: '返回首页',
  280. confirmText: '查看订单',
  281. content: '与医生对话连接失败,不影响开方,请关注订单状态',
  282. success(res) {
  283. if (res.confirm) {
  284. // console.log('用户点击确认');
  285. app.globalData.redirectTab("mine/orderDet/orderDet?id=" + that.query
  286. .orderId);
  287. that.out()
  288. } else if (res.cancel) {
  289. // app.globalData.reLaunchTo('pages/index/index')
  290. // console.log('用户点击取消');
  291. that.out();
  292. that.goHome();
  293. }
  294. }
  295. });
  296. console.warn('login error:', imError, "与医生对话连接失败,不影响开方,请关注订单状态"); // 登录失败的相关信息
  297. });
  298. },
  299. onShow: function() {
  300. let that = this; // 开始登录
  301. that.scrollToBottom();
  302. orderTime = setInterval(() => {
  303. this.getOrder();
  304. }, 3000);
  305. numberTime = setInterval(() => {
  306. this.getNumber();
  307. }, 3000);
  308. let onMessageReceived = function(event) {
  309. // event.data - 存储 Message 对象的数组 - [Message]
  310. let msgList = that.messages;
  311. // console.log("====================================", that.product, event.data[0].payload.text ==
  312. // "__end__", !that.end, that.orderDetail.id, that.orderDetail.state, that.query.orderId);
  313. // console.log(that.orderDetail,event,that)
  314. if ((event.data[0].payload.text == "__end__" && !that.end && that.product) || (that.orderDetail
  315. .state != 21 && that.query.orderId)) {
  316. let content
  317. if (that.orderDetail.state == '22') {
  318. content = '因' + that.orderDetail.orderDrugUser.reasons + ',开方失败,请重新开方'
  319. } else if (that.orderDetail.state != '21') {
  320. content = '您的订单开方成功!'
  321. } else {
  322. content = '医生在线问诊结束,处方稍后就会开出来,请关注您的订单!'
  323. }
  324. uni.showModal({
  325. title: '提示',
  326. cancelText: '返回首页',
  327. confirmText: '查看订单',
  328. content: content,
  329. success(res) {
  330. if (res.confirm) {
  331. // console.log('用户点击确认', that.query
  332. // .orderId);
  333. app.globalData.redirectTab("mine/orderDet/orderDet?id=" + that.query
  334. .orderId);
  335. that.out()
  336. clearInterval(orderTime);
  337. } else if (res.cancel) {
  338. // console.log('用户点击取消');
  339. that.out()
  340. that.goHome();
  341. clearInterval(orderTime);
  342. }
  343. }
  344. });
  345. that.end = true;
  346. } else {
  347. that.end = false
  348. handlerHistoryMsgs(event.data, that);
  349. }
  350. that.scrollToBottom();
  351. };
  352. tim.on(TIM.EVENT.MESSAGE_RECEIVED, onMessageReceived); // 获取当前聊天的历史列表
  353. let mestime = function() {
  354. setTimeout(function() {
  355. that.getMessageList();
  356. }, 1000);
  357. };
  358. tim.on(TIM.EVENT.SDK_READY, mestime); // 监听录音结束
  359. recorderManager.onStop(function(res) {
  360. if (that.recording) {
  361. if (that.cancelRecord) {
  362. uni.hideToast();
  363. that.setData({
  364. cancelRecord: false
  365. });
  366. } else {
  367. // 创建消息实例,接口返回的实例可以上屏
  368. const message = tim.createAudioMessage({
  369. to: that.friendId,
  370. conversationType: TIM.TYPES.CONV_C2C,
  371. payload: {
  372. file: res
  373. },
  374. onProgress: function(event) {}
  375. }); // 发送消息
  376. let promise = tim.sendMessage(message);
  377. promise.then(function(imResponse) {
  378. // 发送成功
  379. that.addMessage(imResponse.data.message, that);
  380. }).catch(function(imError) { // 发送失败
  381. });
  382. that.setData({
  383. recording: false
  384. });
  385. }
  386. } else {
  387. uni.showToast({
  388. title: '说话时间太短',
  389. duration: 1000,
  390. image: "/prescription/static/prescription/image/err.png"
  391. });
  392. }
  393. });
  394. },
  395. onHide() {
  396. clearInterval(orderTime);
  397. },
  398. onUnload() {
  399. // console.log("123456", this.query.orderId, this.orderDetail)
  400. this.orderDetail = {}
  401. delete this.query.orderId
  402. // console.log(this.orderDetail)
  403. this.product = false;
  404. // console.log(orderTime, "789")
  405. clearInterval(orderTime);
  406. // console.log(orderTime, "555")
  407. },
  408. methods: {
  409. // 登出
  410. out() {
  411. let out = tim.logout();
  412. out.then(function(imResponse) {
  413. // console.log(imResponse.data, "======登出成功======"); // 登出成功
  414. }).catch(function(imError) {
  415. // console.warn('logout error:', imError);
  416. });
  417. },
  418. goHome(){
  419. app.globalData.openHome();
  420. },
  421. // 获取订单状态
  422. getOrder() {
  423. // console.log(this.query.orderId)
  424. if (!this.query.orderId) {} else {
  425. if (!this.orderDetail.id || this.orderDetail.state == 21) {
  426. req.getRequest('/api/order/detail', {
  427. id: this.query.orderId
  428. }, data => {
  429. this.orderDetail = data
  430. // console.log(this.orderDetail, data.id)
  431. }, true)
  432. } else if (this.orderDetail.state != 21) {
  433. // console.log(this.orderDetail, this.orderDetail.id, "清除定时器")
  434. clearInterval(orderTime);
  435. }
  436. }
  437. },
  438. // 获取排队人数
  439. getNumber() {
  440. if (this.num - 1 <= 0) {
  441. clearInterval(numberTime);
  442. } else {
  443. req.getRequest('/api/userDrugPeople/getNumber', {
  444. doctorId: JSON.parse(this.query.doctor).id
  445. }, data => {
  446. // console.log(data)
  447. this.setData({
  448. num: data
  449. });
  450. }, true);
  451. }
  452. },
  453. /**
  454. * 获取消息列表
  455. */
  456. getMessageList() {
  457. let that = this;
  458. // console.log(that.conversationID);
  459. let cb = tim.getMessageList({
  460. conversationID: that.conversationID,
  461. //会话列表传递过来的参数
  462. count: 15
  463. });
  464. cb.then(function(imResponse) {
  465. const messageList = imResponse.data.messageList; // 消息列表。
  466. const nextReqMessageID = imResponse.data.nextReqMessageID; // 用于续拉,分页续拉时需传入该字段。
  467. const isCompleted = imResponse.data.isCompleted; // 表示是否已经拉完所有消息。
  468. that.setData({
  469. nextReqMessageID: nextReqMessageID,
  470. isCompleted: isCompleted
  471. });
  472. handlerHistoryMsgs(messageList, that);
  473. that.scrollToBottom();
  474. });
  475. },
  476. /**
  477. * 获取文本的消息
  478. */
  479. getContent: function(e) {
  480. if (e.detail.value == "") {
  481. this.setData({
  482. sendBtn: true
  483. });
  484. } else {
  485. this.setData({
  486. sendBtn: false
  487. });
  488. }
  489. // console.log(e);
  490. var that = this;
  491. that.setData({
  492. content: e.detail.value
  493. });
  494. },
  495. /**
  496. * 发送消息
  497. */
  498. sendMsg(content) {
  499. // console.log(content, typeof content);
  500. if (this.content == "" && typeof content != "string") {
  501. uni.showToast({
  502. title: '请输入内容',
  503. duration: 1000,
  504. icon: 'none'
  505. });
  506. return;
  507. }
  508. var that = this;
  509. // console.log(typeof this.content); // 发送文本消息,Web 端与小程序端相同
  510. // 1. 创建消息实例,接口返回的实例可以上屏
  511. let message = tim.createTextMessage({
  512. to: this.friendId,
  513. conversationType: TIM.TYPES.CONV_C2C,
  514. type: TIM.TYPES.MSG_CUSTOM,
  515. payload: {
  516. text: typeof content == 'string' ? content : this.content
  517. }
  518. });
  519. message._elements.push({
  520. content: {
  521. text: this.query.comunicationid
  522. },
  523. type: "TIMTextElem"
  524. });
  525. // console.log(message); // message.Elem.Text(this.data.conversationID)
  526. // 2. 发送消息
  527. let promise = tim.sendMessage(message);
  528. promise.then(function(imResponse) {
  529. // 发送成功
  530. // console.log(imResponse, imResponse.data.message.payload.text);
  531. if (content !== '__refresh__') {
  532. that.addMessage(imResponse.data.message, that);
  533. that.setCommunication(imResponse.data.message.payload.text);
  534. }
  535. that.titleShow = true
  536. that.setData({
  537. sendBtn: true
  538. });
  539. }).catch(function(imError) { // 发送失败
  540. });
  541. },
  542. /**
  543. * 刷新文本消息
  544. */
  545. addMessage: function(msg, that) {
  546. var messages = that.messages;
  547. // console.log(msg);
  548. messages.push(msg);
  549. that.setData({
  550. messages: messages,
  551. content: '' // 清空输入框文本
  552. });
  553. that.scrollToBottom();
  554. },
  555. /**
  556. * 发送图片消息
  557. */
  558. sendImg() {
  559. let that = this;
  560. uni.chooseImage({
  561. sourceType: ['album'],
  562. // 从相册选择
  563. count: 1,
  564. // 只选一张,目前 SDK 不支持一次发送多张图片
  565. success: function(res) {
  566. // 2. 创建消息实例,接口返回的实例可以上屏
  567. let message = tim.createImageMessage({
  568. to: that.friendId,
  569. conversationType: TIM.TYPES.CONV_C2C,
  570. payload: {
  571. file: res
  572. },
  573. onProgress: function(event) {}
  574. }); // 3. 发送图片
  575. let promise = tim.sendMessage(message);
  576. promise.then(function(imResponse) {
  577. // 发送成功
  578. that.addMessage(imResponse.data.message, that);
  579. }).catch(function(imError) { // 发送失败
  580. });
  581. }
  582. });
  583. },
  584. scrollToBottom: function() {
  585. this.setData({
  586. toView: 'row_' + (this.messages.length - 1)
  587. });
  588. },
  589. previewImage(e) {
  590. let src = '';
  591. uni.previewImage({
  592. current: e.currentTarget.dataset.src,
  593. // 当前显示图片的http链接
  594. urls: [e.currentTarget.dataset.src]
  595. });
  596. },
  597. // 保存聊天记录
  598. setCommunication(content) {
  599. let from = {
  600. communicationId: this.query.comunicationid,
  601. content: content,
  602. orderId: this.query.orderId
  603. };
  604. req.postRequest('/api/userDrugPeople/setCommunication', from, data => {
  605. // console.log(data);
  606. });
  607. },
  608. // 录制语音
  609. startAudio: function() {
  610. uni.showToast({
  611. title: '上滑取消发送',
  612. duration: 10000,
  613. image: "/prescription/static/prescription/image/cancel.png"
  614. });
  615. this.setData({
  616. touchBtn: true
  617. });
  618. if (this.stopFlag) {
  619. return;
  620. }
  621. recorderManager.start(recordOptions);
  622. recorderManager.onError(function(errMsg) {});
  623. },
  624. // # 利用长按判断录音是否太短
  625. onLongpress() {
  626. this.setData({
  627. recording: true
  628. });
  629. },
  630. // 发送录音
  631. onTouchEnd: function() {
  632. uni.hideToast();
  633. let that = this;
  634. that.setData({
  635. touchBtn: false
  636. });
  637. if (that.stopFlag) {
  638. return;
  639. }
  640. if (that.recording) {
  641. recorderManager.stop();
  642. } else {
  643. that.setData({
  644. stopFlag: true
  645. });
  646. setTimeout(() => {
  647. recorderManager.stop();
  648. that.setData({
  649. stopFlag: false
  650. });
  651. }, 400);
  652. }
  653. },
  654. // 播放语音
  655. openAudio(audio) {
  656. // console.log(audio);
  657. let index = audio.currentTarget.dataset.eventid;
  658. this.setData({
  659. audioIndex: index // audioState:false
  660. });
  661. this.audioContext.src = audio.currentTarget.dataset.comkey;
  662. this.audioContext.autoplay = true;
  663. this.audioContext.play();
  664. this.audioContext.onPlay(res => {});
  665. this.audioContext.onEnded(() => {
  666. uni.hideToast();
  667. this.setData({
  668. audioIndex: null
  669. });
  670. // console.log("语音结束了");
  671. });
  672. this.audioContext.onError(res => {});
  673. },
  674. // 上滑取消
  675. onTouchMove(e) {
  676. if (e.touches[0].clientY < 520) {
  677. // # 取消发送
  678. this.setData({
  679. cancelRecord: true
  680. });
  681. uni.showToast({
  682. title: '松开,取消发送',
  683. duration: 10000,
  684. image: "/prescription/static/prescription/image/cancel.png"
  685. });
  686. } else {
  687. // # 不取消
  688. uni.hideToast();
  689. uni.showToast({
  690. title: '上滑取消发送',
  691. duration: 10000,
  692. image: "/prescription/static/prescription/image/cancel.png"
  693. });
  694. this.setData({
  695. cancelRecord: false
  696. });
  697. }
  698. },
  699. // 下拉加载聊天记录
  700. refresh: function(e) {
  701. let that = this;
  702. if (that.ScrollLoading == 1) {
  703. //防止多次触发
  704. return false;
  705. }
  706. if (e.detail.scrollTop < 1) {
  707. that.setData({
  708. ScrollLoading: 1
  709. });
  710. uni.showLoading({
  711. title: '加载中'
  712. });
  713. setTimeout(() => {
  714. let promise = tim.getMessageList({
  715. conversationID: that.conversationID,
  716. nextReqMessageID: that.nextReqMessageID,
  717. count: 15
  718. });
  719. promise.then(function(imResponse) {
  720. const newMessageList = imResponse.data.messageList; // 消息列表。
  721. const nextReqMessageID = imResponse.data
  722. .nextReqMessageID; // 用于续拉,分页续拉时需传入该字段。
  723. const isCompleted = imResponse.data.isCompleted; // 表示是否已经拉完所有消息。
  724. that.setData({
  725. nextReqMessageID: nextReqMessageID,
  726. isCompleted: isCompleted,
  727. messages: newMessageList.concat(that.messages)
  728. });
  729. uni.hideLoading();
  730. that.setData({
  731. ScrollLoading: 0
  732. }); // handlerHistoryMsgs(messageList, that);
  733. });
  734. }, 800);
  735. } // setTimeout(function(){
  736. // var date = new Date();
  737. // },300);
  738. },
  739. // 切换
  740. Audio() {
  741. this.setData({
  742. opration: false
  743. });
  744. },
  745. keyboard() {
  746. this.setData({
  747. opration: true
  748. });
  749. },
  750. moreClick() {
  751. if (this.moreShow) {
  752. this.setData({
  753. moreShow: false,
  754. reply_height: 92,
  755. scroll_height: this.scroll_height - 92
  756. });
  757. }
  758. },
  759. //失去聚焦(软键盘消失)
  760. blur: function(e) {
  761. this.setData({
  762. scroll_height: uni.getSystemInfoSync().windowHeight - 54,
  763. reply_height: 0,
  764. bottomShow: false
  765. });
  766. this.setData({
  767. toView: 'row_' + (this.messages.length - 1)
  768. });
  769. // console.log(this.messages);
  770. },
  771. // /**
  772. // * 获取聚焦
  773. // */
  774. bindfocus: function(e) {
  775. // console.log(e)
  776. keyHeight = e.detail.height;
  777. this.setData({
  778. scroll_height: windowHeight - keyHeight
  779. });
  780. this.setData({
  781. toView: 'row_' + (this.messages.length - 1),
  782. reply_height: keyHeight,
  783. moreShow: true,
  784. bottomShow: true // reply_height: 0,
  785. });
  786. // console.log(windowHeight - keyHeight, keyHeight, this.toView)
  787. } // bindfocus(e) {
  788. // keyHeight = e.detail.height;
  789. // this.setData({
  790. // moreShow: true,
  791. // reply_height: 0,
  792. // scroll_height: (wx.getSystemInfoSync().windowHeight - keyHeight) +'px'
  793. // })
  794. // }
  795. }
  796. };
  797. </script>
  798. <style>
  799. @import "./index.css";
  800. </style>