userchat.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <script>
  2. import chatInput from '../component/chatinput.vue';
  3. import messageShow from '../component/messageshow.vue';
  4. export default {
  5. data() {
  6. return {
  7. style: {
  8. pageHeight: 0,
  9. contentViewHeight: 0,
  10. footViewHeight: 90,
  11. mitemHeight: 0,
  12. },
  13. scrollTop: 0,
  14. messages: [{
  15. user: 'home',
  16. type: '', //input,content
  17. content: '你好',
  18. date:this.getCurrentTime()
  19. }]
  20. }
  21. },
  22. components: {
  23. chatInput,
  24. messageShow
  25. },
  26. created: function () {
  27. this.initWebSocket();
  28. const res = uni.getSystemInfoSync();
  29. this.style.pageHeight = res.windowHeight;
  30. this.style.contentViewHeight = res.windowHeight - uni.getSystemInfoSync().screenWidth / 750 * (100); //像素
  31. },
  32. methods: {
  33. getCurrentTime:function(){
  34. //获取当前时间并打印
  35. var time='';
  36.   let yy = new Date().getFullYear();
  37.   let mm = new Date().getMonth()+1;
  38.   let dd = new Date().getDate();
  39.   let hh = new Date().getHours();
  40.   let mf = new Date().getMinutes()<10 ? '0'+new Date().getMinutes() : new Date().getMinutes();
  41.   let ss = new Date().getSeconds()<10 ? '0'+new Date().getSeconds() : new Date().getSeconds();
  42.   time = yy+'-'+mm+'-'+dd+' '+hh+':'+mf+':'+ss;
  43.    return time;
  44. },
  45. initWebSocket: function () {
  46. // WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
  47. var userId = this.$store.getters.userid;
  48. var sessionId=this.$store.getters.sessionId;
  49. var url = this.$config.apiUrl.replace("https://","wss://").replace("http://","ws://")+"/websocket/"+userId+"/"+sessionId;
  50. console.log('websocket url>>'+url);
  51. this.websock = new WebSocket(url);
  52. this.websock.onopen = this.websocketOnopen;
  53. this.websock.onerror = this.websocketOnerror;
  54. this.websock.onmessage = this.websocketOnmessage;
  55. this.websock.onclose = this.websocketOnclose;
  56. },
  57. websocketOnopen: function () {
  58. console.log("WebSocket连接成功");
  59. //心跳检测重置
  60. //this.heartCheck.reset().start();
  61. },
  62. websocketOnerror: function () {
  63. console.log("WebSocket连接发生错误");
  64. this.reconnect();
  65. },
  66. websocketOnmessage: function (e) {
  67. console.log("-----接收消息-------",e);
  68. this.addMessage('home', e.data, false);
  69. this.scrollToBottom();
  70. //心跳检测重置
  71. //this.heartCheck.reset().start();
  72. },
  73. websocketOnclose: function (e) {
  74. console.log("connection closed (" + e.code + ")");
  75. this.reconnect();
  76. },
  77. websocketSend:function(text) { // 数据发送
  78. try {
  79. this.websock.send(text);
  80. } catch (err) {
  81. console.log("send failed (" + err.code + ")");
  82. }
  83. },
  84. getInputMessage: function (message) { //获取子组件的输入数据
  85. // console.log(message);
  86. this.addMessage('customer', message.content, false);
  87. this.toRobot(message.content);
  88. },
  89. addMessage: function (user, content, hasSub, subcontent) {
  90. var that = this;
  91. that.messages.push({
  92. user: user,
  93. content: content,
  94. date:this.getCurrentTime(),
  95. hasSub: hasSub,
  96. subcontent: subcontent
  97. });
  98. },
  99. scrollToBottom: function () {
  100. var that = this;
  101. var query = uni.createSelectorQuery();
  102. query.selectAll('.m-item').boundingClientRect();
  103. query.select('#scrollview').boundingClientRect();
  104. query.exec(function (res) {
  105. that.style.mitemHeight = 0;
  106. res[0].forEach(function (rect) {
  107. // console.info(rect.height);
  108. that.style.mitemHeight = that.style.mitemHeight + rect.height + 20;
  109. });
  110. if (that.style.mitemHeight > that.style.contentViewHeight) {
  111. that.scrollTop = that.style.mitemHeight - that.style.contentViewHeight;
  112. }
  113. });
  114. },
  115. toRobot: function (info) {
  116. var ToSendUserno=this.$store.getters.chatid;//接收人编号:4567
  117. var message=info+"|"+ToSendUserno+"|"+this.$store.getters.userid//将要发送的信息和内容拼起来,以便于服务端知道消息要发给谁
  118. this.websocketSend(message)
  119. }
  120. }
  121. }
  122. </script>
  123. <template>
  124. <div>
  125. <cu-custom bgColor="bg-gradual-pink" :isBack="true">
  126. <block slot="backText">返回</block>
  127. <block slot="content">{{this.$store.getters.chatname}}</block>
  128. </cu-custom>
  129. <view class="uni-column">
  130. <view class="content" id="content" :style="{height:style.contentViewHeight+'px'}">
  131. <scroll-view id="scrollview" scroll-y="true" :style="{height:style.contentViewHeight+'px'}" :scroll-with-animation="true"
  132. :scroll-top="scrollTop">
  133. <message-show v-for="(message,index) in messages" :key="index" v-bind:message="message" :id="index"></message-show>
  134. <view id="bottom"></view>
  135. </scroll-view>
  136. </view>
  137. <view class="foot">
  138. <chat-input @send-message="getInputMessage" ></chat-input>
  139. </view>
  140. </view>
  141. </div>
  142. </template>
  143. <style>
  144. .uni-column {
  145. display: flex;
  146. flex-direction: column;
  147. background-image: url(../../static/bj001.jpeg );
  148. }
  149. .content {
  150. display: flex;
  151. flex: 1;
  152. }
  153. .foot {
  154. position: fixed;
  155. width: 100%;
  156. left: 0px;
  157. bottom: 0px;
  158. overflow: hidden;
  159. }
  160. </style>