ChatInfo.vue 15 KB

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