im.vue 14 KB

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