im.vue 14 KB

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