info.vue 9.6 KB

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