screen-chat.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <!-- 聊天 -->
  2. <template>
  3. <view class="chat" @click.stop="">
  4. <div class="message-box">
  5. <div class="flex">
  6. <div class="msgLi bg2" v-if="roomInfo.title">
  7. {{ roomInfo.title }}
  8. </div>
  9. </div>
  10. <div class="flex">
  11. <view class="msgLi" style="color: rgb(220, 60, 35);">
  12. {{system.live_notice}}
  13. </view>
  14. </div>
  15. <view class="flex" v-for="(item,index) in messageList1" :key="index" :id="`view${index}`">
  16. <div class="msgLi">
  17. <div class="flex">
  18. {{item.nick}}:
  19. <div class="msg" v-html="getText(item.normal.text)"></div>
  20. </div>
  21. </div>
  22. </view>
  23. </div>
  24. <div class="detail-input" @click="inputShowFun">
  25. <div class="box flex">
  26. {{ $t('live.enter') }}
  27. <image class="emoji-btn" src="/static/image/live/live-6.png" mode="">
  28. </image>
  29. </div>
  30. </div>
  31. <view class="detail-input fiexd" v-if="inputShow">
  32. <view class="box flex">
  33. <view class="detail-input-i flex">
  34. <input type="text" :focus="focusAuto" v-model="content" @focus="focus" @keydown.enter="setMessage" value="" :placeholder="$t('live.enter')" />
  35. </view>
  36. <image class="emoji-btn" src="/static/image/live/live-6.png" mode="" @click="emoji = !emoji">
  37. </image>
  38. <image class="send" @click="setMessage" src="/static/image/send.svg" mode="">
  39. </image>
  40. </view>
  41. <view class="TUI-Emoji" v-if="emoji">
  42. <view v-for="(item, index) in emojiList" :key="index" class="TUI-emoji-image">
  43. <image :data-name="item.emojiName" class="emoji-image" :src="item.url" @tap="handleEnterEmoji">
  44. </image>
  45. </view>
  46. </view>
  47. </view>
  48. </view>
  49. </template>
  50. <script>
  51. import {
  52. emojiName,
  53. emojiUrl,
  54. emojiMap
  55. } from '@/config/emojiMap';
  56. import bus from '@/utils/bus'
  57. export default {
  58. props: ['roomInfo', 'messageList'],
  59. data() {
  60. return {
  61. eyes: false,
  62. emoji: false,
  63. show: false,
  64. tips: 'get code', //验证码倒计时
  65. type: 1,
  66. scrollTop: 0,
  67. emojiList: [],
  68. mask: {
  69. background: 'rgba(0, 0, 0, 0.1)'
  70. },
  71. figtIndex: null,
  72. content: '',
  73. current: 0,
  74. list: [{
  75. cate_name: '礼物'
  76. }
  77. // , {
  78. // cate_name: '礼包'
  79. // }
  80. ],
  81. countryCode: [], //区号
  82. tournamentList: [],
  83. inputShow: false,
  84. focusAuto: false
  85. }
  86. },
  87. beforeMount() {
  88. for (let i = 0; i < emojiName.length; i++) {
  89. this.emojiList.push({
  90. emojiName: emojiName[i],
  91. url: emojiUrl + emojiMap[emojiName[i]]
  92. });
  93. }
  94. },
  95. computed: {
  96. system() {
  97. return this.$store.state.system
  98. },
  99. isLogin() {
  100. return this.$store.state.isLogin
  101. },
  102. infos() {
  103. return this.$store.state.info
  104. },
  105. eyes1() {
  106. return this.$store.state.eyes1
  107. },
  108. messageList1() {
  109. return this.messageList.filter(item => {
  110. return item.type != 102
  111. })
  112. }
  113. },
  114. methods: {
  115. /* 输入框获取焦点 如果打开了表情框需关闭 */
  116. focus() {
  117. if (this.emoji) {
  118. this.emoji = false
  119. }
  120. },
  121. handleEnterEmoji(event) {
  122. this.content += event.currentTarget.dataset.name;
  123. // console.log(event.currentTarget.dataset.name);
  124. // this.$emit('enterEmoji', {
  125. // detail: {
  126. // message: event.currentTarget.dataset.name
  127. // }
  128. // });
  129. },
  130. // 收到的消息
  131. scrollToButtom() {
  132. let that = this;
  133. const query = uni.createSelectorQuery().in(this);
  134. let nodesRef = query.select('#message-scroll');
  135. nodesRef.boundingClientRect(res => {
  136. if (res) {
  137. that.scrollTop = res.height;
  138. }
  139. })
  140. .exec();
  141. },
  142. // 发送消息
  143. setMessage() {
  144. if (!this.content.trim()) return
  145. if (JSON.stringify(this.infos) == '{}') {
  146. // return this.$toUrl('/pages/login/index')
  147. }
  148. let msg = {
  149. type: 105,
  150. normal: {
  151. isXCBarrage: 0, //是否是气泡 barrageIndex
  152. xcBarrageUrl: '', //气泡的图片路径
  153. xcBarrageType: '', //图片路径
  154. text: this.content,
  155. exp_icon: this.infos.exp_icon,
  156. is_guard: this.infos.is_guard,
  157. is_room: this.infos.id == this.roomInfo.id ? 1 : 0,
  158. guard_icon: this.infos.is_guard == 1 ? this.infos.guard.icon : ''
  159. }
  160. }
  161. let to = JSON.stringify(this.roomInfo.id)
  162. const message = uni.$TUIKit.createCustomMessage({
  163. to,
  164. conversationType: 'GROUP',
  165. payload: {
  166. data: JSON.stringify(msg)
  167. }
  168. });
  169. this.$sendTIMMessage(message);
  170. },
  171. $sendTIMMessage(message) {
  172. let that = this;
  173. const SDKAppID = getApp().globalData.SDKAppID;
  174. uni.$TUIKit.sendMessage(message).then((res) => {
  175. let arr = {
  176. data: [res.data.message]
  177. }
  178. bus.$emit('onMessageReceived', arr)
  179. that.content = '';
  180. uni.$aegis.reportEvent({
  181. name: 'sendMessage',
  182. ext1: 'sendMessage-success',
  183. ext2: 'uniTuikitExternal',
  184. ext3: `${SDKAppID}`,
  185. })
  186. }).catch((error) => {
  187. console.log(error);
  188. uni.$aegis.reportEvent({
  189. name: 'sendMessage',
  190. ext1: `sendMessage-failed#error: ${error}`,
  191. ext2: 'uniTuikitExternal',
  192. ext3: `${SDKAppID}`,
  193. })
  194. })
  195. // this.setData({
  196. // displayFlag: ''
  197. // });
  198. },
  199. getText(str) {
  200. // let str = 'jfkdsj[咒骂]dsjfkljsa[憨笑]哈哈哈,[惊恐]'
  201. let exp = /\[(.*?)\]/g; //匹配[*] 大括号里面任意内容的正则
  202. let arr = str.match(exp); //字符串匹配出来的数组
  203. // console.log(arr);
  204. if (!arr) return str;
  205. this.emojiList.forEach((item1, index) => {
  206. arr.map(item => {
  207. // console.log(item1);
  208. if (item1.emojiName === item) {
  209. str = str.replace(item,
  210. `<img src='${item1.url}' style="width:20px;height:20px">`
  211. );
  212. }
  213. // console.log(item);
  214. // let key = `p${item.substr(1, item.length - 2)}`; //记录大括号里的值 用作id 方便取值
  215. // if (!this.salaryVars.hasOwnProperty(key)) this.$set(this.salaryVars, key, '');
  216. }); //循环遍历
  217. })
  218. // let arrNum = 取出所有正则匹配值并转换为input
  219. // console.log(str);
  220. return str;
  221. },
  222. inputShowFun() {
  223. if (this.$store.state.isLogin != 1) {
  224. this.$toUrl('/pages/login/login')
  225. return
  226. }
  227. this.inputShow = true
  228. this.$nextTick(() => {
  229. this.focusAuto = true
  230. })
  231. },
  232. },
  233. }
  234. </script>
  235. <style lang="scss" scoped>
  236. .TUI-Emoji {
  237. background-color: #F5F5F5;
  238. display: flex;
  239. justify-content: flex-start;
  240. flex-wrap: wrap;
  241. height: 480rpx;
  242. overflow-y: scroll;
  243. border-radius: 10rpx;
  244. }
  245. .TUI-emoji-image {
  246. width: 30px;
  247. height: 30px;
  248. margin: 10px;
  249. }
  250. .emoji-image {
  251. width: 100%;
  252. height: 100%;
  253. }
  254. .btn1 {
  255. color: #fff;
  256. height: 58rpx;
  257. width: 160rpx;
  258. line-height: 58rpx;
  259. text-align: center;
  260. border-radius: 100rpx;
  261. background: #DC3C23;
  262. }
  263. .chat {
  264. flex: 1;
  265. min-width: 10%;
  266. max-width: 80%;
  267. position: relative;
  268. margin-left: 26rpx;
  269. }
  270. .detail-input {
  271. color: #000;
  272. position: relative;
  273. background-color: #F5F5F5;
  274. padding: 0 12px;
  275. border-radius: 20px;
  276. .box {
  277. justify-content: space-between;
  278. flex: 1;
  279. height: 34px;
  280. font-size: 14px;
  281. color: #999999;
  282. }
  283. .detail-input-i {
  284. flex: 1;
  285. font-size: 14px;
  286. input {
  287. flex: 1;
  288. height: 34px;
  289. }
  290. }
  291. .emoji-btn {
  292. width: 27px;
  293. height: 27px;
  294. margin-left: 26rpx;
  295. }
  296. .send {
  297. width: 25px;
  298. height: 25px;
  299. margin-left: 26rpx;
  300. }
  301. }
  302. .fiexd {
  303. position: fixed !important;
  304. bottom: 0;
  305. left: 0;
  306. right: 0;
  307. z-index: 9999;
  308. }
  309. .message-box {
  310. position: fixed;
  311. left: 0;
  312. width: 90%;
  313. max-width: 450px;
  314. bottom: 40px;
  315. max-height: 50vh;
  316. overflow-y: auto;
  317. .msgLi {
  318. max-width: 100%;
  319. color: #fff;
  320. font-size: 12px;
  321. margin-top: 4.5px;
  322. line-height: 18px;
  323. padding: 5px 11px;
  324. border-radius: 14px;
  325. background-color: rgba(0, 0, 0, .5);
  326. &.bg2 {
  327. background-color: rgba(0, 5, 70, 0.6);
  328. }
  329. .msg {
  330. flex: 1;
  331. min-width: 10%;
  332. max-width: 100%;
  333. }
  334. }
  335. }
  336. </style>