right-bubble.vue 6.9 KB

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