im.vue 14 KB

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