57 lines
1.3 KiB
TypeScript
57 lines
1.3 KiB
TypeScript
import { type CardType } from './CardType'
|
|
import request from '../request'
|
|
|
|
const file_api = {
|
|
root_url: '/api',
|
|
cards: {
|
|
url: 'cards',
|
|
type: 'json',
|
|
home: 'home',
|
|
},
|
|
}
|
|
|
|
export function GetRealHref(href: string, user_name: string = '$') {
|
|
if (href.startsWith('$')) {
|
|
href = `/card${href.substring(1)}?user_name=${user_name}`
|
|
}
|
|
return href
|
|
}
|
|
|
|
export async function GetCardsData(cards: CardType[], url?: string) {
|
|
await request
|
|
.get(`files/cards/${url ? url : 'index-content.json'}`)
|
|
.then((response) => {
|
|
if (response.status != 200) {
|
|
throw new Error(`HTTP error! status: ${response.status}`)
|
|
// return null
|
|
}
|
|
return response.data
|
|
})
|
|
.then((jsonData) => {
|
|
// console.log(jsonData)
|
|
if (!jsonData || !cards) {
|
|
return null
|
|
}
|
|
cards.length = 0
|
|
jsonData.datas.forEach((i: CardType) => {
|
|
const item = { ...jsonData.default_item, ...i }
|
|
cards.push({
|
|
title: item.title,
|
|
body: item.intro,
|
|
jumpHref: item.url,
|
|
imgSrc: null,
|
|
imgAlt: null,
|
|
btnText: null,
|
|
btnHref: null,
|
|
})
|
|
})
|
|
})
|
|
.catch((error) => {
|
|
console.error('Error fetching data:', error)
|
|
})
|
|
.finally(function () {
|
|
// 总是会执行
|
|
console.log('finally', cards)
|
|
})
|
|
}
|