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" 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. that.$nextTick(function() {
  154. that.scrollToView = 'msg' + (that.unshiftmsg.length - 1)
  155. })
  156. },
  157. // 进行图片的预览
  158. previewImg(e) {
  159. const that = this;
  160. let index = 0;
  161. for (let i = 0; i < that.imgMsg.length; i++) {
  162. if (that.imgMsg[i] == e) {
  163. index = i;
  164. }
  165. }
  166. console.log("index", index)
  167. // 预览图片
  168. uni.previewImage({
  169. current: index,
  170. urls: that.imgMsg,
  171. longPressActions: {
  172. itemList: ['发送给朋友', '保存图片', '收藏'],
  173. success: function(data) {
  174. console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  175. },
  176. fail: function(err) {
  177. console.log(err.errMsg);
  178. }
  179. }
  180. });
  181. },
  182. //接受输入内容
  183. inputs(e) {
  184. const that = this;
  185. //时间间隔处理
  186. let data = {
  187. "room": "1001",
  188. "speaker": "zs",
  189. "content": e.message,
  190. "time": moment().format('YYYY-MM-DD HH:mm:ss'),
  191. "TextType": e.type
  192. };
  193. // 发送给服务器消息
  194. // onSendWS(JSON.stringify(data));
  195. thatthat.unshiftmsg.push(data);
  196. // 跳转到最后一条数据 与前面的:id进行对照
  197. that.$nextTick(function() {
  198. that.scrollToView = 'msg' + (that.unshiftmsg.length - 1)
  199. })
  200. if (e.type == 1) {
  201. that.imgMsg.push(e.message);
  202. }
  203. console.log(e)
  204. },
  205. //输入框高度
  206. heights(e) {
  207. const that = this;
  208. that.inputh = e;
  209. that.goBottom();
  210. },
  211. // 滚动到底部
  212. goBottom() {
  213. const that = this;
  214. that.scrollToView = '';
  215. that.$nextTick(function() {
  216. that.scrollToView = 'msg' + (that.unshiftmsg.length - 1)
  217. })
  218. }
  219. }
  220. }
  221. </script>
  222. <style lang="scss">
  223. .main {
  224. display: flex;
  225. flex-direction: column;
  226. width: 100vw;
  227. height: 100vh;
  228. .one {
  229. .chat {
  230. height: 100%;
  231. .chat-main {
  232. padding-left: 32rpx;
  233. padding-right: 32rpx;
  234. padding-top: 20rpx;
  235. display: flex;
  236. flex-direction: column;
  237. }
  238. .chat-ls {
  239. .chat-time {
  240. font-size: 24rpx;
  241. color: rgba(39, 40, 50, 0.3);
  242. line-height: 34rpx;
  243. padding: 10rpx 0rpx;
  244. text-align: center;
  245. }
  246. .msg-m {
  247. display: flex;
  248. padding: 20rpx 0;
  249. .user-img {
  250. flex: none;
  251. width: 80rpx;
  252. height: 80rpx;
  253. border-radius: 40rpx;
  254. border: 1px solid #c0c0c0;
  255. }
  256. .message {
  257. flex: none;
  258. max-width: 480rpx;
  259. }
  260. .msg-text {
  261. font-size: 32rpx;
  262. color: rgba(39, 40, 50, 1);
  263. line-height: 44rpx;
  264. padding: 18rpx 24rpx;
  265. }
  266. .msg-img {
  267. max-width: 400rpx;
  268. border-radius: 20rpx;
  269. }
  270. }
  271. .msg-left {
  272. flex-direction: row;
  273. .msg-text {
  274. margin-left: 16rpx;
  275. background-color: #f1f1f1;
  276. border-radius: 0rpx 20rpx 20rpx 20rpx;
  277. }
  278. .ms-img {
  279. margin-left: 16rpx;
  280. }
  281. }
  282. .msg-right {
  283. flex-direction: row-reverse;
  284. .msg-text {
  285. margin-right: 16rpx;
  286. background-color: rgba(255, 228, 49, 0.8);
  287. border-radius: 20rpx 0rpx 20rpx 20rpx;
  288. }
  289. .ms-img {
  290. margin-right: 16rpx;
  291. }
  292. }
  293. }
  294. }
  295. }
  296. }
  297. .scroll-view {
  298. position: absolute;
  299. top: 0;
  300. left: 0;
  301. right: 0;
  302. bottom: 0;
  303. .list-scroll-view {
  304. display: flex;
  305. flex-direction: column;
  306. }
  307. }
  308. </style>