left-bubble.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <template>
  2. <view>
  3. <!-- 别人发出的消息 -->
  4. <view class="other" v-if="row.sendUid!=userData.user.operId">
  5. <!-- 右键 -->
  6. <view class="left-click" v-show="row.id==lClickId">
  7. <view @tap="copyFunc(row.msgContext)">复制</view>
  8. <view @tap="deleteFunc(row.id,index)" v-if="row.msgType!=1">删除</view>
  9. <view @tap="forwardFunc(row)" v-if="row.msgType!=7">转发</view>
  10. <view @tap="collectFunc(row)" v-if="row.msgType==1">收藏</view>
  11. <!-- <text @tap="rollBackFunc(row)">撤销</text> -->
  12. </view>
  13. <!-- 左-头像 -->
  14. <view :class="row.msgType==0?'left text':'left'" @tap="linkToCard(row.sendUid)">
  15. <img-cache :src="row.avatar"></img-cache>
  16. </view>
  17. <!-- 右-用户名称-时间-消息 -->
  18. <view class="right">
  19. <view class="username">
  20. <view class="name">{{row.nickName}}</view>
  21. <view class="time">{{row.operTime|formatDate}}</view>
  22. </view>
  23. <!-- 文字消息 -->
  24. <view @longtap="openLeft(row)" v-if="row.msgType==0" class="bubble">
  25. <rich-text :nodes="transformFace(row.msgContext)"></rich-text>
  26. </view>
  27. <!-- 语音消息 -->
  28. <view @longtap="openLeft(row)" v-if="row.msgType==3" class="bubble voice" @tap="playVoice(row)" :class="playMsgid == row.id?'play':''">
  29. <view class="icon other-voice"></view>
  30. <view class="length">{{recordToJson(row.msgContext).length}}</view>
  31. </view>
  32. <!-- 图片消息 -->
  33. <view @longtap="openLeft(row)" v-if="row.msgType==1" class="bubble img" @tap="showPic(`${$url}/${row.msgContext}`)">
  34. <image :src="`${$url}/scale_${row.msgContext}`" style="width:100px;height:100px"></image>
  35. </view>
  36. <!-- 红包 -->
  37. <view v-if="row.msgType==7" @tap="openRedPacket(row,index)">
  38. <div class="message-red-packet-left" :style="redProcess(row.msgContext).surplusMoney===0?'background:#F7DFC3':'background:#F09D47'">
  39. <div class="text">
  40. <span class="packet">恭喜发财,大吉大利</span>
  41. <!-- <image :src="redProcess(row.msgContext).surplusMoney===0?'../../static/img/red-chai.png':'../../static/img/red.png'"></image> -->
  42. </div>
  43. <div :class="redProcess(row.msgContext).surplusMoney===0?'footer2':'footer'">红包</div>
  44. <div class="arrow-org" :style="redProcess(row.msgContext).surplusMoney===0?'background:#F7DFC3':'background:#F09D47'"></div>
  45. </div>
  46. </view>
  47. </view>
  48. </view>
  49. </view>
  50. </template>
  51. <script>
  52. import { transform } from "@/pageC/static/emoji/ChatUtils.js";
  53. export default {
  54. name: 'left-bubble',
  55. components:{
  56. },
  57. props: {
  58. row: {
  59. type: Object,
  60. default() {
  61. return {};
  62. }
  63. },
  64. lClickId: {
  65. type: Number,
  66. default: 0
  67. },
  68. index:{
  69. type:Number,
  70. defalut:0
  71. },
  72. },
  73. data() {
  74. return {
  75. //播放语音相关参数
  76. AUDIO:uni.createInnerAudioContext(),
  77. $url:'',
  78. playMsgid: {
  79. type: Number,
  80. default: 0
  81. },
  82. };
  83. },
  84. mounted() {
  85. //语音自然播放结束
  86. this.AUDIO.onEnded((res)=>{
  87. });
  88. },
  89. filters: {
  90. formatDate: function (e) {
  91. // 获取js 时间戳
  92. let time = new Date().getTime();
  93. // 去掉 js 时间戳后三位
  94. time = parseInt((time - e) / 1000);
  95. // 存储转换值
  96. let s;
  97. if (time < 60 * 10) {
  98. // 十分钟内
  99. return '刚刚';
  100. } else if (time < 60 * 60 && time >= 60 * 10) {
  101. // 超过十分钟少于1小时
  102. s = Math.floor(time / 60);
  103. return s + '分钟前';
  104. } else if (time < 60 * 60 * 24 && time >= 60 * 60) {
  105. // 超过1小时少于24小时
  106. s = Math.floor(time / 60 / 60);
  107. return s + '小时前';
  108. } else if (time < 60 * 60 * 24 * 3 && time >= 60 * 60 * 24) {
  109. // 超过1天少于3天内
  110. s = Math.floor(time / 60 / 60 / 24);
  111. return s + '天前';
  112. } else {
  113. // 超过3天
  114. var date = new Date(e);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
  115. var Y = date.getFullYear() + '-';
  116. var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  117. var D = date.getDate() + ' ';
  118. var h = date.getHours() + ':';
  119. var m = date.getMinutes() + ':';
  120. var ss = date.getSeconds();
  121. return Y+M+D+h+m+ss;
  122. }
  123. }
  124. },
  125. methods:{
  126. openLeft(row){
  127. this.$emit('openLeft', row);
  128. },
  129. deleteFunc(id,index){
  130. this.$emit('deleteF', id, index);
  131. },
  132. // 打开红包
  133. openRedPacket(msg){
  134. this.$emit('openRedPacket',msg);
  135. },
  136. //收藏表情
  137. collectFunc({msgContext}){
  138. this.$socket.addEmoticon(this.userData.user.operId, msgContext, res => {
  139. if(res.success){
  140. uni.showToast({
  141. icon:'none',
  142. title:'添加成功'
  143. })
  144. }
  145. });
  146. },
  147. //处理红包数据
  148. redProcess(msgContext){
  149. let packets = JSON.parse(msgContext).Packets;
  150. let msg = {
  151. description:'好友暂不支持发红包',
  152. money:0,
  153. number:0,
  154. userAvatar:'defalut.jpg',
  155. surplusMoney:0,
  156. Records:[]
  157. }
  158. if(packets==undefined){
  159. return msg;
  160. }
  161. if(packets.length==0){
  162. return msg;
  163. }
  164. return packets[0];
  165. },
  166. // 预览图片
  167. showPic(msg){
  168. uni.previewImage({
  169. indicator:"none",
  170. current: msg,
  171. urls: [msg]
  172. });
  173. },
  174. // 播放语音
  175. playVoice(msg){
  176. let s =JSON.parse(msg.msgContext);
  177. this.playMsgid= msg.id;
  178. this.AUDIO.src = this.$url + s.url;
  179. this.$nextTick(function() {
  180. this.AUDIO.play();
  181. });
  182. },
  183. playMp3(){
  184. let s =JSON.parse(msg.msgContext);
  185. const innerAudioContext = uni.createInnerAudioContext();
  186. innerAudioContext.autoplay = true;
  187. innerAudioContext.src = this.$url + s.url;
  188. innerAudioContext.onPlay(() => {
  189. console.log('开始播放');
  190. });
  191. innerAudioContext.onError((res) => {
  192. console.log(res.errMsg);
  193. console.log(res.errCode);
  194. });
  195. },
  196. recordToJson(msg){
  197. let s =JSON.parse(msg);
  198. return s
  199. },
  200. //复制
  201. copyFunc(content){
  202. uni.setClipboardData({
  203. data:content,
  204. success:()=>{
  205. uni.showToast({
  206. title:"复制成功"
  207. })
  208. }
  209. });
  210. },
  211. //转发
  212. forwardFunc({msgType,msgContext}){
  213. this.$u.route({
  214. url:'pageC/chat/forward',
  215. params:{msgType,msgContext}
  216. });
  217. },
  218. //撤销
  219. rollBackFunc({id,operTime}){
  220. let _this = this
  221. uni.showActionSheet({
  222. itemList: ['确认'],
  223. success: function (res) {
  224. if(res.tapIndex==0){
  225. let type = _this.chatObj.chatType;
  226. let arr = ['deleteFriendMsg','deleteGroupMsg']
  227. _this.$socket[arr[type]](_this.userData.user.operId, id, _this.chatObj.chatId, res => {
  228. if (res.success) {
  229. _this.sendMsg(6, id);
  230. }
  231. })
  232. }
  233. },
  234. fail: function (res) {
  235. console.log(res.errMsg);
  236. }
  237. });
  238. },
  239. //名片
  240. linkToCard(id){
  241. this.$u.route({
  242. url: 'pageC/businessCard/businessCard',
  243. params:{ id: id, source: 1}
  244. })
  245. },
  246. //表情转换
  247. transformFace(text){
  248. return transform(text)
  249. },
  250. }
  251. }
  252. </script>
  253. <style lang="scss">
  254. @import "@/pageC/chat/style.scss";
  255. </style>