ChatInfo.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <template><page-meta :root-font-size="fontSize+'px'"></page-meta>
  2. <member-base :show="false"><view class="scroll-view-wrapper scroll-view div container" :style="'height:'+contentHeight+'px'">
  3. <view class="div common-header-wrap">
  4. <view :style="'height:'+navHeight+'px'"></view>
  5. <view class="common-header-holder"></view>
  6. <view class="common-header-fixed">
  7. <title-header />
  8. <uni-nav-bar :title="t_name" class="common-header" left-icon="back" @clickLeft="goBack()">
  9. <view class="div common-btn btn" slot="right" @click="showPopup('historyVisible')">聊天记录</view>
  10. </uni-nav-bar>
  11. </view>
  12. </view>
  13. <scroll-view :scroll-y="true" class="scroll-view div" :scroll-into-view="'msg_'+(msg_list.length-1)">
  14. <view class="dstouch-chat-con">
  15. <view class="div" v-for="(item,index) in msg_list" :key="index" :id="'msg_'+index">
  16. <chat-message :item="getMessage(item,index,0)" :t_id="t_id"></chat-message>
  17. </view>
  18. </view>
  19. </scroll-view>
  20. <view class="div dstouch-chat-bottom">
  21. <view class="div chat-input-layout"> <view class="span open-smile"><text class="a iconfont" @click="open_smile">&#xe6cd;</text></view>
  22. <view class="div input-box">
  23. <input type="text" v-model="msg" @blur.prevent="inputLoseFocus">
  24. <view class="div common-btn ds-button-small submit" @click="submit()">发送</view>
  25. </view>
  26. </view>
  27. <view class="div chat-smile-layout" v-if="smile_show">
  28. <view class="ul">
  29. <view class="li" v-for="(smile,index) in smilies_array" :key="index" @click="addSmile(index)"><image mode="aspectFit" class="img" :title="smile.text" :alt="smile.text" :src="getSmile(smile.image)"></view>
  30. </view>
  31. </view>
  32. </view>
  33. <!--聊天记录-->
  34. <uni-popup background-color="#fff" ref="historyVisible" type="right" >
  35. <view :style="'width:'+screenWidth+'px'" class="scroll-view-wrapper common-popup-wrapper">
  36. <view class="div common-header-wrap">
  37. <view :style="'height:'+navHeight+'px'"></view>
  38. <view class="common-header-holder"></view>
  39. <view class="common-header-fixed">
  40. <title-header />
  41. <uni-nav-bar title="聊天记录" class="common-header" left-icon="back" @clickLeft="hidePopup('historyVisible')">
  42. </uni-nav-bar>
  43. </view>
  44. </view>
  45. <view class="scroll-view div common-popup-content">
  46. <scroll-view style="position: absolute;top:0;right:0;left:0;bottom:0" :scroll-top="scrollTop" @scrolltolower="loadMore" scroll-y="true">
  47. <view class="div" v-if="historyList && historyList.length">
  48. <chat-message v-for="(item,index) in historyList" :key="item.instant_message_id" :item="getMessage(item,index,1)" :t_id="t_id"></chat-message>
  49. </view>
  50. <empty-record v-else-if="historyList && !historyList.length"></empty-record>
  51. </scroll-view>
  52. </view>
  53. </view>
  54. </uni-popup>
  55. </view></member-base>
  56. </template>
  57. <script>
  58. import {getFontSize} from '@/util/common'
  59. import TitleHeader from '../../TitleHeader'
  60. import MemberBase from '../MemberBase'
  61. import ChatMessage from './ChatMessage'
  62. import EmptyRecord from '../../EmptyRecord'
  63. import { mapState, mapActions } from 'vuex'
  64. import { addInstantMessage, getChatHistory, joinChat, setMessage } from '../../../api/memberInstantMessage'
  65. import { env } from '../../../static/config'
  66. export default {
  67. data(){
  68. return {
  69. canReconnect:true,
  70. navHeight: 0,
  71. scrollTop: 0,
  72. screenWidth:0,
  73. contentHeight:0,
  74. params: { 'page': 0, 'per_page': 20 },
  75. loading: false, // 是否加载更多
  76. isMore: true, // 是否有更多
  77. historyList: false,
  78. t_id: 0,
  79. t_name: '',
  80. goods_id: 0,
  81. smile_show: false,
  82. socket_connect: 0,
  83. msg_list: [],
  84. msg: '',
  85. lockReconnect: false,
  86. timeOut: false,
  87. smilies_array: {
  88. ':smile:': { image: 'smile.gif', text: '微笑' },
  89. ':sad:': { image: 'sad.gif', text: '难过' },
  90. ':biggrin:': { image: 'biggrin.gif', text: '呲牙' },
  91. ':cry:': { image: 'cry.gif', text: '大哭' },
  92. ':huffy:': { image: 'huffy.gif', text: '发怒' },
  93. ':shocked:': { image: 'shocked.gif', text: '惊讶' },
  94. ':tongue:': { image: 'tongue.gif', text: '调皮' },
  95. ':shy:': { image: 'shy.gif', text: '害羞' },
  96. ':titter:': { image: 'titter.gif', text: '偷笑' },
  97. ':sweat:': { image: 'sweat.gif', text: '流汗' },
  98. ':mad:': { image: 'mad.gif', text: '抓狂' },
  99. ':lol:': { image: 'lol.gif', text: '阴险' },
  100. ':loveliness:': { image: 'loveliness.gif', text: '可爱' },
  101. ':funk:': { image: 'funk.gif', text: '惊恐' },
  102. ':curse:': { image: 'curse.gif', text: '咒骂' },
  103. ':dizzy:': { image: 'dizzy.gif', text: '晕' },
  104. ':shutup:': { image: 'shutup.gif', text: '闭嘴' },
  105. ':sleepy:': { image: 'sleepy.gif', text: '睡' },
  106. ':hug:': { image: 'hug.gif', text: '拥抱' },
  107. ':victory:': { image: 'victory.gif', text: '胜利' },
  108. ':sun:': { image: 'sun.gif', text: '太阳' },
  109. ':moon:': { image: 'moon.gif', text: '月亮' },
  110. ':kiss:': { image: 'kiss.gif', text: '示爱' },
  111. ':handshake:': { image: 'handshake.gif', text: '握手' }
  112. }
  113. }
  114. },
  115. beforeDestroy: function () {
  116. this.canReconnect=false
  117. heartCheck.stop()
  118. this.socket_connect = 0
  119. },
  120. components:{
  121. TitleHeader,
  122. MemberBase,
  123. ChatMessage: ChatMessage,
  124. EmptyRecord: EmptyRecord
  125. },
  126. computed:{
  127. fontSize(){
  128. return getFontSize()
  129. },
  130. ...mapState({
  131. config: state => state.config.config,
  132. user: state => state.member.info
  133. })
  134. },
  135. mounted(){
  136. // #ifdef MP-WEIXIN
  137. this.navHeight = uni.getMenuButtonBoundingClientRect().top
  138. // #endif
  139. this.screenWidth=uni.getSystemInfoSync().screenWidth
  140. this.contentHeight=uni.getSystemInfoSync().screenHeight-uni.getSystemInfoSync().statusBarHeight
  141. },
  142. onLoad: function (option) {
  143. this.t_id=option.t_id
  144. this.t_name=option.t_name
  145. this.goods_id=option.goods_id
  146. if (!this.t_id) {
  147. uni.showToast({icon:'none',title: '参数错误'})
  148. uni.navigateBack({delta:1})
  149. }
  150. if (this.config.instant_message_open != '1' || !this.config.instant_message_gateway_url) {
  151. uni.showToast({icon:'none',title: '未开启即时聊天'})
  152. uni.navigateBack({delta:1})
  153. }
  154. this.fetchConfig({}).then(
  155. response => {
  156. this.createWebSocket()
  157. if (this.goods_id) {
  158. this.submit(1)
  159. }
  160. },
  161. error => {
  162. uni.showToast({icon:'none',title: error.message})
  163. }
  164. )
  165. this.loadMore()
  166. },
  167. watch: {
  168. },
  169. methods:{
  170. showPopup(id){
  171. this.$refs[id].open()
  172. },
  173. hidePopup(id){
  174. this.$refs[id].close()
  175. },
  176. goBack(){uni.navigateBack({delta:1})},
  177. ...mapActions({
  178. fetchConfig: 'fetchConfig'
  179. }),
  180. createWebSocket () {
  181. uni.connectSocket({
  182. url: this.config.instant_message_gateway_url,
  183. }).then(res=>{
  184. this.init()
  185. }).catch(error=>{
  186. this.reconnect()
  187. })
  188. },
  189. init () {
  190. uni.onSocketOpen(res=>{
  191. this.wsOpen()
  192. })
  193. uni.onSocketMessage(res=>{
  194. this.wsMessage(res)
  195. })
  196. uni.onSocketClose(res=>{
  197. this.wsClose(res)
  198. })
  199. uni.onSocketError(res=>{
  200. this.wsError(res)
  201. })
  202. },
  203. reconnect () {
  204. if (this.lockReconnect) {
  205. return
  206. };
  207. this.lockReconnect = true
  208. // 没连接上会一直重连,设置延迟避免请求过多
  209. this.timeOut && clearTimeout(this.timeOut)
  210. var _this = this
  211. this.timeOut = setTimeout(function () {
  212. _this.createWebSocket()
  213. _this.lockReconnect = false
  214. }, 4000)
  215. },
  216. wsOpen () {
  217. this.socket_connect = 1
  218. // 心跳检测重置
  219. heartCheck.start()
  220. },
  221. wsMessage (res) {
  222. var message = JSON.parse(res.data)
  223. if (!message) {
  224. uni.showToast({icon:'none',title: '消息转换失败:' + res.data})
  225. return
  226. }
  227. var type = message.type || ''
  228. switch (type) {
  229. // Events.php中返回的init类型的消息,将client_id发给后台进行uid绑定
  230. case 'init':
  231. this.clientId = message.client_id
  232. joinChat(this.clientId).then(res => {
  233. }).catch(error => {
  234. uni.showToast({icon:'none',title: error.message})
  235. })
  236. break
  237. case 'leave':
  238. break
  239. case 'get_msg':// 未读消息
  240. this.filterMsg(message.msg_list)
  241. break
  242. default:
  243. this.filterMsg([message])
  244. }
  245. heartCheck.start()
  246. },
  247. wsClose (res) {
  248. this.socket_connect = 0
  249. if (res.reason) {
  250. console.log('聊天系统连接断开:' + res.reason)
  251. }
  252. if(this.canReconnect){
  253. this.reconnect()
  254. }
  255. },
  256. wsError (res) {
  257. uni.showToast({icon:'none',title: res.errMsg})
  258. this.reconnect()
  259. },
  260. // 解决调起手机软键盘页面被顶到底部再关闭软键盘页面底部留白的问题
  261. inputLoseFocus () {
  262. setTimeout(() => {
  263. this.scrollTop=0
  264. }, 100)
  265. },
  266. loadMore () {
  267. this.loading = true
  268. this.params.page = ++this.params.page
  269. if (this.isMore) {
  270. this.loading = false
  271. this.getHistoryList(true)
  272. }
  273. },
  274. getHistoryList () {
  275. uni.showLoading({ title: '加载中' })
  276. getChatHistory(this.params, this.t_id).then(res => {
  277. uni.hideLoading()
  278. if (res.result.hasmore) {
  279. this.isMore = true
  280. } else {
  281. this.isMore = false
  282. }
  283. let temp = res.result.list
  284. if (temp) {
  285. if (!this.historyList) {
  286. this.historyList = temp
  287. } else {
  288. this.historyList = this.historyList.concat(temp)
  289. }
  290. }
  291. }).catch(function (error) {
  292. uni.hideLoading()
  293. uni.showToast({icon:'none',title: error.message})
  294. })
  295. },
  296. getMessage (item, index, type) {
  297. this.getTime(item, index, type)
  298. if (type) {
  299. return this.historyList[index]
  300. } else {
  301. return this.msg_list[index]
  302. }
  303. },
  304. getTime (item, index, type) {
  305. let time = item.instant_message_add_time
  306. if (type) {
  307. this.historyList[index]['if_show_time'] = false
  308. if (index) {
  309. if (Math.abs(this.historyList[index - 1]['show_time'] - time) > 10) {
  310. this.historyList[index]['show_time'] = time
  311. this.historyList[index]['if_show_time'] = true
  312. } else {
  313. this.historyList[index]['show_time'] = this.historyList[index - 1]['show_time']
  314. }
  315. } else {
  316. this.historyList[index]['show_time'] = time
  317. this.historyList[index]['if_show_time'] = true
  318. }
  319. } else {
  320. this.msg_list[index]['if_show_time'] = false
  321. if (index) {
  322. if (Math.abs(this.msg_list[index - 1]['show_time'] - time) > 10) {
  323. this.msg_list[index]['show_time'] = time
  324. this.msg_list[index]['if_show_time'] = true
  325. } else {
  326. this.msg_list[index]['show_time'] = this.msg_list[index - 1]['show_time']
  327. }
  328. } else {
  329. this.msg_list[index]['show_time'] = time
  330. this.msg_list[index]['if_show_time'] = true
  331. }
  332. }
  333. },
  334. open_smile () {
  335. this.smile_show = !this.smile_show
  336. },
  337. addSmile (code) {
  338. this.msg += code
  339. this.smile_show = false
  340. },
  341. submit (type = 0) {
  342. let msg
  343. if (type == 0) {
  344. if (this.msg == '') {
  345. uni.showToast({icon:'none',title: '请填写内容'})
  346. return
  347. }
  348. msg = this.msg
  349. } else {
  350. msg = this.goods_id
  351. }
  352. addInstantMessage({ to_id: this.t_id, message: msg, message_type: type }).then(res => {
  353. this.msg = ''
  354. let msg = res.result.instant_message_data
  355. this.msg_list.push(msg)
  356. }).catch(function (error) {
  357. uni.showToast({icon:'none',title: error.message})
  358. })
  359. },
  360. getSmile (smile) {
  361. return env.SITE_URL + '/static/plugins/js/smilies/images/' + smile
  362. },
  363. filterMsg (msg_list) {
  364. let msg_id = false
  365. for (var k in msg_list) {
  366. let msg = msg_list[k]
  367. if (msg.instant_message_to_id != this.user.member_id || msg.instant_message_from_id != this.t_id) {
  368. continue
  369. }
  370. msg_id = msg.instant_message_id
  371. this.msg_list.push(msg)
  372. }
  373. if (msg_id) {
  374. if (this.socket_connect) {
  375. setMessage({ max_id: msg_id, f_id: this.t_id })
  376. }
  377. }
  378. }
  379. }
  380. }
  381. // 心跳检测
  382. var heartCheck = {
  383. timeout: 3000,
  384. timeoutObj: null,
  385. start: function () {
  386. var self = this
  387. this.timeoutObj && clearInterval(this.timeoutObj)
  388. this.timeoutObj = setInterval(function () {
  389. // 这里发送一个心跳,后端收到后,返回一个心跳消息,
  390. uni.sendSocketMessage({
  391. data: '123456789'
  392. })
  393. }, this.timeout)
  394. },
  395. stop:function(){
  396. this.timeoutObj && clearInterval(this.timeoutObj)
  397. }
  398. }
  399. </script>
  400. <style scoped lang="scss">
  401. .scroll-view-wrapper{display: flex;flex-direction: column;}
  402. .scroll-view{flex:1;padding-bottom: 2rem;}
  403. .common-header{
  404. .btn{background: #000;color: #fff;box-shadow: 0px 2px 4px #d2d2d2;}
  405. }
  406. .dstouch-chat-con {height: 100%;display: flex;flex-direction: column;justify-content: flex-end;}
  407. .dstouch-chat-bottom {height: 2rem;position: fixed;bottom: 0;width: 100%;}
  408. .chat-input-layout { position: relative; z-index: 1; display: block; height: 2rem; background-color: rgba(255,255,255,0.90)}
  409. .chat-input-layout .open-smile { display: block; width: 1.5rem; height: 1.5rem; padding: 0.25rem;}
  410. .chat-input-layout .open-smile .a { display: block; width: 1.5rem; height: 1.5rem; font-size:1.5rem;color:#6eb6eb}
  411. .chat-input-layout .input-box { position: absolute; z-index: 1; top: 0.24rem; right: 0.4rem; bottom: 0.24rem; left: 2rem; background: #F5F5F5; border: solid 0.05rem #EEE; border-radius: 0.2rem;}
  412. .chat-input-layout .input-box input[type="text"] { background-color: transparent; border: none; width: 100%; height: 100%; padding:.1rem 3rem .1rem .1rem; font-size:$fontSize;box-sizing:border-box;display: block;}
  413. .chat-input-layout .input-box .submit { position: absolute; z-index: 1; top: 0; right:0; display: block; height: 1.4rem; }
  414. .chat-smile-layout { display: block; width: 100%; height: 4.2rem; background-color: #FAFAFA; border-top: solid 0.05rem #DDD;}
  415. .chat-smile-layout .ul { font-size: 0;}
  416. .chat-smile-layout .ul .li { display: inline-block; width: 12.5%; height: 1rem; padding: 0.3rem 0 0 0; text-align: center; vertical-align: middle;}
  417. .chat-smile-layout .ul .li .img { display: inline-block; height: 100%; vertical-align: middle;}
  418. .common-popup-content{background:#f7f7f7;overflow-x:hidden}
  419. </style>