|
|
|
@ -252,28 +252,22 @@ export default createStore({
|
|
|
|
|
// 查询个人借书记录 - 符合接口文档1.6
|
|
|
|
|
async fetchBorrowRecords() {
|
|
|
|
|
const response = await service.get('/user/findone')
|
|
|
|
|
return response.data
|
|
|
|
|
|
|
|
|
|
// 根据接口文档1.6,管理员返回数组,普通用户返回单个对象
|
|
|
|
|
if (response.data && response.data.code === 200) {
|
|
|
|
|
return response.data
|
|
|
|
|
} else if (response.data && response.data.code === 1) {
|
|
|
|
|
throw new Error(response.data.message || '获取借阅记录失败')
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error('获取借阅记录失败')
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 获取当前用户已借书籍 - 符合接口文档2.5
|
|
|
|
|
async fetchBorrowedBooks({ commit }) {
|
|
|
|
|
const response = await service.get('/user/borrow/books')
|
|
|
|
|
|
|
|
|
|
// 按照接口文档2.5,返回书籍数组
|
|
|
|
|
if (response.data && Array.isArray(response.data)) {
|
|
|
|
|
commit('setBorrowedBooks', response.data)
|
|
|
|
|
return { data: response.data }
|
|
|
|
|
// 按照接口文档处理响应
|
|
|
|
|
if (response.data.code === 200) {
|
|
|
|
|
commit('setBorrowedBooks', response.data.data || [])
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error('获取已借书籍失败')
|
|
|
|
|
throw new Error(response.data.message || '获取已借书籍失败')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response.data
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 租借书籍 - 符合接口文档3.1
|
|
|
|
@ -319,7 +313,8 @@ async returnBook({ dispatch }, { title }) {
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 查询全部书籍 - 符合接口文档2.2
|
|
|
|
|
|
|
|
|
|
// 查询全部书籍 - 符合接口文档
|
|
|
|
|
async fetchBooks(_, params = {}) {
|
|
|
|
|
const config = {
|
|
|
|
|
params: {
|
|
|
|
@ -330,22 +325,24 @@ async returnBook({ dispatch }, { title }) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const response = await service.get('/api/select', config)
|
|
|
|
|
|
|
|
|
|
// 根据接口文档2.2,直接返回数组
|
|
|
|
|
console.log('请求书籍:', route.params.title)
|
|
|
|
|
console.log('API响应:', response)
|
|
|
|
|
// 处理不同响应格式
|
|
|
|
|
let list = []
|
|
|
|
|
let total = 0
|
|
|
|
|
|
|
|
|
|
// 检查响应格式
|
|
|
|
|
if (response.data && Array.isArray(response.data)) {
|
|
|
|
|
list = response.data
|
|
|
|
|
total = response.data.length
|
|
|
|
|
if (Array.isArray(response)) {
|
|
|
|
|
list = response
|
|
|
|
|
total = response.length
|
|
|
|
|
} else if (Array.isArray(response.data)) {
|
|
|
|
|
list = response.data
|
|
|
|
|
total = response.data.length
|
|
|
|
|
} else {
|
|
|
|
|
console.warn('API返回格式不符合预期:', response)
|
|
|
|
|
list = []
|
|
|
|
|
total = 0
|
|
|
|
|
} else if (response.data && Array.isArray(response.data.data)) {
|
|
|
|
|
list = response.data.data
|
|
|
|
|
total = response.data.total || response.data.data.length
|
|
|
|
|
} else if (response.data && Array.isArray(response.data.list)) {
|
|
|
|
|
list = response.data.list
|
|
|
|
|
total = response.data.total || response.data.list.length
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
@ -395,19 +392,20 @@ async returnBook({ dispatch }, { title }) {
|
|
|
|
|
|
|
|
|
|
// 管理员删除书籍 - 符合接口文档2.4
|
|
|
|
|
async deleteBook(_, { title }) {
|
|
|
|
|
const response = await service.post('/user/delete',
|
|
|
|
|
`title=${encodeURIComponent(title)}`,
|
|
|
|
|
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if (response.data && response.data.code === 200) {
|
|
|
|
|
return response.data
|
|
|
|
|
} else if (response.data && response.data.code === 1) {
|
|
|
|
|
throw new Error(response.data.message || '删除书籍失败')
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error('删除书籍失败')
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
const response = await service.post('/user/delete',
|
|
|
|
|
`title=${encodeURIComponent(title)}`,
|
|
|
|
|
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 根据接口文档修改响应处理
|
|
|
|
|
if (response.data && response.data.code === 200) {
|
|
|
|
|
return response.data
|
|
|
|
|
} else {
|
|
|
|
|
// 统一处理错误消息
|
|
|
|
|
const errorMsg = response.data?.message || '删除书籍失败'
|
|
|
|
|
throw new Error(errorMsg)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 本周热租榜 - 符合接口文档4.1
|
|
|
|
|
async fetchWeeklyRank() {
|
|
|
|
@ -431,6 +429,11 @@ async returnBook({ dispatch }, { title }) {
|
|
|
|
|
} else {
|
|
|
|
|
return { data: [] }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
})
|