| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920 |
- /**
- * 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
- }
|