info.vue 8.1 KB

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