inquiry.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <template>
  2. <view class="">
  3. <view class="navBox">
  4. <view class="status_bar" :style="{height: iStatusBarHeight + 'px'}"></view>
  5. <view class="nav">
  6. <view class="navLeft" >
  7. <!-- <image src="../static/img/nav_icon_back.png" mode="aspectFit" class="backImg" @click="goback"></image> -->
  8. </view>
  9. <view class="title">询价信息</view>
  10. <view class="goVin" @click="vinScanFn">车架号查询</view>
  11. </view>
  12. </view>
  13. <view class="status_bar" :style="{height: iStatusBarHeight + 'px'}"></view>
  14. <view style="height: 44px;"></view>
  15. <scroll-view class="conversations" scroll-y="true">
  16. <view @click="setClipboardData">token:{{token}}</view>
  17. <view>
  18. <!-- <video src="https://66-goesay.oss-accelerate.aliyuncs.com/goeasy-im-dmsIMTest/3369051043664274_uni-video.mp4" id="videoPlayer"
  19. ></video> -->
  20. </view>
  21. <view v-if="conversations.length > 0">
  22. <view class="scroll-item" v-for="(conversation, key) in conversations" :key="key">
  23. <view class="item-head">
  24. <!-- <image :src="conversation.data.avatar" class="head-icon"></image> -->
  25. <image src="/static/img/icon_me.png" class="head-icon"></image>
  26. <view class="item-head_unread" v-if="conversation.unread">{{ conversation.unread }}</view>
  27. </view>
  28. <view class="scroll-item_info" @click="chat(conversation)">
  29. <view class="item-info-top">
  30. <text class="item-info-top_name">{{ conversation.data.name }}</text>
  31. <view class="item-info-top_time">{{ formatDate(conversation.lastMessage.timestamp) }}</view>
  32. </view>
  33. <view class="item-info-bottom">
  34. <view class="item-info-bottom-item">
  35. <view class="item-info-top_content" v-if="!conversation.lastMessage.recalled">
  36. <text class="unread-text">
  37. {{ conversation.lastMessage.read === false && conversation.lastMessage.senderId === currentUser.id ? '[未读]' : '' }}
  38. </text>
  39. <text v-if="conversation.lastMessage.senderId === currentUser.id">我: </text>
  40. <text v-else>{{ conversation.type === 'group' ? conversation.lastMessage.senderData.name : conversation.data.name }}: </text>
  41. <text v-if="conversation.lastMessage.type === 'text'">{{ conversation.lastMessage.payload.text }}</text>
  42. <text v-else-if="conversation.lastMessage.type === 'video'">[视频消息]</text>
  43. <text v-else-if="conversation.lastMessage.type === 'audio'">[语音消息]</text>
  44. <text v-else-if="conversation.lastMessage.type === 'image'">[图片消息]</text>
  45. <text v-else-if="conversation.lastMessage.type === 'file'">[文件消息]</text>
  46. <text v-else-if="conversation.lastMessage.type === 'order'">[自定义消息:订单]</text>
  47. <text v-else-if="conversation.lastMessage.type === 'pic'">[图片消息]</text>
  48. <text v-else-if="conversation.lastMessage.type === 'car'">[车辆信息]</text>
  49. <text v-else>[[未识别内容]]</text>
  50. </view>
  51. <view class="item-info-top_content" v-else>
  52. <text>
  53. {{conversation.lastMessage.recaller.id === currentUser.id ? '你' : conversation.lastMessage.recaller.data.name}}撤回了一条消息
  54. </text>
  55. </view>
  56. <view class="item-info-bottom_action" @click.stop="showAction(conversation)"></view>
  57. </view>
  58. </view>
  59. </view>
  60. </view>
  61. </view>
  62. <view class="no-conversation" v-else>当前没有会话</view>
  63. <view class="action-container" v-if="actionPopup.visible">
  64. <view class="layer" @click="actionPopup.visible = false"></view>
  65. <view class="action-box">
  66. <view class="action-item" @click="topConversation">
  67. {{ actionPopup.conversation.top ? '取消置顶' : '置顶聊天' }}
  68. </view>
  69. <view class="action-item" @click="deleteConversation">删除聊天</view>
  70. </view>
  71. </view>
  72. </scroll-view>
  73. </view>
  74. </template>
  75. <script>
  76. import {formatDate} from '../lib/utils';
  77. import restApi from '../lib/restapi';
  78. const GoEasy = uni.$GoEasy;
  79. const GRTC = uni.$GRTC;
  80. export default {
  81. name: 'conversation',
  82. data() {
  83. return {
  84. conversations: [],
  85. actionPopup: {
  86. conversation: null,
  87. visible: false
  88. },
  89. currentUser: null,
  90. iStatusBarHeight:'',
  91. token:'',
  92. }
  93. },
  94. onLoad(opt) {
  95. this.token=opt.token
  96. uni.setStorageSync('token', this.token);
  97. this.iStatusBarHeight = uni.getSystemInfoSync().statusBarHeight;
  98. this.imLoginUrl()
  99. },
  100. onShow() {
  101. if(this.token){
  102. this.imLoginUrl()
  103. }
  104. /* uni.$currentUser = uni.getStorageSync('currentUser');
  105. this.currentUser = uni.$currentUser;
  106. console.log(this.currentUser)
  107. if (!this.currentUser) {
  108. uni.navigateTo({ url: './login' });
  109. return;
  110. }
  111. if (GoEasy.getConnectionStatus() === 'disconnected') {
  112. this.connectGoEasy(); //连接goeasy
  113. this.subscribeGroup(); //建立连接后,就应该订阅群聊消息,避免漏掉
  114. }
  115. this.loadConversations(); //加载会话列表
  116. this.initGoEasyListeners(); */
  117. },
  118. onHide() {
  119. if (GoEasy.getConnectionStatus() === 'disconnected') {
  120. return
  121. }
  122. GoEasy.im.off(GoEasy.IM_EVENT.CONVERSATIONS_UPDATED, this.renderConversations);
  123. },
  124. methods: {
  125. formatDate,
  126. connectGoEasy() {
  127. uni.showLoading();
  128. GoEasy.connect({
  129. id: this.currentUser.id,
  130. data: {
  131. name: this.currentUser.name,
  132. avatar: this.currentUser.avatar
  133. },
  134. onSuccess: () => {
  135. console.log('GoEasy connect successfully.')
  136. },
  137. onFailed: (error) => {
  138. console.log('Failed to connect GoEasy, code:' + error.code + ',error:' + error.content);
  139. },
  140. onProgress: (attempts) => {
  141. console.log('GoEasy is connecting', attempts);
  142. }
  143. });
  144. },
  145. setClipboardData(){
  146. uni.setClipboardData({
  147. data: this.token,
  148. success: () => {
  149. uni.showToast({
  150. title: '复制成功'
  151. });
  152. }
  153. });
  154. },
  155. chooseImage(){
  156. uni.chooseImage({
  157. count: 6, //默认9
  158. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  159. sourceType: ['album'], //从相册选择
  160. success: function (res) {
  161. console.log(JSON.stringify(res.tempFilePaths));
  162. },
  163. fail(err) {
  164. }
  165. });
  166. },
  167. imLoginUrl(){
  168. this.$http('imSys/imLoginUrl', {
  169. token:this.token,
  170. },'POST').then(res => {
  171. // console.log(res)
  172. var users={
  173. userId:res.data.userId,
  174. name:res.data.users.name,
  175. avatar:'',
  176. id:res.data.userId,
  177. }
  178. this.currentUser=users
  179. // this.connectGoEasy()
  180. uni.setStorageSync('users', users);
  181. uni.setStorageSync('currentUser', users);
  182. console.log(this.currentUser)
  183. if (!this.currentUser) {
  184. uni.navigateTo({ url: './login' });
  185. return;
  186. }
  187. if (GoEasy.getConnectionStatus() === 'disconnected') {
  188. this.connectGoEasy(); //连接goeasy
  189. this.subscribeGroup(); //建立连接后,就应该订阅群聊消息,避免漏掉
  190. }
  191. this.loadConversations(); //加载会话列表
  192. this.initGoEasyListeners();
  193. })
  194. },
  195. goBack(){
  196. //app交互
  197. var standalone = window.navigator.standalone
  198. var userAgent = window.navigator.userAgent.toLowerCase()
  199. var safari = /safari/.test(userAgent)
  200. var ios = /iphone|ipod|ipad|mac/.test(userAgent)
  201. var android = /android/.test(userAgent)
  202. if (ios) {
  203. if ( true) {//!standalone&& !safari
  204. window.webkit.messageHandlers.goMyNav.postMessage(null)
  205. }
  206. } else if (android) {
  207. window.android.postMessage()
  208. }
  209. },
  210. vinScanFn(){
  211. //app交互
  212. var standalone = window.navigator.standalone
  213. var userAgent = window.navigator.userAgent.toLowerCase()
  214. var safari = /safari/.test(userAgent)
  215. var ios = /iphone|ipod|ipad|mac/.test(userAgent)
  216. var android = /android/.test(userAgent)
  217. if (ios) {
  218. if ( true) {//!standalone&& !safari
  219. window.webkit.messageHandlers.goMyNav.vinScan(null)
  220. }
  221. } else if (android) {
  222. window.android.vinScan()
  223. }
  224. },
  225. initGoEasyListeners() {
  226. GoEasy.im.on(GoEasy.IM_EVENT.CONVERSATIONS_UPDATED, this.renderConversations); //监听会话列表变化
  227. GoEasy.im.off(GoEasy.IM_EVENT.CONVERSATIONS_UPDATED, this.setUnreadAmount); // 移除之前的设置角标回调,防止重复回调
  228. GoEasy.im.on(GoEasy.IM_EVENT.CONVERSATIONS_UPDATED, this.setUnreadAmount); // 设置角标
  229. // #ifdef APP-PLUS || H5
  230. GRTC.off(GRTC.EVENT.RING, this.onRing); //移除之前的监听来电事件,防止重复回调
  231. GRTC.on(GRTC.EVENT.RING, this.onRing); //监听来电事件
  232. // #endif
  233. },
  234. onRing() {
  235. const currentCall = GRTC.currentCall();
  236. if (currentCall.groupId) {
  237. uni.navigateTo({
  238. url: `./rtc/group/ring`,
  239. })
  240. } else {
  241. uni.navigateTo({
  242. url: `./rtc/private/ring`,
  243. })
  244. }
  245. },
  246. // 加载最新的会话列表
  247. loadConversations() {
  248. GoEasy.im.latestConversations({
  249. onSuccess: (result) => {
  250. uni.hideLoading();
  251. let content = result.content;
  252. this.renderConversations(content);
  253. this.setUnreadAmount(content);
  254. },
  255. onFailed: (error) => {
  256. uni.hideLoading();
  257. console.log('获取最新会话列表失败, error:', error);
  258. }
  259. });
  260. },
  261. renderConversations(content) {
  262. this.conversations = content.conversations;
  263. console.log("消息列表")
  264. console.log(this.conversations)
  265. },
  266. setUnreadAmount(content) {
  267. const unreadTotal = content.unreadTotal;
  268. if(unreadTotal > 0) {
  269. uni.setTabBarBadge({
  270. index: 0,
  271. text: unreadTotal.toString()
  272. });
  273. }else{
  274. uni.removeTabBarBadge({index: 0});
  275. }
  276. // #ifdef APP-PLUS
  277. GoEasy.setBadge({
  278. badge: unreadTotal,
  279. onSuccess: function () {
  280. console.log("setBadge successfully.")
  281. },
  282. onFailed: function (error) {
  283. console.log("Failed to setBadge,error:" + error);
  284. }
  285. });
  286. // #endif
  287. },
  288. subscribeGroup() {
  289. let groups = restApi.findGroups(this.currentUser);
  290. let groupIds = groups.map(item => item.id);
  291. GoEasy.im.subscribeGroup({
  292. groupIds: groupIds,
  293. onSuccess: function () {
  294. console.log('订阅群消息成功');
  295. },
  296. onFailed: function (error) {
  297. console.log('订阅群消息失败:', error);
  298. }
  299. });
  300. },
  301. topConversation() { //会话置顶
  302. this.actionPopup.visible = false;
  303. let conversation = this.actionPopup.conversation;
  304. let description = conversation.top ? '取消置顶' : '置顶';
  305. GoEasy.im.topConversation({
  306. conversation: conversation,
  307. top: !conversation.top,
  308. onSuccess: function () {
  309. uni.showToast({
  310. title: description + '成功',
  311. icon: 'none'
  312. });
  313. },
  314. onFailed: function (error) {
  315. console.log(description, '失败:', error);
  316. }
  317. });
  318. },
  319. deleteConversation() {
  320. uni.showModal({
  321. content: '确认删除这条会话吗?',
  322. success: (res) => {
  323. if (res.confirm) {
  324. let conversation = this.actionPopup.conversation;
  325. this.actionPopup.visible = false;
  326. GoEasy.im.removeConversation({
  327. conversation: conversation,
  328. onSuccess: function () {
  329. console.log('删除会话成功');
  330. },
  331. onFailed: function (error) {
  332. console.log(error);
  333. },
  334. });
  335. } else {
  336. this.actionPopup.visible = false;
  337. }
  338. },
  339. })
  340. },
  341. chat(conversation) {
  342. console.log(conversation)
  343. let path = conversation.type === GoEasy.IM_SCENE.PRIVATE ?
  344. './privateChat?to=' + conversation.userId :
  345. './groupChat?to=' + conversation.groupId;
  346. var friend={
  347. id:conversation.userId,
  348. supplierName:conversation.data.name,
  349. avatar:conversation.data.avatar,
  350. }
  351. uni.setStorage({
  352. key: 'friend',
  353. data: friend,
  354. success: function () {
  355. /* uni.navigateTo({
  356. url: './privateChat?to=' + friend.ID
  357. }); */
  358. uni.navigateTo({ url: path });
  359. }
  360. });
  361. },
  362. showAction(conversation) {
  363. this.actionPopup.conversation = conversation;
  364. this.actionPopup.visible = true;
  365. }
  366. }
  367. }
  368. </script>
  369. <style>
  370. page {
  371. height: 100%;
  372. }
  373. .conversations {
  374. width: 750rpx;
  375. overflow-x: hidden;
  376. display: flex;
  377. flex-direction: column;
  378. height: 100%;
  379. }
  380. .conversations .scroll-item {
  381. height: 152rpx;
  382. display: flex;
  383. align-items: center;
  384. padding-left: 32rpx;
  385. }
  386. .conversations .scroll-item .head-icon {
  387. width: 100rpx;
  388. height: 100rpx;
  389. margin-right: 28rpx;
  390. }
  391. .conversations .scroll-item_info {
  392. height: 151rpx;
  393. width: 590rpx;
  394. padding-right: 32rpx;
  395. border-bottom: 1px solid #EFEFEF;
  396. }
  397. .conversations .scroll-item_info .item-info-top {
  398. padding-top: 20rpx;
  399. /* height: 60rpx;
  400. line-height: 60rpx; */
  401. display: flex;
  402. align-items: center;
  403. justify-content: space-between;
  404. }
  405. .conversations .item-info-top_name {
  406. font-size: 30rpx;
  407. color: #262628;
  408. }
  409. .conversations .item-info-top_time {
  410. font-size: 34rpx;
  411. color: rgba(179, 179, 179, 0.8);
  412. }
  413. .conversations .item-info-bottom {
  414. height: 40rpx;
  415. line-height: 40rpx;
  416. overflow: hidden;
  417. }
  418. .conversations .item-info-bottom-item {
  419. display: flex;
  420. justify-content: space-between;
  421. }
  422. .item-info-bottom .item-info-top_content {
  423. font-size: 34rpx;
  424. color: #b3b3b3;
  425. overflow: hidden;
  426. text-overflow: ellipsis;
  427. white-space: nowrap;
  428. }
  429. .item-info-bottom .item-info-bottom_action {
  430. width: 50rpx;
  431. height: 50rpx;
  432. font-size: 34rpx;
  433. background: url("../static/images/action.png") no-repeat center;
  434. background-size: 28rpx 30rpx;
  435. }
  436. .no-conversation {
  437. width: 100%;
  438. text-align: center;
  439. height: 80rpx;
  440. line-height: 80rpx;
  441. font-size: 34rpx;
  442. color: #9D9D9D;
  443. }
  444. .item-head {
  445. position: relative;
  446. }
  447. .item-head .item-head_unread {
  448. padding: 6rpx;
  449. background-color: #EE593C;
  450. color: #FFFFFF;
  451. font-size: 34rpx;
  452. line-height: 28rpx;
  453. border-radius: 24rpx;
  454. min-width: 24rpx;
  455. min-height: 24rpx;
  456. text-align: center;
  457. position: absolute;
  458. top: 0;
  459. right: 15rpx;
  460. }
  461. .action-container {
  462. width: 100%;
  463. height: 100%;
  464. position: fixed;
  465. top: 0;
  466. left: 0;
  467. display: flex;
  468. justify-content: center;
  469. align-items: center;
  470. }
  471. .action-container .layer {
  472. position: absolute;
  473. top: 0;
  474. left: 0;
  475. background: rgba(51, 51, 51, 0.5);
  476. width: 100%;
  477. height: 100%;
  478. z-index: 99;
  479. }
  480. .action-box {
  481. width: 400rpx;
  482. height: 240rpx;
  483. background: #ffffff;
  484. position: relative;
  485. z-index: 100;
  486. border-radius: 20rpx;
  487. overflow: hidden;
  488. }
  489. .action-item {
  490. text-align: center;
  491. line-height: 120rpx;
  492. font-size: 34rpx;
  493. color: #262628;
  494. border-bottom: 1px solid #EFEFEF;
  495. }
  496. .unread-text {
  497. color: #d02129;
  498. }
  499. .backImg{
  500. width: 44rpx;
  501. height: 44rpx;
  502. /* margin-left: 10rpx; */
  503. /* margin-right: 20rpx; */
  504. }
  505. .nav{
  506. height: 44px;
  507. display: flex;
  508. justify-content: space-between;
  509. align-items: center;
  510. border-bottom: 1px solid #eaeaea;
  511. }
  512. .goVin{
  513. width: 200rpx;
  514. color: #3F90F7;
  515. text-align: right;
  516. padding-right: 20rpx;
  517. }
  518. .navLeft{
  519. width: 200rpx;
  520. padding-left: 20rpx;
  521. }
  522. .navBox{
  523. position: fixed;
  524. left: 0;
  525. top: 0;
  526. z-index: 11;
  527. width: 100vw;
  528. }
  529. </style>