liaotianshi.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. })
  213. console.log(userId, '*******************')
  214. uni.connectSocket({
  215. // url: 'ws://192.168.1.17:8881/gameTeamChat/' + userId + '_' + this.teamId,
  216. // url: 'wss://game.shengqianxiong.com.cn/wss/gameTeamChat/' + userId + '_' + this.teamId,
  217. // url: 'ws://192.168.1.17:8180/sqx_fast/chatSocket/' + userId,
  218. url: this.config("WSHOST1") + this.orderId,
  219. data() {
  220. return {
  221. msg: 'Hello'
  222. }
  223. },
  224. header: {
  225. 'content-type': 'application/json'
  226. },
  227. method: 'GET',
  228. success(res) {
  229. uni.hideLoading();
  230. that.getTimeOrListItem1();
  231. // 这里是接口调用成功的回调,不是连接成功的回调,请注意
  232. },
  233. fail(err) {
  234. // 这里是接口调用失败的回调,不是连接失败的回调,请注意
  235. console.log("--------------" + JSON.stringify(err))
  236. }
  237. });
  238. uni.onSocketOpen((res) => {
  239. that.connecting = false
  240. that.connected = true
  241. uni.hideLoading()
  242. // uni.showToast({
  243. // icon: 'none',
  244. // title: '连接成功'
  245. // })
  246. console.log('onOpen', res);
  247. })
  248. uni.onSocketError((err) => {
  249. that.connecting = false
  250. that.connected = false
  251. uni.hideLoading()
  252. uni.showModal({
  253. content: '网络较差,请稍后再试',
  254. showCancel: false
  255. })
  256. console.log('onError', err);
  257. })
  258. uni.onSocketMessage((res) => {
  259. // let that = this;
  260. // let datas = JSON.parse(res.data)
  261. // let data = {
  262. // chat: {
  263. // userHead: '../../../static/logo2.png'
  264. // },
  265. // content: datas.content,
  266. // type: datas.type,
  267. // sendType: datas.sendType
  268. // }
  269. // that.ListItem.push(data);
  270. this.getTimeOrListItem1();
  271. console.log('onMessage', res)
  272. })
  273. uni.onSocketClose((res) => {
  274. that.connected = false
  275. that.startRecive = false
  276. that.msg = false
  277. console.log('onClose', res)
  278. });
  279. },
  280. close() {
  281. uni.closeSocket()
  282. },
  283. getTimeOrListItem1() {
  284. this.$Request.get('/app/ordersChat/selectGameChatDetails?page=1&limit=1000&ordersId=' + this.orderId+'&type=2')
  285. .then(
  286. res => {
  287. this.ListItem = [];
  288. if (res.data) {
  289. var time = '';
  290. res.data.list.forEach(d => {
  291. if (!d.avatar) {
  292. // d.chat.userHead = '../../static/logo.png';
  293. let avatar = this.$queue.getData('avatar');
  294. d.avatar = avatar
  295. }
  296. this.ListItem.push(d);
  297. });
  298. this.ListItem = this.ListItem.reverse();
  299. setTimeout(() => {
  300. uni.pageScrollTo({
  301. scrollTop: 99999,
  302. duration: 0
  303. });
  304. }, 50);
  305. }
  306. uni.hideLoading();
  307. });
  308. },
  309. getChatSave() {
  310. let userId = this.$queue.getData('userId');
  311. let phone = this.$queue.getData('phone');
  312. let userName = this.$queue.getData('userName');
  313. if (!phone) {
  314. phone = this.$queue.getData('userName');
  315. }
  316. let avatar = this.$queue.getData('avatar');
  317. let data = {
  318. userId: userId,
  319. userHead: avatar,
  320. userName: userName,
  321. storeId: '0',
  322. storeHead: '省钱兄陪玩',
  323. storeName: ''
  324. }
  325. this.$Request.postJson('/chat/save', data).then(res => {
  326. if (res.status === 0) {
  327. this.chatId = res.data.chatId;
  328. uni.showLoading({
  329. title: '加载中...'
  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 userId = this.$queue.getData('userId');
  346. let avatar = this.$queue.getData('avatar');
  347. let phone = this.$queue.getData('phone');
  348. let userName = this.$queue.getData('userName');
  349. if (!phone) {
  350. phone = this.$queue.getData('userName');
  351. }
  352. console.log(this.byUserId)
  353. let data = {
  354. content: this.content,
  355. messageType: type,
  356. riderId: this.userId,
  357. chatConversationId: this.chatConversationId,
  358. ordersId: this.orderId
  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>