liaotianshi.vue 14 KB

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