index.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <template>
  2. <view class="main">
  3. <view class="one">
  4. <!-- 聊天内容 -->
  5. <scroll-view class="scroll-view" scroll-y="true" scroll-with-animation="true"
  6. :scroll-into-view="scrollToView" refresher-enabled="true" :refresher-triggered="triggered"
  7. @refresherrefresh="getFresh">
  8. <view class="list-scroll-view">
  9. <view class="chat-ls" v-for="(item,index) in msgList" :key="index" :id="'msg'+ item._id">
  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 !=  user._id">
  14. <image class="user-img"
  15. :src="config.logo_url&&config.logo_url.length>0?config.logo_url[0].url:''">
  16. </image>
  17. <!-- 文字 -->
  18. <view class="message" v-if="item.msg_type =='0'">                           
  19. <view class="msg-text">{{item.content}}</view>
  20. </view>
  21. <!-- 图像 -->
  22. <view class="message img" v-else-if="item.msg_type =='1'" @tap="previewImg(item.content)">
  23. <image :src="item.content" class="msg-img" mode="widthFix"></image>
  24. </view>
  25. </view>
  26. <view class="msg-m msg-right" v-else-if="item.speaker == user._id">
  27. <image class="user-img" :src="user.logo&&user.logo.length>0?user.logo[0].url:''">
  28. </image>
  29. <!-- 文字 -->
  30. <view class="message" v-if="item.msg_type =='0'">
  31. <view class="msg-text">{{item.content}}</view>
  32. </view>
  33. <!-- 图像 -->
  34. <view class="message img" v-else-if="item.msg_type =='1'" @tap="previewImg(item.content)">
  35. <image :src="item.content" class="msg-img" mode="widthFix"></image>
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. </scroll-view>
  41. </view>
  42. <view class="two">
  43. <submit_1 @choseImg="choseImg" @inputs="inputs" @heights="heights"></submit_1>
  44. </view>
  45. </view>
  46. </template>
  47. <script>
  48. import moment from 'moment';
  49. import submit_1 from './common/submit_1.vue';
  50. export default {
  51. components: {
  52. submit_1,
  53. },
  54. data() {
  55. return {
  56. config: {},
  57. user: {},
  58. // 聊天记录
  59. msgList: [],
  60. total: 0,
  61. skip: 0,
  62. limit: 6,
  63. page: 0,
  64. // 判断是否跳到最后一条
  65. is_bottom: true,
  66. // 判断是否下拉刷新复位
  67. triggered: false,
  68. scrollToView: '', //滑动最后一条信息
  69. // 判断是否是选择图片刷新
  70. is_img: false
  71. }
  72. },
  73. onShow: async function() {
  74. const that = this;
  75. that.searchUser();
  76. that.searchConfig();
  77. // 判断是否是选择图片刷新
  78. if (!that.is_img) {
  79. await that.clearPage();
  80. await that.search()
  81. }
  82. },
  83. onPullDownRefresh: async function() {
  84. const that = this;
  85. that.clearPage();
  86. await that.search();
  87. uni.stopPullDownRefresh();
  88. },
  89. methods: {
  90. searchUser() {
  91. const that = this;
  92. try {
  93. const res = uni.getStorageSync('user');
  94. if (res) that.$set(that, `user`, res);
  95. } catch (e) {
  96. uni.showToast({
  97. title: err.errmsg,
  98. icon: 'error',
  99. duration: 2000
  100. });
  101. }
  102. },
  103. searchConfig() {
  104. const that = this;
  105. try {
  106. const res = uni.getStorageSync('config');
  107. if (res) that.$set(that, `config`, res);
  108. } catch (e) {
  109. uni.showToast({
  110. title: err.errmsg,
  111. icon: 'error',
  112. duration: 2000
  113. });
  114. }
  115. },
  116. async search() {
  117. const that = this;
  118. if (!that.user._id) return
  119. let info = {
  120. skip: that.skip,
  121. limit: that.limit,
  122. user: that.user._id
  123. }
  124. const res = await that.$api(`chat`, 'GET', {
  125. ...info,
  126. })
  127. if (res.errcode == '0') {
  128. let list = [...res.data.reverse(), ...that.msgList];
  129. that.$set(that, `msgList`, list)
  130. that.$set(that, `total`, res.total)
  131. } else {
  132. uni.showToast({
  133. title: res.errmsg,
  134. });
  135. }
  136. // 跳转到最后一条数据 与前面的:id进行对照
  137. that.goBottom();
  138. },
  139. // 进行图片的预览
  140. previewImg(e) {
  141. const that = this;
  142. // 预览图片
  143. uni.previewImage({
  144. current: 0,
  145. urls: [e],
  146. longPressActions: {
  147. itemList: ['发送给朋友', '保存图片', '收藏'],
  148. success: function(data) {
  149. console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) +
  150. '张图片');
  151. },
  152. fail: function(err) {
  153. console.log(err.errMsg);
  154. }
  155. }
  156. });
  157. },
  158. // 判断是否是选择图片刷新
  159. choseImg(e) {
  160. const that = this;
  161. that.$set(that, `is_img`, e)
  162. },
  163. //接受输入内容
  164. async inputs(e) {
  165. const that = this;
  166. let user = that.user
  167. if (user._id) {
  168. //时间间隔处理
  169. let data = {
  170. "user": user._id,
  171. "speaker": user._id,
  172. "content": e.message,
  173. "time": moment().format('YYYY-MM-DD HH:mm:ss'),
  174. "msg_type": e.type
  175. };
  176. // 发送给服务器消息
  177. let res = await that.$api(`chat`, `POST`, data);
  178. if (res.errcode == '0') {
  179. that.msgList.push(res.data);
  180. } else {
  181. uni.showToast({
  182. title: res.errmsg,
  183. icon: 'none'
  184. })
  185. }
  186. // 跳转到最后一条数据 与前面的:id进行对照
  187. that.goBottom();
  188. } else {
  189. uni.navigateTo({
  190. url: `/pagesHome/login/index`
  191. })
  192. }
  193. },
  194. //输入框高度
  195. heights(e) {
  196. const that = this;
  197. that.goBottom();
  198. },
  199. // 滚动到底部
  200. async goBottom() {
  201. const that = this;
  202. that.scrollToView = ""
  203. that.$nextTick(function() {
  204. that.scrollToView = 'msg123456789'
  205. })
  206. },
  207. // 下拉刷新分页
  208. getFresh(e) {
  209. const that = this;
  210. that.$set(that, `triggered`, true)
  211. that.$set(that, `is_img`, false)
  212. let msgList = that.msgList;
  213. let limit = that.limit;
  214. setTimeout(() => {
  215. if (that.total > msgList.length) {
  216. uni.showLoading({
  217. title: '加载中',
  218. mask: true
  219. })
  220. let page = that.page + 1;
  221. that.$set(that, `page`, page)
  222. let skip = page * limit;
  223. that.$set(that, `skip`, skip)
  224. that.$set(that, `is_bottom`, false)
  225. that.search();
  226. uni.hideLoading();
  227. } else {
  228. uni.showToast({
  229. title: `没有更多聊天记录了`,
  230. icon: 'none'
  231. })
  232. }
  233. that.triggered = false;
  234. }, 1000)
  235. },
  236. // 分页
  237. toPage(e) {
  238. const that = this;
  239. let list = that.list;
  240. let limit = that.limit;
  241. if (that.total > list.length) {
  242. uni.showLoading({
  243. title: '加载中',
  244. mask: true
  245. })
  246. let page = that.page + 1;
  247. that.$set(that, `page`, page)
  248. let skip = page * limit;
  249. that.$set(that, `skip`, skip)
  250. that.search();
  251. uni.hideLoading();
  252. } else that.$set(that, `is_bottom`, true)
  253. },
  254. // 清空列表
  255. clearPage() {
  256. const that = this;
  257. that.$set(that, `msgList`, [])
  258. that.$set(that, `skip`, 0)
  259. that.$set(that, `limit`, 6)
  260. that.$set(that, `page`, 0)
  261. }
  262. }
  263. }
  264. </script>
  265. <style lang="scss" scoped>
  266. .main {
  267. display: flex;
  268. flex-direction: column;
  269. width: 100vw;
  270. height: 100vh;
  271. overflow: hidden;
  272. .one {
  273. position: relative;
  274. flex-grow: 1;
  275. .scroll-view {
  276. .chat-ls {
  277. padding: 2vw 2vw 0 2vw;
  278. .chat-time {
  279. font-size: 24rpx;
  280. color: rgba(39, 40, 50, 0.3);
  281. line-height: 34rpx;
  282. padding: 10rpx 0rpx;
  283. text-align: center;
  284. }
  285. .msg-m {
  286. display: flex;
  287. padding: 20rpx 0;
  288. .user-img {
  289. flex: none;
  290. width: 80rpx;
  291. height: 80rpx;
  292. border-radius: 40rpx;
  293. border: 1px solid #c0c0c0;
  294. }
  295. .message {
  296. flex: none;
  297. max-width: 480rpx;
  298. }
  299. .img {
  300. margin: 0 20rpx;
  301. }
  302. .msg-text {
  303. font-size: 32rpx;
  304. color: rgba(39, 40, 50, 1);
  305. line-height: 44rpx;
  306. padding: 18rpx 24rpx;
  307. word-break: break-all;
  308. }
  309. .msg-img {
  310. max-width: 400rpx;
  311. border-radius: 20rpx;
  312. }
  313. }
  314. .msg-left {
  315. flex-direction: row;
  316. .msg-text {
  317. word-break: break-all;
  318. margin-left: 16rpx;
  319. background-color: #f1f1f1;
  320. border-radius: 0rpx 20rpx 20rpx 20rpx;
  321. }
  322. .ms-img {
  323. margin-left: 16rpx;
  324. }
  325. }
  326. .msg-right {
  327. flex-direction: row-reverse;
  328. .msg-text {
  329. margin-right: 16rpx;
  330. background-color: var(--f3CColor);
  331. border-radius: 20rpx 0rpx 20rpx 20rpx;
  332. }
  333. .ms-img {
  334. margin-right: 16rpx;
  335. }
  336. }
  337. }
  338. }
  339. }
  340. .two {
  341. background-color: #f0f0f0;
  342. border-top: 1px solid rgba(39, 40, 50, 0.1);
  343. }
  344. }
  345. .scroll-view {
  346. position: absolute;
  347. top: 0;
  348. left: 0;
  349. right: 0;
  350. bottom: 0;
  351. .list-scroll-view {
  352. display: flex;
  353. flex-direction: column;
  354. }
  355. }
  356. .is_bottom {
  357. width: 100%;
  358. text-align: center;
  359. text {
  360. padding: 2vw 0;
  361. display: inline-block;
  362. color: var(--f85Color);
  363. font-size: var(--font14Size);
  364. }
  365. }
  366. </style>