index.vue 9.1 KB

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