mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-06 23:21:16 +08:00
38 lines
978 B
JavaScript
38 lines
978 B
JavaScript
import { authState, getToken } from '~/utils/auth'
|
|
|
|
async function searchPost(apiBaseUrl, keyword) {
|
|
return await fetch(`${apiBaseUrl}/api/search/posts/title?keyword=${keyword}`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Authorization: `Bearer ${getToken()}`,
|
|
},
|
|
})
|
|
}
|
|
|
|
export default (apiBaseUrl, websiteBaseUrl) => {
|
|
return {
|
|
key: '#',
|
|
hint: async (keyword) => {
|
|
if (!keyword.trim()) return []
|
|
try {
|
|
const response = await searchPost(apiBaseUrl, keyword)
|
|
if (response.ok) {
|
|
const body = await response.json()
|
|
let value = ''
|
|
return (
|
|
body.map((item) => ({
|
|
value: `[🔗${item.title}](${websiteBaseUrl}/posts/${item.id})`,
|
|
html: `<div><i class="fas fa-link"></i> ${item.title}</div>`,
|
|
})) ?? []
|
|
)
|
|
} else {
|
|
return []
|
|
}
|
|
} catch {
|
|
return []
|
|
}
|
|
},
|
|
}
|
|
}
|