/** * API请求工具类 */ import config from './config.js' // API基础URL - 从配置文件读取 const BASE_URL = config.BASE_URL /** * 统一请求方法 */ function request(url, method = 'GET', data = {}) { return new Promise((resolve, reject) => { // 显示加载提示 if (config.SHOW_LOADING && method !== 'GET') { uni.showLoading({ title: '请求中...', mask: false }) } uni.request({ url: BASE_URL + url, method: method, data: data, header: { 'Content-Type': 'application/json' }, timeout: config.TIMEOUT, success: (res) => { // 隐藏加载提示 if (config.SHOW_LOADING && method !== 'GET') { uni.hideLoading() } if (res.statusCode === 200) { if (res.data.code === 200) { resolve(res.data) } else { reject(new Error(res.data.message || '请求失败')) } } else { reject(new Error('网络请求失败,状态码:' + res.statusCode)) } }, fail: (err) => { // 隐藏加载提示 if (config.SHOW_LOADING && method !== 'GET') { uni.hideLoading() } console.error('API请求失败:', err) reject(new Error('网络请求失败,请检查网络连接')) } }) }) } /** * GET请求 */ function get(url, data = {}) { return request(url, 'GET', data) } /** * POST请求 */ function post(url, data = {}) { return request(url, 'POST', data) } /** * PUT请求 */ function put(url, data = {}) { return request(url, 'PUT', data) } /** * DELETE请求 */ function del(url, data = {}) { return request(url, 'DELETE', data) } // ============================================ // 用户相关API // ============================================ /** * 用户登录 */ export function login(username, password) { return post('/user/login', { username: username, password: password }) } /** * 用户注册 */ export function register(data) { return post('/user/register', data) } /** * 获取用户信息 */ export function getUserById(id) { return get(`/user/${id}`) } // ============================================ // 书籍相关API // ============================================ /** * 分页查询书籍 */ export function getBooks(params) { return get('/book/list', params) } /** * 获取书籍详情 * @param {Number} id 书籍ID * @param {Number} userId 用户ID(可选,用于VIP权限检查) */ export function getBookById(id, userId) { const params = {} if (userId !== undefined && userId !== null) { params.userId = userId } return get(`/book/${id}`, params) } /** * 获取今日推荐 */ export function getTodayRecommend(limit = 8) { return get('/book/today-recommend', { limit: limit }) } /** * 获取畅销书籍 */ export function getBestsellers(limit = 10) { return get('/book/bestsellers', { limit: limit }) } /** * 获取全部书籍(分页) */ export function getAllBooks(page = 1, size = 10) { return get('/book/all', { page: page, size: size }) } /** * 获取排行榜书籍 */ export function getRankingBooks(limit = 10) { return get('/book/ranking', { limit: limit }) } /** * 获取热门书籍 */ export function getPopularBooks(limit = 10) { return get('/book/popular', { limit: limit }) } /** * 获取新书榜 */ export function getNewBooks(limit = 10) { return get('/book/new', { limit: limit }) } /** * 获取VIP书籍(分页) */ export function getVipBooks(page = 1, size = 10) { return get('/book/vip', { page: page, size: size }) } /** * 获取精品书单 */ export function getFeaturedList(limit = 4) { return get('/book/featured-list', { limit: limit }) } // ============================================ // 分类相关API // ============================================ /** * 获取所有分类列表 */ export function getAllCategories() { return get('/category/list') } /** * 根据ID获取分类详情 */ export function getCategoryById(id) { return get(`/category/${id}`) } /** * 获取更多推荐书籍 */ export function getMoreRecommend(limit = 6) { return get('/book/more-recommend', { limit: limit }) } /** * 创建书籍 */ export function createBook(data) { return post('/book', data) } /** * 获取个人资料 */ export function getUserProfile(userId) { return get('/user/profile', { userId }) } /** * 更新个人资料 */ export function updateUserProfile(payload) { return request('/user/profile', 'PUT', payload) } /** * 更新书籍 */ export function updateBook(id, data) { return put(`/book/${id}`, data) } /** * 删除书籍 */ export function deleteBook(id) { return del(`/book/${id}`) } /** * 批量删除书籍 */ export function deleteBooks(ids) { return del('/book/batch', ids) } // ============================================ // 书架相关API // ============================================ /** * 添加书籍到书架 */ export function addToBookshelf(userId, bookId) { return post('/bookshelf/add', { userId: userId, bookId: bookId }) } /** * 从书架移除书籍 */ export function removeFromBookshelf(userId, bookId) { return request('/bookshelf/remove', 'DELETE', { userId: userId, bookId: bookId }) } /** * 获取用户书架列表 */ export function getBookshelfList(userId) { return get('/bookshelf/list', { userId: userId }) } /** * 检查书籍是否在书架中 */ export function checkInBookshelf(userId, bookId) { return get('/bookshelf/check', { userId: userId, bookId: bookId }) } /** * 更新阅读进度 */ export function updateReadProgress(userId, bookId, readProgress) { return put('/bookshelf/progress', { userId: userId, bookId: bookId, readProgress: readProgress }) } // ============================================ // 听书书架相关API // ============================================ /** * 添加听书到书架 */ export function addAudiobookToBookshelf(userId, audiobookId) { return post('/audiobook-bookshelf/add', { userId: userId, audiobookId: audiobookId }) } /** * 从书架移除听书 */ export function removeAudiobookFromBookshelf(userId, audiobookId) { return request('/audiobook-bookshelf/remove', 'DELETE', { userId: userId, audiobookId: audiobookId }) } /** * 获取用户听书书架列表 */ export function getAudiobookBookshelfList(userId) { return get('/audiobook-bookshelf/list', { userId: userId }) } /** * 检查听书是否在书架中 */ export function checkAudiobookInBookshelf(userId, audiobookId) { return get('/audiobook-bookshelf/check', { userId: userId, audiobookId: audiobookId }) } /** * 更新听书进度 */ export function updateAudiobookListeningProgress(userId, audiobookId, chapterId, position) { return put('/audiobook-bookshelf/progress', { userId: userId, audiobookId: audiobookId, lastListenedChapterId: chapterId, lastListenedPosition: position }) } // ============================================ // 听书相关API // ============================================ /** * 获取最近上新 */ export function getRecentAudiobooks(limit = 8) { return get('/audiobook/recent', { limit: limit }) } /** * 获取热门听书 */ export function getPopularAudiobooks(limit = 10) { return get('/audiobook/popular', { limit: limit }) } /** * 获取推荐听书 */ export function getRecommendAudiobooks(limit = 10) { return get('/audiobook/recommend', { limit: limit }) } /** * 获取畅听榜 */ export function getRankingAudiobooks(limit = 10) { return get('/audiobook/ranking', { limit: limit }) } /** * 获取听书详情(包含章节列表) */ export function getAudiobookDetail(id, userId = null) { const params = userId ? { userId } : {} return get(`/audiobook/${id}`, params) } /** * 获取章节详情 * @param {Number} chapterId 章节ID * @param {Number} userId 用户ID(可选,用于VIP权限检查) */ export function getChapterDetail(chapterId, userId) { const params = {} if (userId !== undefined && userId !== null) { params.userId = userId } return get(`/audiobook/chapter/${chapterId}`, params) } /** * 记录听书历史 */ export function recordListeningHistory(userId, audiobookId, chapterId) { return post('/audiobook/history', { userId: userId, audiobookId: audiobookId, chapterId: chapterId }) } /** * 保存听书进度 */ export function saveListeningProgress(userId, audiobookId, chapterId, currentPosition, duration) { return post('/audiobook/progress', { userId: userId, audiobookId: audiobookId, chapterId: chapterId, currentPosition: currentPosition, duration: duration }) } /** * 获取听书进度 */ export function getListeningProgress(userId, chapterId) { return get('/audiobook/progress', { userId: userId, chapterId: chapterId }) } /** * 播放章节(增加播放次数) */ export function playChapter(audiobookId, chapterId) { return post('/audiobook/play', { audiobookId: audiobookId, chapterId: chapterId }) } // ============================================ // 书籍章节相关API // ============================================ /** * 根据书籍ID获取章节列表 */ export function getBookChapters(bookId) { return get('/chapter/list', { bookId: bookId }) } /** * 根据章节ID获取章节详情(包含内容) */ export function getBookChapterDetail(chapterId) { return get(`/chapter/${chapterId}`) } // ============================================ // 搜索相关API // ============================================ /** * 搜索书籍(模糊查询) * @param {String} keyword - 搜索关键词 * @param {Number} page - 页码,默认1 * @param {Number} size - 每页数量,默认20 */ export function searchBooks(keyword, page = 1, size = 20) { return get('/book/search', { keyword: keyword, page: page, size: size }) } /** * 获取热门搜索关键词 * @param {Number} limit - 返回数量,默认10 */ export function getPopularKeywords(limit = 10) { return get('/book/popular-keywords', { limit: limit }) } // ============================================ // 听书检索API // ============================================ /** * 听书分页搜索(模糊查询) * @param {String} keyword 关键词 * @param {Number} page 页码 * @param {Number} size 每页数量 * 其他可选:categoryId, status, isVip, isFree */ export function searchAudiobooks({ keyword, page = 1, size = 20, categoryId, status, isVip, isFree } = {}) { const params = { keyword, page, size } if (categoryId !== undefined && categoryId !== null) params.categoryId = categoryId if (status !== undefined && status !== null) params.status = status if (isVip !== undefined && isVip !== null) params.isVip = isVip if (isFree !== undefined && isFree !== null) params.isFree = isFree return get('/audiobook/list', params) } // ============================================ // 自定义轮播与榜单(公共读取) // ============================================ export function getBannersByCode(code) { return get('/banner/list', { code }) } /** * 获取所有启用的排行榜类型 */ export function getAllRankingGroups() { return get('/ranking/groups') } /** * 根据排行榜类型code获取排行榜书籍(支持分类筛选) * @param {String} code 排行榜类型code(如:bestseller、popular) * @param {Number} categoryId 分类ID(可选) */ export function getRankingByCode(code, categoryId = null) { const params = { code } if (categoryId !== null && categoryId !== undefined) { params.categoryId = categoryId } return get('/ranking/list', params) } // ============================================ // 浏览记录相关API // ============================================ /** * 记录浏览历史 * @param {Number} userId 用户ID * @param {Number} bookId 书籍ID */ export function recordBrowsingHistory(userId, bookId) { return post('/browsing-history/record', { userId: userId, bookId: bookId }) } /** * 获取用户的浏览记录列表 * @param {Number} userId 用户ID */ export function getBrowsingHistory(userId) { return get('/browsing-history/list', { userId: userId }) } /** * 清空用户的浏览记录 * @param {Number} userId 用户ID */ export function clearBrowsingHistory(userId) { return del('/browsing-history/clear', { userId: userId }) } // ============================================ // 评论相关API // ============================================ /** * 发表评论 * @param {Number} userId 用户ID * @param {Number} bookId 书籍ID * @param {String} content 评论内容 */ export function addComment(userId, bookId, content) { return post('/comment/add', { userId: userId, bookId: bookId, content: content }) } /** * 获取书籍的评论列表 * @param {Number} bookId 书籍ID * @param {Number} userId 当前用户ID(可选,用于判断是否已点赞) */ export function getComments(bookId, userId = null) { const params = { bookId } if (userId !== null && userId !== undefined) { params.userId = userId } return get('/comment/list', params) } /** * 点赞/取消点赞评论 * @param {Number} userId 用户ID * @param {Number} commentId 评论ID */ export function toggleCommentLike(userId, commentId) { return post('/comment/like', { userId: userId, commentId: commentId }) } // ============================================ // 搜索历史相关API // ============================================ /** * 记录搜索历史 * @param {Number} userId 用户ID * @param {String} keyword 搜索关键词 */ export function recordSearchHistory(userId, keyword) { return post('/search-history/record', { userId: userId, keyword: keyword }) } /** * 获取用户的搜索历史列表 * @param {Number} userId 用户ID * @param {Number} limit 返回数量,默认10 */ export function getSearchHistory(userId, limit = 10) { return get('/search-history/list', { userId: userId, limit: limit }) } /** * 清空用户的搜索历史 * @param {Number} userId 用户ID */ export function clearSearchHistory(userId) { return del('/search-history/clear', { userId: userId }) } // ============================================ // 意见反馈相关API // ============================================ /** * 提交意见反馈 * @param {Object} feedbackData 反馈数据 * @param {Number} feedbackData.userId 用户ID * @param {String} feedbackData.type 反馈类型(bug/suggestion/other) * @param {String} feedbackData.description 详细描述 * @param {Array} feedbackData.images 图片URL列表 */ export function submitFeedback(feedbackData) { return post('/feedback/submit', feedbackData) } /** * 获取用户的反馈列表 * @param {Number} userId 用户ID * @param {Number} page 页码,默认1 * @param {Number} size 每页数量,默认10 */ export function getFeedbackList(userId, page = 1, size = 10) { return get('/feedback/list', { userId: userId, page: page, size: size }) } /** * 获取反馈详情 * @param {Number} id 反馈ID */ export function getFeedbackById(id) { return get(`/feedback/${id}`) } // ============================================ // 消息相关API // ============================================ /** * 获取用户的消息列表 * @param {Number} userId 用户ID * @param {Number} isRead 是否已读(0-未读,1-已读,不传则查询全部) * @param {String} type 消息类型(like/comment/reply,不传则查询全部) * @param {Number} page 页码,默认1 * @param {Number} size 每页数量,默认20 */ export function getMessages(userId, isRead, type, page = 1, size = 20) { const params = { userId } if (isRead !== undefined && isRead !== null) { params.isRead = isRead } if (type) { params.type = type } params.page = page params.size = size return get('/message/list', params) } /** * 获取未读消息数量 * @param {Number} userId 用户ID */ export function getUnreadMessageCount(userId) { return get('/message/unread-count', { userId }) } /** * 标记消息为已读 * @param {Number} id 消息ID */ export function markMessageAsRead(id) { return put(`/message/read/${id}`, {}) } /** * 批量标记所有消息为已读 * @param {Number} userId 用户ID */ export function markAllMessagesAsRead(userId) { return put('/message/read-all', { userId }) } // ============================================ // VIP相关API // ============================================ /** * 获取VIP套餐列表 */ export function getVipPlans() { return get('/vip/plans') } /** * 购买VIP * @param {Object} purchaseData 购买数据 * @param {Number} purchaseData.userId 用户ID * @param {String} purchaseData.vipType VIP类型(month/quarter/year) * @param {String} purchaseData.vipName VIP名称 * @param {Number} purchaseData.price 价格 * @param {String} purchaseData.paymentMethod 支付方式(alipay/wechat/other) */ export function purchaseVip(purchaseData) { return post('/vip/purchase', purchaseData) } /** * 确认支付(支付回调) * @param {Number} recordId 记录ID * @param {String} transactionId 交易流水号(可选) */ export function confirmVipPayment(recordId, transactionId) { return post('/vip/confirm-payment', { recordId: recordId, transactionId: transactionId }) } /** * 检查用户VIP状态 * @param {Number} userId 用户ID */ export function checkVipStatus(userId) { return get('/vip/check', { userId }) } /** * 获取用户的VIP充值记录列表 * @param {Number} userId 用户ID * @param {Number} paymentStatus 支付状态(0-待支付,1-已支付,不传则查询全部) * @param {Number} page 页码,默认1 * @param {Number} size 每页数量,默认10 */ export function getVipRecords(userId, paymentStatus, page = 1, size = 10) { const params = { userId, page, size } if (paymentStatus !== undefined && paymentStatus !== null) { params.paymentStatus = paymentStatus } return get('/vip/records', params) } export default { login, register, getUserById, getBooks, getBookById, getTodayRecommend, getBestsellers, getAllBooks, getRankingBooks, getPopularBooks, getNewBooks, getVipBooks, createBook, updateBook, deleteBook, deleteBooks, addToBookshelf, removeFromBookshelf, getBookshelfList, checkInBookshelf, updateReadProgress, getAllCategories, getCategoryById, getRecentAudiobooks, getPopularAudiobooks, getRecommendAudiobooks, getRankingAudiobooks, getAudiobookDetail, getChapterDetail, recordListeningHistory, saveListeningProgress, getListeningProgress, playChapter, getBookChapters, getBookChapterDetail, searchBooks, getPopularKeywords, searchAudiobooks, getBannersByCode, getRankingByCode, getAllRankingGroups, recordBrowsingHistory, getBrowsingHistory, clearBrowsingHistory, addComment, getComments, toggleCommentLike, recordSearchHistory, getSearchHistory, clearSearchHistory, submitFeedback, getFeedbackList, getFeedbackById, getMessages, getUnreadMessageCount, markMessageAsRead, markAllMessagesAsRead, getVipPlans, purchaseVip, confirmVipPayment, checkVipStatus, getVipRecords }