im.vue 14 KB

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