index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. * @Author: ZhengXiaowei
  3. * @Date: 2019-07-24 17:21:47
  4. * @LastEditors: Please set LastEditors
  5. * @LastEditTime: 2021-06-28 15:51:55
  6. * @Description: file content
  7. */
  8. import Axios from "axios";
  9. import axios from "./axios";
  10. import {
  11. bookListFormat,
  12. bookFormat,
  13. urlFormat,
  14. contentFormat,
  15. recordFormat,
  16. shelfFormat
  17. } from "./util";
  18. import cache from "../util/cache.js";
  19. import { Subscribe, Pay } from "../view/namespace.js";
  20. import bus from "../util/bus";
  21. import { getKeyByWay } from "../util/index";
  22. //获取首页(书城)内容
  23. let index = {};
  24. export function getIndex(type) {
  25. //male or female
  26. if (type === "boy") type = "male";
  27. else if (type === "girl") type = "female";
  28. else type = "male";
  29. if (index[type]) return Promise.resolve(index[type]);
  30. return axios(`/books/${type}/index`).then(r => {
  31. r.forEach(list => {
  32. bookListFormat(list.books);
  33. });
  34. index[type] = r;
  35. return r;
  36. });
  37. }
  38. /**
  39. 获取书库书籍
  40. 关键字 key String
  41. 分类id category_id Number
  42. 页码 page String
  43. 完结 status 完结:1 | 连载:0
  44. */
  45. export function getBook(params) {
  46. getBook.source = Axios.CancelToken.source();
  47. return axios("/books/library", { cancelToken: getBook.source.token, params })
  48. .then(r => {
  49. bookListFormat(r.list);
  50. return r;
  51. })
  52. .catch(function(thrown) {
  53. if (!Axios.isCancel(thrown)) {
  54. return Promise.reject(thrown);
  55. }
  56. });
  57. }
  58. //获取分类
  59. let category = null;
  60. export function getCategory() {
  61. if (category) return Promise.resolve(category);
  62. return (category = axios("/books/getCategory").then(r => {
  63. r.unshift({
  64. name: "不限",
  65. id: 0
  66. });
  67. return r;
  68. }));
  69. }
  70. // 获取详情
  71. // 创建一个书籍详情的缓存池
  72. let detail = new cache({
  73. meta: {
  74. name: "书籍详情"
  75. },
  76. limit: 40
  77. });
  78. //更新书籍信息中的是否在书架上(is_on_user_shelf)
  79. function detailUpdate(id, status) {
  80. let book = detail.find(id);
  81. if (book.complete) {
  82. book.result.data.is_on_user_shelf = status;
  83. }
  84. }
  85. export function getDetail(id, s = "", pos = "") {
  86. return detail
  87. .getAsync(id, function() {
  88. return axios("/book/" + id + "?s=" + s + "&pos=" + pos).then(r => {
  89. return {
  90. id,
  91. data: r
  92. };
  93. });
  94. })
  95. .then(r => {
  96. return bookFormat(r.result.data);
  97. });
  98. }
  99. // 获取目录
  100. // 创建一个目录的缓存池
  101. let catalog = new cache({
  102. meta: {
  103. name: "目录"
  104. },
  105. limit: 20
  106. });
  107. export function getCatalog(id, page = 1, s = "") {
  108. return catalog
  109. .getAsync(id + "+" + page, function() {
  110. return axios(`/books/${id}/catalog`, {
  111. params: { page_size: 100, page, s }
  112. }).then(r => {
  113. return {
  114. id: id + "+" + page,
  115. data: r.list,
  116. meta: r.meta
  117. };
  118. });
  119. })
  120. .then(r => {
  121. return r.result;
  122. });
  123. }
  124. // 获取同款推荐
  125. // 创建一个同款推荐的缓存池
  126. let similar = new cache({
  127. meta: {
  128. name: "同款推荐"
  129. },
  130. limit: 20
  131. });
  132. export function getSimilar(bid, category_id) {
  133. return similar
  134. .getAsync(bid, function() {
  135. return axios(`/books/similar`, { params: { category_id, bid } }).then(
  136. r => {
  137. let data = bookListFormat(r);
  138. detail.push(
  139. data.map(v => {
  140. return {
  141. id: v.book_id,
  142. data: v
  143. };
  144. })
  145. );
  146. return {
  147. id: bid,
  148. data: data
  149. };
  150. }
  151. );
  152. })
  153. .then(r => {
  154. return r.result.data;
  155. });
  156. }
  157. //获取用户信息
  158. let userinfo = null;
  159. export function getUserInfo() {
  160. // if (userinfo) return Promise.resolve(userinfo)
  161. return axios("/userinfo").then(r => {
  162. return r;
  163. });
  164. }
  165. //获取书架
  166. let shelf = null;
  167. export function getShelf() {
  168. if (shelf) return Promise.resolve(shelf);
  169. return axios("/userShelfBooks").then(r => {
  170. r = shelfFormat(r);
  171. return r;
  172. });
  173. }
  174. //删除书架
  175. export function delShelf(bid) {
  176. return axios("/userShelfBooks/delete?bid=" + bid).then(r => {
  177. detailUpdate(bid, 0);
  178. return r;
  179. });
  180. }
  181. //添加书架
  182. export function addShelf(bid) {
  183. return axios.post("/userShelfBooks", { bid }).then(r => {
  184. shelf = null;
  185. detailUpdate(bid, 1);
  186. return r;
  187. });
  188. }
  189. //获取阅读记录
  190. let readerRecord = null;
  191. export function getReadrecord() {
  192. if (readerRecord) return Promise.resolve(readerRecord);
  193. return axios("/readrecord").then(r => {
  194. return r.map(book => {
  195. book.book_id = book.bid;
  196. return book;
  197. });
  198. });
  199. }
  200. // 添加阅读记录
  201. export function setReadrecord(params) {
  202. return axios.post("/readrecord", params);
  203. }
  204. //删除阅读记录
  205. export function delReadrecord(bid) {
  206. return axios.post("/readrecord/delete", { bid }).then(r => {
  207. return r;
  208. });
  209. }
  210. // 签到记录
  211. export function getSignRecord(page = 1) {
  212. return axios(`/user/sign_record?page=${page}`);
  213. }
  214. const send_order_id = localStorage.getItem("send_order_id");
  215. //获取章节内容
  216. // 创建一个同款推荐的缓存池
  217. /*
  218. 错误处理
  219. 10012 未关注(默认链接)
  220. 10014 全本订阅余额不足(rmb)
  221. 10015 章订余额不足(充值)
  222. 10016 购买章节(书币)
  223. 10017 购买图书(书币)
  224. 10019 全本订阅余额不足(充值)
  225. 10023 未关注 (跳转渠道接诶)
  226. ----- 以上是弹窗 -------
  227. 10020 全本订阅余额不足,直接进入充值页
  228. 10021 章节订阅余额不足,直接进入充值页
  229. 10022 进入版权站
  230. */
  231. let content = new cache({
  232. meta: {
  233. name: "章节内容"
  234. },
  235. limit: 20
  236. });
  237. export function getContent(bid, cid, adStatus = 0, from, code = "") {
  238. let url = options.adTargetId
  239. ? `/books/${bid}/chapters/${cid}?ad_status=${adStatus}&page_from=${from}&send_oerder_id=${send_order_id}&code=${encodeURIComponent(
  240. code
  241. )}`
  242. : `/books/${bid}/chapters/${cid}?page_from=${from}&send_oerder_id=${send_order_id}&code=${encodeURIComponent(
  243. code
  244. )}`;
  245. return content
  246. .getAsync(bid + "+" + cid, function() {
  247. return axios(url).then(r => {
  248. return {
  249. id: bid + "+" + cid,
  250. data: contentFormat(r)
  251. };
  252. });
  253. })
  254. .then(r => {
  255. if (!r.fresh) {
  256. setReadrecord({ bid, cid, chapter_name: r.result.data.chapter_name });
  257. }
  258. // TODO: 暂时这么做
  259. // let cacheTap = localStorage.getItem(cid)
  260. // if (cacheTap) {
  261. // r.result.data.chapter_comment = cacheTap
  262. // }
  263. return r.result.data;
  264. });
  265. }
  266. //余额支付
  267. export function subscribeByBalance(bid, cid, remind) {
  268. let params = remind ? { params: { remind: 1 } } : {};
  269. return axios(`/books/${bid}/balance/chapterOrders/${cid}`, params).then(r => {
  270. content.push({
  271. id: bid + "+" + cid,
  272. data: contentFormat(r)
  273. });
  274. return r;
  275. });
  276. }
  277. //RMB支付
  278. export function subscribeByRMB(params) {
  279. const a = document.createElement("a");
  280. const redirect = {
  281. host: location.origin,
  282. pathname: "/reader",
  283. query: {
  284. bid: params.book_id,
  285. cid: params.chapter_id
  286. }
  287. };
  288. const href = {
  289. host: window.options.pay_url,
  290. query: {
  291. product_id: params.product_id,
  292. uid: window.options.uid,
  293. distribution_channel_id: window.options.distribution_channel_id,
  294. send_order_id: window.options.send_order_id,
  295. pay_redirect_url: encodeURIComponent(urlFormat(redirect))
  296. }
  297. };
  298. a.href = urlFormat(href);
  299. a.click();
  300. }
  301. // 排行帮
  302. //type Int (点击帮:1|字数帮:2|新书榜:3)
  303. //time Int (周:1|月:2|总:3)
  304. let rank = {};
  305. export function getRank(type = 1, time = 1) {
  306. let id = type + "+" + time;
  307. if (rank[id]) return Promise.resolve(rank);
  308. return axios("/books/rank", {
  309. params: {
  310. type,
  311. time
  312. }
  313. }).then(r => {
  314. bookListFormat(r.male);
  315. bookListFormat(r.female);
  316. rank[id] = r;
  317. return rank;
  318. });
  319. }
  320. //充值产品列表
  321. let product = null;
  322. export function getProductList(bid) {
  323. if (product) return product;
  324. else return axios("/order/chargeList", { params: { bid } });
  325. }
  326. //充值
  327. export function recharge({
  328. product_id,
  329. bid,
  330. cid,
  331. use_coupon,
  332. from_detail_catalog,
  333. last_bid,
  334. last_cid,
  335. sequence,
  336. send_order_id,
  337. yun
  338. }) {
  339. const a = document.createElement("a");
  340. const token = getKeyByWay('token');
  341. const redirect =
  342. bid && cid
  343. ? {
  344. host: location.origin,
  345. pathname: "/pay",
  346. query: {
  347. bid,
  348. cid,
  349. from_detail_catalog,
  350. last_bid,
  351. last_cid,
  352. sequence,
  353. send_order_id,
  354. yun,
  355. isRecharge: true // 微信充值完回到充值中心,如果从目录或阅读页跳转过来,回去时需要传入上一页id或目录进行判断
  356. }
  357. }
  358. : {
  359. host: location.origin,
  360. pathname: "/pay",
  361. query: {
  362. isRecharge: true // 微信充值完回到充值中心,如果从个人中心来充值,无需传入其他参数
  363. }
  364. };
  365. const href = {
  366. host: window.options.pay_url,
  367. query: {
  368. product_id: product_id,
  369. token: token ? `${token}` : "",
  370. use_coupon,
  371. distribution_channel_id: window.options.distribution_channel_id,
  372. send_order_id: window.options.send_order_id,
  373. from: window.options.from,
  374. bid,
  375. cid,
  376. pay_redirect_url: encodeURIComponent(urlFormat(redirect))
  377. }
  378. };
  379. // window.open(urlFormat(href),'微信支付',true)
  380. a.href = urlFormat(href);
  381. a.click();
  382. }
  383. //单本消费记录
  384. export function getRecordOrderByBook(page) {
  385. return axios("/order/bookOrderList", { params: { page } });
  386. }
  387. //章节消费记录
  388. export function getRecordOrderByChapter(page) {
  389. return axios("/order/chapterOrderList", { params: { page } });
  390. }
  391. //充值记录
  392. export function getRecordRecharge(page) {
  393. return axios("/order/chargeRecordLists", { params: { page } }).then(r => {
  394. recordFormat(r.list);
  395. return r;
  396. });
  397. }
  398. //赠送记录
  399. export function getRecordSend(page) {
  400. return axios("/getGivenRocords", { params: { page } }).then(r => {
  401. return r;
  402. });
  403. }
  404. // 尾页推荐
  405. export function getReadOverRecommend(bid) {
  406. return axios("/books/readOverRecommend", { params: { bid } }).then(r => {
  407. bookListFormat(r.recommend_result);
  408. return r;
  409. });
  410. }
  411. // 获取弱关二维码
  412. export function getweakSubscribeQR() {
  413. return axios("/subscribe/qrcode");
  414. }
  415. // 用户行为
  416. export function PostUserBehavior(data) {
  417. return axios.post("/userBehavior", data);
  418. }
  419. // 错误捕获
  420. export function undefinedCollect(data) {
  421. return axios.post("/error/undefinedCollect", data);
  422. }
  423. // 章节底部评价
  424. export function chapterComment(bid, chapter, tags) {
  425. return axios.post("/chapter/comment", { bid, chapter, tags });
  426. }
  427. // 获取章节底部评价
  428. export function getChapterComment(bid, chapter) {
  429. return axios("/chapter/getComment", { params: { bid, chapter } });
  430. }
  431. // 签到页面
  432. export function toSign() {
  433. return axios("user/sign");
  434. }
  435. // 获取礼物列表
  436. export function getGiftList() {
  437. return axios("/gift/getGiftsList");
  438. }
  439. // 送礼
  440. export function sendGift(data) {
  441. return axios.post("/gift/sendGifts", data);
  442. }
  443. // 送礼记录
  444. export function getSendGiftRecord(params) {
  445. return axios("/gift/getUserSendGiftsRecord", { params });
  446. }
  447. // 消费记录中的送礼记录
  448. export function getUserSendGiftRecord(page) {
  449. return axios("/gift/getUserGiftsConsumeRecord", { params: { page } });
  450. }
  451. // 获取代付用户数据
  452. export function getSubstitutePay(product_id) {
  453. return axios("/order/substitutePay", { params: { product_id } });
  454. }
  455. // 获取代付充值列表
  456. export function getSubstitutePayList(product_id, su) {
  457. return axios("/order/substitutePayChargeList", {
  458. params: { product_id, su }
  459. });
  460. }
  461. // 代充
  462. export function substituteRecharge({ product_id, uid }) {
  463. const a = document.createElement("a");
  464. const href = {
  465. host: window.options.pay_url,
  466. query: {
  467. product_id: product_id,
  468. suid: window.options.uid,
  469. uid: uid,
  470. distribution_channel_id: window.options.distribution_channel_id,
  471. send_order_id: window.options.send_order_id,
  472. from: window.options.from
  473. }
  474. };
  475. a.href = urlFormat(href);
  476. a.click();
  477. }
  478. // 最近阅读轮播
  479. export function recentSlideList() {
  480. return axios("/books/H5SmartRecommendBooks?pos=h5RecentReadLoop");
  481. }
  482. // 获取代付充值列表
  483. export function getbookFromWhere(bid, fromwhere) {
  484. return axios("/bookFromWhere/" + bid, { params: { fromwhere } });
  485. }
  486. // 获取分享按钮是否显示
  487. export function getRecordShare(bid, cid) {
  488. return axios("/user/recordShare", { params: { bid, cid } });
  489. }
  490. // 获取分享签名
  491. export function getWechatJsConfig(bid, cid, url) {
  492. return axios("/chapter/getWechatJsConfig", { params: { bid, cid, url } });
  493. }
  494. // 广告点击情况
  495. export function recordAdClickStatus(bid, cid, type = "UNLOCK") {
  496. return axios.post("/user/advisitstat", {
  497. bid,
  498. cid,
  499. type
  500. });
  501. }
  502. // 尾页推荐催更
  503. export function upgBookStatus(bid) {
  504. return axios("/user/urgeUpdate", { params: { bid } });
  505. }
  506. // 获取举报内容
  507. export function getReportList() {
  508. return axios("/complaints/getComplaintTags");
  509. }
  510. // 举报
  511. export function uploadReport(data) {
  512. return axios.post("/complaints/add", data);
  513. }
  514. // 获取广告banner
  515. export function getReaderAdBanner() {
  516. return axios("//banner.66kshu.com/b.gif");
  517. }
  518. // 发送验证码
  519. export function getPhoneCode(phone) {
  520. return axios.post("/bindphone/sendcode", { phone });
  521. }
  522. // 绑定手机
  523. export function phoneBind(data) {
  524. return axios.post("/bindphone/bind", data);
  525. }
  526. // 获取验证码
  527. export function getCode(data) {
  528. return axios.post("/bindphone/sendcode", data);
  529. }
  530. // 获取fakebook详情
  531. export function getBookDetail(id) {
  532. return axios("/red_book/getBookDetail?id=" + id);
  533. }
  534. // 获取fakebook详情
  535. export function getChapterList(id) {
  536. return axios("/red_book/getChapterList?id=" + id);
  537. }
  538. // 查询包月订单是否成功
  539. export function isMonthOrderSuc() {
  540. return axios("/monthorder/issuccess");
  541. }
  542. // 配置wx分享
  543. export function getWxConfig(url) {
  544. return axios.post("/weixin/jsSdkConfig", { url });
  545. }
  546. // 个人中心客服
  547. export function getCustomQRCode(distribution_channel_id, openid) {
  548. return axios("/custom_qrcode", {
  549. params: { distribution_channel_id, openid }
  550. });
  551. }
  552. // 限免书单
  553. export function getBookLimitFree() {
  554. return axios("/books/freeBook");
  555. }
  556. // 新版排行列表
  557. export function getRankListV2() {
  558. return axios("/books/rankList");
  559. }
  560. //用户优惠券列表
  561. export function getUserCoupon() {
  562. return axios("/user/userCoupon");
  563. }
  564. //支付可用优惠券
  565. export function getOrderCoupon() {
  566. return axios("/order/effectiveCoupon");
  567. }
  568. //登录平台
  569. export function userLogin(data) {
  570. return axios("/login", { params: data });
  571. }
  572. //绑定手机号新版
  573. export function bindUserPhone(data) {
  574. return axios.post("/bindphone/bind", data);
  575. }
  576. //获取中间页数据
  577. export function getYun(id) {
  578. return axios(`/yun/${id}`);
  579. }
  580. //获取最近阅读记录
  581. export function getRecentReader() {
  582. return axios(`/readrecord/getLastRead`);
  583. }
  584. //换取userId
  585. export function uuidsGetTokens(uuid) {
  586. return axios(`/hashuserToToken`, { params: { hashuser: uuid } });
  587. }
  588. //记录用户加桌状态
  589. export function setUserMode(status, send_order_id) {
  590. return axios.post(`/setAddDeskStatus`, {
  591. add_desk_status: status,
  592. send_order_id
  593. });
  594. }
  595. //强加桌模型切换
  596. export function changeAddMode() {
  597. return axios("/getAddDeskImage");
  598. }
  599. //新版登录
  600. export function newLogin(params) {
  601. return axios.post("/loginByFingerprint", params);
  602. }
  603. //新版注册
  604. export function newRegister(data) {
  605. return axios.post("/register", data);
  606. }
  607. //新版刷新token
  608. export function newRefreshToken(params) {
  609. return axios("/refreshToken", { params });
  610. }
  611. //新版销毁旧token
  612. export function newDestoryToken(data) {
  613. return axios.post("/destroyToken", data);
  614. }
  615. //更新token的指纹
  616. export function setFingerprint(data) {
  617. return axios.post("/setFingerprint", data);
  618. }