feat:封装axios接口

This commit is contained in:
2026-07-28 22:57:45 +08:00
parent efa27f126d
commit 1f88341019
13 changed files with 306 additions and 277 deletions

View File

@@ -1,82 +1,60 @@
import { defineStore } from 'pinia'
import type { ILibraryFile, ITreeNode } from '@/types/library.ts'
import type { ILibraryFile } from '@/types/library.ts'
import {
addLibraryFileApi,
queryLibraryFileByIdApi,
queryLibraryFileListApi,
updateLibraryFileApi
} from '@/apis/library.ts'
import type { IHandleApi, IOptions, ITreeNode } from '@/types'
import { ElMessage } from 'element-plus'
import { getModuleOptions, getSubjectOptions, getSubjectTree } from '@/utils/library.ts'
export const useLibraryStore = defineStore('library', {
state: () => ({
selectedSubject: 1,
selectedModule: 101,
subjectTree: [
{
id: 1,
name: '中医学',
children: [
{ id: 101, name: '中医基础理论' },
{ id: 102, name: '中医诊断学' },
{ id: 103, name: '中药学' },
{ id: 104, name: '方剂学' },
{ id: 105, name: '中医内科学' },
{ id: 106, name: '中医外科学' },
{ id: 107, name: '中医妇科学' },
{ id: 108, name: '中医儿科学' },
{ id: 109, name: '针灸学' },
{ id: 110, name: '推拿学' },
{ id: 111, name: '中医骨伤科学' },
{ id: 112, name: '黄帝内经' },
{ id: 113, name: '伤寒论' },
{ id: 114, name: '金匮要略' },
{ id: 115, name: '温病学' },
{ id: 116, name: '中医养生学' }
]
},
{
id: 2,
name: '公务员',
children: [
{ id: 201, name: '言语理解与表达' },
{ id: 202, name: '数量关系' },
{ id: 203, name: '判断推理' },
{ id: 204, name: '资料分析' },
{ id: 205, name: '常识判断' },
{ id: 206, name: '申论写作' },
{ id: 207, name: '公文写作' },
{ id: 208, name: '公共基础知识' },
{ id: 209, name: '面试技巧' },
{ id: 210, name: '时政热点' },
{ id: 211, name: '法律基础' }
]
}
] as ITreeNode[],
selectedSubject: '',
selectedModule: '',
subjectTree: [] as ITreeNode[],
fileList: [] as ILibraryFile[],
currentFile: {} as ILibraryFile,
fileDetailDialogVisible: false,
fileDialogVisible: false
fileDialogVisible: false,
hasGenerateQuestion: false,
fileApiType: 'ADD' as IHandleApi,
subjectOptions: [] as IOptions[],
moduleOptions: [] as IOptions[]
}),
actions: {
queryFileList() {
this.fileList = [
{
id: 1,
subject: '中医学',
module: '中医基础理疗',
name: '向量代数.pdf',
size: 2.4,
uploadTime: '2分钟前',
type: 'pdf',
questions: [
{ id: 1, type: 'single', question: '设 a=(1,2,3), b=(2,-1,1),求 a·b = ?', options: ['A. 3', 'B. 5', 'C. 7', 'D. 9'], answer: ['A'] }
]
},
{
id: 2,
subject: '中医学',
module: '中医基础理疗',
name: '向量代数精讲.png',
size: 156,
uploadTime: '3小时前',
type: 'image',
questions: []
}
]
async queryFileList() {
this.fileList = []
this.fileList = await queryLibraryFileListApi()
this.subjectTree = getSubjectTree(this.fileList)
this.subjectOptions = getSubjectOptions(this.subjectTree)
this.selectedSubject = this.subjectOptions[0].label
this.moduleOptions = getModuleOptions(this.subjectTree, this.selectedSubject)
this.selectedModule = this.moduleOptions[0].label
},
async queryFileByList(id: number) {
this.currentFile = await queryLibraryFileByIdApi(id)
},
async handleFileApi(file: ILibraryFile) {
switch (this.fileApiType) {
case 'ADD':
await addLibraryFileApi(file)
ElMessage.success('新增资料成功')
break
case 'UPDATE':
await updateLibraryFileApi(file.id as number, file)
ElMessage.success('更新资料成功')
break
case 'DELETE':
ElMessage.success('删除资料成功')
break
default:
break
}
this.fileDialogVisible = false
await this.queryFileList()
},
getEmptyFile(): ILibraryFile {
return {
@@ -87,6 +65,7 @@ export const useLibraryStore = defineStore('library', {
subject: '',
uploadTime: '',
type: '',
content: '',
questions: []
}
}