info.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <template>
  2. <mobile-frame>
  3. <view class="main">
  4. <view class="one">
  5. <!-- 聊天内容 -->
  6. <scroll-view class="chat" scroll-y="true" scroll-with-animation="true" :scroll-into-view="scrollToView">
  7.     <view class="chat-main" :style="{paddingBottom:inputh+'px'}">
  8. <view class="chat-ls" v-for="(item,index) in unshiftmsg" :key="index" :id="'msg'+ index">
  9. <view class="chat-time" v-if="item.time != ''">
  10. {{item.time}}
  11. </view>
  12. <view class="msg-m msg-left" v-if="item.speaker !=  friendName">
  13. <image class="user-img" :src="item.file&&item.file.length>0?item.file[0].url:''">
  14. </image>
  15. <!-- 文字 -->
  16. <view class="message" v-if="item.TextType == 0">                           
  17. <view class="msg-text">{{item.content}}</view>
  18. </view>
  19. <!-- 图像 -->
  20. <view class="message" v-else @tap="previewImg(item.content)">
  21. <image :src="item.content" class="msg-img" mode="widthFix"></image>
  22. </view>
  23. </view>
  24. <view class="msg-m msg-right" v-if="item.speaker == friendName">
  25. <image class="user-img" :src="item.file&&item.file.length>0?item.file[0].url:''">
  26. </image>
  27. <!-- 文字 -->
  28. <view class="message" v-if="item.TextType == 0">
  29. <view class="msg-text">{{item.content}}</view>
  30. </view>
  31. <!-- 图像 -->
  32. <view class="message" v-else @tap="previewImg(item.content)">
  33. <image :src="item.content" class="msg-img" mode="widthFix"></image>
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. </scroll-view>
  39. <submit_1 @inputs="inputs" @heights="heights"></submit_1>
  40. </view>
  41. </view>
  42. </mobile-frame>
  43. </template>
  44. <script>
  45. import moment from 'moment';
  46. import submit_1 from './components/submit_1.vue';
  47. export default {
  48. components: {
  49. submit_1,
  50. },
  51. data() {
  52. return {
  53. friendName: "zs",
  54. info: {},
  55. id: '',
  56. name: '',
  57. msg: [],
  58. // 反转数据接收
  59. unshiftmsg: [{
  60. "speaker": "zs",
  61. "content": "这是第一条未读消息",
  62. "time": "2023-01-12 12:21:03",
  63. "TextType": 0
  64. },
  65. {
  66. "speaker": "yy",
  67. "content": "这是第二条未读消息",
  68. "time": "2023-01-12 12:22:58",
  69. "TextType": 0
  70. },
  71. {
  72. "speaker": "zs",
  73. "content": "这是第三条未读消息",
  74. "time": "2023-01-12 12:22:03",
  75. "TextType": 0
  76. },
  77. {
  78. "speaker": "yy",
  79. "content": "这是第四条未读消息",
  80. "time": "2023-01-12 12:21:58",
  81. "TextType": 0
  82. },
  83. {
  84. "speaker": "zs",
  85. "content": "这是第五条未读消息",
  86. "time": "2023-01-12 12:22:03",
  87. "TextType": 0
  88. },
  89. {
  90. "speaker": "yy",
  91. "content": "这是第六条未读消息",
  92. "time": "2023-01-12 12:21:58",
  93. "TextType": 0
  94. },
  95. {
  96. "speaker": "zs",
  97. "content": "这是第七条未读消息",
  98. "time": "2023-01-12 12:22:03",
  99. "TextType": 0
  100. },
  101. {
  102. "speaker": "yy",
  103. "content": "这是第八条未读消息",
  104. "time": "2023-01-12 12:21:58",
  105. "TextType": 0
  106. },
  107. {
  108. "speaker": "zs",
  109. "content": "这是第九条未读消息",
  110. "time": "2023-01-12 12:22:03",
  111. "TextType": 0
  112. },
  113. {
  114. "speaker": "zs",
  115. "content": "这是第十条未读消息",
  116. "time": "2023-01-12 12:21:58",
  117. "TextType": 0
  118. },
  119. ],
  120. imgMsg: [],
  121. inputh: '60',
  122. scrollToView: '',
  123. };
  124. },
  125. onLoad: async function(e) {
  126. const that = this;
  127. that.$set(that, `id`, e.id);
  128. that.$set(that, `name`, e.name);
  129. // await that.searchOther();
  130. if (e.name) {
  131. uni.setNavigationBarTitle({
  132. title: e.name
  133. });
  134. }
  135. if (e.id) await that.search()
  136. },
  137. methods: {
  138. async search() {
  139. const that = this;
  140. let res;
  141. res = await that.$api(`/chatRecord`, `GET`, {
  142. room: that.id
  143. }, 'chat');
  144. if (res.errcode == '0') {
  145. console.log(res);
  146. } else {
  147. uni.showToast({
  148. title: res.errmsg,
  149. icon: 'none'
  150. })
  151. }
  152. // 跳转到最后一条数据 与前面的:id进行对照
  153. this.$nextTick(function() {
  154. this.scrollToView = 'msg' + (this.unshiftmsg.length - 1)
  155. })
  156. },
  157. // 进行图片的预览
  158. previewImg(e) {
  159. let index = 0;
  160. for (let i = 0; i < this.imgMsg.length; i++) {
  161. if (this.imgMsg[i] == e) {
  162. index = i;
  163. }
  164. }
  165. console.log("index", index)
  166. // 预览图片
  167. uni.previewImage({
  168. current: index,
  169. urls: this.imgMsg,
  170. longPressActions: {
  171. itemList: ['发送给朋友', '保存图片', '收藏'],
  172. success: function(data) {
  173. console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  174. },
  175. fail: function(err) {
  176. console.log(err.errMsg);
  177. }
  178. }
  179. });
  180. },
  181. //接受输入内容
  182. inputs(e) {
  183. //时间间隔处理
  184. let data = {
  185. "room": "1001",
  186. "speaker": "zs",
  187. "content": e.message,
  188. "time": moment().format('YYYY-MM-DD HH:mm:ss'),
  189. "TextType": e.type
  190. };
  191. // 发送给服务器消息
  192. // onSendWS(JSON.stringify(data));
  193. this.unshiftmsg.push(data);
  194. // 跳转到最后一条数据 与前面的:id进行对照
  195. this.$nextTick(function() {
  196. this.scrollToView = 'msg' + (this.unshiftmsg.length - 1)
  197. })
  198. if (e.type == 1) {
  199. this.imgMsg.push(e.message);
  200. }
  201. console.log(e)
  202. },
  203. //输入框高度
  204. heights(e) {
  205. this.inputh = e;
  206. this.goBottom();
  207. },
  208. // 滚动到底部
  209. goBottom() {
  210. this.scrollToView = '';
  211. this.$nextTick(function() {
  212. this.scrollToView = 'msg' + (this.unshiftmsg.length - 1)
  213. })
  214. }
  215. }
  216. }
  217. </script>
  218. <style lang="scss">
  219. .main {
  220. display: flex;
  221. flex-direction: column;
  222. width: 100vw;
  223. height: 100vh;
  224. .one {
  225. .chat {
  226. height: 100%;
  227. .chat-main {
  228. padding-left: 32rpx;
  229. padding-right: 32rpx;
  230. padding-top: 20rpx;
  231. display: flex;
  232. flex-direction: column;
  233. }
  234. .chat-ls {
  235. .chat-time {
  236. font-size: 24rpx;
  237. color: rgba(39, 40, 50, 0.3);
  238. line-height: 34rpx;
  239. padding: 10rpx 0rpx;
  240. text-align: center;
  241. }
  242. .msg-m {
  243. display: flex;
  244. padding: 20rpx 0;
  245. .user-img {
  246. flex: none;
  247. width: 80rpx;
  248. height: 80rpx;
  249. border-radius: 40rpx;
  250. border: 1px solid #c0c0c0;
  251. }
  252. .message {
  253. flex: none;
  254. max-width: 480rpx;
  255. }
  256. .msg-text {
  257. font-size: 32rpx;
  258. color: rgba(39, 40, 50, 1);
  259. line-height: 44rpx;
  260. padding: 18rpx 24rpx;
  261. }
  262. .msg-img {
  263. max-width: 400rpx;
  264. border-radius: 20rpx;
  265. }
  266. }
  267. .msg-left {
  268. flex-direction: row;
  269. .msg-text {
  270. margin-left: 16rpx;
  271. background-color: #f1f1f1;
  272. border-radius: 0rpx 20rpx 20rpx 20rpx;
  273. }
  274. .ms-img {
  275. margin-left: 16rpx;
  276. }
  277. }
  278. .msg-right {
  279. flex-direction: row-reverse;
  280. .msg-text {
  281. margin-right: 16rpx;
  282. background-color: rgba(255, 228, 49, 0.8);
  283. border-radius: 20rpx 0rpx 20rpx 20rpx;
  284. }
  285. .ms-img {
  286. margin-right: 16rpx;
  287. }
  288. }
  289. }
  290. }
  291. }
  292. }
  293. .scroll-view {
  294. position: absolute;
  295. top: 0;
  296. left: 0;
  297. right: 0;
  298. bottom: 0;
  299. .list-scroll-view {
  300. display: flex;
  301. flex-direction: column;
  302. }
  303. }
  304. </style>