im.vue 14 KB

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