im.vue 14 KB

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