index.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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.name||user.nickname)" fontSize="22"
  27. randomBgColor color-index="7"></u-avatar>
  28. <!-- 文字 -->
  29. <view class="message" v-if="item.msg_type =='0'">
  30. <view class="msg-text">{{item.content}}</view>
  31. </view>
  32. <!-- 图像 -->
  33. <view class="message img" v-else-if="item.msg_type =='1'" @tap="previewImg(item.content)">
  34. <image :src="item.content" class="msg-img" mode="widthFix"></image>
  35. </view>
  36. </view>
  37. </view>
  38. <view id="msg123456789"></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. await that.goBottom(that.msgList[that.msgList.length - 1]);
  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. await that.goBottom(that.msgList[that.msgList.length - 1]);
  188. } else {
  189. uni.navigateTo({
  190. url: `/pagesHome/login/index`
  191. })
  192. }
  193. },
  194. //输入框高度
  195. async heights(e) {
  196. const that = this;
  197. await that.goBottom(that.msgList[that.msgList.length - 1]);
  198. },
  199. // 滚动到底部
  200. async goBottom(item) {
  201. const that = this;
  202. that.$nextTick(function() {
  203. that.$set(that, `scrollToView`, `msg${item._id}`)
  204. })
  205. },
  206. // 下拉刷新分页
  207. getFresh(e) {
  208. const that = this;
  209. that.$set(that, `triggered`, true)
  210. that.$set(that, `is_img`, false)
  211. let msgList = that.msgList;
  212. let limit = that.limit;
  213. setTimeout(() => {
  214. if (that.total > msgList.length) {
  215. uni.showLoading({
  216. title: '加载中',
  217. mask: true
  218. })
  219. let page = that.page + 1;
  220. that.$set(that, `page`, page)
  221. let skip = page * limit;
  222. that.$set(that, `skip`, skip)
  223. that.$set(that, `is_bottom`, false)
  224. that.search();
  225. uni.hideLoading();
  226. } else {
  227. uni.showToast({
  228. title: `没有更多聊天记录了`,
  229. icon: 'none'
  230. })
  231. }
  232. that.triggered = false;
  233. }, 1000)
  234. },
  235. // 分页
  236. toPage(e) {
  237. const that = this;
  238. let list = that.list;
  239. let limit = that.limit;
  240. if (that.total > list.length) {
  241. uni.showLoading({
  242. title: '加载中',
  243. mask: true
  244. })
  245. let page = that.page + 1;
  246. that.$set(that, `page`, page)
  247. let skip = page * limit;
  248. that.$set(that, `skip`, skip)
  249. that.search();
  250. uni.hideLoading();
  251. } else that.$set(that, `is_bottom`, true)
  252. },
  253. // 清空列表
  254. clearPage() {
  255. const that = this;
  256. that.$set(that, `msgList`, [])
  257. that.$set(that, `skip`, 0)
  258. that.$set(that, `limit`, 6)
  259. that.$set(that, `page`, 0)
  260. },
  261. formatName(str) {
  262. if (str) return str.substr(0, 1) + new Array(str.length).join('');
  263. }
  264. }
  265. }
  266. </script>
  267. <style lang="scss" scoped>
  268. .main {
  269. display: flex;
  270. flex-direction: column;
  271. width: 100vw;
  272. height: 100vh;
  273. overflow: hidden;
  274. .one {
  275. position: relative;
  276. flex-grow: 1;
  277. .scroll-view {
  278. .chat-ls {
  279. padding: 2vw 2vw 0 2vw;
  280. .chat-time {
  281. font-size: 24rpx;
  282. color: rgba(39, 40, 50, 0.3);
  283. line-height: 34rpx;
  284. padding: 10rpx 0rpx;
  285. text-align: center;
  286. }
  287. .msg-m {
  288. display: flex;
  289. padding: 20rpx 0;
  290. .user-img {
  291. flex: none;
  292. width: 80rpx;
  293. height: 80rpx;
  294. border-radius: 40rpx;
  295. border: 1px solid #c0c0c0;
  296. }
  297. .message {
  298. flex: none;
  299. max-width: 480rpx;
  300. }
  301. .img {
  302. margin: 0 20rpx;
  303. }
  304. .msg-text {
  305. font-size: 32rpx;
  306. color: rgba(39, 40, 50, 1);
  307. line-height: 44rpx;
  308. padding: 18rpx 24rpx;
  309. word-break: break-all;
  310. }
  311. .msg-img {
  312. max-width: 400rpx;
  313. border-radius: 20rpx;
  314. }
  315. }
  316. .msg-left {
  317. flex-direction: row;
  318. .msg-text {
  319. margin-left: 16rpx;
  320. background-color: var(--f1Color);
  321. ;
  322. border-radius: 0rpx 20rpx 20rpx 20rpx;
  323. }
  324. .ms-img {
  325. margin-left: 16rpx;
  326. }
  327. }
  328. .msg-right {
  329. flex-direction: row-reverse;
  330. .msg-text {
  331. margin-right: 16rpx;
  332. background-color: var(--f3CColor);
  333. border-radius: 20rpx 0rpx 20rpx 20rpx;
  334. }
  335. .ms-img {
  336. margin-right: 16rpx;
  337. }
  338. }
  339. }
  340. }
  341. }
  342. .two {
  343. background-color: #f0f0f0;
  344. border-top: 1px solid rgba(39, 40, 50, 0.1);
  345. }
  346. }
  347. .scroll-view {
  348. position: absolute;
  349. top: 0;
  350. left: 0;
  351. right: 0;
  352. bottom: 0;
  353. .list-scroll-view {
  354. display: flex;
  355. flex-direction: column;
  356. }
  357. }
  358. .is_bottom {
  359. width: 100%;
  360. text-align: center;
  361. text {
  362. padding: 2vw 0;
  363. display: inline-block;
  364. color: var(--f85Color);
  365. font-size: var(--font14Size);
  366. }
  367. }
  368. </style>