info.vue 9.7 KB

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