inquiry.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <template>
  2. <scroll-view class="conversations" scroll-y="true">
  3. <view v-if="conversations.length > 0">
  4. <view class="scroll-item" v-for="(conversation, key) in conversations" :key="key">
  5. <view class="item-head">
  6. <image :src="conversation.data.avatar" class="head-icon"></image>
  7. <view class="item-head_unread" v-if="conversation.unread">{{ conversation.unread }}</view>
  8. </view>
  9. <view class="scroll-item_info" @click="chat(conversation)">
  10. <view class="item-info-top">
  11. <text class="item-info-top_name">{{ conversation.data.name }}</text>
  12. <view class="item-info-top_time">{{ formatDate(conversation.lastMessage.timestamp) }}</view>
  13. </view>
  14. <view class="item-info-bottom">
  15. <view class="item-info-bottom-item">
  16. <view class="item-info-top_content" v-if="!conversation.lastMessage.recalled">
  17. <text class="unread-text">
  18. {{ conversation.lastMessage.read === false && conversation.lastMessage.senderId === currentUser.id ? '[未读]' : '' }}
  19. </text>
  20. <text v-if="conversation.lastMessage.senderId === currentUser.id">我: </text>
  21. <text v-else>{{ conversation.type === 'group' ? conversation.lastMessage.senderData.name : conversation.data.name }}: </text>
  22. <text v-if="conversation.lastMessage.type === 'text'">{{ conversation.lastMessage.payload.text }}</text>
  23. <text v-else-if="conversation.lastMessage.type === 'video'">[视频消息]</text>
  24. <text v-else-if="conversation.lastMessage.type === 'audio'">[语音消息]</text>
  25. <text v-else-if="conversation.lastMessage.type === 'image'">[图片消息]</text>
  26. <text v-else-if="conversation.lastMessage.type === 'file'">[文件消息]</text>
  27. <text v-else-if="conversation.lastMessage.type === 'order'">[自定义消息:订单]</text>
  28. <text v-else-if="conversation.lastMessage.type === 'pic'">[图片消息]</text>
  29. <text v-else>[[未识别内容]]</text>
  30. </view>
  31. <view class="item-info-top_content" v-else>
  32. <text>
  33. {{conversation.lastMessage.recaller.id === currentUser.id ? '你' : conversation.lastMessage.recaller.data.name}}撤回了一条消息
  34. </text>
  35. </view>
  36. <view class="item-info-bottom_action" @click.stop="showAction(conversation)"></view>
  37. </view>
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. <view class="no-conversation" v-else>当前没有会话</view>
  43. <view class="action-container" v-if="actionPopup.visible">
  44. <view class="layer" @click="actionPopup.visible = false"></view>
  45. <view class="action-box">
  46. <view class="action-item" @click="topConversation">
  47. {{ actionPopup.conversation.top ? '取消置顶' : '置顶聊天' }}
  48. </view>
  49. <view class="action-item" @click="deleteConversation">删除聊天</view>
  50. </view>
  51. </view>
  52. </scroll-view>
  53. </template>
  54. <script>
  55. import {formatDate} from '../lib/utils';
  56. import restApi from '../lib/restapi';
  57. const GoEasy = uni.$GoEasy;
  58. const GRTC = uni.$GRTC;
  59. export default {
  60. name: 'conversation',
  61. data() {
  62. return {
  63. conversations: [],
  64. actionPopup: {
  65. conversation: null,
  66. visible: false
  67. },
  68. currentUser: null
  69. }
  70. },
  71. onShow() {
  72. uni.$currentUser = uni.getStorageSync('currentUser');
  73. this.currentUser = uni.$currentUser;
  74. console.log(this.currentUser)
  75. if (!this.currentUser) {
  76. uni.navigateTo({ url: './login' });
  77. return;
  78. }
  79. if (GoEasy.getConnectionStatus() === 'disconnected') {
  80. this.connectGoEasy(); //连接goeasy
  81. this.subscribeGroup(); //建立连接后,就应该订阅群聊消息,避免漏掉
  82. }
  83. this.loadConversations(); //加载会话列表
  84. this.initGoEasyListeners();
  85. },
  86. onHide() {
  87. if (GoEasy.getConnectionStatus() === 'disconnected') {
  88. return
  89. }
  90. GoEasy.im.off(GoEasy.IM_EVENT.CONVERSATIONS_UPDATED, this.renderConversations);
  91. },
  92. methods: {
  93. formatDate,
  94. connectGoEasy() {
  95. uni.showLoading();
  96. GoEasy.connect({
  97. id: this.currentUser.id,
  98. data: {
  99. name: this.currentUser.name,
  100. avatar: this.currentUser.avatar
  101. },
  102. onSuccess: () => {
  103. console.log('GoEasy connect successfully.')
  104. },
  105. onFailed: (error) => {
  106. console.log('Failed to connect GoEasy, code:' + error.code + ',error:' + error.content);
  107. },
  108. onProgress: (attempts) => {
  109. console.log('GoEasy is connecting', attempts);
  110. }
  111. });
  112. },
  113. initGoEasyListeners() {
  114. GoEasy.im.on(GoEasy.IM_EVENT.CONVERSATIONS_UPDATED, this.renderConversations); //监听会话列表变化
  115. GoEasy.im.off(GoEasy.IM_EVENT.CONVERSATIONS_UPDATED, this.setUnreadAmount); // 移除之前的设置角标回调,防止重复回调
  116. GoEasy.im.on(GoEasy.IM_EVENT.CONVERSATIONS_UPDATED, this.setUnreadAmount); // 设置角标
  117. // #ifdef APP-PLUS || H5
  118. GRTC.off(GRTC.EVENT.RING, this.onRing); //移除之前的监听来电事件,防止重复回调
  119. GRTC.on(GRTC.EVENT.RING, this.onRing); //监听来电事件
  120. // #endif
  121. },
  122. onRing() {
  123. const currentCall = GRTC.currentCall();
  124. if (currentCall.groupId) {
  125. uni.navigateTo({
  126. url: `./rtc/group/ring`,
  127. })
  128. } else {
  129. uni.navigateTo({
  130. url: `./rtc/private/ring`,
  131. })
  132. }
  133. },
  134. // 加载最新的会话列表
  135. loadConversations() {
  136. GoEasy.im.latestConversations({
  137. onSuccess: (result) => {
  138. uni.hideLoading();
  139. let content = result.content;
  140. this.renderConversations(content);
  141. this.setUnreadAmount(content);
  142. },
  143. onFailed: (error) => {
  144. uni.hideLoading();
  145. console.log('获取最新会话列表失败, error:', error);
  146. }
  147. });
  148. },
  149. renderConversations(content) {
  150. this.conversations = content.conversations;
  151. console.log("消息列表")
  152. console.log(this.conversations)
  153. },
  154. setUnreadAmount(content) {
  155. const unreadTotal = content.unreadTotal;
  156. if(unreadTotal > 0) {
  157. uni.setTabBarBadge({
  158. index: 0,
  159. text: unreadTotal.toString()
  160. });
  161. }else{
  162. uni.removeTabBarBadge({index: 0});
  163. }
  164. // #ifdef APP-PLUS
  165. GoEasy.setBadge({
  166. badge: unreadTotal,
  167. onSuccess: function () {
  168. console.log("setBadge successfully.")
  169. },
  170. onFailed: function (error) {
  171. console.log("Failed to setBadge,error:" + error);
  172. }
  173. });
  174. // #endif
  175. },
  176. subscribeGroup() {
  177. let groups = restApi.findGroups(this.currentUser);
  178. let groupIds = groups.map(item => item.id);
  179. GoEasy.im.subscribeGroup({
  180. groupIds: groupIds,
  181. onSuccess: function () {
  182. console.log('订阅群消息成功');
  183. },
  184. onFailed: function (error) {
  185. console.log('订阅群消息失败:', error);
  186. }
  187. });
  188. },
  189. topConversation() { //会话置顶
  190. this.actionPopup.visible = false;
  191. let conversation = this.actionPopup.conversation;
  192. let description = conversation.top ? '取消置顶' : '置顶';
  193. GoEasy.im.topConversation({
  194. conversation: conversation,
  195. top: !conversation.top,
  196. onSuccess: function () {
  197. uni.showToast({
  198. title: description + '成功',
  199. icon: 'none'
  200. });
  201. },
  202. onFailed: function (error) {
  203. console.log(description, '失败:', error);
  204. }
  205. });
  206. },
  207. deleteConversation() {
  208. uni.showModal({
  209. content: '确认删除这条会话吗?',
  210. success: (res) => {
  211. if (res.confirm) {
  212. let conversation = this.actionPopup.conversation;
  213. this.actionPopup.visible = false;
  214. GoEasy.im.removeConversation({
  215. conversation: conversation,
  216. onSuccess: function () {
  217. console.log('删除会话成功');
  218. },
  219. onFailed: function (error) {
  220. console.log(error);
  221. },
  222. });
  223. } else {
  224. this.actionPopup.visible = false;
  225. }
  226. },
  227. })
  228. },
  229. chat(conversation) {
  230. let path = conversation.type === GoEasy.IM_SCENE.PRIVATE ?
  231. './privateChat?to=' + conversation.userId :
  232. './groupChat?to=' + conversation.groupId;
  233. var friend={
  234. id:conversation.userId,
  235. supplierName:conversation.data.name,
  236. avatar:conversation.data.avatar,
  237. }
  238. uni.setStorage({
  239. key: 'friend',
  240. data: friend,
  241. success: function () {
  242. /* uni.navigateTo({
  243. url: './privateChat?to=' + friend.ID
  244. }); */
  245. uni.navigateTo({ url: path });
  246. }
  247. });
  248. },
  249. showAction(conversation) {
  250. this.actionPopup.conversation = conversation;
  251. this.actionPopup.visible = true;
  252. }
  253. }
  254. }
  255. </script>
  256. <style>
  257. page {
  258. height: 100%;
  259. }
  260. .conversations {
  261. width: 750rpx;
  262. overflow-x: hidden;
  263. display: flex;
  264. flex-direction: column;
  265. height: 100%;
  266. }
  267. .conversations .scroll-item {
  268. height: 152rpx;
  269. display: flex;
  270. align-items: center;
  271. padding-left: 32rpx;
  272. }
  273. .conversations .scroll-item .head-icon {
  274. width: 100rpx;
  275. height: 100rpx;
  276. margin-right: 28rpx;
  277. }
  278. .conversations .scroll-item_info {
  279. height: 151rpx;
  280. width: 590rpx;
  281. padding-right: 32rpx;
  282. border-bottom: 1px solid #EFEFEF;
  283. }
  284. .conversations .scroll-item_info .item-info-top {
  285. padding-top: 20rpx;
  286. height: 60rpx;
  287. line-height: 60rpx;
  288. display: flex;
  289. align-items: center;
  290. justify-content: space-between;
  291. }
  292. .conversations .item-info-top_name {
  293. font-size: 34rpx;
  294. color: #262628;
  295. }
  296. .conversations .item-info-top_time {
  297. font-size: 34rpx;
  298. color: rgba(179, 179, 179, 0.8);
  299. }
  300. .conversations .item-info-bottom {
  301. height: 40rpx;
  302. line-height: 40rpx;
  303. overflow: hidden;
  304. }
  305. .conversations .item-info-bottom-item {
  306. display: flex;
  307. justify-content: space-between;
  308. }
  309. .item-info-bottom .item-info-top_content {
  310. font-size: 34rpx;
  311. color: #b3b3b3;
  312. overflow: hidden;
  313. text-overflow: ellipsis;
  314. white-space: nowrap;
  315. }
  316. .item-info-bottom .item-info-bottom_action {
  317. width: 50rpx;
  318. height: 50rpx;
  319. font-size: 34rpx;
  320. background: url("../static/images/action.png") no-repeat center;
  321. background-size: 28rpx 30rpx;
  322. }
  323. .no-conversation {
  324. width: 100%;
  325. text-align: center;
  326. height: 80rpx;
  327. line-height: 80rpx;
  328. font-size: 34rpx;
  329. color: #9D9D9D;
  330. }
  331. .item-head {
  332. position: relative;
  333. }
  334. .item-head .item-head_unread {
  335. padding: 6rpx;
  336. background-color: #EE593C;
  337. color: #FFFFFF;
  338. font-size: 34rpx;
  339. line-height: 28rpx;
  340. border-radius: 24rpx;
  341. min-width: 24rpx;
  342. min-height: 24rpx;
  343. text-align: center;
  344. position: absolute;
  345. top: 0;
  346. right: 15rpx;
  347. }
  348. .action-container {
  349. width: 100%;
  350. height: 100%;
  351. position: fixed;
  352. top: 0;
  353. left: 0;
  354. display: flex;
  355. justify-content: center;
  356. align-items: center;
  357. }
  358. .action-container .layer {
  359. position: absolute;
  360. top: 0;
  361. left: 0;
  362. background: rgba(51, 51, 51, 0.5);
  363. width: 100%;
  364. height: 100%;
  365. z-index: 99;
  366. }
  367. .action-box {
  368. width: 400rpx;
  369. height: 240rpx;
  370. background: #ffffff;
  371. position: relative;
  372. z-index: 100;
  373. border-radius: 20rpx;
  374. overflow: hidden;
  375. }
  376. .action-item {
  377. text-align: center;
  378. line-height: 120rpx;
  379. font-size: 34rpx;
  380. color: #262628;
  381. border-bottom: 1px solid #EFEFEF;
  382. }
  383. .unread-text {
  384. color: #d02129;
  385. }
  386. </style>