ChapterController.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. $is_next_day = date('Y-m-d', strtotime($this->user_info->created_at)) == date('Y-m-d', strtotime('-1 days'));
  256. if ($is_next_day) {
  257. $job = new UserRententionJob($this->uid, now(), $this->user_info->created_at);
  258. dispatch($job)->onConnection('rabbitmq')->onQueue('user_rentention_queue');
  259. }
  260. if ($chapter->is_vip == 0) {
  261. ReadRecordService::addReadRecord([
  262. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  263. 'cid' => $cid, 'chapter_name' => $chapter->name
  264. ]);
  265. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  266. }
  267. //已经付费
  268. if ($this->getOrderRecord($bid, $cid)) {
  269. ReadRecordService::addReadRecord([
  270. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  271. 'cid' => $cid, 'chapter_name' => $chapter->name
  272. ]);
  273. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  274. }
  275. //未付费 要提醒
  276. $user_info = $this->user_info;
  277. //未付费 余额不足
  278. $fee = $this->getPrice($book_info, $chapter->size);
  279. $data = [
  280. 'book_id' => $oldbid,
  281. 'book_name' => $book_info->book_name,
  282. 'chapter_name' => $chapter->name,
  283. 'chapter_id' => $cid,
  284. 'pay_type' => $book_info->charge_type,
  285. 'fee' => $fee,
  286. 'user_balance' => $user_info->balance,
  287. 'product_id' => $book_info->product_id,
  288. 'uid' => $this->uid,
  289. 'distribution_channel_id' => $this->distribution_channel_id,
  290. 'is_discount' => 0,
  291. 'discount_fee' => '',
  292. 'discount' => ''
  293. ];
  294. if ($user_info['balance'] < $fee) {
  295. if ($book_info->charge_type == 'BOOK') {
  296. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  297. } elseif ($book_info->charge_type == 'CHAPTER') {
  298. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  299. } else {
  300. return response()->error('QAPP_SYS_ERROR');
  301. }
  302. }
  303. //付费 不提醒
  304. if ($this->balancePay($book_info, $cid, $chapter->size, $chapter->name, 0)) {
  305. ReadRecordService::addReadRecord([
  306. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  307. 'cid' => $cid, 'chapter_name' => $chapter->name
  308. ]);
  309. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  310. } else {
  311. if ($book_info->charge_type == 'BOOK') {
  312. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  313. } elseif ($book_info->charge_type == 'CHAPTER') {
  314. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  315. } else {
  316. return response()->error('QAPP_SYS_ERROR');
  317. }
  318. }
  319. }
  320. /**
  321. * @apiVersion 1.0.0
  322. * @apiDescription 余额支付
  323. * @api {get} books/{bid}/balance/chapterOrders/{cid} 余额支付
  324. * @apiParam {String} [token] token
  325. * @apiHeader {String} [Authorization] token 两个token任选其一
  326. * @apiGroup Chapter
  327. * @apiName pay
  328. * @apiParam (Int) remind 提醒
  329. * @apiSuccess {int} code 状态码
  330. * @apiSuccess {String} msg 信息
  331. * @apiSuccess {object} data 结果集
  332. * @apiSuccess {Int} data.chapter_id 章节id
  333. * @apiSuccess {String} data.chapter_name 章节名称
  334. * @apiSuccess {Int} data.chapter_sequence 序号
  335. * @apiSuccess {Int} data.chapter_is_vip 是否vip
  336. * @apiSuccess {Int} data.chapter_size 章节大小
  337. * @apiSuccess {Int} data.prev_cid 上一章节id
  338. * @apiSuccess {Int} data.next_cid 下一章节
  339. * @apiSuccess {String} data.recent_update_at 更新时间
  340. * @apiSuccess {String} data.chapter_content 章节内容
  341. * @apiSuccessExample {json} Success-Response:
  342. * HTTP/1.1 200 OK
  343. * {
  344. * code: 0,
  345. * msg: "",
  346. * data: {
  347. * chapter_id: 5,
  348. * chapter_name: "第1240章 不是我",
  349. * chapter_sequence: 1239,
  350. * chapter_is_vip: 1,
  351. * chapter_size: 2422,
  352. * prev_cid: 0,
  353. * next_cid: 0,
  354. * recent_update_at: 2017-11-20 15:01:56,
  355. * chapter_content: "叶妩被司行霈的阴阳怪气一吓,思路偏得太远了。 她张口结舌,忘记了自己要说什么。",
  356. * }
  357. * }
  358. */
  359. public function pay(Request $request, $bid, $cid)
  360. {
  361. $remind = (int) $request->input('remind');
  362. $oldbid = $bid;
  363. $bid = BookService::decodeBidStatic($bid);
  364. $book_info = BookConfigService::getBookById($bid);;
  365. if (empty($book_info)) response()->error('QAPP_SYS_ERROR');
  366. if ($book_info->is_on_shelf == 0 || $book_info->is_on_shelf == 3) {
  367. if (!$this->isBookOrdered($bid)) {
  368. response()->error('QAPP_OFF_SHELF');
  369. }
  370. }
  371. //获取章节
  372. $chapter = ChapterService::getChapterNameById($cid, $bid);
  373. if (!$chapter) {
  374. return response()->error('QAPP_SYS_ERROR');
  375. }
  376. if ($this->balancePay($book_info, $cid, $chapter->size, $chapter->name, $remind)) {
  377. ReadRecordService::addReadRecord([
  378. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  379. 'cid' => $cid, 'chapter_name' => $chapter->name
  380. ]);
  381. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  382. } else {
  383. $fee = $this->getPrice($book_info, $chapter->size);
  384. $data = [
  385. 'book_id' => $oldbid,
  386. 'book_name' => $book_info->book_name,
  387. 'chapter_name' => $chapter->name,
  388. 'chapter_id' => $cid,
  389. 'pay_type' => $book_info->charge_type,
  390. 'fee' => $fee,
  391. 'user_balance' => $this->user_info['balance'],
  392. 'product_id' => $book_info->product_id,
  393. 'uid' => $this->uid,
  394. 'distribution_channel_id' => $this->distribution_channel_id,
  395. 'is_discount' => 0,
  396. 'discount_fee' => '',
  397. 'discount' => ''
  398. ];
  399. if ($book_info->charge_type == 'BOOK') {
  400. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  401. } elseif ($book_info->charge_type == 'CHAPTER') {
  402. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  403. } else {
  404. return response()->error('QAPP_SYS_ERROR');
  405. }
  406. }
  407. }
  408. /**
  409. * 余额支付
  410. * @param $book_info
  411. * @param $chapter_id
  412. * @param $chapter_size
  413. * @return bool
  414. */
  415. protected function balancePay($book_info, $chapter_id, $chapter_size, $chapter_name, $is_remind)
  416. {
  417. $fee = $this->getPrice($book_info, $chapter_size);
  418. if ((int) $this->user_info['balance'] >= $fee) {
  419. if ($this->bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)) {
  420. return true;
  421. }
  422. return false;
  423. } else {
  424. return false;
  425. }
  426. }
  427. /**
  428. * 获取章节内容
  429. * @param $bid
  430. * @param $cid
  431. * @return bool|mixed
  432. */
  433. protected function getChapter($bid, $cid, $chapter)
  434. {
  435. $chapter_content = ChapterService::getChapter($bid, $cid);
  436. if (!$chapter_content) return false;
  437. $chapter->content = trim(str_replace($chapter_content->name, '', $chapter_content->content));
  438. //统计点击率
  439. $key = 'book_click_num_bid_' . $bid;
  440. $field = date('Y-m-d');
  441. $old = Redis::hget($key, $field);
  442. if (!$old) $old = 0;
  443. Redis::hset($key, $field, $old + 1);
  444. return $chapter;
  445. }
  446. /**
  447. * 添加订购记录
  448. * @param $book_info
  449. * @param $chapter_id
  450. * @param $fee
  451. * @return bool
  452. */
  453. protected function bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)
  454. {
  455. if ($book_info['charge_type'] == 'BOOK') {
  456. $data = [
  457. 'uid' => $this->uid,
  458. 'fee' => $fee,
  459. 'u' => $this->send_order_id,
  460. 'distribution_channel_id' => $this->distribution_channel_id,
  461. 'bid' => $book_info->bid,
  462. 'book_name' => $book_info->book_name,
  463. 'send_order_id' => $this->send_order_id,
  464. ];
  465. return BookOrderService::addOrderRecodeAndDecrUserBalance($data, $this->uid);
  466. } else {
  467. $data = [
  468. 'uid' => $this->uid,
  469. 'fee' => $fee,
  470. 'cid' => $chapter_id,
  471. 'bid' => $book_info->bid,
  472. 'distribution_channel_id' => $this->distribution_channel_id,
  473. 'book_name' => $book_info->book_name,
  474. 'chapter_name' => $chapter_name,
  475. 'send_order_id' => $this->send_order_id,
  476. 'is_remind' => $is_remind
  477. ];
  478. if ($is_remind) {
  479. $this->addOrderRemind($book_info->bid);
  480. }
  481. return ChapterOrderService::addOrderAndDecrUserBalance($data, $this->uid);
  482. }
  483. }
  484. protected function addOrderRemind($bid)
  485. {
  486. if (ChapterReminderService::checkIsNoReminder($this->uid, $bid)) {
  487. return true;
  488. } else {
  489. ChapterReminderService::add($this->uid, $bid);
  490. return true;
  491. }
  492. }
  493. /**
  494. * 是否订购提醒
  495. * @param $chapter_id
  496. * @return bool
  497. */
  498. protected function isOrderRemind($bid)
  499. {
  500. $is_no_reminder = ChapterReminderService::checkIsNoReminder($this->uid, $bid) ? 1 : 0;
  501. return $is_no_reminder == 0;
  502. }
  503. /**
  504. * 用户是否关注
  505. * @param $uid
  506. * @return bool
  507. */
  508. protected function getSubscribe()
  509. {
  510. $res = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $this->uid]);
  511. if ($res) return true;
  512. return false;
  513. }
  514. /**
  515. * 获取订购记录
  516. * @param $book_info
  517. * @param $chapter_id
  518. * @return bool
  519. */
  520. protected function getOrderRecord($bid, $chapter_id)
  521. {
  522. //包年记录
  523. $uid = $this->uid;
  524. $res = YearOrderService::getRecord($uid);
  525. if ($res) return true;
  526. $res = null;
  527. //单本订购记录
  528. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  529. if ($res) return true;
  530. $res = null;
  531. //章节订购记录
  532. $chapterOrder = new ChapterOrderService();
  533. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  534. return false;
  535. }
  536. /**
  537. * 计算价格
  538. * @param $book_info
  539. * @param $chapter_size
  540. * @return float
  541. */
  542. protected function getPrice($book_info, $chapter_size = 0)
  543. {
  544. if ($book_info->charge_type == 'BOOK')
  545. return $book_info->price * 100;
  546. return ceil($chapter_size / 100);
  547. }
  548. /**
  549. * 用户添加标签
  550. * @param $book_info
  551. */
  552. protected function addTag($book_info)
  553. {
  554. if (!UserDeepReadTagService::isAddTag($this->uid, $book_info->bid)) {
  555. try {
  556. UserDeepReadTagService::addTag([
  557. 'uid' => $this->uid,
  558. 'bid' => $book_info->bid,
  559. 'book_name' => $book_info->book_name,
  560. 'category_id' => $book_info->category_id,
  561. 'category_name' => $book_info->category_name,
  562. 'sex_preference' => $book_info->channel_name ? $book_info->channel_name : '',
  563. 'distribution_channel_id' => $this->distribution_channel_id ? $this->distribution_channel_id : '0',
  564. 'send_order_id' => $this->send_order_id,
  565. ]);
  566. } catch (\Exception $e) {
  567. }
  568. }
  569. }
  570. protected function isBookOrdered($bid)
  571. {
  572. $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $bid);
  573. if ($chapter_order) return true;
  574. $res = BookOrderService::getRecordByuidBid($this->uid, $bid);
  575. if ($res) return true;
  576. return false;
  577. }
  578. /**
  579. * 判断是否需要充值
  580. */
  581. private function isBookNeedCharge(int $bid, float $price)
  582. {
  583. $book_order = BookOrderService::getRecordByuidBid($this->uid, $bid);
  584. if ($book_order) {
  585. return false;
  586. } else {
  587. $user_info = $this->user_info;
  588. return $user_info['balance'] < $price;
  589. }
  590. }
  591. /**
  592. * 判断章节是否需要充值
  593. */
  594. private function isChapterNeedCharge(int $bid, int $cid, float $price)
  595. {
  596. $chapter_order = ChapterOrderService::checkIsOrderedStatic($this->uid, $bid, $cid);
  597. if ($chapter_order) {
  598. return false;
  599. } else {
  600. $user_info = $this->user_info;
  601. return $user_info['balance'] < $price;
  602. }
  603. }
  604. }