details.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. <view style="color: #fff; text-align: center; line-height: 90rpx; font-size: 40rpx;">我</view>
  16. </view>
  17. </view>
  18. <!-- you -->
  19. <view class="list_msgBox2" v-if="item.is_right == false">
  20. <view class="list_img">
  21. <view style="color: #fff; text-align: center; line-height: 90rpx; font-size: 40rpx;">{{ tltName.slice(0, 1) }}</view>
  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" :adjust-position="true" class="details_input" maxlength="30" 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. var API = App.globalData;
  43. export default {
  44. data() {
  45. return {
  46. phoneHeight: '', // 获取当前的屏幕高度
  47. value: "", // input框值
  48. tltName: '', // 头部标题
  49. val: {}, // 接收用户数据
  50. msg_list: [], // 消息列表
  51. // 警告提示
  52. show: false,
  53. // 聊天页面时时滚动样式
  54. // style: {
  55. // pageHeight: 0,
  56. // contentViewHeight: 0,
  57. // footViewHeight: 90,
  58. // mitemHeight: 0
  59. // },
  60. scrollTop: 0,
  61. }
  62. },
  63. watch: {
  64. msg_list(val) {
  65. // 实现打开聊天框后滚动条定位到最下方
  66. // this.$nextTick(() => {
  67. // var div = document.getElementById("msgBox");
  68. // div.scrollTop = div.scrollHeight;
  69. // console.log(div.scrollHeight)
  70. // const query = uni.createSelectorQuery().in(this);
  71. // query.select('.details_ul').boundingClientRect(data => {
  72. // console.log(data)
  73. // }).exec();
  74. // });
  75. },
  76. videoData(news, old) {
  77. console.log('触发了2222222222222222222222')
  78. console.log(news)
  79. uni.switchTab({
  80. url: '../response/index'
  81. })
  82. // if (news) {
  83. // }
  84. },
  85. listData(news, old) {
  86. console.log('有变化了啊-------------', news)
  87. // console.log(news, old)
  88. this.msg_list = []
  89. for (var i = 0; i < news.length; i++) {
  90. if (news[i].real_name == this.tltName) {
  91. this.val = news[i]
  92. this.msg_list = news[i].msg_list.reverse();
  93. // this.msg_list = news[i].msg_list;
  94. }
  95. }
  96. this.$forceUpdate()
  97. console.log(this.msg_list, '新数组啊啊-------------')
  98. },
  99. // 视频消息
  100. videoData() {
  101. return this.$store.state.video
  102. },
  103. },
  104. computed: {
  105. listData() {
  106. return store.state.list
  107. },
  108. },
  109. onLoad(options) {
  110. var that = this
  111. var data = this.$store.state.list
  112. that.msg_list = []
  113. that.tltName = options.data
  114. for (var j = 0; j < data.length; j++) {
  115. if (options.data == data[j].real_name) {
  116. for (var i = 0; i < data[j].msg_list.length; i++) {
  117. that.val = data[j].msg_list
  118. that.msg_list = data[j].msg_list.reverse();
  119. }
  120. }
  121. }
  122. // that.msg_list = []
  123. // that.tltName = data.real_name
  124. // var arrayData = data
  125. // for (var i = 0; i < arrayData.length; i++) {
  126. // if (arrayData[i].real_name == data.real_name) {
  127. // that.val = arrayData[i]
  128. // that.msg_list = arrayData[i].msg_list.reverse();
  129. // }
  130. // }
  131. uni.getSystemInfo({ //异步获取。
  132. success(res) {
  133. that.phoneHeight = res.windowHeight - 13; //窗口高度
  134. }
  135. });
  136. },
  137. methods: {
  138. // 实时监控input框数据
  139. sendChange(data) {
  140. this.value = data
  141. },
  142. // 按回车键发送数据
  143. search() {
  144. var that = this
  145. console.log(that.val)
  146. if (that.value !== "") {
  147. var obj = {
  148. 'action': 'send', // 动作标识,必填
  149. 'recv_user_id': that.val.user_id, // 接收人用户id, 非必填
  150. 'data': {
  151. 'msg_status': false, // 消息未读
  152. 'msg_info': that.value, // 发送消息
  153. }
  154. }
  155. console.log(that.value.user_id)
  156. // App.globalData.userId = null
  157. App.globalData.userId = that.val.user_id
  158. console.log(obj, '发送的数据', that.val.user_id, App.globalData.socketTask)
  159. that.transmit(obj)
  160. } else {
  161. that.$refs.uToast.show({
  162. message: '不能为空!',
  163. type: 'error',
  164. icon: true,
  165. })
  166. }
  167. },
  168. // websocket发送
  169. transmit(obj) {
  170. var that = this
  171. // App.globalData.userId = that.value.user_id
  172. App.globalData.msgSetTime = setTimeout(() => {
  173. that.$refs.uToast.show({
  174. message: '消息发送失败,请关闭程序重试!',
  175. type: 'error',
  176. icon: true,
  177. })
  178. }, 10000)
  179. API.socketTask.send({
  180. data: JSON.stringify(obj),
  181. async success(res) {
  182. that.msg_list = [...that.msg_list, {
  183. msg_info: that.value,
  184. is_right: true
  185. }]
  186. that.value = ''
  187. },
  188. fali(err) {
  189. console.log('消息发送失败了', err)
  190. },
  191. complete(com) {
  192. console.log('消息发送完成,但不知道成没成功', com)
  193. }
  194. });
  195. },
  196. lower(e) {
  197. console.log(e)
  198. },
  199. },
  200. onReady() {
  201. // 修改头部标题栏
  202. uni.setNavigationBarTitle({
  203. title: this.tltName
  204. });
  205. },
  206. // 监听页面返回
  207. // onBackPress() {
  208. // console.log(App.globalData.list, '这是改变前')
  209. // getApp().globalData.list = App.globalData.list.sort()
  210. // // App.globalData.list = App.globalData.list.sort()
  211. // console.log(App.globalData.list, '这是改变后')
  212. // },
  213. }
  214. </script>
  215. <style lang="scss" scoped>
  216. page {
  217. overflow: hidden;
  218. }
  219. .details_box {
  220. height: 100%;
  221. .details_ul {
  222. height: 93%;
  223. overflow-y: auto;
  224. padding: 0 0 120rpx 0;
  225. .details_list {
  226. .list_msgBox1 {
  227. display: flex;
  228. margin-top: 22rpx;
  229. justify-content: flex-end;
  230. .list_msgText {
  231. background: #60fba5;
  232. color: #000;
  233. padding: 30rpx 15rpx 30rpx 25rpx;
  234. line-height: 24px;
  235. border-radius: 8rpx;
  236. width: 325rpx;
  237. }
  238. .list_triangle {
  239. width: 0;
  240. height: 0;
  241. border-top: 6px solid transparent;
  242. border-left: 10px solid #60fba5;
  243. border-bottom: 5px solid transparent;
  244. margin: 35rpx 0 0 0;
  245. }
  246. .list_img {
  247. // image {
  248. // width: 96rpx;
  249. // height: 96rpx;
  250. // }
  251. view {
  252. width: 96rpx;
  253. height: 96rpx;
  254. border-radius: 20rpx;
  255. background-image: linear-gradient(#a7fbc6, #60fba5);
  256. margin: 0 10rpx 0 0;
  257. }
  258. }
  259. }
  260. .list_msgBox2 {
  261. display: flex;
  262. margin-top: 22rpx;
  263. justify-content: flex-start;
  264. .list_img {
  265. // image {
  266. // width: 96rpx;
  267. // height: 96rpx;
  268. // }
  269. view {
  270. width: 96rpx;
  271. height: 96rpx;
  272. border-radius: 20rpx;
  273. background-image: linear-gradient(#addbff, #5cacff);
  274. margin: 0 0 0 10rpx;
  275. }
  276. }
  277. .list_triangle {
  278. width: 0;
  279. height: 0;
  280. border-top: 5px solid transparent;
  281. border-right: 10px solid #eae9eb;
  282. border-bottom: 6px solid transparent;
  283. margin: 35rpx 0 0 0;
  284. }
  285. .list_msgText {
  286. width: 325rpx;
  287. background: #eae9eb;
  288. color: #000;
  289. padding: 30rpx 15rpx 30rpx 25rpx;
  290. line-height: 24px;
  291. border-radius: 8rpx;
  292. }
  293. }
  294. }
  295. }
  296. // 输入框
  297. .details_input {
  298. border: 0;
  299. border-top: 1px solid #eaeaea;
  300. position: fixed;
  301. bottom: 0;
  302. left: 0;
  303. width: 96%;
  304. height: 90rpx;
  305. border-radius: 0;
  306. background: #fff;
  307. }
  308. }
  309. </style>