liaotianshi.vue 14 KB

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