index.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <template>
  2. <view class="contacts-page">
  3. <!-- 自定义顶栏:头像 + 通讯录 + 搜索(不显示加号) -->
  4. <view class="custom-header">
  5. <view class="header-left" @click="onAvatarClick">
  6. <UserAvatar
  7. :name="currentUser.name"
  8. :id="currentUser.id"
  9. :src="currentUser.avatar"
  10. :size="80"
  11. unit="rpx"
  12. />
  13. <view class="org-info">
  14. <text class="user-name">通讯录</text>
  15. </view>
  16. </view>
  17. <view class="header-right">
  18. <view class="icon-btn" @click="onSearchToggle">
  19. <image class="icon-img" src="/static/icons/search.svg" mode="aspectFit" />
  20. </view>
  21. </view>
  22. </view>
  23. <!-- A:点击搜索图标展开输入框 -->
  24. <view v-if="showSearch" class="search-bar">
  25. <input
  26. class="search-input"
  27. v-model="keyword"
  28. placeholder="搜索联系人"
  29. confirm-type="search"
  30. @confirm="doSearch"
  31. />
  32. <view class="search-action" @click="doSearch">
  33. <text>搜索</text>
  34. </view>
  35. </view>
  36. <scroll-view
  37. class="message-list"
  38. scroll-y
  39. :refresher-enabled="refresherEnabled"
  40. :refresher-triggered="refresherTriggered"
  41. @refresherrefresh="onRefresh"
  42. @scrolltolower="onLoadMore"
  43. >
  44. <view v-if="!isSearchMode && loadingMore" class="empty-tip">加载中...</view>
  45. <view v-if="loading" class="empty-tip">加载中...</view>
  46. <view v-else-if="!userList || userList.length === 0" class="empty-tip">暂无联系人</view>
  47. <view
  48. v-else
  49. class="message-item"
  50. v-for="u in userList"
  51. :key="String(u.id)"
  52. @click="openContact(u)"
  53. >
  54. <view class="item-left">
  55. <view class="avatar-wrap">
  56. <UserAvatar
  57. :name="u.name"
  58. :id="String(u.id)"
  59. :src="''"
  60. :size="80"
  61. unit="rpx"
  62. />
  63. </view>
  64. <view class="item-content">
  65. <view class="item-row">
  66. <text class="item-title">{{ u.name }}</text>
  67. </view>
  68. <text class="item-desc">{{ u.english_name || '' }}</text>
  69. </view>
  70. </view>
  71. </view>
  72. </scroll-view>
  73. </view>
  74. </template>
  75. <script>
  76. import { nextTick, onMounted, ref } from 'vue'
  77. import UserAvatar from '../../components/UserAvatar.vue'
  78. import { getToken, listUsers, searchUsers } from '../../utils/api'
  79. const USER_KEY = 'current_user'
  80. export default {
  81. components: { UserAvatar },
  82. setup() {
  83. const limit = 20
  84. const skip = ref(0)
  85. const hasMore = ref(true)
  86. const loadingMore = ref(false)
  87. const refresherTriggered = ref(false)
  88. // 防止首屏渲染阶段下拉刷新事件自动触发导致重复请求
  89. const refresherEnabled = ref(false)
  90. const currentUser = ref({ name: '', id: '', avatar: '' })
  91. const showSearch = ref(false)
  92. const keyword = ref('')
  93. const userList = ref([])
  94. const loading = ref(false)
  95. // 非搜索分页:当 keyword 为空时启用;搜索模式下不进行上拉分页
  96. const isSearchMode = ref(false)
  97. function loadCurrentUser() {
  98. try {
  99. const raw = uni.getStorageSync(USER_KEY)
  100. if (raw && typeof raw === 'object') {
  101. currentUser.value = {
  102. name: raw.name || raw.nickname || '',
  103. id: String(raw.id ?? raw.user_id ?? ''),
  104. avatar: raw.avatar || raw.avatar_url || ''
  105. }
  106. }
  107. } catch (e) {}
  108. }
  109. function normalizeUsers(res) {
  110. if (Array.isArray(res)) return res
  111. if (res && Array.isArray(res.items)) return res.items
  112. return []
  113. }
  114. async function fetchInitial() {
  115. const token = getToken()
  116. if (!token) {
  117. userList.value = []
  118. return
  119. }
  120. // 非搜索初始化:重置分页游标
  121. skip.value = 0
  122. hasMore.value = true
  123. loadingMore.value = false
  124. loading.value = true
  125. try {
  126. // 先展示一段“空关键字”的分页结果,避免页面一开始是空
  127. const res = await listUsers(token, 0, limit, '')
  128. userList.value = normalizeUsers(res)
  129. // 估算是否还有更多数据
  130. const items = normalizeUsers(res)
  131. if (!items || items.length < limit) hasMore.value = false
  132. } finally {
  133. loading.value = false
  134. // 首屏加载完成后再启用下拉刷新,避免初始化阶段重复请求
  135. refresherEnabled.value = true
  136. }
  137. }
  138. async function fetchNonSearchPage({ reset = false } = {}) {
  139. const token = getToken()
  140. if (!token) return
  141. if (loading.value || loadingMore.value) return
  142. if (!hasMore.value && !reset) return
  143. if (reset) {
  144. skip.value = 0
  145. hasMore.value = true
  146. userList.value = []
  147. }
  148. if (reset) loading.value = true
  149. else loadingMore.value = true
  150. try {
  151. const res = await listUsers(token, skip.value, limit, '')
  152. const items = normalizeUsers(res)
  153. if (!items.length) {
  154. hasMore.value = false
  155. return
  156. }
  157. userList.value = reset ? items : userList.value.concat(items)
  158. if (items.length < limit) hasMore.value = false
  159. else skip.value += limit
  160. } finally {
  161. loading.value = false
  162. loadingMore.value = false
  163. }
  164. }
  165. async function onRefresh() {
  166. refresherTriggered.value = true
  167. try {
  168. if (isSearchMode.value) {
  169. // 搜索模式下只刷新第一页(不做分页)
  170. await doSearch()
  171. } else {
  172. await fetchNonSearchPage({ reset: true })
  173. }
  174. } finally {
  175. refresherTriggered.value = false
  176. }
  177. }
  178. async function doSearch() {
  179. const token = getToken()
  180. if (!token) {
  181. userList.value = []
  182. return
  183. }
  184. const q = String(keyword.value || '').trim()
  185. // 搜索关键词为空 => 非搜索列表(启用分页)
  186. if (!q) {
  187. isSearchMode.value = false
  188. await fetchNonSearchPage({ reset: true })
  189. return
  190. }
  191. isSearchMode.value = true
  192. loading.value = true
  193. try {
  194. // 有关键字就走推荐的 /users/search
  195. const res = await searchUsers(token, q, limit)
  196. userList.value = normalizeUsers(res)
  197. } finally {
  198. loading.value = false
  199. }
  200. }
  201. function openContact(u) {
  202. const id = String(u?.id ?? '')
  203. uni.navigateTo({
  204. url:
  205. '/pages/contact-detail/index?contactId=' +
  206. encodeURIComponent(id) +
  207. '&contactName=' +
  208. encodeURIComponent(u?.name || '') +
  209. '&contactEnglishName=' +
  210. encodeURIComponent(u?.english_name || '') +
  211. '&contactStatus=' +
  212. encodeURIComponent(u?.status || '')
  213. })
  214. }
  215. function onSearchToggle() {
  216. // 顶部搜索按钮:统一进入全局搜索列表页
  217. showSearch.value = false
  218. uni.navigateTo({ url: '/pages/search-center/index' })
  219. }
  220. function onAvatarClick() {
  221. uni.navigateTo({ url: '/pages/profile/index' })
  222. }
  223. /** Tab 切换 / 从子页返回时刷新列表(与下拉刷新逻辑一致) */
  224. async function refreshOnShow() {
  225. loadCurrentUser()
  226. if (isSearchMode.value) {
  227. await doSearch()
  228. } else {
  229. await fetchNonSearchPage({ reset: true })
  230. }
  231. }
  232. onMounted(() => {
  233. loadCurrentUser()
  234. fetchInitial()
  235. })
  236. return {
  237. currentUser,
  238. showSearch,
  239. keyword,
  240. userList,
  241. loading,
  242. loadingMore,
  243. refresherTriggered,
  244. refresherEnabled,
  245. isSearchMode,
  246. openContact,
  247. onAvatarClick,
  248. onSearchToggle,
  249. doSearch,
  250. onRefresh,
  251. refreshOnShow,
  252. onLoadMore: () => {
  253. // 上拉分页只对非搜索列表生效
  254. if (isSearchMode.value) return
  255. fetchNonSearchPage({ reset: false })
  256. }
  257. }
  258. },
  259. async onShow() {
  260. if (this.refreshOnShow) await this.refreshOnShow()
  261. }
  262. }
  263. </script>
  264. <style scoped>
  265. .contacts-page {
  266. /* 高度占满视口,不再手动减 tabBar,高度保持 100vh 以保证 scroll-view 有明确高度 */
  267. height: 100vh;
  268. display: flex;
  269. flex-direction: column;
  270. background: #fff;
  271. }
  272. /* 顶部安全区:与消息页 custom-header 对齐 */
  273. .custom-header {
  274. display: flex;
  275. align-items: center;
  276. justify-content: space-between;
  277. padding: 24rpx 24rpx 24rpx 32rpx;
  278. padding-top: 88rpx;
  279. padding-top: max(88rpx, calc(24rpx + constant(safe-area-inset-top)));
  280. padding-top: max(88rpx, calc(24rpx + env(safe-area-inset-top)));
  281. background: #fff;
  282. border-bottom: 1rpx solid #eee;
  283. }
  284. .header-left {
  285. display: flex;
  286. align-items: center;
  287. flex: 1;
  288. min-width: 0;
  289. }
  290. .org-info {
  291. margin-left: 24rpx;
  292. display: flex;
  293. flex-direction: column;
  294. min-width: 0;
  295. }
  296. .user-name {
  297. font-size: 32rpx;
  298. font-weight: 600;
  299. color: #111827;
  300. }
  301. .header-right {
  302. display: flex;
  303. align-items: center;
  304. gap: 24rpx;
  305. }
  306. .icon-btn {
  307. width: 64rpx;
  308. height: 64rpx;
  309. display: flex;
  310. align-items: center;
  311. justify-content: center;
  312. }
  313. .icon-img {
  314. width: 44rpx;
  315. height: 44rpx;
  316. opacity: 0.85;
  317. }
  318. .search-bar {
  319. display: flex;
  320. align-items: center;
  321. padding: 16rpx 24rpx;
  322. border-bottom: 1rpx solid #eee;
  323. background: #fff;
  324. }
  325. .search-input {
  326. flex: 1;
  327. min-width: 0;
  328. height: 64rpx;
  329. padding: 0 20rpx;
  330. background: #f0f0f0;
  331. border-radius: 16rpx;
  332. font-size: 28rpx;
  333. box-sizing: border-box;
  334. }
  335. .search-action {
  336. margin-left: 16rpx;
  337. padding: 12rpx 20rpx;
  338. background: #259653;
  339. color: #fff;
  340. border-radius: 16rpx;
  341. font-size: 26rpx;
  342. }
  343. .message-list {
  344. flex: 1;
  345. min-height: 0;
  346. height: 0;
  347. padding-bottom: var(--window-bottom, 50px);
  348. box-sizing: border-box;
  349. }
  350. .empty-tip {
  351. padding: 80rpx 32rpx;
  352. text-align: center;
  353. font-size: 28rpx;
  354. color: #999;
  355. }
  356. .message-item {
  357. padding: 28rpx 32rpx;
  358. border-bottom: 1rpx solid #f0f0f0;
  359. }
  360. .item-left {
  361. display: flex;
  362. align-items: flex-start;
  363. }
  364. .avatar-wrap {
  365. position: relative;
  366. flex-shrink: 0;
  367. }
  368. .item-content {
  369. flex: 1;
  370. margin-left: 24rpx;
  371. min-width: 0;
  372. }
  373. .item-row {
  374. display: flex;
  375. align-items: center;
  376. justify-content: space-between;
  377. margin-bottom: 8rpx;
  378. }
  379. .item-title {
  380. font-size: 34rpx;
  381. color: #333;
  382. flex: 1;
  383. overflow: hidden;
  384. text-overflow: ellipsis;
  385. white-space: nowrap;
  386. }
  387. .item-desc {
  388. font-size: 26rpx;
  389. color: #999;
  390. overflow: hidden;
  391. text-overflow: ellipsis;
  392. white-space: nowrap;
  393. display: block;
  394. }
  395. </style>