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 src="/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/image/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 src="/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. export default {
  64. data() {
  65. return {
  66. liaotian: '',
  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. };
  102. },
  103. computed: {
  104. showMsg() {
  105. if (this.connected) {
  106. if (this.msg) {
  107. return '收到消息:' + this.msg
  108. } else {
  109. return '等待接收消息'
  110. }
  111. } else {
  112. return '尚未连接'
  113. }
  114. }
  115. },
  116. onUnload() {
  117. uni.closeSocket()
  118. uni.hideLoading()
  119. },
  120. onLoad(d) {
  121. this.userId = this.$queue.getData('userId');
  122. this.byUserId = d.byUserId
  123. this.chatConversationId = d.chatConversationId;
  124. this.connect();
  125. if (d.byNickName) {
  126. uni.setNavigationBarTitle({
  127. title: d.byNickName
  128. });
  129. }
  130. //聊天公告 814
  131. this.$Request.getT('/app/common/type/814').then(res => {
  132. if (res.code == 0) {
  133. if (res.data && res.data.value) {
  134. this.liaotian = res.data.value
  135. }
  136. }
  137. });
  138. },
  139. onShow() {
  140. if (this.connected || this.connecting) {
  141. } else {
  142. this.connect();
  143. }
  144. },
  145. onHide() {
  146. uni.closeSocket()
  147. },
  148. methods: {
  149. copy(content) {
  150. uni.showModal({
  151. title: '温馨提示',
  152. content: '确认要复制此文字吗?',
  153. showCancel: true,
  154. cancelText: '取消',
  155. confirmText: '确认',
  156. success: res => {
  157. if (res.confirm) {
  158. uni.setClipboardData({
  159. data: content,
  160. success: r => {
  161. this.$queue.showToast('复制成功');
  162. }
  163. });
  164. }
  165. }
  166. });
  167. },
  168. getDateDiff(data) {
  169. // 传进来的data必须是日期格式,不能是时间戳
  170. //var str = data;
  171. //将字符串转换成时间格式
  172. var timePublish = new Date(data);
  173. var timeNow = new Date();
  174. var minute = 1000 * 60;
  175. var hour = minute * 60;
  176. var day = hour * 24;
  177. var month = day * 30;
  178. var result = "2";
  179. var diffValue = timeNow - timePublish;
  180. var diffMonth = diffValue / month;
  181. var diffWeek = diffValue / (7 * day);
  182. var diffDay = diffValue / day;
  183. var diffHour = diffValue / hour;
  184. var diffMinute = diffValue / minute;
  185. if (diffMonth > 3) {
  186. result = timePublish.getFullYear() + "-";
  187. result += timePublish.getMonth() + "-";
  188. result += timePublish.getDate();
  189. } else if (diffMonth > 1) { //月
  190. result = data.substring(0, 10);
  191. } else if (diffWeek > 1) { //周
  192. result = data.substring(0, 10);
  193. } else if (diffDay > 1) { //天
  194. result = data.substring(0, 10);
  195. } else if (diffHour > 1) { //小时
  196. result = parseInt(diffHour) + "小时前";
  197. } else if (diffMinute > 1) { //分钟
  198. result = parseInt(diffMinute) + "分钟前";
  199. } else {
  200. result = "刚刚";
  201. }
  202. return result;
  203. },
  204. connect() {
  205. let that = this;
  206. let userId = that.$queue.getData('userId');
  207. if (that.connected || that.connecting) {
  208. uni.showModal({
  209. content: '正在连接或者已经连接,请勿重复连接',
  210. showCancel: false
  211. })
  212. return false
  213. }
  214. that.connecting = true
  215. uni.showLoading({
  216. title: '连接中...'
  217. })
  218. console.log(userId, '*******************')
  219. uni.connectSocket({
  220. // url: 'ws://192.168.1.17:8881/gameTeamChat/' + userId + '_' + this.teamId,
  221. // url: 'wss://game.shengqianxiong.com.cn/wss/gameTeamChat/' + userId + '_' + this.teamId,
  222. // url: 'ws://192.168.1.17:8180/sqx_fast/chatSocket/' + userId,
  223. url: this.config("WSHOST") + userId,
  224. data() {
  225. return {
  226. msg: 'Hello'
  227. }
  228. },
  229. header: {
  230. 'content-type': 'application/json'
  231. },
  232. method: 'GET',
  233. success(res) {
  234. uni.hideLoading();
  235. that.getTimeOrListItem1();
  236. // 这里是接口调用成功的回调,不是连接成功的回调,请注意
  237. },
  238. fail(err) {
  239. // 这里是接口调用失败的回调,不是连接失败的回调,请注意
  240. console.log("--------------" + JSON.stringify(err))
  241. }
  242. });
  243. uni.onSocketOpen((res) => {
  244. that.connecting = false
  245. that.connected = true
  246. uni.hideLoading()
  247. // uni.showToast({
  248. // icon: 'none',
  249. // title: '连接成功'
  250. // })
  251. console.log('onOpen', res);
  252. });
  253. uni.onSocketError((err) => {
  254. that.connecting = false
  255. that.connected = false
  256. uni.hideLoading()
  257. uni.showModal({
  258. content: '网络较差,请稍后再试',
  259. showCancel: false
  260. })
  261. console.log('onError', err);
  262. });
  263. uni.onSocketMessage(function(res) {
  264. console.log('收到服务器内容:' + JSON.stringify(res));
  265. setTimeout(() => {
  266. that.getTimeOrListItem1();
  267. }, 50);
  268. });
  269. uni.onSocketClose((res) => {
  270. that.connected = false
  271. that.startRecive = false
  272. that.msg = false
  273. console.log('onClose', res)
  274. });
  275. },
  276. close() {
  277. uni.closeSocket()
  278. },
  279. getTimeOrListItem1() {
  280. this.$Request.getT('/app/chat/selectChatContent?page=1&limit=1000&chatConversationId=' + this
  281. .chatConversationId).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('mobile');
  308. let userName = this.$queue.getData('nickName');
  309. if (!phone) {
  310. phone = this.$queue.getData('nickName');
  311. }
  312. let avatar = this.$queue.getData('avatar');
  313. let data = {
  314. userId: this.byUserId,
  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. });
  327. this.getTimeOrListItem1();
  328. }
  329. });
  330. },
  331. setChatSave(type) {
  332. //type:1文字 2图片
  333. if (type === 1 && this.content == '') {
  334. this.$queue.showToast('请输入聊天内容');
  335. return;
  336. }
  337. // if (this.$queue.getChatSearchKeys(this.content)) {
  338. // uni.showToast({
  339. // title: "输入内容带有非法关键字请重新输入",
  340. // mask: false,
  341. // duration: 1500,
  342. // icon: "none"
  343. // });
  344. // return;
  345. // }
  346. // if (this.chatId == '' || this.chatId == undefined) {
  347. // this.$queue.showToast('网络较差,请稍后再试');
  348. // return;
  349. // }
  350. let userId = this.$queue.getData('userId');
  351. let avatar = this.$queue.getData('avatar');
  352. let phone = this.$queue.getData('phone');
  353. let userName = this.$queue.getData('userName');
  354. if (!phone) {
  355. phone = this.$queue.getData('userName');
  356. }
  357. console.log(this.byUserId)
  358. let data = {
  359. content: this.content,
  360. messageType: type,
  361. userId: this.byUserId,
  362. chatConversationId: this.chatConversationId,
  363. }
  364. data = JSON.stringify(data);
  365. let that = this;
  366. uni.sendSocketMessage({
  367. data: data,
  368. success(res) {
  369. let avatar = that.$queue.getData('avatar');
  370. if (!avatar) {
  371. avatar = '../../static/logo.png';
  372. }
  373. let data = {
  374. chat: {
  375. userHead: avatar
  376. },
  377. content: that.content,
  378. type: type,
  379. userId: userId
  380. }
  381. console.log(data, 'data99999999999999999')
  382. // that.ListItem.push(data);
  383. setTimeout(() => {
  384. that.getTimeOrListItem1();
  385. }, 50);
  386. console.log(that.content);
  387. },
  388. fail(err) {
  389. console.log(err);
  390. }
  391. })
  392. this.content = '';
  393. },
  394. //发送图片
  395. chooseImage() {
  396. uni.chooseImage({
  397. count: 1,
  398. sourceType: ['album', 'camera'],
  399. success: res => {
  400. for (let i = 0; i < res.tempFilePaths.length; i++) {
  401. this.$queue.showLoading("上传中...");
  402. uni.uploadFile({ // 上传接口
  403. // url: this.config("APIHOST1") + '/alioss/upload', //真实的接口地址
  404. url: 'https://shunfengche.xianmxkj.com/sqx_fast/alioss/upload', //真实的接口地址
  405. filePath: res.tempFilePaths[i],
  406. name: 'file',
  407. success: (uploadFileRes) => {
  408. console.log(uploadFileRes)
  409. this.content = JSON.parse(uploadFileRes.data).data;
  410. this.setChatSave(2);
  411. uni.hideLoading();
  412. }
  413. });
  414. }
  415. }
  416. })
  417. },
  418. config: function(name) {
  419. var info = null;
  420. if (name) {
  421. var name2 = name.split("."); //字符分割
  422. if (name2.length > 1) {
  423. info = configdata[name2[0]][name2[1]] || null;
  424. } else {
  425. info = configdata[name] || null;
  426. }
  427. if (info == null) {
  428. let web_config = cache.get("web_config");
  429. if (web_config) {
  430. if (name2.length > 1) {
  431. info = web_config[name2[0]][name2[1]] || null;
  432. } else {
  433. info = web_config[name] || null;
  434. }
  435. }
  436. }
  437. }
  438. return info;
  439. },
  440. //查看大图
  441. viewImg(item) {
  442. let imgsArray = [];
  443. imgsArray[0] = item;
  444. uni.previewImage({
  445. current: 0,
  446. urls: imgsArray
  447. });
  448. },
  449. },
  450. };
  451. </script>
  452. <style lang="scss">
  453. @import "./css/style.scss";
  454. page {
  455. background-color: #F7F7F7;
  456. }
  457. .bg {
  458. background-color: #FFFFFF;
  459. }
  460. .input-box {
  461. position: fixed;
  462. bottom: 0;
  463. left: 0;
  464. height: 120rpx;
  465. width: 100%;
  466. display: flex;
  467. box-sizing: content-box;
  468. z-index: 999;
  469. /* background-color: #ececec; */
  470. /* padding: 0 5rpx; */
  471. }
  472. .chat-listitem {
  473. display: flex;
  474. margin-top: 20rpx;
  475. padding: 10rpx;
  476. }
  477. .chat-listitem-text {
  478. color: #FFFFFF;
  479. background: #557EFD;
  480. margin-top: 10rpx;
  481. width: fit-content;
  482. padding: 15rpx;
  483. font-size: 30rpx;
  484. height: max-content;
  485. word-wrap: break-word;
  486. word-break: break-all;
  487. border-radius: 10rpx;
  488. }
  489. .chat-listitem-text1 {
  490. /* color: #FFFFFF; */
  491. background: #FFFFFF;
  492. margin-top: 10rpx;
  493. width: fit-content;
  494. padding: 15rpx;
  495. font-size: 30rpx;
  496. height: max-content;
  497. word-wrap: break-word;
  498. word-break: break-all;
  499. border-radius: 10rpx;
  500. }
  501. .chat-listitem-image-type4 {
  502. /* color: #FFFFFF; */
  503. background: #FFFFFF;
  504. width: fit-content;
  505. font-size: 30rpx;
  506. height: max-content;
  507. word-wrap: break-word;
  508. word-break: break-all;
  509. border-top-left-radius: 20rpx;
  510. border-top-right-radius: 20rpx;
  511. }
  512. .chat-listitem-image {
  513. margin-top: 5rpx;
  514. width: 75rpx;
  515. height: 75rpx;
  516. border-radius: 5rpx;
  517. }
  518. .save {
  519. width: 130rpx;
  520. text-align: center;
  521. border-radius: 70rpx;
  522. height: 70rpx;
  523. color: #FFF;
  524. background: #557EFD;
  525. margin: 5rpx 10rpx 0;
  526. line-height: 70rpx;
  527. }
  528. </style>