|
|
@ -1,18 +1,12 @@
|
|
|
|
import { createStore } from 'vuex'
|
|
|
|
import { createStore } from 'vuex'
|
|
|
|
import service from '../utils/request'
|
|
|
|
import service from '../utils/request'
|
|
|
|
|
|
|
|
|
|
|
|
// // 配置axios基础路径
|
|
|
|
|
|
|
|
// axios.defaults.baseURL = 'http://localhost:8877'
|
|
|
|
|
|
|
|
// // 允许跨域携带cookie
|
|
|
|
|
|
|
|
// axios.defaults.withCredentials = true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default createStore({
|
|
|
|
export default createStore({
|
|
|
|
state: {
|
|
|
|
state: {
|
|
|
|
user: null,
|
|
|
|
user: null,
|
|
|
|
balance: 0,
|
|
|
|
balance: 0,
|
|
|
|
vipLevel: 0,
|
|
|
|
vipLevel: 0,
|
|
|
|
borrowedBooks: []
|
|
|
|
borrowedBooks: []
|
|
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
getters: {
|
|
|
|
getters: {
|
|
|
|
isAuthenticated: state => !!state.user,
|
|
|
|
isAuthenticated: state => !!state.user,
|
|
|
@ -44,13 +38,12 @@ export default createStore({
|
|
|
|
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
|
|
|
|
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if (response.data.code !== 200) {
|
|
|
|
|
|
|
|
throw new Error(response.data.message || '登录失败')
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 登录成功后获取用户信息
|
|
|
|
// 登录成功后获取用户信息
|
|
|
|
await dispatch('fetchUser')
|
|
|
|
const userInfo = await dispatch('fetchUser')
|
|
|
|
return response.data
|
|
|
|
return {
|
|
|
|
|
|
|
|
...response.data,
|
|
|
|
|
|
|
|
user: userInfo
|
|
|
|
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 用户注册
|
|
|
|
// 用户注册
|
|
|
@ -60,70 +53,82 @@ export default createStore({
|
|
|
|
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
|
|
|
|
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if (response.data.code !== 200) {
|
|
|
|
|
|
|
|
throw new Error(response.data.message || '注册失败')
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response.data
|
|
|
|
return response.data
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 获取当前用户信息
|
|
|
|
// 获取当前用户信息 - 符合接口文档
|
|
|
|
async fetchUser({ commit, dispatch }) {
|
|
|
|
async fetchUser({ commit, dispatch }) {
|
|
|
|
const response = await service.get('/user/getinfo')
|
|
|
|
try {
|
|
|
|
|
|
|
|
const userData = await service.get('/user/getinfo')
|
|
|
|
// 接口可能返回两种格式:
|
|
|
|
|
|
|
|
// 1. 带code的标准格式:{code:200, message:..., data: null}
|
|
|
|
// 用户信息接口直接返回用户对象
|
|
|
|
// 2. 直接返回用户信息:{username: "张三", pic: "..."}
|
|
|
|
commit('setUser', {
|
|
|
|
if (response.data.code === 200) {
|
|
|
|
username: userData.username || '',
|
|
|
|
// 情况1:如果code=200但data为null,可能接口直接在根节点返回用户信息
|
|
|
|
pic: userData.pic || '',
|
|
|
|
const userData = response.data.data || response.data;
|
|
|
|
admin: userData.admin || false
|
|
|
|
// 过滤掉非用户信息字段(如code/message)
|
|
|
|
})
|
|
|
|
const filteredUser = {
|
|
|
|
|
|
|
|
username: userData.username || '',
|
|
|
|
// 获取关联信息
|
|
|
|
pic: userData.pic || ''
|
|
|
|
try {
|
|
|
|
};
|
|
|
|
await dispatch('fetchBalanceAndVip')
|
|
|
|
commit('setUser', filteredUser);
|
|
|
|
} catch (balanceError) {
|
|
|
|
await dispatch('fetchBalanceAndVip');
|
|
|
|
console.error('获取余额信息失败:', balanceError)
|
|
|
|
await dispatch('fetchBorrowedBooks');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 处理接口返回非200的情况(如未登录)
|
|
|
|
try {
|
|
|
|
commit('setUser', null);
|
|
|
|
await dispatch('fetchBorrowedBooks')
|
|
|
|
}
|
|
|
|
} catch (booksError) {
|
|
|
|
|
|
|
|
console.error('获取借阅书籍失败:', booksError)
|
|
|
|
return response.data;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
return userData
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
commit('clearUser')
|
|
|
|
|
|
|
|
throw error
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 获取余额和VIP等级
|
|
|
|
// 获取余额和VIP等级
|
|
|
|
async fetchBalanceAndVip({ commit }) {
|
|
|
|
async fetchBalanceAndVip({ commit }) {
|
|
|
|
const response = await service.post('/user/findmoney')
|
|
|
|
try {
|
|
|
|
|
|
|
|
const response = await service.post('/user/findmoney')
|
|
|
|
if (response.data.code === 200) {
|
|
|
|
|
|
|
|
// 解析消息提取余额和VIP等级
|
|
|
|
// 解析消息提取余额和VIP等级
|
|
|
|
const balanceMatch = response.data.message.match(/余额为:(\d+\.\d+)元/)
|
|
|
|
const message = response.data.message || ''
|
|
|
|
const vipMatch = response.data.message.match(/当前VIP等级为:(\d+)/)
|
|
|
|
const balanceMatch = message.match(/余额为:(\d+\.\d+)元/)
|
|
|
|
|
|
|
|
const vipMatch = message.match(/当前VIP等级为:(\d+)/)
|
|
|
|
|
|
|
|
|
|
|
|
if (balanceMatch && vipMatch) {
|
|
|
|
if (balanceMatch && vipMatch) {
|
|
|
|
commit('setBalanceAndVip', {
|
|
|
|
commit('setBalanceAndVip', {
|
|
|
|
balance: parseFloat(balanceMatch[1]),
|
|
|
|
balance: parseFloat(balanceMatch[1]),
|
|
|
|
vip: parseInt(vipMatch[1])
|
|
|
|
vip: parseInt(vipMatch[1])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.error('无法解析余额信息:', message)
|
|
|
|
|
|
|
|
// 尝试从数据字段获取
|
|
|
|
|
|
|
|
if (response.data.data) {
|
|
|
|
|
|
|
|
commit('setBalanceAndVip', {
|
|
|
|
|
|
|
|
balance: parseFloat(response.data.data.balance) || 0,
|
|
|
|
|
|
|
|
vip: parseInt(response.data.data.vipLevel) || 0
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
console.error('获取余额失败:', error)
|
|
|
|
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 账户充值
|
|
|
|
// 账户充值
|
|
|
|
async recharge(_, { money }) {
|
|
|
|
async recharge(_, { money }) {
|
|
|
|
const response = await service.post('/user/recharge',
|
|
|
|
const response = await service.post('/user/recharge',
|
|
|
|
`money=${money}`,
|
|
|
|
`money=${money}`,
|
|
|
|
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
|
|
|
|
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
// 充值后刷新余额
|
|
|
|
if (response.data.code !== 200) {
|
|
|
|
await dispatch('fetchBalanceAndVip')
|
|
|
|
throw new Error(response.data.message || '充值失败')
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response.data
|
|
|
|
return response.data
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
@ -133,136 +138,130 @@ export default createStore({
|
|
|
|
return { code: 200, message: '已退出登录' }
|
|
|
|
return { code: 200, message: '已退出登录' }
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 获取借阅记录
|
|
|
|
// 查询个人借书记录
|
|
|
|
async fetchBorrowRecords() {
|
|
|
|
async fetchBorrowRecords() {
|
|
|
|
const response = await service.get('/user/findone')
|
|
|
|
const response = await service.get('/user/findone')
|
|
|
|
|
|
|
|
|
|
|
|
if (response.data.code !== 200) {
|
|
|
|
|
|
|
|
throw new Error(response.data.message || '获取借阅记录失败')
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response.data
|
|
|
|
return response.data
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 获取当前用户已借书籍
|
|
|
|
// 获取当前用户已借书籍
|
|
|
|
async fetchBorrowedBooks({ commit }) {
|
|
|
|
async fetchBorrowedBooks({ commit }) {
|
|
|
|
const response = await service.get('/user/borrow/books')
|
|
|
|
const response = await service.get('/user/borrow/books')
|
|
|
|
|
|
|
|
|
|
|
|
if (response.data.code === 200) {
|
|
|
|
// 按照接口文档处理响应
|
|
|
|
commit('setBorrowedBooks', response.data.data || [])
|
|
|
|
if (response.data.code === 200) {
|
|
|
|
}
|
|
|
|
commit('setBorrowedBooks', response.data.data || [])
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return response.data
|
|
|
|
throw new Error(response.data.message || '获取已借书籍失败')
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 借书
|
|
|
|
// 租借书籍
|
|
|
|
async borrowBook(_, { title }) {
|
|
|
|
async borrowBook(_, { title }) {
|
|
|
|
const response = await service.post('/borrow/borrowbook',
|
|
|
|
const response = await service.post('/borrow/borrowbook',
|
|
|
|
`title=${encodeURIComponent(title)}`,
|
|
|
|
`title=${encodeURIComponent(title)}`,
|
|
|
|
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
|
|
|
|
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if (response.data.code !== 200) {
|
|
|
|
|
|
|
|
throw new Error(response.data.message || '借阅失败')
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response.data
|
|
|
|
return response.data
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 还书
|
|
|
|
// 归还书籍
|
|
|
|
async returnBook(_, { title }) {
|
|
|
|
async returnBook(_, { title }) {
|
|
|
|
const response = await service.post('/borrow/returnbook',
|
|
|
|
const response = await service.post('/borrow/returnbook',
|
|
|
|
`title=${encodeURIComponent(title)}`,
|
|
|
|
`title=${encodeURIComponent(title)}`,
|
|
|
|
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
|
|
|
|
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if (response.data.code !== 200) {
|
|
|
|
|
|
|
|
throw new Error(response.data.message || '归还失败')
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response.data
|
|
|
|
return response.data
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 查询全部书籍 - 符合接口文档
|
|
|
|
async fetchBooks(_, params = {}) {
|
|
|
|
async fetchBooks(_, params = {}) {
|
|
|
|
let url = '/api/select'
|
|
|
|
const config = {
|
|
|
|
const queryParams = new URLSearchParams()
|
|
|
|
params: {
|
|
|
|
|
|
|
|
page: params.page || 1,
|
|
|
|
if (params.page) queryParams.append('page', params.page)
|
|
|
|
pageSize: params.pageSize || 10,
|
|
|
|
if (params.pageSize) queryParams.append('pageSize', params.pageSize)
|
|
|
|
title: params.keyword || ''
|
|
|
|
if (params.keyword) queryParams.append('title', params.keyword)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (queryParams.toString()) {
|
|
|
|
|
|
|
|
url += `?${queryParams.toString()}`
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const response = await service.get(url)
|
|
|
|
const response = await service.get('/api/select', config)
|
|
|
|
|
|
|
|
|
|
|
|
if (response.data.code !== 200) {
|
|
|
|
// 处理不同响应格式
|
|
|
|
throw new Error(response.data.message || '获取书籍列表失败')
|
|
|
|
let list = []
|
|
|
|
|
|
|
|
let total = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(response)) {
|
|
|
|
|
|
|
|
list = response
|
|
|
|
|
|
|
|
total = response.length
|
|
|
|
|
|
|
|
} else if (Array.isArray(response.data)) {
|
|
|
|
|
|
|
|
list = response.data
|
|
|
|
|
|
|
|
total = response.data.length
|
|
|
|
|
|
|
|
} 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 {
|
|
|
|
return {
|
|
|
|
data: {
|
|
|
|
data: {
|
|
|
|
list: response.data.data || [],
|
|
|
|
list,
|
|
|
|
total: response.data.data?.length || 0
|
|
|
|
total
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
async fetchBookById(_, id) {
|
|
|
|
// 根据书名查单本书 - 符合接口文档
|
|
|
|
const response = await service.get(`/api/selectone?id=${id}`)
|
|
|
|
async fetchBookByTitle(_, title) {
|
|
|
|
|
|
|
|
const response = await service.get('/api/selectone', {
|
|
|
|
|
|
|
|
params: { title }
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
if (response.data.code !== 200) {
|
|
|
|
// 处理不同响应格式
|
|
|
|
throw new Error(response.data.message || '获取书籍详情失败')
|
|
|
|
let book = null
|
|
|
|
|
|
|
|
if (response.data) {
|
|
|
|
|
|
|
|
book = response.data.data || response.data
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
book = response
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return { data: response.data.data }
|
|
|
|
return { data: book }
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 新增书籍
|
|
|
|
async addBook(_, bookData) {
|
|
|
|
async addBook(_, bookData) {
|
|
|
|
const response = await service.post('/api/add', bookData, {
|
|
|
|
const response = await service.post('/api/add', bookData, {
|
|
|
|
headers: { 'Content-Type': 'application/json' }
|
|
|
|
headers: { 'Content-Type': 'application/json' }
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
if (response.data.code !== 200) {
|
|
|
|
|
|
|
|
throw new Error(response.data.message || '添加书籍失败')
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response.data
|
|
|
|
return response.data
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 管理员删除书籍
|
|
|
|
async deleteBook(_, { title }) {
|
|
|
|
async deleteBook(_, { title }) {
|
|
|
|
const response = await service.post('/user/delete',
|
|
|
|
const response = await service.post('/user/delete',
|
|
|
|
`title=${encodeURIComponent(title)}`,
|
|
|
|
`title=${encodeURIComponent(title)}`,
|
|
|
|
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
|
|
|
|
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if (response.data.code !== 200) {
|
|
|
|
|
|
|
|
throw new Error(response.data.message || '删除书籍失败')
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response.data
|
|
|
|
return response.data
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 本周热租榜 - 符合接口文档
|
|
|
|
async fetchWeeklyRank() {
|
|
|
|
async fetchWeeklyRank() {
|
|
|
|
const response = await service.get('/api/rank/weekly')
|
|
|
|
const response = await service.get('/api/rank/weekly')
|
|
|
|
|
|
|
|
return { data: Array.isArray(response) ? response : response.data || [] }
|
|
|
|
if (response.data.code !== 200) {
|
|
|
|
|
|
|
|
throw new Error(response.data.message || '获取本周热租榜失败')
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { data: response.data.data || [] }
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 本月热租榜 - 符合接口文档
|
|
|
|
async fetchMonthlyRank() {
|
|
|
|
async fetchMonthlyRank() {
|
|
|
|
const response = await service.get('/api/rank/monthly')
|
|
|
|
const response = await service.get('/api/rank/monthly')
|
|
|
|
|
|
|
|
return { data: Array.isArray(response) ? response : response.data || [] }
|
|
|
|
if (response.data.code !== 200) {
|
|
|
|
|
|
|
|
throw new Error(response.data.message || '获取本月热租榜失败')
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { data: response.data.data || [] }
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|