info.vue 9.3 KB

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