api.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const utils_config = require("./config.js");
  4. const BASE_URL = utils_config.config.BASE_URL;
  5. function request(url, method = "GET", data = {}) {
  6. return new Promise((resolve, reject) => {
  7. if (method !== "GET") {
  8. common_vendor.index.showLoading({
  9. title: "请求中...",
  10. mask: false
  11. });
  12. }
  13. common_vendor.index.request({
  14. url: BASE_URL + url,
  15. method,
  16. data,
  17. header: {
  18. "Content-Type": "application/json"
  19. },
  20. timeout: utils_config.config.TIMEOUT,
  21. success: (res) => {
  22. if (method !== "GET") {
  23. common_vendor.index.hideLoading();
  24. }
  25. if (res.statusCode === 200) {
  26. if (res.data.code === 200) {
  27. resolve(res.data);
  28. } else {
  29. reject(new Error(res.data.message || "请求失败"));
  30. }
  31. } else {
  32. reject(new Error("网络请求失败,状态码:" + res.statusCode));
  33. }
  34. },
  35. fail: (err) => {
  36. if (method !== "GET") {
  37. common_vendor.index.hideLoading();
  38. }
  39. common_vendor.index.__f__("error", "at utils/api.js:53", "API请求失败:", err);
  40. reject(new Error("网络请求失败,请检查网络连接"));
  41. }
  42. });
  43. });
  44. }
  45. function get(url, data = {}) {
  46. return request(url, "GET", data);
  47. }
  48. function post(url, data = {}) {
  49. return request(url, "POST", data);
  50. }
  51. function put(url, data = {}) {
  52. return request(url, "PUT", data);
  53. }
  54. function del(url, data = {}) {
  55. return request(url, "DELETE", data);
  56. }
  57. function login(username, password) {
  58. return post("/user/login", {
  59. username,
  60. password
  61. });
  62. }
  63. function register(data) {
  64. return post("/user/register", data);
  65. }
  66. function getBooks(params) {
  67. return get("/book/list", params);
  68. }
  69. function getBookById(id, userId) {
  70. const params = {};
  71. if (userId !== void 0 && userId !== null) {
  72. params.userId = userId;
  73. }
  74. return get(`/book/${id}`, params);
  75. }
  76. function getTodayRecommend(limit = 8) {
  77. return get("/book/today-recommend", {
  78. limit
  79. });
  80. }
  81. function getBestsellers(limit = 10) {
  82. return get("/book/bestsellers", {
  83. limit
  84. });
  85. }
  86. function getAllBooks(page = 1, size = 10) {
  87. return get("/book/all", {
  88. page,
  89. size
  90. });
  91. }
  92. function getRankingBooks(limit = 10) {
  93. return get("/book/ranking", {
  94. limit
  95. });
  96. }
  97. function getPopularBooks(limit = 10) {
  98. return get("/book/popular", {
  99. limit
  100. });
  101. }
  102. function getNewBooks(limit = 10) {
  103. return get("/book/new", {
  104. limit
  105. });
  106. }
  107. function getVipBooks(page = 1, size = 10) {
  108. return get("/book/vip", {
  109. page,
  110. size
  111. });
  112. }
  113. function getFeaturedList(limit = 4) {
  114. return get("/book/featured-list", {
  115. limit
  116. });
  117. }
  118. function getAllCategories() {
  119. return get("/category/list");
  120. }
  121. function getMoreRecommend(limit = 6) {
  122. return get("/book/more-recommend", {
  123. limit
  124. });
  125. }
  126. function getUserProfile(userId) {
  127. return get("/user/profile", { userId });
  128. }
  129. function updateUserProfile(payload) {
  130. return request("/user/profile", "PUT", payload);
  131. }
  132. function addToBookshelf(userId, bookId) {
  133. return post("/bookshelf/add", {
  134. userId,
  135. bookId
  136. });
  137. }
  138. function getBookshelfList(userId) {
  139. return get("/bookshelf/list", {
  140. userId
  141. });
  142. }
  143. function checkInBookshelf(userId, bookId) {
  144. return get("/bookshelf/check", {
  145. userId,
  146. bookId
  147. });
  148. }
  149. function addAudiobookToBookshelf(userId, audiobookId) {
  150. return post("/audiobook-bookshelf/add", {
  151. userId,
  152. audiobookId
  153. });
  154. }
  155. function removeAudiobookFromBookshelf(userId, audiobookId) {
  156. return request("/audiobook-bookshelf/remove", "DELETE", {
  157. userId,
  158. audiobookId
  159. });
  160. }
  161. function getAudiobookBookshelfList(userId) {
  162. return get("/audiobook-bookshelf/list", {
  163. userId
  164. });
  165. }
  166. function checkAudiobookInBookshelf(userId, audiobookId) {
  167. return get("/audiobook-bookshelf/check", {
  168. userId,
  169. audiobookId
  170. });
  171. }
  172. function getRecentAudiobooks(limit = 8) {
  173. return get("/audiobook/recent", {
  174. limit
  175. });
  176. }
  177. function getPopularAudiobooks(limit = 10) {
  178. return get("/audiobook/popular", {
  179. limit
  180. });
  181. }
  182. function getRecommendAudiobooks(limit = 10) {
  183. return get("/audiobook/recommend", {
  184. limit
  185. });
  186. }
  187. function getRankingAudiobooks(limit = 10) {
  188. return get("/audiobook/ranking", {
  189. limit
  190. });
  191. }
  192. function getAudiobookDetail(id, userId = null) {
  193. const params = userId ? { userId } : {};
  194. return get(`/audiobook/${id}`, params);
  195. }
  196. function getChapterDetail(chapterId, userId) {
  197. const params = {};
  198. if (userId !== void 0 && userId !== null) {
  199. params.userId = userId;
  200. }
  201. return get(`/audiobook/chapter/${chapterId}`, params);
  202. }
  203. function recordListeningHistory(userId, audiobookId, chapterId) {
  204. return post("/audiobook/history", {
  205. userId,
  206. audiobookId,
  207. chapterId
  208. });
  209. }
  210. function saveListeningProgress(userId, audiobookId, chapterId, currentPosition, duration) {
  211. return post("/audiobook/progress", {
  212. userId,
  213. audiobookId,
  214. chapterId,
  215. currentPosition,
  216. duration
  217. });
  218. }
  219. function getListeningProgress(userId, chapterId) {
  220. return get("/audiobook/progress", {
  221. userId,
  222. chapterId
  223. });
  224. }
  225. function playChapter(audiobookId, chapterId) {
  226. return post("/audiobook/play", {
  227. audiobookId,
  228. chapterId
  229. });
  230. }
  231. function getBookChapters(bookId) {
  232. return get("/chapter/list", {
  233. bookId
  234. });
  235. }
  236. function getBookChapterDetail(chapterId) {
  237. return get(`/chapter/${chapterId}`);
  238. }
  239. function searchBooks(keyword, page = 1, size = 20) {
  240. return get("/book/search", {
  241. keyword,
  242. page,
  243. size
  244. });
  245. }
  246. function getPopularKeywords(limit = 10) {
  247. return get("/book/popular-keywords", {
  248. limit
  249. });
  250. }
  251. function searchAudiobooks({ keyword, page = 1, size = 20, categoryId, status, isVip, isFree } = {}) {
  252. const params = { keyword, page, size };
  253. if (categoryId !== void 0 && categoryId !== null)
  254. params.categoryId = categoryId;
  255. if (status !== void 0 && status !== null)
  256. params.status = status;
  257. if (isVip !== void 0 && isVip !== null)
  258. params.isVip = isVip;
  259. if (isFree !== void 0 && isFree !== null)
  260. params.isFree = isFree;
  261. return get("/audiobook/list", params);
  262. }
  263. function getBannersByCode(code) {
  264. return get("/banner/list", { code });
  265. }
  266. function getAllRankingGroups() {
  267. return get("/ranking/groups");
  268. }
  269. function getRankingByCode(code, categoryId = null) {
  270. const params = { code };
  271. if (categoryId !== null && categoryId !== void 0) {
  272. params.categoryId = categoryId;
  273. }
  274. return get("/ranking/list", params);
  275. }
  276. function recordBrowsingHistory(userId, bookId) {
  277. return post("/browsing-history/record", {
  278. userId,
  279. bookId
  280. });
  281. }
  282. function getBrowsingHistory(userId) {
  283. return get("/browsing-history/list", {
  284. userId
  285. });
  286. }
  287. function clearBrowsingHistory(userId) {
  288. return del("/browsing-history/clear", {
  289. userId
  290. });
  291. }
  292. function addComment(userId, bookId, content) {
  293. return post("/comment/add", {
  294. userId,
  295. bookId,
  296. content
  297. });
  298. }
  299. function getComments(bookId, userId = null) {
  300. const params = { bookId };
  301. if (userId !== null && userId !== void 0) {
  302. params.userId = userId;
  303. }
  304. return get("/comment/list", params);
  305. }
  306. function toggleCommentLike(userId, commentId) {
  307. return post("/comment/like", {
  308. userId,
  309. commentId
  310. });
  311. }
  312. function recordSearchHistory(userId, keyword) {
  313. return post("/search-history/record", {
  314. userId,
  315. keyword
  316. });
  317. }
  318. function getSearchHistory(userId, limit = 10) {
  319. return get("/search-history/list", {
  320. userId,
  321. limit
  322. });
  323. }
  324. function clearSearchHistory(userId) {
  325. return del("/search-history/clear", {
  326. userId
  327. });
  328. }
  329. function submitFeedback(feedbackData) {
  330. return post("/feedback/submit", feedbackData);
  331. }
  332. function getMessages(userId, isRead, type, page = 1, size = 20) {
  333. const params = { userId };
  334. if (isRead !== void 0 && isRead !== null) {
  335. params.isRead = isRead;
  336. }
  337. if (type) {
  338. params.type = type;
  339. }
  340. params.page = page;
  341. params.size = size;
  342. return get("/message/list", params);
  343. }
  344. function markMessageAsRead(id) {
  345. return put(`/message/read/${id}`, {});
  346. }
  347. function getVipPlans() {
  348. return get("/vip/plans");
  349. }
  350. function purchaseVip(purchaseData) {
  351. return post("/vip/purchase", purchaseData);
  352. }
  353. function getVipRecords(userId, paymentStatus, page = 1, size = 10) {
  354. const params = { userId, page, size };
  355. if (paymentStatus !== void 0 && paymentStatus !== null) {
  356. params.paymentStatus = paymentStatus;
  357. }
  358. return get("/vip/records", params);
  359. }
  360. exports.addAudiobookToBookshelf = addAudiobookToBookshelf;
  361. exports.addComment = addComment;
  362. exports.addToBookshelf = addToBookshelf;
  363. exports.checkAudiobookInBookshelf = checkAudiobookInBookshelf;
  364. exports.checkInBookshelf = checkInBookshelf;
  365. exports.clearBrowsingHistory = clearBrowsingHistory;
  366. exports.clearSearchHistory = clearSearchHistory;
  367. exports.getAllBooks = getAllBooks;
  368. exports.getAllCategories = getAllCategories;
  369. exports.getAllRankingGroups = getAllRankingGroups;
  370. exports.getAudiobookBookshelfList = getAudiobookBookshelfList;
  371. exports.getAudiobookDetail = getAudiobookDetail;
  372. exports.getBannersByCode = getBannersByCode;
  373. exports.getBestsellers = getBestsellers;
  374. exports.getBookById = getBookById;
  375. exports.getBookChapterDetail = getBookChapterDetail;
  376. exports.getBookChapters = getBookChapters;
  377. exports.getBooks = getBooks;
  378. exports.getBookshelfList = getBookshelfList;
  379. exports.getBrowsingHistory = getBrowsingHistory;
  380. exports.getChapterDetail = getChapterDetail;
  381. exports.getComments = getComments;
  382. exports.getFeaturedList = getFeaturedList;
  383. exports.getListeningProgress = getListeningProgress;
  384. exports.getMessages = getMessages;
  385. exports.getMoreRecommend = getMoreRecommend;
  386. exports.getNewBooks = getNewBooks;
  387. exports.getPopularAudiobooks = getPopularAudiobooks;
  388. exports.getPopularBooks = getPopularBooks;
  389. exports.getPopularKeywords = getPopularKeywords;
  390. exports.getRankingAudiobooks = getRankingAudiobooks;
  391. exports.getRankingBooks = getRankingBooks;
  392. exports.getRankingByCode = getRankingByCode;
  393. exports.getRecentAudiobooks = getRecentAudiobooks;
  394. exports.getRecommendAudiobooks = getRecommendAudiobooks;
  395. exports.getSearchHistory = getSearchHistory;
  396. exports.getTodayRecommend = getTodayRecommend;
  397. exports.getUserProfile = getUserProfile;
  398. exports.getVipBooks = getVipBooks;
  399. exports.getVipPlans = getVipPlans;
  400. exports.getVipRecords = getVipRecords;
  401. exports.login = login;
  402. exports.markMessageAsRead = markMessageAsRead;
  403. exports.playChapter = playChapter;
  404. exports.purchaseVip = purchaseVip;
  405. exports.recordBrowsingHistory = recordBrowsingHistory;
  406. exports.recordListeningHistory = recordListeningHistory;
  407. exports.recordSearchHistory = recordSearchHistory;
  408. exports.register = register;
  409. exports.removeAudiobookFromBookshelf = removeAudiobookFromBookshelf;
  410. exports.saveListeningProgress = saveListeningProgress;
  411. exports.searchAudiobooks = searchAudiobooks;
  412. exports.searchBooks = searchBooks;
  413. exports.submitFeedback = submitFeedback;
  414. exports.toggleCommentLike = toggleCommentLike;
  415. exports.updateUserProfile = updateUserProfile;
  416. //# sourceMappingURL=../../.sourcemap/mp-weixin/utils/api.js.map