friend.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <view class="tab-page friend">
  3. <nav-bar add search @add="onAddNewFriends" @search="showSearch = !showSearch">好友</nav-bar>
  4. <view class="nav-bar" v-if="showSearch">
  5. <view class="nav-search">
  6. <uni-search-bar v-model="searchText" radius="100" cancelButton="none"
  7. placeholder="点击搜索好友"></uni-search-bar>
  8. </view>
  9. </view>
  10. <view class="friend-tip" v-if="!hasFriends">
  11. <view class="tip-icon">
  12. <text class="iconfont icon-add-friend"></text>
  13. </view>
  14. <view class="tip-title">还没有好友</view>
  15. <view class="tip-content">添加好友,开始精彩的聊天之旅</view>
  16. <button type="primary" @click="onAddNewFriends">添加好友</button>
  17. </view>
  18. <view class="friend-items" v-else>
  19. <up-index-list :index-list="friendIdx" :sticky="false" :custom-nav-height="customNavHeight">
  20. <template v-for="(friends, i) in friendGroups">
  21. <up-index-item>
  22. <up-index-anchor :text="friendIdx[i] == '*' ? '在线' : friendIdx[i]"></up-index-anchor>
  23. <view v-for="(friend, idx) in friends" :key="idx">
  24. <friend-item :friend="friend"></friend-item>
  25. </view>
  26. </up-index-item>
  27. </template>
  28. </up-index-list>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. import { pinyin } from 'pinyin-pro';
  34. export default {
  35. data() {
  36. return {
  37. showSearch: false,
  38. searchText: ''
  39. }
  40. },
  41. methods: {
  42. onAddNewFriends() {
  43. uni.navigateTo({
  44. url: "/pages/friend/friend-add"
  45. })
  46. },
  47. firstLetter(strText) {
  48. // 使用pinyin-pro库将中文转换为拼音
  49. let pinyinOptions = {
  50. toneType: 'none', // 无声调
  51. type: 'normal' // 普通拼音
  52. };
  53. let pyText = pinyin(strText, pinyinOptions);
  54. return pyText[0];
  55. },
  56. isEnglish(character) {
  57. return /^[A-Za-z]+$/.test(character);
  58. }
  59. },
  60. computed: {
  61. friendGroupMap() {
  62. // 按首字母分组
  63. let groupMap = new Map();
  64. this.friendStore.friends.forEach((f) => {
  65. if (f.deleted || (this.searchText && !f.nickName.includes(this.searchText))) {
  66. return;
  67. }
  68. let letter = this.firstLetter(f.nickName).toUpperCase();
  69. // 非英文一律为#组
  70. if (!this.isEnglish(letter)) {
  71. letter = "#"
  72. }
  73. if (f.online) {
  74. letter = '*'
  75. }
  76. if (groupMap.has(letter)) {
  77. groupMap.get(letter).push(f);
  78. } else {
  79. groupMap.set(letter, [f]);
  80. }
  81. })
  82. // 排序
  83. let arrayObj = Array.from(groupMap);
  84. arrayObj.sort((a, b) => {
  85. // #组在最后面
  86. if (a[0] == '#' || b[0] == '#') {
  87. return b[0].localeCompare(a[0])
  88. }
  89. return a[0].localeCompare(b[0])
  90. })
  91. groupMap = new Map(arrayObj.map(i => [i[0], i[1]]));
  92. return groupMap;
  93. },
  94. friendIdx() {
  95. return Array.from(this.friendGroupMap.keys());
  96. },
  97. friendGroups() {
  98. return Array.from(this.friendGroupMap.values());
  99. },
  100. hasFriends() {
  101. return this.friendStore.friends.some(f => !f.deleted);
  102. },
  103. customNavHeight() {
  104. let h = 50;
  105. // #ifdef APP-PLUS
  106. h += uni.getSystemInfoSync().statusBarHeight;
  107. // #endif
  108. console.log("customNavHeight:", h)
  109. return h;
  110. }
  111. }
  112. }
  113. </script>
  114. <style lang="scss" scoped>
  115. .friend {
  116. position: relative;
  117. display: flex;
  118. flex-direction: column;
  119. :deep(.u-index-anchor) {
  120. height: 60rpx !important;
  121. background-color: unset !important;
  122. border-bottom: none !important;
  123. }
  124. :deep(.u-index-anchor__text) {
  125. color: $im-text-color !important;
  126. }
  127. .friend-tip {
  128. position: absolute;
  129. top: 50%;
  130. left: 50%;
  131. transform: translate(-50%, -50%);
  132. display: flex;
  133. flex-direction: column;
  134. align-items: center;
  135. padding: 60rpx 40rpx;
  136. text-align: center;
  137. width: 80%;
  138. max-width: 500rpx;
  139. .tip-icon {
  140. width: 120rpx;
  141. height: 120rpx;
  142. background: linear-gradient(135deg, #f8f9fa, #e9ecef);
  143. border-radius: 50%;
  144. display: flex;
  145. align-items: center;
  146. justify-content: center;
  147. margin-bottom: 40rpx;
  148. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
  149. border: 2rpx solid $im-bg-active;
  150. .iconfont {
  151. font-size: 56rpx;
  152. color: #6c757d;
  153. opacity: 0.8;
  154. }
  155. }
  156. .tip-title {
  157. font-size: $im-font-size-large;
  158. color: $im-text-color;
  159. font-weight: 500;
  160. margin-bottom: 20rpx;
  161. }
  162. .tip-content {
  163. font-size: $im-font-size-smaller;
  164. color: $im-text-color-lighter;
  165. line-height: 1.6;
  166. margin-bottom: 50rpx;
  167. }
  168. }
  169. .friend-items {
  170. flex: 1;
  171. padding: 0;
  172. overflow: hidden;
  173. position: relative;
  174. .scroll-bar {
  175. height: 100%;
  176. }
  177. }
  178. }
  179. </style>