details.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <template>
  2. <view class="details_box" :style="'height:' + phoneHeight + 'px'">
  3. <!-- 对话列表 -->
  4. <scroll-view id="scrollview" class="chat-window" scroll-y="true" :scroll-with-animation="true"
  5. :scroll-top="scrollTop" @scrolltolower="lower">
  6. <view class="details_ul" id="msgBox">
  7. <view class="details_list" v-for="item in msg_list">
  8. <!-- me -->
  9. <view class="list_msgBox1" v-if="item.is_right == true">
  10. <view class="list_msgText">
  11. {{ item.msg_info }}
  12. </view>
  13. <view class="list_triangle"></view>
  14. <view class="list_img">
  15. <image src="../../static/image/7.png" mode=""></image>
  16. </view>
  17. </view>
  18. <!-- you -->
  19. <view class="list_msgBox2" v-if="item.is_right == false">
  20. <view class="list_img">
  21. <image src="../../static/image/6.png" mode=""></image>
  22. </view>
  23. <view class="list_triangle"></view>
  24. <view class="list_msgText">
  25. {{ item.msg_info }}
  26. </view>
  27. </view>
  28. </view>
  29. </view>
  30. </scroll-view>
  31. <!-- input输入框 -->
  32. <u--input type="text" class="details_input" maxlength="200" confirm-type="send" @confirm="search()"
  33. placeholder="请输入内容" border="surround" v-model="value" @change="sendChange"></u--input>
  34. <!-- 警告提示 -->
  35. <u-toast ref="uToast"></u-toast>
  36. </view>
  37. </template>
  38. <script>
  39. import store from '@/store/index.js'; //需要引入store
  40. let App = getApp();
  41. var API = App.globalData.socketTask;
  42. export default {
  43. data() {
  44. return {
  45. phoneHeight: '', // 获取当前的屏幕高度
  46. value: "", // input框值
  47. tltName: '', // 头部标题
  48. val: {}, // 接收用户数据
  49. msg_list: [], // 消息列表
  50. // 警告提示
  51. show: false,
  52. // 聊天页面时时滚动样式
  53. // style: {
  54. // pageHeight: 0,
  55. // contentViewHeight: 0,
  56. // footViewHeight: 90,
  57. // mitemHeight: 0
  58. // },
  59. scrollTop: 0,
  60. }
  61. },
  62. watch: {
  63. msg_list(val) {
  64. // 实现打开聊天框后滚动条定位到最下方
  65. // this.$nextTick(() => {
  66. // // var div = document.getElementById("msgBox");
  67. // // div.scrollTop = div.scrollHeight;
  68. // // console.log(div.scrollHeight)
  69. // // const query = uni.createSelectorQuery().in(this);
  70. // // query.select('.details_ul').boundingClientRect(data => {
  71. // // console.log(data)
  72. // // }).exec();
  73. // });
  74. },
  75. listData(news, old) {
  76. // console.log(news, old)
  77. for (var i = 0; i < news.length; i++) {
  78. if (news[i].real_name == this.tltName) {
  79. this.val = news[i]
  80. this.msg_list = news[i].msg_list.reverse();
  81. }
  82. }
  83. }
  84. },
  85. computed: {
  86. listData() {
  87. return store.state.list
  88. },
  89. },
  90. onLoad(options) {
  91. console.log(store.state)
  92. var that = this
  93. var data = JSON.parse(options.data)
  94. that.tltName = data.real_name
  95. var arrayData = App.globalData.list
  96. for (var i = 0; i < arrayData.length; i++) {
  97. if (arrayData[i].real_name == data.real_name) {
  98. that.val = arrayData[i]
  99. that.msg_list = arrayData[i].msg_list.reverse();
  100. }
  101. }
  102. uni.getSystemInfo({ //异步获取。
  103. success(res) {
  104. that.phoneHeight = res.windowHeight - 13; //窗口高度
  105. }
  106. });
  107. },
  108. methods: {
  109. // 实时监控input框数据
  110. sendChange(data) {
  111. this.value = data
  112. },
  113. // 按回车键发送数据
  114. search() {
  115. var that = this
  116. if (that.value !== "") {
  117. var obj = {
  118. 'action': 'send', // 动作标识,必填
  119. 'recv_user_id': that.val.user_id, // 接收人用户id, 非必填
  120. 'data': {
  121. 'msg_status': false, // 消息未读
  122. 'msg_info': that.value, // 发送消息
  123. }
  124. }
  125. App.globalData.userId = this.value
  126. that.transmit(obj)
  127. } else {
  128. this.$refs.uToast.show({
  129. message: '不能为空!',
  130. type: 'error',
  131. icon: true,
  132. })
  133. }
  134. },
  135. // websocket发送
  136. transmit(obj) {
  137. var that = this
  138. API.send({
  139. data: JSON.stringify(obj),
  140. async success(res) {
  141. console.log("消息发送成功");
  142. that.msg_list = [...that.msg_list, {
  143. msg_info: that.value,
  144. is_right: true
  145. }]
  146. that.value = ''
  147. },
  148. });
  149. },
  150. lower(e) {
  151. console.log(e)
  152. },
  153. },
  154. onReady() {
  155. // 修改头部标题栏
  156. uni.setNavigationBarTitle({
  157. title: this.tltName
  158. });
  159. },
  160. }
  161. </script>
  162. <style lang="scss" scoped>
  163. page {
  164. overflow: hidden;
  165. }
  166. .details_box {
  167. height: 100%;
  168. .details_ul {
  169. height: 93%;
  170. overflow-y: auto;
  171. .details_list {
  172. .list_msgBox1 {
  173. display: flex;
  174. margin-top: 22rpx;
  175. justify-content: flex-end;
  176. .list_msgText {
  177. background: #60fba5;
  178. color: #000;
  179. padding: 30rpx 15rpx 30rpx 25rpx;
  180. line-height: 24px;
  181. border-radius: 8rpx;
  182. width: 325rpx;
  183. }
  184. .list_triangle {
  185. width: 0;
  186. height: 0;
  187. border-top: 6px solid transparent;
  188. border-left: 10px solid #60fba5;
  189. border-bottom: 5px solid transparent;
  190. margin: 35rpx 0 0 0;
  191. }
  192. .list_img {
  193. image {
  194. width: 96rpx;
  195. height: 96rpx;
  196. }
  197. }
  198. }
  199. .list_msgBox2 {
  200. display: flex;
  201. margin-top: 22rpx;
  202. justify-content: flex-start;
  203. .list_img {
  204. image {
  205. width: 96rpx;
  206. height: 96rpx;
  207. }
  208. }
  209. .list_triangle {
  210. width: 0;
  211. height: 0;
  212. border-top: 5px solid transparent;
  213. border-right: 10px solid #eae9eb;
  214. border-bottom: 6px solid transparent;
  215. margin: 35rpx 0 0 0;
  216. }
  217. .list_msgText {
  218. width: 325rpx;
  219. background: #eae9eb;
  220. color: #000;
  221. padding: 30rpx 15rpx 30rpx 25rpx;
  222. line-height: 24px;
  223. border-radius: 8rpx;
  224. }
  225. }
  226. }
  227. }
  228. // 输入框
  229. .details_input {
  230. border: 0;
  231. border-top: 1px solid #eaeaea;
  232. position: fixed;
  233. bottom: 0;
  234. left: 0;
  235. width: 96%;
  236. height: 90rpx;
  237. border-radius: 0;
  238. background: #fff;
  239. }
  240. }
  241. </style>