Skip to content

Commit

Permalink
fix: data[students.json]
Browse files Browse the repository at this point in the history
  • Loading branch information
U1805 committed Jan 21, 2025
1 parent 75425cc commit 696326f
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 175 deletions.
4 changes: 2 additions & 2 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ window.addEventListener('resize', () => {
<div class="list-item__mark" v-if="item.School" @click.stop=""
@click=" searchSchool = searchSchool === item.School ? '' : item.School" role="button"
tabindex="0" @keydown.enter=" searchSchool = searchSchool === item.School ? '' : item.School">
<img v-lazy="getSchaleSchoolIcon(item.School)" :alt="`${item.School} icon`" />
<img v-lazy="getSchoolIcon(item.School)" :alt="`${item.School} icon`" />
</div>
<div class="list-item__avatars" @click.stop="" v-show="item === studentShowAvatars">
<img v-for="(avatar, index) in item.Avatars" :key="index" v-lazy="avatar"
Expand All @@ -101,7 +101,7 @@ import { ref, watch } from 'vue'
import { RouterLink, RouterView } from 'vue-router'
import i18n from '@/locales/i18n'
import { baseStudent, studentInfo } from '@/assets/utils/interface'
import { getStudents, getSchaleSchoolIcon } from '@/assets/utils/request'
import { getStudents, getSchoolIcon } from '@/assets/utils/request'
import { download } from '@/assets/imgUtils/download'
import { store } from '@/assets/storeUtils/store'
import { talkHistory } from '@/assets/storeUtils/talkHistory'
Expand Down
2 changes: 1 addition & 1 deletion src/assets/storeUtils/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const store = reactive({
showPlayerDialog: false,
showSettingDialog: false,
storyKey: '10005',
storyList: {} as { [key: string]: string[] },
storyList: {} as Record<string, string>,
storyFile: '1000501',

setData() {
Expand Down
85 changes: 85 additions & 0 deletions src/assets/utils/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import axios from 'axios'
import { ProxyConfig } from './interface'

/*
缓存请求结果
*/
function memorize(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value
const cache = new Map()

descriptor.value = function (...args: any[]) {
const key = JSON.stringify(args)
if (cache.has(key)) {
return cache.get(key)
}
const result = originalMethod.apply(this, args)
cache.set(key, result)
return result
}

return descriptor
}

export class Resource {
config: ProxyConfig | null = null

@memorize
async loadConfig() {
let configUrl = '/api/Momotalk/imageDomain.json'
configUrl = configUrl.replace('/api', 'https://BlueArcbox.github.io/resources')

const response = await axios.get<ProxyConfig>(configUrl)
this.config = response.data
}

proxy(url: string): string
proxy(url: string[]): string[]

@memorize
proxy(url: string | string[]): string | string[] {
if (!this.config) {
throw new Error('Config not loaded. Call loadConfig before using proxy.')
}

if (Array.isArray(url)) {
return url.map((u) => this.proxy(u) as string)
}

// 替换域名
for (const [oldDomain, newDomain] of Object.entries(this.config.domain)) {
if (url.startsWith(oldDomain)) {
url = url.replace(oldDomain, newDomain)
}
}

// 添加代理
Object.keys(this.config?.proxy).forEach((targetDomain) => {
if (url.indexOf(targetDomain) !== -1) {
const proxyDomain = this.config?.proxy[targetDomain]['domain']
const proxyParams = this.config?.proxy[targetDomain]['param']

url = `${proxyDomain}${encodeURIComponent(url as string)}${proxyParams}`
}
})

return url
}

@memorize
async getData(file: string) {
if (!this.config) {
throw new Error('Config not loaded. Call loadConfig before using getData.')
}
const response = await axios.get(this.proxy(file))
return response.data
}

@memorize
getSchale() {
if (!this.config) {
throw new Error('Config not loaded. Call loadConfig before using getSchale.')
}
return this.config.schale
}
}
37 changes: 25 additions & 12 deletions src/assets/utils/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,28 @@ interface studentInfo {
cnt: number
}

/* Aru: studentInfo = {
"Id": 10000,
"Name": "阿露",
"Birthday": "3月12日",
"Avatar": ["https://static.wikia.nocookie.net/blue-archive/images/d/de/Aru_Icon.png"],
"Bio": "什么都能解决哦!",
"Nickname": ["Aru", "社长", "亚瑠", "阿鲁"],
"cnt": 0
}*/
interface LocalStudent {
Id: number
Bio: Record<string, string>
Avatar: string[]
Nickname: string[]
Fixed: Array<{
ItemName: keyof studentInfo
ItemValue: string
}>
Related: {
ItemId: number
ItemType: string
} | null
}

interface SchaleStudent {
Id: number
Name: string
Birthday: string
PathName: string
School: string
}

interface Talk extends baseStudent {
type: number // 0: student| 1: sensei| 2: story| 3: choice| 4:system
Expand All @@ -34,8 +47,8 @@ interface Talk extends baseStudent {

interface ProxyConfig {
schale: string
domain: { [key: string]: string }
proxy: { [key: string]: { domain: string; param: string } }
domain: Record<string, string>
proxy: Record<string, { domain: string; param: string }>
}

export { baseStudent, studentInfo, Talk, ProxyConfig }
export { baseStudent, studentInfo, LocalStudent, SchaleStudent, Talk, ProxyConfig }
Loading

0 comments on commit 696326f

Please sign in to comment.