info.vue 9.3 KB

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