index.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <template>
  2. <view class="index">
  3. <!-- <view class="index-tab">
  4. <u-tabs :list="list" :is-scroll="false" :current="current" font-size="32" :bold="false" @change="tabChange"
  5. bar-width="100"></u-tabs>
  6. </view> -->
  7. <!-- 聊天记录 -->
  8. <view class="index-records" v-if="current === 0">
  9. <view class="index-records-list">
  10. <view class="index-records-list-item" v-for="(item, index) in conversationList" :key="index" @click="toChat(item)">
  11. <view class="item-img">
  12. <u-avatar :src="item.userProfile.avatar" size="100"></u-avatar>
  13. </view>
  14. <view class="item-text">
  15. <view class="item-user">
  16. {{ item.userProfile.nick }}
  17. </view>
  18. <view class="item-text-info">
  19. <rich-text :nodes="nodesFliter(item.lastMessage.messageForShow)"></rich-text>
  20. </view>
  21. </view>
  22. <view class="item-msg">
  23. <view class="item-msg-icon" v-if="item.unreadCount">{{ item.unreadCount }}</view>
  24. </view>
  25. </view>
  26. </view>
  27. </view>
  28. <!-- 好友列表 -->
  29. <view class="index-friends" v-if="current === 1">
  30. <view class="index-friends-list">
  31. <view class="index-friends-list-item" v-for="(item, index) in friendList" :key="index" @click="checkUserToRoom(item)">
  32. <view class="user-img">
  33. <u-avatar :src="item.img" size="100"></u-avatar>
  34. </view>
  35. <view class="user-name">
  36. {{ item.user }}
  37. </view>
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. </template>
  43. <script>
  44. import {
  45. mapState
  46. } from "vuex";
  47. export default {
  48. data() {
  49. return {
  50. current: 0,
  51. list: [{
  52. name: '聊天记录'
  53. }
  54. ],
  55. friendList: [
  56. { userId: '1', user: '1', img: 'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1735490596,2760195857&fm=26&gp=0.jpg' },
  57. { userId: '2', user: '2', img: 'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1735490596,2760195857&fm=26&gp=0.jpg' }
  58. ]
  59. }
  60. },
  61. computed: {
  62. ...mapState({
  63. isLogin: state => state.isLogin,
  64. isSDKReady: state => state.isSDKReady,
  65. conversationList: state => state.conversationList,
  66. })
  67. },
  68. watch: {
  69. isSDKReady(val) {
  70. if (val) {
  71. // 更新用户自己的基础资料(头像+昵称+个性签名)
  72. this.updateUserInfo()
  73. // 请求会话列表
  74. this.getConversationList()
  75. }
  76. }
  77. },
  78. onShow() {
  79. if (!this.isLogin) {
  80. this.$u.route({
  81. url: 'pages/login/login'
  82. })
  83. } else {
  84. if (this.isSDKReady) {
  85. this.getConversationList()
  86. }
  87. }
  88. },
  89. methods: {
  90. /**
  91. * tab切换触发
  92. * @param {Object} index
  93. */
  94. tabChange(index) {
  95. this.current = index
  96. if (this.isSDKReady) {
  97. this.getConversationList()
  98. }
  99. },
  100. /**
  101. * 获取消息列表
  102. */
  103. getConversationList() {
  104. this.tim.getConversationList().then(res => {
  105. let conversationList = res.data.conversationList; // 会话列表,用该列表覆盖原有的会话列表
  106. if (conversationList.length) {
  107. this.$store.commit("updateConversationList", conversationList);
  108. }
  109. })
  110. },
  111. /**
  112. * 聊天的节点加上外层的div
  113. * @param {Object} str
  114. */
  115. nodesFliter(str) {
  116. let nodeStr = '<div style="align-items: center;word-wrap:break-word;">' + str + '</div>'
  117. return nodeStr
  118. },
  119. /**
  120. * 提交用户的基础信息到Im
  121. */
  122. updateUserInfo() {
  123. // 将已经登陆的用户信息 提交到IM中
  124. // let userInfo = JSON.parse(uni.getStorageSync('userInfo'))
  125. // this.tim.updateMyProfile({
  126. // nick: userInfo.user,
  127. // avatar: userInfo.img,
  128. // gender: this.$TIM.TYPES.GENDER_MALE,
  129. // selfSignature: '暂无个性签名',
  130. // allowType: this.$TIM.TYPES.ALLOW_TYPE_ALLOW_ANY
  131. // }).then((res) => {
  132. // console.log('提交资料成功')
  133. // }).catch((err) => {
  134. // console.warn('updateMyProfile error:', imError); // 更新资料失败的相关信息
  135. // });
  136. },
  137. /**
  138. * 去聊天
  139. * @param {Object} item
  140. */
  141. toChat(item) {
  142. console.log('聊天用户:', item)
  143. this.$store.commit('updateConversationActive', item)
  144. setTimeout(() => {
  145. this.$u.route({
  146. url: 'pages/chat/chat'
  147. })
  148. }, 200)
  149. },
  150. /**
  151. * @param {Object} toUserInfo
  152. */
  153. checkUserToRoom(toUserInfo) {
  154. this.$store.commit('createConversationActive', toUserInfo.userId)
  155. this.$u.route({
  156. url: 'pages/chat/chat'
  157. })
  158. }
  159. }
  160. }
  161. </script>
  162. <style lang="scss" scoped>
  163. .index {
  164. padding: 30rpx;
  165. &-tab {
  166. padding-bottom: 10rpx;
  167. margin-bottom: 30rpx;
  168. border-bottom: solid 2px #eee;
  169. }
  170. &-records {
  171. &-list {
  172. &-item {
  173. width: 100%;
  174. padding: 20rpx 0;
  175. overflow: hidden;
  176. border-bottom: 1px solid #eee;
  177. display: flex;
  178. justify-content: space-between;
  179. .item-text {
  180. margin-left: 30rpx;
  181. width: 500rpx;
  182. height: 100rpx;
  183. color: #666;
  184. font-size: 28rpx;
  185. }
  186. .item-user {
  187. height: 60rpx;
  188. line-height: 60rpx;
  189. color: 333;
  190. font-size: 32rpx;
  191. }
  192. .item-text-info {
  193. height: 60rpx;
  194. line-height: 60rpx;
  195. color: #666;
  196. font-szie: 24rpx;
  197. overflow: hidden;
  198. text-overflow: ellipsis;
  199. white-space: nowrap;
  200. }
  201. .item-msg {
  202. width: 40rpx;
  203. height: 100rpx;
  204. }
  205. .item-msg-icon {
  206. width: 40rpx;
  207. height: 40rpx;
  208. border-radius: 50%;
  209. background: #f06c7a;
  210. color: #fff;
  211. line-height: 40rpx;
  212. margin-top: 30rpx;
  213. text-align: center;
  214. font-size: 24rpx;
  215. }
  216. }
  217. }
  218. }
  219. &-friends {
  220. &-list {
  221. &-item {
  222. display: flex;
  223. align-items: center;
  224. padding: 20rpx 0;
  225. width: 100%;
  226. cursor: pointer;
  227. border-bottom: 1px solid #eee;
  228. .user-img {
  229. margin-right: 20rpx;
  230. }
  231. .user-name {
  232. width: 250rpx;
  233. color: #666;
  234. font-weight: 500;
  235. }
  236. }
  237. }
  238. }
  239. }
  240. </style>