im.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. <template>
  2. <view>
  3. <view style="width: 100%; padding-bottom: 140rpx">
  4. <view style="display: flex; flex-direction: column" v-for="(item, index) in ListItem" :key="index">
  5. <view style="margin-top: 15rpx; width: 100%; text-align: center; font-size: 26rpx; color: #999999">
  6. {{ item.createTime }}
  7. </view>
  8. <view v-if="item.shopId" style="width: 83%; margin-left: 15%">
  9. <view class="chat-listitem" style="float: right">
  10. <view v-if="item.content && item.messageType === 1" @longpress="copy(item.content)" class="chat-listitem-text" style="margin-right: 20rpx">
  11. {{ item.content }}
  12. </view>
  13. <image
  14. @tap="viewImg(item.content)"
  15. v-if="item.content && item.messageType === 2"
  16. :src="item.content"
  17. style="height: 200rpx; width: 200rpx; margin-right: 20rpx"
  18. ></image>
  19. <view>
  20. <image v-if="item.shopCover" :src="item.shopCover" class="chat-listitem-image"></image>
  21. <image v-else src="../../static/logo.png" class="chat-listitem-image"></image>
  22. </view>
  23. </view>
  24. </view>
  25. <view v-if="!item.shopId" style="width: 83%; margin-right: 15%">
  26. <view class="chat-listitem" style="float: left; margin-left: 10rpx">
  27. <view v-if="item.userId">
  28. <image :src="item.userAvatar" class="chat-listitem-image"></image>
  29. </view>
  30. <view v-if="item.riderId">
  31. <image :src="item.riderAvatar" class="chat-listitem-image"></image>
  32. </view>
  33. <view>
  34. <!-- <view class="margin-left">{{item.userName}}</view> -->
  35. <view v-if="item.content && item.messageType === 1" class="chat-listitem-text" style="margin-left: 20rpx">{{ item.content }}</view>
  36. <image
  37. @tap="viewImg(item.content)"
  38. v-if="item.content && item.messageType === 2"
  39. :src="item.content"
  40. style="height: 200rpx; width: 200rpx; margin-left: 20rpx"
  41. ></image>
  42. </view>
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. <!-- 跳转订单 -->
  48. <view class="order" @click="bindorder()">
  49. <image src="../../static/images/img/order.png"></image>
  50. </view>
  51. <!-- 底部聊天输入框 -->
  52. <view class="input-box">
  53. <view class="justify-between padding-lr align-center" style="display: flex; width: 100%; background-color: #eeeeee">
  54. <image src="../../static/images/img/add.png" @click="chooseImage(['album'])" style="width: 50rpx; height: 50rpx; margin-right: 12rpx; border-radius: 50upx"></image>
  55. <input
  56. confirm-type="send"
  57. @confirm="setChatSave(1)"
  58. type="text"
  59. v-model="content"
  60. style="width: 72%; height: 70rpx; background: #fff; margin: 0 10rpx; border-radius: 5rpx; padding-left: 10rpx"
  61. />
  62. <view class="save" @tap="setChatSave(1)">发送</view>
  63. </view>
  64. </view>
  65. <!-- 用于图片压缩的canvas画布 -->
  66. <canvas
  67. :style="{
  68. width: cw + 'px',
  69. height: cw + 'px',
  70. position: 'absolute',
  71. zIndex: -1,
  72. left: '-10000rpx',
  73. top: '-10000rpx'
  74. }"
  75. canvas-id="zipCanvas"
  76. ></canvas>
  77. <!--画布结束-->
  78. </view>
  79. </template>
  80. <script>
  81. import configdata from '../../common/config.js'
  82. import getLessLimitSizeImage from '@/utils/imageCompress.js'
  83. export default {
  84. data() {
  85. return {
  86. connected: false,
  87. connecting: false,
  88. msg: false,
  89. type4: [],
  90. listRight: {
  91. chat: {
  92. userHead: ''
  93. },
  94. content: '',
  95. sendType: 1,
  96. type: 1
  97. },
  98. content: '',
  99. chatId: '',
  100. type: 1,
  101. ListItem: [],
  102. ShopState: false,
  103. ShopordersId: '',
  104. Shopimage: '',
  105. Shopmoney: '',
  106. ShopTitle: '',
  107. orderState: false,
  108. ordersId: '',
  109. userId: '',
  110. orderimage: '',
  111. orderNum: '',
  112. teamId: '',
  113. hand: 1,
  114. index: 0,
  115. page: 0,
  116. size: 1000,
  117. countDown: '',
  118. ordersId: '',
  119. byUserId: '',
  120. //画板边长默认是屏幕宽度,正方形画布
  121. cw: uni.getSystemInfoSync().windowWidth
  122. }
  123. },
  124. computed: {
  125. showMsg() {
  126. if (this.connected) {
  127. if (this.msg) {
  128. return '收到消息:' + this.msg
  129. } else {
  130. return '等待接收消息'
  131. }
  132. } else {
  133. return '尚未连接'
  134. }
  135. }
  136. },
  137. onUnload() {
  138. // uni.closeSocket()
  139. uni.hideLoading()
  140. },
  141. onLoad(d) {
  142. this.userId = this.$queue.getData('userId')
  143. // this.byUserId = d.byUserId
  144. this.ordersId = d.orderId
  145. console.log(this.ordersId)
  146. this.connect()
  147. // if (d.teamName) {
  148. // uni.setNavigationBarTitle({
  149. // title: d.teamName
  150. // });
  151. // }
  152. },
  153. onShow() {
  154. if (this.connected || this.connecting) {
  155. } else {
  156. this.connect()
  157. }
  158. },
  159. onHide() {
  160. // uni.closeSocket()
  161. },
  162. onUnload() {
  163. this.close()
  164. },
  165. methods: {
  166. // 跳转订单
  167. bindorder() {
  168. console.log(this.ordersId)
  169. uni.navigateTo({
  170. url: '/pages/order/orderDet?orderId=' + this.ordersId
  171. })
  172. },
  173. copy(content) {
  174. uni.showModal({
  175. title: '温馨提示',
  176. content: '确认要复制此文字吗?',
  177. showCancel: true,
  178. cancelText: '取消',
  179. confirmText: '确认',
  180. success: (res) => {
  181. if (res.confirm) {
  182. uni.setClipboardData({
  183. data: content,
  184. success: (r) => {
  185. this.$queue.showToast('复制成功')
  186. }
  187. })
  188. }
  189. }
  190. })
  191. },
  192. getDateDiff(data) {
  193. // 传进来的data必须是日期格式,不能是时间戳
  194. //var str = data;
  195. //将字符串转换成时间格式
  196. var timePublish = new Date(data)
  197. var timeNow = new Date()
  198. var minute = 1000 * 60
  199. var hour = minute * 60
  200. var day = hour * 24
  201. var month = day * 30
  202. var result = '2'
  203. var diffValue = timeNow - timePublish
  204. var diffMonth = diffValue / month
  205. var diffWeek = diffValue / (7 * day)
  206. var diffDay = diffValue / day
  207. var diffHour = diffValue / hour
  208. var diffMinute = diffValue / minute
  209. if (diffMonth > 3) {
  210. result = timePublish.getFullYear() + '-'
  211. result += timePublish.getMonth() + '-'
  212. result += timePublish.getDate()
  213. } else if (diffMonth > 1) {
  214. //月
  215. result = data.substring(0, 10)
  216. } else if (diffWeek > 1) {
  217. //周
  218. result = data.substring(0, 10)
  219. } else if (diffDay > 1) {
  220. //天
  221. result = data.substring(0, 10)
  222. } else if (diffHour > 1) {
  223. //小时
  224. result = parseInt(diffHour) + '小时前'
  225. } else if (diffMinute > 1) {
  226. //分钟
  227. result = parseInt(diffMinute) + '分钟前'
  228. } else {
  229. result = '刚刚'
  230. }
  231. return result
  232. },
  233. connect() {
  234. let that = this
  235. let userId = that.$queue.getData('userId')
  236. if (that.connected || that.connecting) {
  237. uni.showModal({
  238. content: '正在连接或者已经连接,请勿重复连接',
  239. showCancel: false
  240. })
  241. return false
  242. }
  243. that.connecting = true
  244. uni.showLoading({
  245. title: '连接中...',
  246. mask: true // 是否显示透明蒙层,防止触摸穿透
  247. })
  248. console.log(userId, '*******************')
  249. uni.connectSocket({
  250. // url: 'ws://192.168.1.17:8881/gameTeamChat/' + userId + '_' + this.teamId,
  251. // url: 'wss://game.shengqianxiong.com.cn/wss/gameTeamChat/' + userId + '_' + this.teamId,
  252. // url: 'ws://74165o0188.uicp.fun/sqx_fast/chatSocket/' + userId,
  253. url: this.config('WSHOST1') + this.ordersId,
  254. data() {
  255. return {
  256. msg: 'Hello'
  257. }
  258. },
  259. header: {
  260. 'content-type': 'application/json'
  261. },
  262. method: 'GET',
  263. success(res) {
  264. uni.hideLoading()
  265. that.getTimeOrListItem1()
  266. // 这里是接口调用成功的回调,不是连接成功的回调,请注意
  267. },
  268. fail(err) {
  269. // 这里是接口调用失败的回调,不是连接失败的回调,请注意
  270. console.log('--------------' + JSON.stringify(err))
  271. }
  272. })
  273. uni.onSocketOpen((res) => {
  274. that.connecting = false
  275. that.connected = true
  276. uni.hideLoading()
  277. // uni.showToast({
  278. // icon: 'none',
  279. // title: '连接成功'
  280. // })
  281. console.log('onOpen', res)
  282. })
  283. uni.onSocketError((err) => {
  284. that.connecting = false
  285. that.connected = false
  286. uni.hideLoading()
  287. uni.showModal({
  288. content: '网络较差,请稍后再试',
  289. showCancel: false
  290. })
  291. console.log('onError', err)
  292. })
  293. uni.onSocketMessage(function (res) {
  294. console.log('收到服务器内容:' + JSON.stringify(res))
  295. setTimeout(() => {
  296. that.getTimeOrListItem1()
  297. }, 50)
  298. })
  299. uni.onSocketClose((res) => {
  300. that.connected = false
  301. that.startRecive = false
  302. that.msg = false
  303. console.log('onClose', res)
  304. })
  305. },
  306. close() {
  307. uni.closeSocket()
  308. },
  309. getTimeOrListItem1() {
  310. let data = {
  311. page: 1,
  312. limit: 1000,
  313. ordersId: this.ordersId
  314. }
  315. this.$Request.get('/shop/ordersChat/selectGameChatDetails', data).then((res) => {
  316. this.ListItem = []
  317. if (res.data) {
  318. var time = ''
  319. res.data.list.forEach((d) => {
  320. if (!d.avatar) {
  321. // d.chat.userHead = '../../static/logo.png';
  322. let avatar = this.$queue.getData('avatar')
  323. d.avatar = avatar
  324. }
  325. this.ListItem.push(d)
  326. })
  327. this.ListItem = this.ListItem.reverse()
  328. setTimeout(() => {
  329. uni.pageScrollTo({
  330. scrollTop: 99999,
  331. duration: 0
  332. })
  333. }, 50)
  334. }
  335. uni.hideLoading()
  336. })
  337. },
  338. getChatSave() {
  339. let userId = this.$queue.getData('userId')
  340. let phone = this.$queue.getData('phone')
  341. let userName = this.$queue.getData('userName')
  342. if (!phone) {
  343. phone = this.$queue.getData('userName')
  344. }
  345. let avatar = this.$queue.getData('avatar')
  346. let data = {
  347. userId: userId,
  348. userHead: avatar,
  349. userName: userName,
  350. storeId: '0',
  351. storeHead: '同城外卖商家',
  352. storeName: ''
  353. }
  354. this.$Request.postJson('/chat/save', data).then((res) => {
  355. if (res.status === 0) {
  356. this.chatId = res.data.chatId
  357. uni.showLoading({
  358. title: '加载中...',
  359. mask: true // 是否显示透明蒙层,防止触摸穿透
  360. })
  361. this.getTimeOrListItem1()
  362. }
  363. })
  364. },
  365. setChatSave(type) {
  366. //type:1文字 2图片
  367. if (type === 1 && this.content == '') {
  368. this.$queue.showToast('请输入聊天内容')
  369. return
  370. }
  371. // if (this.chatId == '' || this.chatId == undefined) {
  372. // this.$queue.showToast('网络较差,请稍后再试');
  373. // return;
  374. // }
  375. let shopId = this.$queue.getData('shopId')
  376. let userId = this.$queue.getData('userId')
  377. let avatar = this.$queue.getData('avatar')
  378. let phone = this.$queue.getData('phone')
  379. let userName = this.$queue.getData('userName')
  380. if (!phone) {
  381. phone = this.$queue.getData('userName')
  382. }
  383. // console.log(this.byUserId)
  384. let data = {
  385. content: this.content,
  386. messageType: type,
  387. shopId: shopId,
  388. ordersId: this.ordersId,
  389. chatConversationId: this.chatConversationId,
  390. userId: userId
  391. }
  392. data = JSON.stringify(data)
  393. let that = this
  394. uni.sendSocketMessage({
  395. data: data,
  396. success(res) {
  397. let avatar = that.$queue.getData('avatar')
  398. if (!avatar) {
  399. avatar = '../../static/logo.png'
  400. }
  401. let data = {
  402. chat: {
  403. userHead: avatar
  404. },
  405. content: that.content,
  406. type: type,
  407. userId: userId
  408. }
  409. console.log(data, 'data99999999999999999')
  410. // that.ListItem.push(data);
  411. setTimeout(() => {
  412. that.getTimeOrListItem1()
  413. }, 50)
  414. console.log(that.content)
  415. },
  416. fail(err) {
  417. console.log(err)
  418. }
  419. })
  420. this.content = ''
  421. },
  422. //发送图片
  423. chooseImage(sourceType) {
  424. uni.chooseImage({
  425. count: 1,
  426. sourceType: ['album', 'camera'],
  427. success: (res) => {
  428. for (let i = 0; i < res.tempFiles.length; i++) {
  429. //这里的id和页面中写的html代码的canvas的id要一致
  430. let canvasId = 'zipCanvas'
  431. //原图的路径
  432. let imagePath = res.tempFiles[i].path
  433. //大小限制
  434. let limitSize = 1024 * 2
  435. //初始绘画区域是画布自身的宽度也就是屏幕宽度
  436. let drawWidth = uni.getSystemInfoSync().windowWidth
  437. let that = this
  438. let token = uni.getStorageSync('shopToken')
  439. getLessLimitSizeImage(canvasId, imagePath, limitSize, drawWidth, that, (resPath) => {
  440. uni.showLoading({
  441. title: '上传中'
  442. })
  443. uni.uploadFile({
  444. url: that.config('APIHOST1') + '/admin/new-file/upload',
  445. filePath: resPath,
  446. header: {
  447. token: token
  448. },
  449. name: 'file',
  450. success: (uploadFileRes) => {
  451. console.log(uploadFileRes)
  452. this.content = JSON.parse(uploadFileRes.data).data
  453. this.setChatSave(2)
  454. uni.hideLoading()
  455. },
  456. fail: () => {
  457. uni.showToast({
  458. title: '上传失败',
  459. icon: 'error'
  460. })
  461. }
  462. })
  463. })
  464. }
  465. }
  466. })
  467. },
  468. config: function (name) {
  469. var info = null
  470. if (name) {
  471. var name2 = name.split('.') //字符分割
  472. if (name2.length > 1) {
  473. info = configdata[name2[0]][name2[1]] || null
  474. } else {
  475. info = configdata[name] || null
  476. }
  477. if (info == null) {
  478. // let web_config = cache.get("web_config");
  479. // if (web_config) {
  480. // if (name2.length > 1) {
  481. // info = web_config[name2[0]][name2[1]] || null;
  482. // } else {
  483. // info = web_config[name] || null;
  484. // }
  485. // }
  486. }
  487. }
  488. return info
  489. },
  490. //查看大图
  491. viewImg(item) {
  492. let imgsArray = []
  493. imgsArray[0] = item
  494. uni.previewImage({
  495. current: 0,
  496. urls: imgsArray
  497. })
  498. }
  499. }
  500. }
  501. </script>
  502. <style>
  503. page {
  504. /* background: #1c1b20; */
  505. }
  506. .input-box {
  507. position: fixed;
  508. bottom: 0;
  509. left: 0;
  510. height: 120rpx;
  511. width: 100%;
  512. display: flex;
  513. box-sizing: content-box;
  514. z-index: 999;
  515. /* background-color: #ececec; */
  516. /* padding: 0 5rpx; */
  517. }
  518. .chat-listitem {
  519. display: flex;
  520. margin-top: 20rpx;
  521. padding: 10rpx;
  522. }
  523. .chat-listitem-text {
  524. color: #000000;
  525. background: #ffffff;
  526. margin-top: 10rpx;
  527. width: fit-content;
  528. padding: 15rpx;
  529. font-size: 30rpx;
  530. height: max-content;
  531. word-wrap: break-word;
  532. word-break: break-all;
  533. border-radius: 10rpx;
  534. }
  535. .chat-listitem-image-type4 {
  536. color: #000000;
  537. background: #ffffff;
  538. width: fit-content;
  539. font-size: 30rpx;
  540. height: max-content;
  541. word-wrap: break-word;
  542. word-break: break-all;
  543. border-top-left-radius: 20rpx;
  544. border-top-right-radius: 20rpx;
  545. }
  546. .chat-listitem-image {
  547. margin-top: 5rpx;
  548. width: 75rpx;
  549. height: 75rpx;
  550. border-radius: 5rpx;
  551. }
  552. .save {
  553. width: 130rpx;
  554. text-align: center;
  555. border-radius: 70rpx;
  556. height: 70rpx;
  557. color: #fff;
  558. background: #1789fd;
  559. margin: 5rpx 10rpx 0;
  560. line-height: 70rpx;
  561. }
  562. .order {
  563. position: fixed;
  564. bottom: 65px;
  565. right: 15px;
  566. }
  567. .order image {
  568. width: 120rpx;
  569. height: 120rpx;
  570. }
  571. </style>