im.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. })
  215. console.log(userId, '*******************')
  216. uni.connectSocket({
  217. // url: 'ws://192.168.1.17:8881/gameTeamChat/' + userId + '_' + this.teamId,
  218. // url: 'wss://game.shengqianxiong.com.cn/wss/gameTeamChat/' + userId + '_' + this.teamId,
  219. // url: 'ws://74165o0188.uicp.fun/sqx_fast/chatSocket/' + userId,
  220. url: this.config("WSHOST1") + this.ordersId,
  221. data() {
  222. return {
  223. msg: 'Hello'
  224. }
  225. },
  226. header: {
  227. 'content-type': 'application/json'
  228. },
  229. method: 'GET',
  230. success(res) {
  231. uni.hideLoading();
  232. that.getTimeOrListItem1();
  233. // 这里是接口调用成功的回调,不是连接成功的回调,请注意
  234. },
  235. fail(err) {
  236. // 这里是接口调用失败的回调,不是连接失败的回调,请注意
  237. console.log("--------------" + JSON.stringify(err))
  238. }
  239. });
  240. uni.onSocketOpen((res) => {
  241. that.connecting = false
  242. that.connected = true
  243. uni.hideLoading()
  244. // uni.showToast({
  245. // icon: 'none',
  246. // title: '连接成功'
  247. // })
  248. console.log('onOpen', res);
  249. });
  250. uni.onSocketError((err) => {
  251. that.connecting = false
  252. that.connected = false
  253. uni.hideLoading()
  254. uni.showModal({
  255. content: '网络较差,请稍后再试',
  256. showCancel: false
  257. })
  258. console.log('onError', err);
  259. });
  260. uni.onSocketMessage(function(res) {
  261. console.log('收到服务器内容:' + JSON.stringify(res));
  262. setTimeout(() => {
  263. that.getTimeOrListItem1();
  264. }, 50);
  265. });
  266. uni.onSocketClose((res) => {
  267. that.connected = false
  268. that.startRecive = false
  269. that.msg = false
  270. console.log('onClose', res)
  271. });
  272. },
  273. close() {
  274. uni.closeSocket()
  275. },
  276. getTimeOrListItem1() {
  277. let data={
  278. page:1,
  279. limit:1000,
  280. ordersId:this.ordersId
  281. }
  282. this.$Request.get('/shop/ordersChat/selectGameChatDetails',data
  283. ).then(
  284. res => {
  285. this.ListItem = [];
  286. if (res.data) {
  287. var time = '';
  288. res.data.list.forEach(d => {
  289. if (!d.avatar) {
  290. // d.chat.userHead = '../../static/logo.png';
  291. let avatar = this.$queue.getData('avatar');
  292. d.avatar = avatar
  293. }
  294. this.ListItem.push(d);
  295. });
  296. this.ListItem = this.ListItem.reverse();
  297. setTimeout(() => {
  298. uni.pageScrollTo({
  299. scrollTop: 99999,
  300. duration: 0
  301. });
  302. }, 50);
  303. }
  304. uni.hideLoading();
  305. });
  306. },
  307. getChatSave() {
  308. let userId = this.$queue.getData('userId');
  309. let phone = this.$queue.getData('phone');
  310. let userName = this.$queue.getData('userName');
  311. if (!phone) {
  312. phone = this.$queue.getData('userName');
  313. }
  314. let avatar = this.$queue.getData('avatar');
  315. let data = {
  316. userId: userId,
  317. userHead: avatar,
  318. userName: userName,
  319. storeId: '0',
  320. storeHead: '同城外卖商家',
  321. storeName: ''
  322. }
  323. this.$Request.postJson('/chat/save', data).then(res => {
  324. if (res.status === 0) {
  325. this.chatId = res.data.chatId;
  326. uni.showLoading({
  327. title: '加载中...'
  328. });
  329. this.getTimeOrListItem1();
  330. }
  331. });
  332. },
  333. setChatSave(type) {
  334. //type:1文字 2图片
  335. if (type === 1 && this.content == '') {
  336. this.$queue.showToast('请输入聊天内容');
  337. return;
  338. }
  339. // if (this.chatId == '' || this.chatId == undefined) {
  340. // this.$queue.showToast('网络较差,请稍后再试');
  341. // return;
  342. // }
  343. let shopId = this.$queue.getData('shopId');
  344. let userId = this.$queue.getData('userId');
  345. let avatar = this.$queue.getData('avatar');
  346. let phone = this.$queue.getData('phone');
  347. let userName = this.$queue.getData('userName');
  348. if (!phone) {
  349. phone = this.$queue.getData('userName');
  350. }
  351. // console.log(this.byUserId)
  352. let data = {
  353. content: this.content,
  354. messageType: type,
  355. shopId: shopId,
  356. ordersId: this.ordersId,
  357. chatConversationId: this.chatConversationId,
  358. userId: userId,
  359. }
  360. data = JSON.stringify(data);
  361. let that = this;
  362. uni.sendSocketMessage({
  363. data: data,
  364. success(res) {
  365. let avatar = that.$queue.getData('avatar');
  366. if (!avatar) {
  367. avatar = '../../static/logo.png';
  368. }
  369. let data = {
  370. chat: {
  371. userHead: avatar
  372. },
  373. content: that.content,
  374. type: type,
  375. userId: userId
  376. }
  377. console.log(data, 'data99999999999999999')
  378. // that.ListItem.push(data);
  379. setTimeout(() => {
  380. that.getTimeOrListItem1();
  381. }, 50);
  382. console.log(that.content);
  383. },
  384. fail(err) {
  385. console.log(err);
  386. }
  387. })
  388. this.content = '';
  389. },
  390. //发送图片
  391. chooseImage(sourceType) {
  392. uni.chooseImage({
  393. count: 1,
  394. sourceType: ['album', 'camera'],
  395. success: res => {
  396. for (let i = 0; i < res.tempFilePaths.length; i++) {
  397. this.$queue.showLoading("上传中...");
  398. uni.uploadFile({ // 上传接口
  399. url: this.config("APIHOST1") + '/alioss/upload', //真实的接口地址
  400. filePath: res.tempFilePaths[i],
  401. name: 'file',
  402. success: (uploadFileRes) => {
  403. console.log(uploadFileRes)
  404. this.content = JSON.parse(uploadFileRes.data).data;
  405. this.setChatSave(2);
  406. uni.hideLoading();
  407. }
  408. });
  409. }
  410. }
  411. })
  412. },
  413. config: function(name) {
  414. var info = null;
  415. if (name) {
  416. var name2 = name.split("."); //字符分割
  417. if (name2.length > 1) {
  418. info = configdata[name2[0]][name2[1]] || null;
  419. } else {
  420. info = configdata[name] || null;
  421. }
  422. if (info == null) {
  423. // let web_config = cache.get("web_config");
  424. // if (web_config) {
  425. // if (name2.length > 1) {
  426. // info = web_config[name2[0]][name2[1]] || null;
  427. // } else {
  428. // info = web_config[name] || null;
  429. // }
  430. // }
  431. }
  432. }
  433. return info;
  434. },
  435. //查看大图
  436. viewImg(item) {
  437. let imgsArray = [];
  438. imgsArray[0] = item;
  439. uni.previewImage({
  440. current: 0,
  441. urls: imgsArray
  442. });
  443. },
  444. },
  445. };
  446. </script>
  447. <style>
  448. page {
  449. /* background: #1c1b20; */
  450. }
  451. .input-box {
  452. position: fixed;
  453. bottom: 0;
  454. left: 0;
  455. height: 120rpx;
  456. width: 100%;
  457. display: flex;
  458. box-sizing: content-box;
  459. z-index: 999;
  460. /* background-color: #ececec; */
  461. /* padding: 0 5rpx; */
  462. }
  463. .chat-listitem {
  464. display: flex;
  465. margin-top: 20rpx;
  466. padding: 10rpx;
  467. }
  468. .chat-listitem-text {
  469. color: #000000;
  470. background: #FFFFFF;
  471. margin-top: 10rpx;
  472. width: fit-content;
  473. padding: 15rpx;
  474. font-size: 30rpx;
  475. height: max-content;
  476. word-wrap: break-word;
  477. word-break: break-all;
  478. border-radius: 10rpx;
  479. }
  480. .chat-listitem-image-type4 {
  481. color: #000000;
  482. background: #FFFFFF;
  483. width: fit-content;
  484. font-size: 30rpx;
  485. height: max-content;
  486. word-wrap: break-word;
  487. word-break: break-all;
  488. border-top-left-radius: 20rpx;
  489. border-top-right-radius: 20rpx;
  490. }
  491. .chat-listitem-image {
  492. margin-top: 5rpx;
  493. width: 75rpx;
  494. height: 75rpx;
  495. border-radius: 5rpx;
  496. }
  497. .save {
  498. width: 130rpx;
  499. text-align: center;
  500. border-radius: 70rpx;
  501. height: 70rpx;
  502. color: #FFF;
  503. background: #1789FD;
  504. margin: 5rpx 10rpx 0;
  505. line-height: 70rpx;
  506. }
  507. .order {
  508. position: fixed;
  509. bottom: 65px;
  510. right: 15px;
  511. }
  512. .order image {
  513. width: 120rpx;
  514. height: 120rpx;
  515. }
  516. </style>