info.vue 7.4 KB

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