info.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. uni.navigateTo({
  239. url: `/pagesHome/shop/index`
  240. })
  241. },
  242. //输入框高度
  243. heights(e) {
  244. const that = this;
  245. that.goBottom();
  246. },
  247. // 滚动到底部
  248. goBottom() {
  249. const that = this;
  250. that.scrollToView = '';
  251. that.$nextTick(function() {
  252. that.scrollToView = 'msg' + (that.msgList.length - 1)
  253. })
  254. },
  255. // 下拉刷新分页
  256. getFresh(e) {
  257. const that = this;
  258. that.triggered = true;
  259. let list = that.msgList;
  260. let limit = that.limit;
  261. setTimeout(() => {
  262. if (that.total > list.length) {
  263. uni.showLoading({
  264. title: '加载中',
  265. mask: true
  266. })
  267. let page = that.page + 1;
  268. that.$set(that, `page`, page)
  269. let skip = page * limit;
  270. that.$set(that, `skip`, skip)
  271. that.$set(that, `is_bottom`, false)
  272. that.search();
  273. uni.hideLoading();
  274. } else {
  275. uni.showToast({
  276. title: `没有更多聊天记录了`,
  277. icon: 'none'
  278. })
  279. }
  280. that.triggered = false;
  281. }, 1000)
  282. },
  283. clearPage() {
  284. const that = this;
  285. that.$set(that, `msgList`, [])
  286. that.$set(that, `skip`, 0)
  287. that.$set(that, `limit`, 10)
  288. that.$set(that, `page`, 0)
  289. },
  290. }
  291. }
  292. </script>
  293. <style lang="scss">
  294. .main {
  295. display: flex;
  296. flex-direction: column;
  297. width: 100vw;
  298. height: 100vh;
  299. overflow: hidden;
  300. .one {
  301. position: relative;
  302. flex-grow: 1;
  303. .scroll-view {
  304. .chat-ls {
  305. padding: 2vw 2vw 0 2vw;
  306. .chat-time {
  307. font-size: 24rpx;
  308. color: rgba(39, 40, 50, 0.3);
  309. line-height: 34rpx;
  310. padding: 10rpx 0rpx;
  311. text-align: center;
  312. }
  313. .msg-m {
  314. display: flex;
  315. padding: 20rpx 0;
  316. .user-img {
  317. flex: none;
  318. width: 80rpx;
  319. height: 80rpx;
  320. border-radius: 40rpx;
  321. border: 1px solid #c0c0c0;
  322. }
  323. .message {
  324. flex: none;
  325. max-width: 480rpx;
  326. }
  327. .img {
  328. margin: 0 20rpx 0 0;
  329. }
  330. .msg-text {
  331. font-size: 32rpx;
  332. color: rgba(39, 40, 50, 1);
  333. line-height: 44rpx;
  334. padding: 18rpx 24rpx;
  335. word-break: break-all;
  336. }
  337. .msg-img {
  338. max-width: 400rpx;
  339. border-radius: 20rpx;
  340. }
  341. }
  342. .msg-left {
  343. flex-direction: row;
  344. .msg-text {
  345. word-break: break-all;
  346. margin-left: 16rpx;
  347. background-color: #f1f1f1;
  348. border-radius: 0rpx 20rpx 20rpx 20rpx;
  349. }
  350. .ms-img {
  351. margin-left: 16rpx;
  352. }
  353. }
  354. .msg-right {
  355. flex-direction: row-reverse;
  356. .msg-text {
  357. margin-right: 16rpx;
  358. background-color: rgba(255, 228, 49, 0.8);
  359. border-radius: 20rpx 0rpx 20rpx 20rpx;
  360. }
  361. .ms-img {
  362. margin-right: 16rpx;
  363. }
  364. }
  365. }
  366. }
  367. }
  368. .two {
  369. background-color: #f0f0f0;
  370. border-top: 1px solid rgba(39, 40, 50, 0.1);
  371. }
  372. }
  373. .scroll-view {
  374. position: absolute;
  375. top: 0;
  376. left: 0;
  377. right: 0;
  378. bottom: 0;
  379. .list-scroll-view {
  380. display: flex;
  381. flex-direction: column;
  382. }
  383. }
  384. </style>