im.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. console.log(this.ordersId)
  278. this.$Request.get('/shop/ordersChat/selectGameChatDetails?page=1&limit=1000&ordersId=' +
  279. this.ordersId).then(
  280. res => {
  281. this.ListItem = [];
  282. if (res.data) {
  283. var time = '';
  284. res.data.list.forEach(d => {
  285. if (!d.avatar) {
  286. // d.chat.userHead = '../../static/logo.png';
  287. let avatar = this.$queue.getData('avatar');
  288. d.avatar = avatar
  289. }
  290. this.ListItem.push(d);
  291. });
  292. this.ListItem = this.ListItem.reverse();
  293. setTimeout(() => {
  294. uni.pageScrollTo({
  295. scrollTop: 99999,
  296. duration: 0
  297. });
  298. }, 50);
  299. }
  300. uni.hideLoading();
  301. });
  302. },
  303. getChatSave() {
  304. let userId = this.$queue.getData('userId');
  305. let phone = this.$queue.getData('phone');
  306. let userName = this.$queue.getData('userName');
  307. if (!phone) {
  308. phone = this.$queue.getData('userName');
  309. }
  310. let avatar = this.$queue.getData('avatar');
  311. let data = {
  312. userId: userId,
  313. userHead: avatar,
  314. userName: userName,
  315. storeId: '0',
  316. storeHead: '同城外卖商家',
  317. storeName: ''
  318. }
  319. this.$Request.postJson('/chat/save', data).then(res => {
  320. if (res.status === 0) {
  321. this.chatId = res.data.chatId;
  322. uni.showLoading({
  323. title: '加载中...'
  324. });
  325. this.getTimeOrListItem1();
  326. }
  327. });
  328. },
  329. setChatSave(type) {
  330. //type:1文字 2图片
  331. if (type === 1 && this.content == '') {
  332. this.$queue.showToast('请输入聊天内容');
  333. return;
  334. }
  335. // if (this.chatId == '' || this.chatId == undefined) {
  336. // this.$queue.showToast('网络较差,请稍后再试');
  337. // return;
  338. // }
  339. let shopId = this.$queue.getData('shopId');
  340. let userId = this.$queue.getData('userId');
  341. let avatar = this.$queue.getData('avatar');
  342. let phone = this.$queue.getData('phone');
  343. let userName = this.$queue.getData('userName');
  344. if (!phone) {
  345. phone = this.$queue.getData('userName');
  346. }
  347. // console.log(this.byUserId)
  348. let data = {
  349. content: this.content,
  350. messageType: type,
  351. shopId: shopId,
  352. ordersId: this.ordersId,
  353. chatConversationId: this.chatConversationId,
  354. userId: userId,
  355. }
  356. data = JSON.stringify(data);
  357. let that = this;
  358. uni.sendSocketMessage({
  359. data: data,
  360. success(res) {
  361. let avatar = that.$queue.getData('avatar');
  362. if (!avatar) {
  363. avatar = '../../static/logo.png';
  364. }
  365. let data = {
  366. chat: {
  367. userHead: avatar
  368. },
  369. content: that.content,
  370. type: type,
  371. userId: userId
  372. }
  373. console.log(data, 'data99999999999999999')
  374. // that.ListItem.push(data);
  375. setTimeout(() => {
  376. that.getTimeOrListItem1();
  377. }, 50);
  378. console.log(that.content);
  379. },
  380. fail(err) {
  381. console.log(err);
  382. }
  383. })
  384. this.content = '';
  385. },
  386. //发送图片
  387. chooseImage(sourceType) {
  388. uni.chooseImage({
  389. count: 1,
  390. sourceType: ['album', 'camera'],
  391. success: res => {
  392. for (let i = 0; i < res.tempFilePaths.length; i++) {
  393. this.$queue.showLoading("上传中...");
  394. uni.uploadFile({ // 上传接口
  395. url: this.config("APIHOST1") + '/alioss/upload', //真实的接口地址
  396. filePath: res.tempFilePaths[i],
  397. name: 'file',
  398. success: (uploadFileRes) => {
  399. console.log(uploadFileRes)
  400. this.content = JSON.parse(uploadFileRes.data).data;
  401. this.setChatSave(2);
  402. uni.hideLoading();
  403. }
  404. });
  405. }
  406. }
  407. })
  408. },
  409. config: function(name) {
  410. var info = null;
  411. if (name) {
  412. var name2 = name.split("."); //字符分割
  413. if (name2.length > 1) {
  414. info = configdata[name2[0]][name2[1]] || null;
  415. } else {
  416. info = configdata[name] || null;
  417. }
  418. if (info == null) {
  419. // let web_config = cache.get("web_config");
  420. // if (web_config) {
  421. // if (name2.length > 1) {
  422. // info = web_config[name2[0]][name2[1]] || null;
  423. // } else {
  424. // info = web_config[name] || null;
  425. // }
  426. // }
  427. }
  428. }
  429. return info;
  430. },
  431. //查看大图
  432. viewImg(item) {
  433. let imgsArray = [];
  434. imgsArray[0] = item;
  435. uni.previewImage({
  436. current: 0,
  437. urls: imgsArray
  438. });
  439. },
  440. },
  441. };
  442. </script>
  443. <style>
  444. page {
  445. /* background: #1c1b20; */
  446. }
  447. .input-box {
  448. position: fixed;
  449. bottom: 0;
  450. left: 0;
  451. height: 120rpx;
  452. width: 100%;
  453. display: flex;
  454. box-sizing: content-box;
  455. z-index: 999;
  456. /* background-color: #ececec; */
  457. /* padding: 0 5rpx; */
  458. }
  459. .chat-listitem {
  460. display: flex;
  461. margin-top: 20rpx;
  462. padding: 10rpx;
  463. }
  464. .chat-listitem-text {
  465. color: #000000;
  466. background: #FFFFFF;
  467. margin-top: 10rpx;
  468. width: fit-content;
  469. padding: 15rpx;
  470. font-size: 30rpx;
  471. height: max-content;
  472. word-wrap: break-word;
  473. word-break: break-all;
  474. border-radius: 10rpx;
  475. }
  476. .chat-listitem-image-type4 {
  477. color: #000000;
  478. background: #FFFFFF;
  479. width: fit-content;
  480. font-size: 30rpx;
  481. height: max-content;
  482. word-wrap: break-word;
  483. word-break: break-all;
  484. border-top-left-radius: 20rpx;
  485. border-top-right-radius: 20rpx;
  486. }
  487. .chat-listitem-image {
  488. margin-top: 5rpx;
  489. width: 75rpx;
  490. height: 75rpx;
  491. border-radius: 5rpx;
  492. }
  493. .save {
  494. width: 130rpx;
  495. text-align: center;
  496. border-radius: 70rpx;
  497. height: 70rpx;
  498. color: #FFF;
  499. background: #1789FD;
  500. margin: 5rpx 10rpx 0;
  501. line-height: 70rpx;
  502. }
  503. .order {
  504. position: fixed;
  505. bottom: 65px;
  506. right: 15px;
  507. }
  508. .order image {
  509. width: 120rpx;
  510. height: 120rpx;
  511. }
  512. </style>