ChapterController.php 24 KB

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