| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- <template>
- <view class="search-center-page">
- <!-- 顶部搜索栏(带“取消”) -->
- <view class="search-header">
- <view class="header-row">
- <view class="search-wrap">
- <image class="search-icon" src="/static/icons/search.svg" mode="aspectFit" />
- <input
- class="search-input"
- v-model="keyword"
- placeholder="搜索"
- confirm-type="search"
- @confirm="onConfirm"
- />
- </view>
- <view class="cancel" @click="onCancel">取消</view>
- </view>
- </view>
- <!-- 分类 chips(点击后滚动到对应区块) -->
- <scroll-view class="chips-scroll" scroll-x :show-scrollbar="false">
- <view class="chips-row">
- <view
- v-for="c in chips"
- :key="c.key"
- class="chip"
- :class="{ active: c.key === activeKey }"
- @click="onChipClick(c.key)"
- >
- {{ c.label }}
- </view>
- </view>
- </scroll-view>
- <scroll-view
- class="results-scroll"
- scroll-y
- :scroll-into-view="scrollIntoView"
- scroll-with-animation
- >
- <view class="empty-state" v-if="!keyword.trim()">
- <text class="empty-text">请输入关键词开始搜索</text>
- </view>
- <!-- 消息(本地已加载缓存,见 searchCachedMessages) -->
- <view id="sec-messages" class="section" v-if="keyword.trim()">
- <view class="section-header">
- <text class="section-title">消息</text>
- <view class="section-arrow">></view>
- </view>
- <view class="section-body">
- <view v-if="messageHits.length" class="list">
- <view
- v-for="h in messageHits"
- :key="String(h.messageId || '') + '-' + String(h.peerId)"
- class="row-item"
- @click="openMessageHit(h)"
- >
- <UserAvatar
- :name="h.peerName || '会话'"
- :id="String(h.peerId)"
- :src="''"
- :size="64"
- unit="rpx"
- />
- <view class="row-main">
- <text class="row-title">{{ h.peerName || '会话' }}</text>
- <text class="row-sub">{{ h.preview || '' }}</text>
- </view>
- </view>
- </view>
- <view v-else class="empty-sub">{{ messageSearchEmptyText }}</view>
- </view>
- </view>
- <!-- 联络人 -->
- <view id="sec-contacts" class="section" v-if="keyword.trim()">
- <view class="section-header">
- <text class="section-title">联络人</text>
- <view class="section-arrow">></view>
- </view>
- <view class="section-body">
- <view v-if="contacts.length" class="list">
- <view
- v-for="u in contacts"
- :key="String(u.id)"
- class="row-item"
- @click="openContact(u)"
- >
- <UserAvatar
- :name="u.name"
- :id="String(u.id)"
- :src="''"
- :size="64"
- unit="rpx"
- />
- <view class="row-main">
- <text class="row-title">{{ u.name }}</text>
- <text class="row-sub">{{ u.english_name || '' }}</text>
- </view>
- </view>
- </view>
- <view v-else class="empty-sub">暂无匹配联系人</view>
- </view>
- </view>
- <!-- 应用 -->
- <view id="sec-apps" class="section" v-if="keyword.trim()">
- <view class="section-header">
- <text class="section-title">应用</text>
- <view class="section-arrow">></view>
- </view>
- <view class="section-body">
- <view v-if="apps.length" class="list">
- <view
- v-for="a in apps"
- :key="String(a.id)"
- class="row-item"
- @click="openApp(a)"
- >
- <SystemAvatar :name="a.name" :size="64" unit="rpx" />
- <view class="row-main">
- <text class="row-title">{{ a.name }}</text>
- <text class="row-sub">{{ a.categoryName || '' }}</text>
- </view>
- </view>
- </view>
- <view v-else class="empty-sub">暂无匹配应用</view>
- </view>
- </view>
- <view class="bottom-spacer" />
- </scroll-view>
- <!-- 加载提示(只在搜索中显示) -->
- <view class="loading-mask" v-if="loading">
- <text class="loading-text">搜索中...</text>
- </view>
- </view>
- </template>
- <script>
- import UserAvatar from '../../components/UserAvatar.vue'
- import SystemAvatar from '../../components/SystemAvatar.vue'
- import { getToken, getLaunchpadApps, searchUsers, ssoLogin } from '../../utils/api'
- import { chatStore } from '../../store/chat'
- export default {
- components: { UserAvatar, SystemAvatar },
- data() {
- return {
- keyword: '',
- activeKey: 'contacts',
- scrollIntoView: '',
- loading: false,
- contacts: [],
- apps: [],
- messageHits: [],
- chips: [
- { key: 'messages', label: '消息' },
- { key: 'contacts', label: '联系人' },
- { key: 'apps', label: '应用' }
- ]
- }
- },
- computed: {
- messageSearchEmptyText() {
- return '暂无匹配消息(仅搜索已加载的聊天记录)'
- }
- },
- onLoad(options) {
- try {
- const kw = options && options.keyword ? options.keyword : ''
- if (/%[0-9A-Fa-f]{2}/.test(String(kw))) this.keyword = decodeURIComponent(String(kw))
- else this.keyword = String(kw || '')
- } catch (e) {}
- // 如果从外部带了关键字,直接触发一次搜索
- if (this.keyword.trim()) {
- this.doSearch()
- }
- },
- methods: {
- onCancel() {
- uni.navigateBack()
- },
- onConfirm() {
- this.doSearch()
- },
- onChipClick(key) {
- this.activeKey = key
- // scroll-into-view 需要存在于 scroll-view 内的子节点 id
- this.scrollIntoView = 'sec-' + key
- },
- async doSearch() {
- const q = String(this.keyword || '').trim()
- if (!q) {
- this.contacts = []
- this.apps = []
- this.messageHits = []
- return
- }
- this.loading = true
- try {
- const token = getToken()
- if (!token) {
- this.contacts = []
- this.apps = []
- this.messageHits = []
- uni.showToast({ title: '请先登录', icon: 'none' })
- return
- }
- const [contactsRes, appsRes] = await Promise.allSettled([
- searchUsers(token, q, 20),
- (async () => {
- const res = await getLaunchpadApps()
- const items = res && Array.isArray(res.items) ? res.items : []
- const filtered = items.filter((it) => {
- const name = String(it.app_name || '')
- return name.includes(q)
- })
- return filtered.map((it) => ({
- id: it.app_id ?? it.id,
- name: it.app_name || '应用',
- categoryName: it.category_name || '分类'
- }))
- })()
- ])
- let contacts = []
- if (contactsRes.status === 'fulfilled' && contactsRes.value) {
- const res = contactsRes.value
- if (Array.isArray(res)) contacts = res
- else if (res && Array.isArray(res.items)) contacts = res.items
- }
- this.contacts = contacts
- this.apps = appsRes.status === 'fulfilled' && Array.isArray(appsRes.value) ? appsRes.value : []
- this.messageHits = this.searchCachedMessages(q)
- } finally {
- this.loading = false
- }
- },
- /** 在 chatStore.messages 中按正文/标题匹配(仅已加载会话) */
- searchCachedMessages(q) {
- const needle = String(q || '').trim().toLowerCase()
- if (!needle) return []
- const contacts = chatStore.contacts || []
- const peerName = (id) => {
- const c = contacts.find((x) => String(x.user_id || x.id) === String(id))
- return (c && (c.app_name || c.title)) ? String(c.app_name || c.title) : ''
- }
- const hits = []
- const byContact = chatStore.messages || {}
- for (const pid of Object.keys(byContact)) {
- const list = byContact[pid] || []
- for (const msg of list) {
- if (msg && msg.tempId) continue
- const mid = msg.id != null ? String(msg.id) : ''
- if (mid.startsWith('temp')) continue
- const blob = [msg.content, msg.title].filter(Boolean).join(' ')
- if (!blob.toLowerCase().includes(needle)) continue
- const preview = String(msg.content || msg.title || '').trim()
- const ts = msg.createdAt ? new Date(msg.createdAt).getTime() : 0
- hits.push({
- messageId: mid,
- peerId: String(pid),
- peerName: peerName(pid),
- preview: preview.length > 120 ? preview.slice(0, 120) + '…' : preview,
- _ts: ts
- })
- }
- }
- hits.sort((a, b) => b._ts - a._ts)
- return hits.slice(0, 20).map(({ _ts, ...h }) => h)
- },
- openMessageHit(h) {
- if (!h || !h.peerId) return
- let url = '/pages/chat/index?contactId=' + encodeURIComponent(h.peerId)
- if (h.peerName) url += '&contactName=' + encodeURIComponent(h.peerName)
- if (h.messageId) url += '&scrollToMessageId=' + encodeURIComponent(h.messageId)
- uni.navigateTo({ url })
- },
- openContact(u) {
- const id = String(u?.id ?? '').trim()
- if (!id) return
- uni.navigateTo({
- url:
- '/pages/contact-detail/index?contactId=' +
- encodeURIComponent(id) +
- '&contactName=' +
- encodeURIComponent(u?.name || '') +
- '&contactEnglishName=' +
- encodeURIComponent(u?.english_name || '')
- })
- },
- async openApp(app) {
- if (!app || !app.id) return
- uni.showLoading({ title: '打开中...' })
- try {
- const res = await ssoLogin(app.id)
- const redirectUrl = res && (res.redirect_url || res.redirectUrl)
- if (!redirectUrl) {
- uni.showToast({ title: '打开失败', icon: 'none' })
- return
- }
- const pageUrl =
- '/pages/webview/index?url=' +
- encodeURIComponent(redirectUrl) +
- '&title=' +
- encodeURIComponent(app.name || '应用')
- uni.navigateTo({ url: pageUrl })
- } catch (e) {
- uni.showToast({ title: '打开失败', icon: 'none' })
- } finally {
- uni.hideLoading()
- }
- }
- }
- }
- </script>
- <style scoped>
- .search-center-page {
- height: 100vh;
- background: #f5f5f7;
- display: flex;
- flex-direction: column;
- position: relative;
- }
- .search-header {
- background: #fff;
- border-bottom: 1rpx solid #eee;
- padding: 0 24rpx 20rpx 24rpx;
- /* 顶部安全区:与消息/通讯录页面 custom-header 对齐,避免刘海屏点击不到 */
- padding-top: 88rpx;
- padding-top: max(88rpx, calc(24rpx + constant(safe-area-inset-top)));
- padding-top: max(88rpx, calc(24rpx + env(safe-area-inset-top)));
- }
- .header-row {
- display: flex;
- align-items: center;
- gap: 16rpx;
- }
- .search-wrap {
- flex: 1;
- min-width: 0;
- display: flex;
- align-items: center;
- gap: 12rpx;
- padding: 14rpx 18rpx;
- border-radius: 999rpx;
- background: #f0f0f0;
- }
- .search-icon {
- width: 28rpx;
- height: 28rpx;
- opacity: 0.75;
- }
- .search-input {
- flex: 1;
- min-width: 0;
- font-size: 28rpx;
- height: 44rpx;
- line-height: 44rpx;
- padding: 0;
- }
- .cancel {
- color: #259653;
- font-size: 28rpx;
- font-weight: 600;
- padding: 10rpx 8rpx;
- white-space: nowrap;
- }
- .chips-scroll {
- background: #fff;
- border-bottom: 1rpx solid #eee;
- }
- .chips-row {
- display: flex;
- align-items: center;
- gap: 16rpx;
- padding: 18rpx 24rpx;
- }
- .chip {
- padding: 10rpx 20rpx;
- border-radius: 999rpx;
- background: #f3f4f6;
- color: #111827;
- font-size: 26rpx;
- white-space: nowrap;
- }
- .chip.active {
- background: rgba(37, 150, 83, 0.12);
- color: #259653;
- font-weight: 700;
- }
- .results-scroll {
- flex: 1;
- min-height: 0;
- height: 0;
- padding: 20rpx 24rpx 0;
- box-sizing: border-box;
- }
- .empty-state {
- padding: 120rpx 10rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- }
- .empty-text {
- color: #9ca3af;
- font-size: 28rpx;
- }
- .section {
- margin-bottom: 22rpx;
- background: #fff;
- border-radius: 24rpx;
- padding: 18rpx 18rpx 14rpx;
- box-shadow: 0 2rpx 10rpx rgba(17, 24, 39, 0.04);
- }
- .section-header {
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 0 6rpx 10rpx;
- }
- .section-title {
- font-size: 30rpx;
- font-weight: 800;
- color: #111827;
- }
- .section-arrow {
- color: #9ca3af;
- font-size: 32rpx;
- font-weight: 600;
- }
- .section-body {
- padding: 6rpx 6rpx 0;
- }
- .list {
- display: flex;
- flex-direction: column;
- gap: 14rpx;
- }
- .row-item {
- display: flex;
- align-items: center;
- gap: 18rpx;
- padding: 14rpx 12rpx;
- border-radius: 18rpx;
- }
- .row-main {
- flex: 1;
- min-width: 0;
- }
- .row-title {
- display: block;
- font-size: 30rpx;
- font-weight: 700;
- color: #111827;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
- .row-sub {
- display: block;
- margin-top: 6rpx;
- font-size: 24rpx;
- color: #6b7280;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
- .empty-sub {
- padding: 22rpx 6rpx 28rpx;
- color: #9ca3af;
- font-size: 26rpx;
- }
- .badge {
- min-width: 42rpx;
- height: 36rpx;
- padding: 0 12rpx;
- border-radius: 18rpx;
- background: #f5222d;
- color: #fff;
- font-size: 22rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- font-weight: 700;
- }
- .loading-mask {
- position: absolute;
- left: 0;
- right: 0;
- top: 0;
- bottom: 0;
- background: rgba(255, 255, 255, 0.65);
- display: flex;
- align-items: flex-start;
- justify-content: center;
- padding-top: 240rpx;
- z-index: 10;
- }
- .loading-text {
- background: rgba(255, 255, 255, 0.95);
- padding: 18rpx 28rpx;
- border-radius: 20rpx;
- font-size: 28rpx;
- color: #111827;
- font-weight: 700;
- }
- .bottom-spacer {
- height: 60rpx;
- }
- </style>
|