ChapterController.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\Book;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\QuickApp\BaseController;
  5. use Redis;
  6. use App\Modules\Book\Services\ChapterService;
  7. use App\Modules\User\Services\ReadRecordService;
  8. use App\Http\Controllers\QuickApp\Book\Transformers\ChapterTransformer;
  9. use App\Modules\Book\Services\BookConfigService;
  10. use App\Http\Controllers\QuickApp\Book\Transformers\ChapterListTransformer;
  11. use App\Modules\Book\Services\BookService;
  12. use App\Modules\Subscribe\Services\BookOrderService;
  13. use App\Modules\Subscribe\Services\ChapterOrderService;
  14. use App\Modules\Subscribe\Services\YearOrderService;
  15. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  16. use App\Modules\Subscribe\Services\ChapterReminderService;
  17. use App\Modules\User\Services\UserDeepReadTagService;
  18. class ChapterController extends BaseController
  19. {
  20. /**
  21. * @apiDefine Chapter 章节
  22. */
  23. /**
  24. * @apiVersion 1.0.0
  25. * @apiDescription 章节列表不分页
  26. * @api {get} books/{bid}/allcatalog 章节列表不分页
  27. * @apiParam {String} [token] token
  28. * @apiHeader {String} [Authorization] token 两个token任选其一
  29. * @apiGroup Chapter
  30. * @apiName getCatalog
  31. * @apiSuccess {int} code 状态码
  32. * @apiSuccess {String} msg 信息
  33. * @apiSuccess {object} data 结果集
  34. * @apiSuccess {Array} data.list 分页结果集
  35. * @apiSuccess {Int} data.list.bid bid
  36. * @apiSuccess {Int} data.list.chapter_id 章节id
  37. * @apiSuccess {String} data.list.chapter_name 章节名称
  38. * @apiSuccess {Int} data.list.chapter_sequence 序号
  39. * @apiSuccess {Int} data.list.chapter_is_vip 是否vip
  40. * @apiSuccess {Int} data.list.chapter_size 章节大小
  41. * @apiSuccess {Int} data.list.prev_cid 上一章节id
  42. * @apiSuccess {Int} data.list.next_cid 下一章节
  43. * @apiSuccess {String} data.list.recent_update_at 更新时间
  44. * @apiSuccess {Int} data.list.is_need_charge 是否需要充值
  45. * @apiSuccess {object} data.meta 分页信息
  46. * @apiSuccess {Int} data.meta.total 总条数
  47. * @apiSuccess {Int} data.meta.per_page 每页条数
  48. * @apiSuccess {Int} data.meta.current_page 当前页
  49. * @apiSuccess {Int} data.meta.last_page 最后页
  50. * @apiSuccess {String} data.meta.next_page_url 下一页
  51. * @apiSuccess {String} data.meta.prev_page_url 上一页
  52. * @apiSuccessExample {json} Success-Response:
  53. * HTTP/1.1 200 OK
  54. * {
  55. * code: 0,
  56. * msg: "",
  57. * data:
  58. * [
  59. * {
  60. * bid: 5,
  61. * chapter_id: 5,
  62. * chapter_name: "第1240章 不是我",
  63. * chapter_sequence: 1239,
  64. * chapter_is_vip: 1,
  65. * chapter_size: 2422,
  66. * prev_cid: 0,
  67. * next_cid: 0,
  68. * recent_update_at: 2017-11-20 15:01:56,
  69. * is_need_charge: 0,
  70. * },
  71. * {
  72. * bid: 5,
  73. * chapter_id: 5,
  74. * chapter_name: "第1240章 不是我",
  75. * chapter_sequence: 1239,
  76. * chapter_is_vip: 1,
  77. * chapter_size: 2422,
  78. * prev_cid: 0,
  79. * next_cid: 0,
  80. * recent_update_at: 2017-11-20 15:01:56,
  81. * is_need_charge: 0,
  82. * },
  83. * ]
  84. * }
  85. */
  86. public function getCatalog(Request $request, $bid)
  87. {
  88. $bid = BookService::decodeBidStatic($bid);
  89. $lists = ChapterService::getChapterLists($bid);
  90. $book_info = BookConfigService::getBookById($bid);
  91. if (!$book_info) {
  92. return response()->error('PARAM_ERROR');
  93. }
  94. $lists = $this->getChapterCatalog($bid, $lists, $book_info);
  95. return response()->collection(new ChapterListTransformer, $lists);
  96. }
  97. /**
  98. * @apiVersion 1.0.0
  99. * @apiDescription 章节列表分页
  100. * @api {get} books/{bid}/catalog 章节列表分页
  101. * @apiParam {String} [token] token
  102. * @apiHeader {String} [Authorization] token 两个token任选其一
  103. * @apiGroup Chapter
  104. * @apiName getCatalogPerPage
  105. * @apiParam {Int} page_size 分页大小(默认15)
  106. * @apiParam {Int} page 页码(默认1)
  107. * @apiSuccess {int} code 状态码
  108. * @apiSuccess {String} msg 信息
  109. * @apiSuccess {object} data 结果集
  110. * @apiSuccess {Array} data.list 分页结果集
  111. * @apiSuccess {Int} data.list.bid bid
  112. * @apiSuccess {Int} data.list.chapter_id 章节id
  113. * @apiSuccess {String} data.list.chapter_name 章节名称
  114. * @apiSuccess {Int} data.list.chapter_sequence 序号
  115. * @apiSuccess {Int} data.list.chapter_is_vip 是否vip
  116. * @apiSuccess {Int} data.list.chapter_size 章节大小
  117. * @apiSuccess {Int} data.list.prev_cid 上一章节id
  118. * @apiSuccess {Int} data.list.next_cid 下一章节
  119. * @apiSuccess {String} data.list.recent_update_at 更新时间
  120. * @apiSuccess {Int} data.list.is_need_charge 是否需要充值
  121. * @apiSuccess {object} data.meta 分页信息
  122. * @apiSuccess {Int} data.meta.total 总条数
  123. * @apiSuccess {Int} data.meta.per_page 每页条数
  124. * @apiSuccess {Int} data.meta.current_page 当前页
  125. * @apiSuccess {Int} data.meta.last_page 最后页
  126. * @apiSuccess {String} data.meta.next_page_url 下一页
  127. * @apiSuccess {String} data.meta.prev_page_url 上一页
  128. * @apiSuccessExample {json} Success-Response:
  129. * HTTP/1.1 200 OK
  130. * {
  131. * code: 0,
  132. * msg: "",
  133. * data:
  134. * list:[
  135. * {
  136. * bid: 5,
  137. * chapter_id: 5,
  138. * chapter_name: "第1240章 不是我",
  139. * chapter_sequence: 1239,
  140. * chapter_is_vip: 1,
  141. * chapter_size: 2422,
  142. * prev_cid: 0,
  143. * next_cid: 0,
  144. * recent_update_at: 2017-11-20 15:01:56,
  145. * is_need_charge: 0,
  146. * },
  147. * {
  148. * bid: 5,
  149. * chapter_id: 5,
  150. * chapter_name: "第1240章 不是我",
  151. * chapter_sequence: 1239,
  152. * chapter_is_vip: 1,
  153. * chapter_size: 2422,
  154. * prev_cid: 0,
  155. * next_cid: 0,
  156. * recent_update_at: 2017-11-20 15:01:56,
  157. * is_need_charge: 0,
  158. * },
  159. * ]
  160. * meta:{
  161. * total: 1253,
  162. * per_page: 15,
  163. * current_page: 1,
  164. * last_page: 84,
  165. * next_page_url: "http://myapi.cn/api/books/1/chapter?page=2",
  166. * prev_page_url: ""
  167. * }
  168. * }
  169. */
  170. public function getCatalogPerPage(Request $request, $bid)
  171. {
  172. $bid = BookService::decodeBidStatic($bid);
  173. $book_info = BookConfigService::getBookById($bid);
  174. if (!$book_info) {
  175. return response()->error('PARAM_ERROR');
  176. }
  177. $page_size = $request->input('page_size', 15);
  178. if ($page_size >= 100) $page_size = 100;
  179. $res = ChapterService::getChapterListsPage($bid, $page_size);
  180. $lists = $this->getChapterCatalog($bid, $res, $book_info);
  181. return response()->pagination(new ChapterListTransformer, $lists);
  182. }
  183. private function getChapterCatalog(int $bid, $chapters, $book_info)
  184. {
  185. switch ($book_info->charge_type) {
  186. case 'BOOK':
  187. $price = $this->getPrice($book_info);
  188. $is_need_charge = $this->isBookNeedCharge($bid, $price);
  189. foreach ($chapters as $v) {
  190. $v->is_need_charge = $v->is_vip ? $is_need_charge : false;
  191. $v->price = $price;
  192. }
  193. break;
  194. default:
  195. foreach ($chapters as $v) {
  196. $v->price = $v->is_vip ? $this->getPrice($book_info, $v->size) : 0;
  197. $v->is_need_charge = $v->is_vip ? $this->isChapterNeedCharge($bid, $v->id, $v->price) : false;
  198. }
  199. break;
  200. }
  201. return $chapters;
  202. }
  203. /**
  204. * @apiVersion 1.0.0
  205. * @apiDescription 章节内容
  206. * @api {get} books/{bid}/chapters/{chapter_id} 章节内容
  207. * @apiParam {String} [token] token
  208. * @apiHeader {String} [Authorization] token 两个token任选其一
  209. * @apiGroup Chapter
  210. * @apiName index
  211. * @apiSuccess {int} code 状态码
  212. * @apiSuccess {String} msg 信息
  213. * @apiSuccess {object} data 结果集
  214. * @apiSuccess {Int} data.chapter_id 章节id
  215. * @apiSuccess {String} data.chapter_name 章节名称
  216. * @apiSuccess {Int} data.chapter_sequence 序号
  217. * @apiSuccess {Int} data.chapter_is_vip 是否vip
  218. * @apiSuccess {Int} data.chapter_size 章节大小
  219. * @apiSuccess {Int} data.prev_cid 上一章节id
  220. * @apiSuccess {Int} data.next_cid 下一章节
  221. * @apiSuccess {String} data.recent_update_at 更新时间
  222. * @apiSuccess {String} data.chapter_content 章节内容
  223. * @apiSuccessExample {json} Success-Response:
  224. * HTTP/1.1 200 OK
  225. * {
  226. * code: 0,
  227. * msg: "",
  228. * data: {
  229. * chapter_id: 5,
  230. * chapter_name: "第1240章 不是我",
  231. * chapter_sequence: 1239,
  232. * chapter_is_vip: 1,
  233. * chapter_size: 2422,
  234. * prev_cid: 0,
  235. * next_cid: 0,
  236. * recent_update_at: 2017-11-20 15:01:56,
  237. * chapter_content: "叶妩被司行霈的阴阳怪气一吓,思路偏得太远了。 她张口结舌,忘记了自己要说什么。",
  238. * }
  239. * }
  240. */
  241. public function index(Request $request, $bid, $cid)
  242. {
  243. $oldbid = $bid;
  244. $bid = BookService::decodeBidStatic($bid);
  245. //获取图书信息
  246. $book_info = BookConfigService::getBookById($bid);
  247. if (empty($book_info))
  248. return response()->error('QAPP_SYS_ERROR');
  249. //获取章节信息
  250. $chapter = ChapterService::getChapterNameById($cid, $bid);
  251. if (!$chapter) {
  252. return response()->error('QAPP_SYS_ERROR');
  253. }
  254. if ($chapter->is_vip == 0) {
  255. ReadRecordService::addReadRecord([
  256. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  257. 'cid' => $cid, 'chapter_name' => $chapter->name
  258. ]);
  259. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  260. }
  261. //已经付费
  262. if ($this->getOrderRecord($bid, $cid)) {
  263. ReadRecordService::addReadRecord([
  264. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  265. 'cid' => $cid, 'chapter_name' => $chapter->name
  266. ]);
  267. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  268. }
  269. //未付费 要提醒
  270. $user_info = $this->user_info;
  271. //未付费 余额不足
  272. $fee = $this->getPrice($book_info, $chapter->size);
  273. $data = [
  274. 'book_id' => $oldbid,
  275. 'book_name' => $book_info->book_name,
  276. 'chapter_name' => $chapter->name,
  277. 'chapter_id' => $cid,
  278. 'pay_type' => $book_info->charge_type,
  279. 'fee' => $fee,
  280. 'user_balance' => $user_info->balance,
  281. 'product_id' => $book_info->product_id,
  282. 'uid' => $this->uid,
  283. 'distribution_channel_id' => $this->distribution_channel_id,
  284. 'is_discount' => 0,
  285. 'discount_fee' => '',
  286. 'discount' => ''
  287. ];
  288. if ($user_info['balance'] < $fee) {
  289. if ($book_info->charge_type == 'BOOK') {
  290. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  291. } elseif ($book_info->charge_type == 'CHAPTER') {
  292. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  293. } else {
  294. return response()->error('QAPP_SYS_ERROR');
  295. }
  296. }
  297. //付费 不提醒
  298. if ($this->balancePay($book_info, $cid, $chapter->size, $chapter->name, 0)) {
  299. ReadRecordService::addReadRecord([
  300. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  301. 'cid' => $cid, 'chapter_name' => $chapter->name
  302. ]);
  303. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  304. } else {
  305. if ($book_info->charge_type == 'BOOK') {
  306. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  307. } elseif ($book_info->charge_type == 'CHAPTER') {
  308. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  309. } else {
  310. return response()->error('QAPP_SYS_ERROR');
  311. }
  312. }
  313. }
  314. /**
  315. * @apiVersion 1.0.0
  316. * @apiDescription 余额支付
  317. * @api {get} books/{bid}/balance/chapterOrders/{cid} 余额支付
  318. * @apiParam {String} [token] token
  319. * @apiHeader {String} [Authorization] token 两个token任选其一
  320. * @apiGroup Chapter
  321. * @apiName pay
  322. * @apiParam (Int) remind 提醒
  323. * @apiSuccess {int} code 状态码
  324. * @apiSuccess {String} msg 信息
  325. * @apiSuccess {object} data 结果集
  326. * @apiSuccess {Int} data.chapter_id 章节id
  327. * @apiSuccess {String} data.chapter_name 章节名称
  328. * @apiSuccess {Int} data.chapter_sequence 序号
  329. * @apiSuccess {Int} data.chapter_is_vip 是否vip
  330. * @apiSuccess {Int} data.chapter_size 章节大小
  331. * @apiSuccess {Int} data.prev_cid 上一章节id
  332. * @apiSuccess {Int} data.next_cid 下一章节
  333. * @apiSuccess {String} data.recent_update_at 更新时间
  334. * @apiSuccess {String} data.chapter_content 章节内容
  335. * @apiSuccessExample {json} Success-Response:
  336. * HTTP/1.1 200 OK
  337. * {
  338. * code: 0,
  339. * msg: "",
  340. * data: {
  341. * chapter_id: 5,
  342. * chapter_name: "第1240章 不是我",
  343. * chapter_sequence: 1239,
  344. * chapter_is_vip: 1,
  345. * chapter_size: 2422,
  346. * prev_cid: 0,
  347. * next_cid: 0,
  348. * recent_update_at: 2017-11-20 15:01:56,
  349. * chapter_content: "叶妩被司行霈的阴阳怪气一吓,思路偏得太远了。 她张口结舌,忘记了自己要说什么。",
  350. * }
  351. * }
  352. */
  353. public function pay(Request $request, $bid, $cid)
  354. {
  355. $remind = (int) $request->input('remind');
  356. $oldbid = $bid;
  357. $bid = BookService::decodeBidStatic($bid);
  358. $book_info = BookConfigService::getBookById($bid);;
  359. if (empty($book_info)) response()->error('QAPP_SYS_ERROR');
  360. if ($book_info->is_on_shelf == 0 || $book_info->is_on_shelf == 3) {
  361. if (!$this->isBookOrdered($bid)) {
  362. response()->error('QAPP_OFF_SHELF');
  363. }
  364. }
  365. //获取章节
  366. $chapter = ChapterService::getChapterNameById($cid, $bid);
  367. if (!$chapter) {
  368. return response()->error('QAPP_SYS_ERROR');
  369. }
  370. if ($this->balancePay($book_info, $cid, $chapter->size, $chapter->name, $remind)) {
  371. ReadRecordService::addReadRecord([
  372. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  373. 'cid' => $cid, 'chapter_name' => $chapter->name
  374. ]);
  375. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  376. } else {
  377. $fee = $this->getPrice($book_info, $chapter->size);
  378. $data = [
  379. 'book_id' => $oldbid,
  380. 'book_name' => $book_info->book_name,
  381. 'chapter_name' => $chapter->name,
  382. 'chapter_id' => $cid,
  383. 'pay_type' => $book_info->charge_type,
  384. 'fee' => $fee,
  385. 'user_balance' => $this->user_info['balance'],
  386. 'product_id' => $book_info->product_id,
  387. 'uid' => $this->uid,
  388. 'distribution_channel_id' => $this->distribution_channel_id,
  389. 'is_discount' => 0,
  390. 'discount_fee' => '',
  391. 'discount' => ''
  392. ];
  393. if ($book_info->charge_type == 'BOOK') {
  394. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  395. } elseif ($book_info->charge_type == 'CHAPTER') {
  396. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  397. } else {
  398. return response()->error('QAPP_SYS_ERROR');
  399. }
  400. }
  401. }
  402. /**
  403. * 余额支付
  404. * @param $book_info
  405. * @param $chapter_id
  406. * @param $chapter_size
  407. * @return bool
  408. */
  409. protected function balancePay($book_info, $chapter_id, $chapter_size, $chapter_name, $is_remind)
  410. {
  411. $fee = $this->getPrice($book_info, $chapter_size);
  412. if ((int)$this->user_info['balance'] >= $fee) {
  413. if ($this->bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)) {
  414. return true;
  415. }
  416. return false;
  417. } else {
  418. return false;
  419. }
  420. }
  421. /**
  422. * 获取章节内容
  423. * @param $bid
  424. * @param $cid
  425. * @return bool|mixed
  426. */
  427. protected function getChapter($bid, $cid, $chapter)
  428. {
  429. $chapter_content = ChapterService::getChapter($bid, $cid);
  430. if (!$chapter_content) return false;
  431. $chapter->content = trim(str_replace($chapter_content->name, '', $chapter_content->content));
  432. //统计点击率
  433. $key = 'book_click_num_bid_' . $bid;
  434. $field = date('Y-m-d');
  435. $old = Redis::hget($key, $field);
  436. if (!$old) $old = 0;
  437. Redis::hset($key, $field, $old + 1);
  438. return $chapter;
  439. }
  440. /**
  441. * 添加订购记录
  442. * @param $book_info
  443. * @param $chapter_id
  444. * @param $fee
  445. * @return bool
  446. */
  447. protected function bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)
  448. {
  449. if ($book_info['charge_type'] == 'BOOK') {
  450. $data = [
  451. 'uid' => $this->uid,
  452. 'fee' => $fee,
  453. 'u' => $this->send_order_id,
  454. 'distribution_channel_id' => $this->distribution_channel_id,
  455. 'bid' => $book_info->bid,
  456. 'book_name' => $book_info->book_name,
  457. 'send_order_id' => $this->send_order_id,
  458. ];
  459. return BookOrderService::addOrderRecodeAndDecrUserBalance($data, $this->uid);
  460. } else {
  461. $data = [
  462. 'uid' => $this->uid,
  463. 'fee' => $fee,
  464. 'cid' => $chapter_id,
  465. 'bid' => $book_info->bid,
  466. 'distribution_channel_id' => $this->distribution_channel_id,
  467. 'book_name' => $book_info->book_name,
  468. 'chapter_name' => $chapter_name,
  469. 'send_order_id' => $this->send_order_id,
  470. 'is_remind' => $is_remind
  471. ];
  472. if ($is_remind) {
  473. $this->addOrderRemind($book_info->bid);
  474. }
  475. return ChapterOrderService::addOrderAndDecrUserBalance($data, $this->uid);
  476. }
  477. }
  478. protected function addOrderRemind($bid)
  479. {
  480. if (ChapterReminderService::checkIsNoReminder($this->uid, $bid)) {
  481. return true;
  482. } else {
  483. ChapterReminderService::add($this->uid, $bid);
  484. return true;
  485. }
  486. }
  487. /**
  488. * 是否订购提醒
  489. * @param $chapter_id
  490. * @return bool
  491. */
  492. protected function isOrderRemind($bid)
  493. {
  494. $is_no_reminder = ChapterReminderService::checkIsNoReminder($this->uid, $bid) ? 1 : 0;
  495. return $is_no_reminder == 0;
  496. }
  497. /**
  498. * 用户是否关注
  499. * @param $uid
  500. * @return bool
  501. */
  502. protected function getSubscribe()
  503. {
  504. $res = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $this->uid]);
  505. if ($res) return true;
  506. return false;
  507. }
  508. /**
  509. * 获取订购记录
  510. * @param $book_info
  511. * @param $chapter_id
  512. * @return bool
  513. */
  514. protected function getOrderRecord($bid, $chapter_id)
  515. {
  516. //包年记录
  517. $uid = $this->uid;
  518. $res = YearOrderService::getRecord($uid);
  519. if ($res) return true;
  520. $res = null;
  521. //单本订购记录
  522. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  523. if ($res) return true;
  524. $res = null;
  525. //章节订购记录
  526. $chapterOrder = new ChapterOrderService();
  527. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  528. return false;
  529. }
  530. /**
  531. * 计算价格
  532. * @param $book_info
  533. * @param $chapter_size
  534. * @return float
  535. */
  536. protected function getPrice($book_info, $chapter_size = 0)
  537. {
  538. if ($book_info->charge_type == 'BOOK')
  539. return $book_info->price * 100;
  540. return ceil($chapter_size / 100);
  541. }
  542. /**
  543. * 用户添加标签
  544. * @param $book_info
  545. */
  546. protected function addTag($book_info)
  547. {
  548. if (!UserDeepReadTagService::isAddTag($this->uid, $book_info->bid)) {
  549. try {
  550. UserDeepReadTagService::addTag([
  551. 'uid' => $this->uid,
  552. 'bid' => $book_info->bid,
  553. 'book_name' => $book_info->book_name,
  554. 'category_id' => $book_info->category_id,
  555. 'category_name' => $book_info->category_name,
  556. 'sex_preference' => $book_info->channel_name ? $book_info->channel_name : '',
  557. 'distribution_channel_id' => $this->distribution_channel_id ? $this->distribution_channel_id : '0',
  558. 'send_order_id' => $this->send_order_id,
  559. ]);
  560. } catch (\Exception $e) { }
  561. }
  562. }
  563. protected function isBookOrdered($bid)
  564. {
  565. $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $bid);
  566. if ($chapter_order) return true;
  567. $res = BookOrderService::getRecordByuidBid($this->uid, $bid);
  568. if ($res) return true;
  569. return false;
  570. }
  571. /**
  572. * 判断是否需要充值
  573. */
  574. private function isBookNeedCharge(int $bid, float $price)
  575. {
  576. $book_order = BookOrderService::getRecordByuidBid($this->uid, $bid);
  577. if ($book_order) {
  578. return false;
  579. } else {
  580. $user_info = $this->user_info;
  581. return $user_info['balance'] < $price;
  582. }
  583. }
  584. /**
  585. * 判断章节是否需要充值
  586. */
  587. private function isChapterNeedCharge(int $bid, int $cid, float $price)
  588. {
  589. $chapter_order = ChapterOrderService::checkIsOrderedStatic($this->uid, $bid, $cid);
  590. if ($chapter_order) {
  591. return false;
  592. } else {
  593. $user_info = $this->user_info;
  594. return $user_info['balance'] < $price;
  595. }
  596. }
  597. }