friend.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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="friendIdx.length == 0">
  11. 温馨提示:您现在还没有任何好友,快点击右上方'+'按钮添加好友吧~
  12. </view>
  13. <view class="friend-items" v-else>
  14. <up-index-list :index-list="friendIdx">
  15. <template v-for="(friends, i) in friendGroups">
  16. <up-index-item>
  17. <up-index-anchor :text="friendIdx[i] == '*' ? '在线' : friendIdx[i]"></up-index-anchor>
  18. <view v-for="(friend, idx) in friends" :key="idx">
  19. <friend-item :friend="friend"></friend-item>
  20. </view>
  21. </up-index-item>
  22. </template>
  23. </up-index-list>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. import { pinyin } from 'pinyin-pro';
  29. export default {
  30. data() {
  31. return {
  32. showSearch: false,
  33. searchText: ''
  34. }
  35. },
  36. methods: {
  37. onAddNewFriends() {
  38. uni.navigateTo({
  39. url: "/pages/friend/friend-add"
  40. })
  41. },
  42. firstLetter(strText) {
  43. // 使用pinyin-pro库将中文转换为拼音
  44. let pinyinOptions = {
  45. toneType: 'none', // 无声调
  46. type: 'normal' // 普通拼音
  47. };
  48. let pyText = pinyin(strText, pinyinOptions);
  49. return pyText[0];
  50. },
  51. isEnglish(character) {
  52. return /^[A-Za-z]+$/.test(character);
  53. }
  54. },
  55. computed: {
  56. friendGroupMap() {
  57. // 按首字母分组
  58. let groupMap = new Map();
  59. this.friendStore.friends.forEach((f) => {
  60. if (f.deleted || (this.searchText && !f.nickName.includes(this.searchText))) {
  61. return;
  62. }
  63. let letter = this.firstLetter(f.nickName).toUpperCase();
  64. // 非英文一律为#组
  65. if (!this.isEnglish(letter)) {
  66. letter = "#"
  67. }
  68. if (f.online) {
  69. letter = '*'
  70. }
  71. if (groupMap.has(letter)) {
  72. groupMap.get(letter).push(f);
  73. } else {
  74. groupMap.set(letter, [f]);
  75. }
  76. })
  77. // 排序
  78. let arrayObj = Array.from(groupMap);
  79. arrayObj.sort((a, b) => {
  80. // #组在最后面
  81. if (a[0] == '#' || b[0] == '#') {
  82. return b[0].localeCompare(a[0])
  83. }
  84. return a[0].localeCompare(b[0])
  85. })
  86. groupMap = new Map(arrayObj.map(i => [i[0], i[1]]));
  87. return groupMap;
  88. },
  89. friendIdx() {
  90. return Array.from(this.friendGroupMap.keys());
  91. },
  92. friendGroups() {
  93. return Array.from(this.friendGroupMap.values());
  94. }
  95. }
  96. }
  97. </script>
  98. <style lang="scss" scoped>
  99. .friend {
  100. position: relative;
  101. display: flex;
  102. flex-direction: column;
  103. :deep(.u-index-anchor) {
  104. height: 60rpx !important;
  105. background-color: unset !important;
  106. border-bottom: none !important;
  107. }
  108. :deep(.u-index-anchor__text) {
  109. color: $im-text-color !important;
  110. }
  111. :deep(.u-index-list__letter__item) {
  112. width: 48rpx !important;
  113. height: 48rpx !important;
  114. }
  115. :deep(.u-index-list__letter__item__index) {
  116. font-size: $im-font-size-small !important;
  117. }
  118. .friend-tip {
  119. position: absolute;
  120. top: 400rpx;
  121. padding: 50rpx;
  122. text-align: center;
  123. line-height: 50rpx;
  124. color: $im-text-color-lighter;
  125. }
  126. .friend-items {
  127. flex: 1;
  128. padding: 0;
  129. overflow: hidden;
  130. position: relative;
  131. .scroll-bar {
  132. height: 100%;
  133. }
  134. }
  135. }
  136. </style>