index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. <template>
  2. <view class="search-center-page">
  3. <!-- 顶部搜索栏(带“取消”) -->
  4. <view class="search-header">
  5. <view class="header-row">
  6. <view class="search-wrap">
  7. <image class="search-icon" src="/static/icons/search.svg" mode="aspectFit" />
  8. <input
  9. class="search-input"
  10. v-model="keyword"
  11. placeholder="搜索"
  12. confirm-type="search"
  13. @confirm="onConfirm"
  14. />
  15. </view>
  16. <view class="cancel" @click="onCancel">取消</view>
  17. </view>
  18. </view>
  19. <!-- 分类 chips(点击后滚动到对应区块) -->
  20. <scroll-view class="chips-scroll" scroll-x :show-scrollbar="false">
  21. <view class="chips-row">
  22. <view
  23. v-for="c in chips"
  24. :key="c.key"
  25. class="chip"
  26. :class="{ active: c.key === activeKey }"
  27. @click="onChipClick(c.key)"
  28. >
  29. {{ c.label }}
  30. </view>
  31. </view>
  32. </scroll-view>
  33. <scroll-view
  34. class="results-scroll"
  35. scroll-y
  36. :scroll-into-view="scrollIntoView"
  37. scroll-with-animation
  38. >
  39. <view class="empty-state" v-if="!keyword.trim()">
  40. <text class="empty-text">请输入关键词开始搜索</text>
  41. </view>
  42. <!-- 消息(本地已加载缓存,见 searchCachedMessages) -->
  43. <view id="sec-messages" class="section" v-if="keyword.trim()">
  44. <view class="section-header">
  45. <text class="section-title">消息</text>
  46. <view class="section-arrow">></view>
  47. </view>
  48. <view class="section-body">
  49. <view v-if="messageHits.length" class="list">
  50. <view
  51. v-for="h in messageHits"
  52. :key="String(h.messageId || '') + '-' + String(h.peerId)"
  53. class="row-item"
  54. @click="openMessageHit(h)"
  55. >
  56. <UserAvatar
  57. :name="h.peerName || '会话'"
  58. :id="String(h.peerId)"
  59. :src="''"
  60. :size="64"
  61. unit="rpx"
  62. />
  63. <view class="row-main">
  64. <text class="row-title">{{ h.peerName || '会话' }}</text>
  65. <text class="row-sub">{{ h.preview || '' }}</text>
  66. </view>
  67. </view>
  68. </view>
  69. <view v-else class="empty-sub">{{ messageSearchEmptyText }}</view>
  70. </view>
  71. </view>
  72. <!-- 联络人 -->
  73. <view id="sec-contacts" class="section" v-if="keyword.trim()">
  74. <view class="section-header">
  75. <text class="section-title">联络人</text>
  76. <view class="section-arrow">></view>
  77. </view>
  78. <view class="section-body">
  79. <view v-if="contacts.length" class="list">
  80. <view
  81. v-for="u in contacts"
  82. :key="String(u.id)"
  83. class="row-item"
  84. @click="openContact(u)"
  85. >
  86. <UserAvatar
  87. :name="u.name"
  88. :id="String(u.id)"
  89. :src="''"
  90. :size="64"
  91. unit="rpx"
  92. />
  93. <view class="row-main">
  94. <text class="row-title">{{ u.name }}</text>
  95. <text class="row-sub">{{ u.english_name || '' }}</text>
  96. </view>
  97. </view>
  98. </view>
  99. <view v-else class="empty-sub">暂无匹配联系人</view>
  100. </view>
  101. </view>
  102. <!-- 应用 -->
  103. <view id="sec-apps" class="section" v-if="keyword.trim()">
  104. <view class="section-header">
  105. <text class="section-title">应用</text>
  106. <view class="section-arrow">></view>
  107. </view>
  108. <view class="section-body">
  109. <view v-if="apps.length" class="list">
  110. <view
  111. v-for="a in apps"
  112. :key="String(a.id)"
  113. class="row-item"
  114. @click="openApp(a)"
  115. >
  116. <SystemAvatar :name="a.name" :size="64" unit="rpx" />
  117. <view class="row-main">
  118. <text class="row-title">{{ a.name }}</text>
  119. <text class="row-sub">{{ a.categoryName || '' }}</text>
  120. </view>
  121. </view>
  122. </view>
  123. <view v-else class="empty-sub">暂无匹配应用</view>
  124. </view>
  125. </view>
  126. <view class="bottom-spacer" />
  127. </scroll-view>
  128. <!-- 加载提示(只在搜索中显示) -->
  129. <view class="loading-mask" v-if="loading">
  130. <text class="loading-text">搜索中...</text>
  131. </view>
  132. </view>
  133. </template>
  134. <script>
  135. import UserAvatar from '../../components/UserAvatar.vue'
  136. import SystemAvatar from '../../components/SystemAvatar.vue'
  137. import { getToken, getLaunchpadApps, searchUsers, ssoLogin } from '../../utils/api'
  138. import { chatStore } from '../../store/chat'
  139. export default {
  140. components: { UserAvatar, SystemAvatar },
  141. data() {
  142. return {
  143. keyword: '',
  144. activeKey: 'contacts',
  145. scrollIntoView: '',
  146. loading: false,
  147. contacts: [],
  148. apps: [],
  149. messageHits: [],
  150. chips: [
  151. { key: 'messages', label: '消息' },
  152. { key: 'contacts', label: '联系人' },
  153. { key: 'apps', label: '应用' }
  154. ]
  155. }
  156. },
  157. computed: {
  158. messageSearchEmptyText() {
  159. return '暂无匹配消息(仅搜索已加载的聊天记录)'
  160. }
  161. },
  162. onLoad(options) {
  163. try {
  164. const kw = options && options.keyword ? options.keyword : ''
  165. if (/%[0-9A-Fa-f]{2}/.test(String(kw))) this.keyword = decodeURIComponent(String(kw))
  166. else this.keyword = String(kw || '')
  167. } catch (e) {}
  168. // 如果从外部带了关键字,直接触发一次搜索
  169. if (this.keyword.trim()) {
  170. this.doSearch()
  171. }
  172. },
  173. methods: {
  174. onCancel() {
  175. uni.navigateBack()
  176. },
  177. onConfirm() {
  178. this.doSearch()
  179. },
  180. onChipClick(key) {
  181. this.activeKey = key
  182. // scroll-into-view 需要存在于 scroll-view 内的子节点 id
  183. this.scrollIntoView = 'sec-' + key
  184. },
  185. async doSearch() {
  186. const q = String(this.keyword || '').trim()
  187. if (!q) {
  188. this.contacts = []
  189. this.apps = []
  190. this.messageHits = []
  191. return
  192. }
  193. this.loading = true
  194. try {
  195. const token = getToken()
  196. if (!token) {
  197. this.contacts = []
  198. this.apps = []
  199. this.messageHits = []
  200. uni.showToast({ title: '请先登录', icon: 'none' })
  201. return
  202. }
  203. const [contactsRes, appsRes] = await Promise.allSettled([
  204. searchUsers(token, q, 20),
  205. (async () => {
  206. const res = await getLaunchpadApps()
  207. const items = res && Array.isArray(res.items) ? res.items : []
  208. const filtered = items.filter((it) => {
  209. const name = String(it.app_name || '')
  210. return name.includes(q)
  211. })
  212. return filtered.map((it) => ({
  213. id: it.app_id ?? it.id,
  214. name: it.app_name || '应用',
  215. categoryName: it.category_name || '分类'
  216. }))
  217. })()
  218. ])
  219. let contacts = []
  220. if (contactsRes.status === 'fulfilled' && contactsRes.value) {
  221. const res = contactsRes.value
  222. if (Array.isArray(res)) contacts = res
  223. else if (res && Array.isArray(res.items)) contacts = res.items
  224. }
  225. this.contacts = contacts
  226. this.apps = appsRes.status === 'fulfilled' && Array.isArray(appsRes.value) ? appsRes.value : []
  227. this.messageHits = this.searchCachedMessages(q)
  228. } finally {
  229. this.loading = false
  230. }
  231. },
  232. /** 在 chatStore.messages 中按正文/标题匹配(仅已加载会话) */
  233. searchCachedMessages(q) {
  234. const needle = String(q || '').trim().toLowerCase()
  235. if (!needle) return []
  236. const contacts = chatStore.contacts || []
  237. const peerName = (id) => {
  238. const c = contacts.find((x) => String(x.user_id || x.id) === String(id))
  239. return (c && (c.app_name || c.title)) ? String(c.app_name || c.title) : ''
  240. }
  241. const hits = []
  242. const byContact = chatStore.messages || {}
  243. for (const pid of Object.keys(byContact)) {
  244. const list = byContact[pid] || []
  245. for (const msg of list) {
  246. if (msg && msg.tempId) continue
  247. const mid = msg.id != null ? String(msg.id) : ''
  248. if (mid.startsWith('temp')) continue
  249. const blob = [msg.content, msg.title].filter(Boolean).join(' ')
  250. if (!blob.toLowerCase().includes(needle)) continue
  251. const preview = String(msg.content || msg.title || '').trim()
  252. const ts = msg.createdAt ? new Date(msg.createdAt).getTime() : 0
  253. hits.push({
  254. messageId: mid,
  255. peerId: String(pid),
  256. peerName: peerName(pid),
  257. preview: preview.length > 120 ? preview.slice(0, 120) + '…' : preview,
  258. _ts: ts
  259. })
  260. }
  261. }
  262. hits.sort((a, b) => b._ts - a._ts)
  263. return hits.slice(0, 20).map(({ _ts, ...h }) => h)
  264. },
  265. openMessageHit(h) {
  266. if (!h || !h.peerId) return
  267. let url = '/pages/chat/index?contactId=' + encodeURIComponent(h.peerId)
  268. if (h.peerName) url += '&contactName=' + encodeURIComponent(h.peerName)
  269. if (h.messageId) url += '&scrollToMessageId=' + encodeURIComponent(h.messageId)
  270. uni.navigateTo({ url })
  271. },
  272. openContact(u) {
  273. const id = String(u?.id ?? '').trim()
  274. if (!id) return
  275. uni.navigateTo({
  276. url:
  277. '/pages/contact-detail/index?contactId=' +
  278. encodeURIComponent(id) +
  279. '&contactName=' +
  280. encodeURIComponent(u?.name || '') +
  281. '&contactEnglishName=' +
  282. encodeURIComponent(u?.english_name || '')
  283. })
  284. },
  285. async openApp(app) {
  286. if (!app || !app.id) return
  287. uni.showLoading({ title: '打开中...' })
  288. try {
  289. const res = await ssoLogin(app.id)
  290. const redirectUrl = res && (res.redirect_url || res.redirectUrl)
  291. if (!redirectUrl) {
  292. uni.showToast({ title: '打开失败', icon: 'none' })
  293. return
  294. }
  295. const pageUrl =
  296. '/pages/webview/index?url=' +
  297. encodeURIComponent(redirectUrl) +
  298. '&title=' +
  299. encodeURIComponent(app.name || '应用')
  300. uni.navigateTo({ url: pageUrl })
  301. } catch (e) {
  302. uni.showToast({ title: '打开失败', icon: 'none' })
  303. } finally {
  304. uni.hideLoading()
  305. }
  306. }
  307. }
  308. }
  309. </script>
  310. <style scoped>
  311. .search-center-page {
  312. height: 100vh;
  313. background: #f5f5f7;
  314. display: flex;
  315. flex-direction: column;
  316. position: relative;
  317. }
  318. .search-header {
  319. background: #fff;
  320. border-bottom: 1rpx solid #eee;
  321. padding: 0 24rpx 20rpx 24rpx;
  322. /* 顶部安全区:与消息/通讯录页面 custom-header 对齐,避免刘海屏点击不到 */
  323. padding-top: 88rpx;
  324. padding-top: max(88rpx, calc(24rpx + constant(safe-area-inset-top)));
  325. padding-top: max(88rpx, calc(24rpx + env(safe-area-inset-top)));
  326. }
  327. .header-row {
  328. display: flex;
  329. align-items: center;
  330. gap: 16rpx;
  331. }
  332. .search-wrap {
  333. flex: 1;
  334. min-width: 0;
  335. display: flex;
  336. align-items: center;
  337. gap: 12rpx;
  338. padding: 14rpx 18rpx;
  339. border-radius: 999rpx;
  340. background: #f0f0f0;
  341. }
  342. .search-icon {
  343. width: 28rpx;
  344. height: 28rpx;
  345. opacity: 0.75;
  346. }
  347. .search-input {
  348. flex: 1;
  349. min-width: 0;
  350. font-size: 28rpx;
  351. height: 44rpx;
  352. line-height: 44rpx;
  353. padding: 0;
  354. }
  355. .cancel {
  356. color: #259653;
  357. font-size: 28rpx;
  358. font-weight: 600;
  359. padding: 10rpx 8rpx;
  360. white-space: nowrap;
  361. }
  362. .chips-scroll {
  363. background: #fff;
  364. border-bottom: 1rpx solid #eee;
  365. }
  366. .chips-row {
  367. display: flex;
  368. align-items: center;
  369. gap: 16rpx;
  370. padding: 18rpx 24rpx;
  371. }
  372. .chip {
  373. padding: 10rpx 20rpx;
  374. border-radius: 999rpx;
  375. background: #f3f4f6;
  376. color: #111827;
  377. font-size: 26rpx;
  378. white-space: nowrap;
  379. }
  380. .chip.active {
  381. background: rgba(37, 150, 83, 0.12);
  382. color: #259653;
  383. font-weight: 700;
  384. }
  385. .results-scroll {
  386. flex: 1;
  387. min-height: 0;
  388. height: 0;
  389. padding: 20rpx 24rpx 0;
  390. box-sizing: border-box;
  391. }
  392. .empty-state {
  393. padding: 120rpx 10rpx;
  394. display: flex;
  395. align-items: center;
  396. justify-content: center;
  397. }
  398. .empty-text {
  399. color: #9ca3af;
  400. font-size: 28rpx;
  401. }
  402. .section {
  403. margin-bottom: 22rpx;
  404. background: #fff;
  405. border-radius: 24rpx;
  406. padding: 18rpx 18rpx 14rpx;
  407. box-shadow: 0 2rpx 10rpx rgba(17, 24, 39, 0.04);
  408. }
  409. .section-header {
  410. display: flex;
  411. align-items: center;
  412. justify-content: space-between;
  413. padding: 0 6rpx 10rpx;
  414. }
  415. .section-title {
  416. font-size: 30rpx;
  417. font-weight: 800;
  418. color: #111827;
  419. }
  420. .section-arrow {
  421. color: #9ca3af;
  422. font-size: 32rpx;
  423. font-weight: 600;
  424. }
  425. .section-body {
  426. padding: 6rpx 6rpx 0;
  427. }
  428. .list {
  429. display: flex;
  430. flex-direction: column;
  431. gap: 14rpx;
  432. }
  433. .row-item {
  434. display: flex;
  435. align-items: center;
  436. gap: 18rpx;
  437. padding: 14rpx 12rpx;
  438. border-radius: 18rpx;
  439. }
  440. .row-main {
  441. flex: 1;
  442. min-width: 0;
  443. }
  444. .row-title {
  445. display: block;
  446. font-size: 30rpx;
  447. font-weight: 700;
  448. color: #111827;
  449. overflow: hidden;
  450. text-overflow: ellipsis;
  451. white-space: nowrap;
  452. }
  453. .row-sub {
  454. display: block;
  455. margin-top: 6rpx;
  456. font-size: 24rpx;
  457. color: #6b7280;
  458. overflow: hidden;
  459. text-overflow: ellipsis;
  460. white-space: nowrap;
  461. }
  462. .empty-sub {
  463. padding: 22rpx 6rpx 28rpx;
  464. color: #9ca3af;
  465. font-size: 26rpx;
  466. }
  467. .badge {
  468. min-width: 42rpx;
  469. height: 36rpx;
  470. padding: 0 12rpx;
  471. border-radius: 18rpx;
  472. background: #f5222d;
  473. color: #fff;
  474. font-size: 22rpx;
  475. display: flex;
  476. align-items: center;
  477. justify-content: center;
  478. font-weight: 700;
  479. }
  480. .loading-mask {
  481. position: absolute;
  482. left: 0;
  483. right: 0;
  484. top: 0;
  485. bottom: 0;
  486. background: rgba(255, 255, 255, 0.65);
  487. display: flex;
  488. align-items: flex-start;
  489. justify-content: center;
  490. padding-top: 240rpx;
  491. z-index: 10;
  492. }
  493. .loading-text {
  494. background: rgba(255, 255, 255, 0.95);
  495. padding: 18rpx 28rpx;
  496. border-radius: 20rpx;
  497. font-size: 28rpx;
  498. color: #111827;
  499. font-weight: 700;
  500. }
  501. .bottom-spacer {
  502. height: 60rpx;
  503. }
  504. </style>