index.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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></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. 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. formatName(str) {
  263. if (str) return str.substr(0, 1) + new Array(str.length).join('');
  264. }
  265. }
  266. }
  267. </script>
  268. <style lang="scss" scoped>
  269. .main {
  270. display: flex;
  271. flex-direction: column;
  272. width: 100vw;
  273. height: 100vh;
  274. overflow: hidden;
  275. .one {
  276. position: relative;
  277. flex-grow: 1;
  278. .scroll-view {
  279. .chat-ls {
  280. padding: 2vw 2vw 0 2vw;
  281. .chat-time {
  282. font-size: 24rpx;
  283. color: rgba(39, 40, 50, 0.3);
  284. line-height: 34rpx;
  285. padding: 10rpx 0rpx;
  286. text-align: center;
  287. }
  288. .msg-m {
  289. display: flex;
  290. padding: 20rpx 0;
  291. .user-img {
  292. flex: none;
  293. width: 80rpx;
  294. height: 80rpx;
  295. border-radius: 40rpx;
  296. border: 1px solid #c0c0c0;
  297. }
  298. .message {
  299. flex: none;
  300. max-width: 480rpx;
  301. }
  302. .img {
  303. margin: 0 20rpx;
  304. }
  305. .msg-text {
  306. font-size: 32rpx;
  307. color: rgba(39, 40, 50, 1);
  308. line-height: 44rpx;
  309. padding: 18rpx 24rpx;
  310. word-break: break-all;
  311. }
  312. .msg-img {
  313. max-width: 400rpx;
  314. border-radius: 20rpx;
  315. }
  316. }
  317. .msg-left {
  318. flex-direction: row;
  319. .msg-text {
  320. margin-left: 16rpx;
  321. background-color: var(--f1Color);
  322. ;
  323. border-radius: 0rpx 20rpx 20rpx 20rpx;
  324. }
  325. .ms-img {
  326. margin-left: 16rpx;
  327. }
  328. }
  329. .msg-right {
  330. flex-direction: row-reverse;
  331. .msg-text {
  332. margin-right: 16rpx;
  333. background-color: var(--f3CColor);
  334. border-radius: 20rpx 0rpx 20rpx 20rpx;
  335. }
  336. .ms-img {
  337. margin-right: 16rpx;
  338. }
  339. }
  340. }
  341. }
  342. }
  343. .two {
  344. background-color: #f0f0f0;
  345. border-top: 1px solid rgba(39, 40, 50, 0.1);
  346. }
  347. }
  348. .scroll-view {
  349. position: absolute;
  350. top: 0;
  351. left: 0;
  352. right: 0;
  353. bottom: 0;
  354. .list-scroll-view {
  355. display: flex;
  356. flex-direction: column;
  357. }
  358. }
  359. .is_bottom {
  360. width: 100%;
  361. text-align: center;
  362. text {
  363. padding: 2vw 0;
  364. display: inline-block;
  365. color: var(--f85Color);
  366. font-size: var(--font14Size);
  367. }
  368. }
  369. </style>