index.vue 12 KB

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