ChapterController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\Book;
  3. use App\Modules\SendOrder\Services\SendOrderService;
  4. use App\Modules\Statistic\Services\WapVisitStatService;
  5. use Illuminate\Http\Request;
  6. use App\Http\Controllers\QuickApp\BaseController;
  7. use Redis;
  8. use App\Modules\Book\Services\ChapterService;
  9. use App\Modules\User\Services\ReadRecordService;
  10. use App\Http\Controllers\QuickApp\Book\Transformers\ChapterTransformer;
  11. use App\Modules\Book\Services\BookConfigService;
  12. use App\Http\Controllers\QuickApp\Book\Transformers\ChapterListTransformer;
  13. use App\Jobs\UserRententionJob;
  14. use App\Modules\Book\Services\BookService;
  15. use App\Modules\Subscribe\Services\BookOrderService;
  16. use App\Modules\Subscribe\Services\ChapterOrderService;
  17. use App\Modules\Subscribe\Services\YearOrderService;
  18. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  19. use App\Modules\Subscribe\Services\ChapterReminderService;
  20. use App\Modules\User\Services\UserDeepReadTagService;
  21. use App\Modules\UserTask\Services\BaseTask;
  22. use App\Modules\UserTask\Services\UserTaskService;
  23. class ChapterController extends BaseController
  24. {
  25. private $book_info;
  26. public function getCatalog(Request $request, $bid)
  27. {
  28. $bid = BookService::decodeBidStatic($bid);
  29. $lists = ChapterService::getChapterLists($bid);
  30. $book_info = BookConfigService::getBookById($bid);
  31. if (!$book_info) {
  32. return response()->error('PARAM_ERROR');
  33. }
  34. $lists = $this->getChapterCatalog($bid, $lists, $book_info);
  35. return response()->collection(new ChapterListTransformer, $lists);
  36. }
  37. public function getCatalogPerPage(Request $request, $bid)
  38. {
  39. $bid = BookService::decodeBidStatic($bid);
  40. $book_info = BookConfigService::getBookById($bid);
  41. if (!$book_info) {
  42. return response()->error('PARAM_ERROR');
  43. }
  44. $page_size = $request->input('page_size', 15);
  45. if ($page_size >= 100) $page_size = 100;
  46. $res = ChapterService::getChapterListsPage($bid, $page_size);
  47. $lists = $this->getChapterCatalog($bid, $res, $book_info);
  48. return response()->pagination(new ChapterListTransformer, $lists);
  49. }
  50. private function getChapterCatalog(int $bid, $chapters, $book_info)
  51. {
  52. // 查询书籍是否限免
  53. $isFree = BookConfigService::judgeBookIsFree($bid);
  54. //渠道自定义vip章节
  55. $vip_sequence = Redis::hget('channel:chapterfee:setting:' . $this->distribution_channel_id, $bid);
  56. switch ($book_info->charge_type) {
  57. case 'BOOK':
  58. $price = $this->getPrice($book_info);
  59. $is_need_charge = $this->isBookNeedCharge($bid, $price);
  60. foreach ($chapters as $v) {
  61. $v->is_need_charge = $v->is_vip ? $is_need_charge : false;
  62. $v->price = $price;
  63. // 限免判断
  64. if ($isFree) {
  65. $v->is_need_charge = false;
  66. $v->price = 0;
  67. }
  68. if($vip_sequence){
  69. if($v->sequence >= $vip_sequence){
  70. $v->is_vip = 1;
  71. }else{
  72. $v->is_vip = 0;
  73. }
  74. }
  75. }
  76. break;
  77. default:
  78. foreach ($chapters as $v) {
  79. // 限免判断
  80. if ($isFree) {
  81. $v->is_need_charge = false;
  82. $v->price = 0;
  83. } else {
  84. $v->price = $v->is_vip ? $this->getPrice($book_info, $v->size) : 0;
  85. $v->is_need_charge = $v->is_vip ? $this->isChapterNeedCharge($bid, $v->id, $v->price) : false;
  86. }
  87. if($vip_sequence){
  88. if($v->sequence >= $vip_sequence){
  89. $v->is_vip = 1;
  90. }else{
  91. $v->is_vip = 0;
  92. }
  93. }
  94. }
  95. break;
  96. }
  97. return $chapters;
  98. }
  99. public function index(Request $request, $bid, $cid)
  100. {
  101. $oldbid = $bid;
  102. $bid = BookService::decodeBidStatic($bid);
  103. //获取图书信息
  104. $book_info = BookConfigService::getBookById($bid);
  105. if (empty($book_info))
  106. return response()->error('QAPP_SYS_ERROR');
  107. $this->book_info = $book_info;
  108. //获取章节信息
  109. $chapter = ChapterService::getChapterNameById($cid, $bid);
  110. if (!$chapter) {
  111. return response()->error('QAPP_SYS_ERROR');
  112. }
  113. $is_next_day = date('Y-m-d', strtotime($this->user_info->created_at)) == date('Y-m-d', strtotime('-1 days'));
  114. if ($is_next_day) {
  115. $job = new UserRententionJob($this->uid, now(), $this->user_info->created_at);
  116. dispatch($job)->onConnection('rabbitmq')->onQueue('user_rentention_queue');
  117. }
  118. //自定义vip章节
  119. $vip_sequence = Redis::hget('channel:chapterfee:setting:' . $this->distribution_channel_id, $bid);
  120. if($vip_sequence){
  121. if($chapter->sequence >= $vip_sequence){
  122. $chapter->is_vip = 1;
  123. }else{
  124. $chapter->is_vip = 0;
  125. }
  126. }
  127. if ($chapter->is_vip == 0) {
  128. ReadRecordService::addReadLog($this->uid, [
  129. 'distribution_channel_id' => $this->distribution_channel_id,
  130. 'bid' => $bid,
  131. 'cid' => $chapter->id,
  132. 'uid' => $this->uid,
  133. 'send_order_id' => $this->send_order_id,
  134. 'sequence' => $chapter->sequence,
  135. ]);
  136. ReadRecordService::addReadRecord([
  137. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  138. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  139. ]);
  140. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  141. }
  142. // 书籍是否限免
  143. $free = BookConfigService::judgeBookIsFree($bid);
  144. if ($free) {
  145. ReadRecordService::addReadLog($this->uid, [
  146. 'distribution_channel_id' => $this->distribution_channel_id,
  147. 'bid' => $bid,
  148. 'cid' => $chapter->id,
  149. 'uid' => $this->uid,
  150. 'send_order_id' => $this->send_order_id,
  151. 'sequence' => $chapter->sequence,
  152. ]);
  153. ReadRecordService::addReadRecord([
  154. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  155. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  156. ]);
  157. if ($chapter->is_vip == 1) {
  158. $fee = $this->getPrice($book_info, $chapter->size);
  159. $now = date('Y-m-d');
  160. Redis::hincrby('qapp:book:free:virtual:' . $free->id, $now, $fee);
  161. Redis::sadd('qapp:free:virtual' . $now, $free->id);
  162. }
  163. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  164. }
  165. //已经付费
  166. if ($this->getOrderRecord($bid, $cid)) {
  167. ReadRecordService::addReadLog($this->uid, [
  168. 'distribution_channel_id' => $this->distribution_channel_id,
  169. 'bid' => $bid,
  170. 'cid' => $chapter->id,
  171. 'uid' => $this->uid,
  172. 'send_order_id' => $this->send_order_id,
  173. 'sequence' => $chapter->sequence,
  174. ]);
  175. ReadRecordService::addReadRecord([
  176. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  177. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  178. ]);
  179. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  180. }
  181. //未付费 要提醒
  182. $user_info = $this->user_info;
  183. //未付费 余额不足
  184. $fee = $this->getPrice($book_info, $chapter->size);
  185. $data = [
  186. 'book_id' => $oldbid,
  187. 'book_name' => $book_info->book_name,
  188. 'chapter_name' => $chapter->name,
  189. 'chapter_id' => $cid,
  190. 'pay_type' => $book_info->charge_type,
  191. 'fee' => $fee,
  192. 'user_balance' => $user_info->balance,
  193. 'product_id' => $book_info->product_id,
  194. 'uid' => $this->uid,
  195. 'distribution_channel_id' => $this->distribution_channel_id,
  196. 'is_discount' => 0,
  197. 'discount_fee' => '',
  198. 'discount' => ''
  199. ];
  200. if ($user_info['balance'] < $fee) {
  201. if ($book_info->charge_type == 'BOOK') {
  202. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  203. } elseif ($book_info->charge_type == 'CHAPTER') {
  204. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  205. } else {
  206. return response()->error('QAPP_SYS_ERROR');
  207. }
  208. }
  209. //付费 不提醒
  210. if ($this->balancePay($book_info, $cid, $chapter->size, $chapter->name, 0)) {
  211. UserTaskService::addUserTaskQueue($this->uid, BaseTask::read, UserTaskService::judge_trigger);
  212. ReadRecordService::addReadLog($this->uid, [
  213. 'distribution_channel_id' => $this->distribution_channel_id,
  214. 'bid' => $bid,
  215. 'cid' => $chapter->id,
  216. 'uid' => $this->uid,
  217. 'send_order_id' => $this->send_order_id,
  218. 'sequence' => $chapter->sequence,
  219. ]);
  220. ReadRecordService::addReadRecord([
  221. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  222. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  223. ]);
  224. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  225. } else {
  226. if ($book_info->charge_type == 'BOOK') {
  227. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  228. } elseif ($book_info->charge_type == 'CHAPTER') {
  229. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  230. } else {
  231. return response()->error('QAPP_SYS_ERROR');
  232. }
  233. }
  234. }
  235. public function pay(Request $request, $bid, $cid)
  236. {
  237. $remind = (int)$request->input('remind');
  238. $oldbid = $bid;
  239. $bid = BookService::decodeBidStatic($bid);
  240. $book_info = BookConfigService::getBookById($bid);;
  241. if (empty($book_info)) response()->error('QAPP_SYS_ERROR');
  242. if ($book_info->is_on_shelf == 0 || $book_info->is_on_shelf == 3) {
  243. if (!$this->isBookOrdered($bid)) {
  244. response()->error('QAPP_OFF_SHELF');
  245. }
  246. }
  247. //获取章节
  248. $chapter = ChapterService::getChapterNameById($cid, $bid);
  249. if (!$chapter) {
  250. return response()->error('QAPP_SYS_ERROR');
  251. }
  252. if ($this->balancePay($book_info, $cid, $chapter->size, $chapter->name, $remind)) {
  253. ReadRecordService::addReadRecord([
  254. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  255. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  256. ]);
  257. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  258. } else {
  259. $fee = $this->getPrice($book_info, $chapter->size);
  260. $data = [
  261. 'book_id' => $oldbid,
  262. 'book_name' => $book_info->book_name,
  263. 'chapter_name' => $chapter->name,
  264. 'chapter_id' => $cid,
  265. 'pay_type' => $book_info->charge_type,
  266. 'fee' => $fee,
  267. 'user_balance' => $this->user_info['balance'],
  268. 'product_id' => $book_info->product_id,
  269. 'uid' => $this->uid,
  270. 'distribution_channel_id' => $this->distribution_channel_id,
  271. 'is_discount' => 0,
  272. 'discount_fee' => '',
  273. 'discount' => ''
  274. ];
  275. if ($book_info->charge_type == 'BOOK') {
  276. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  277. } elseif ($book_info->charge_type == 'CHAPTER') {
  278. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  279. } else {
  280. return response()->error('QAPP_SYS_ERROR');
  281. }
  282. }
  283. }
  284. /**
  285. * 余额支付
  286. * @param $book_info
  287. * @param $chapter_id
  288. * @param $chapter_size
  289. * @return bool
  290. */
  291. protected function balancePay($book_info, $chapter_id, $chapter_size, $chapter_name, $is_remind)
  292. {
  293. $fee = $this->getPrice($book_info, $chapter_size);
  294. if ((int)$this->user_info['balance'] >= $fee) {
  295. if ($this->bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)) {
  296. return true;
  297. }
  298. return false;
  299. } else {
  300. return false;
  301. }
  302. }
  303. /**
  304. * 获取章节内容
  305. * @param $bid
  306. * @param $cid
  307. * @return bool|mixed
  308. */
  309. protected function getChapter($bid, $cid, $chapter)
  310. {
  311. $chapter_content = ChapterService::getChapter($bid, $cid);
  312. if (!$chapter_content) return false;
  313. $chapter->content = trim(str_replace($chapter_content->name, '', $chapter_content->content));
  314. //统计点击率
  315. $key = 'book_click_num_bid_' . $bid;
  316. $field = date('Y-m-d');
  317. $old = Redis::hget($key, $field);
  318. if (!$old) $old = 0;
  319. Redis::hset($key, $field, $old + 1);
  320. $force_add_desk_type = $this->addDesktopType($bid, $chapter->sequence);
  321. $chapter->force_add_desk_type = $force_add_desk_type;
  322. //统计
  323. $this->stats();
  324. return $chapter;
  325. }
  326. /**
  327. * 添加订购记录
  328. * @param $book_info
  329. * @param $chapter_id
  330. * @param $fee
  331. * @return bool
  332. */
  333. protected function bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)
  334. {
  335. if ($book_info['charge_type'] == 'BOOK') {
  336. $data = [
  337. 'uid' => $this->uid,
  338. 'fee' => $fee,
  339. 'u' => $this->send_order_id,
  340. 'distribution_channel_id' => $this->distribution_channel_id,
  341. 'bid' => $book_info->bid,
  342. 'book_name' => $book_info->book_name,
  343. 'send_order_id' => $this->send_order_id,
  344. ];
  345. return BookOrderService::addOrderRecodeAndDecrUserBalance($data, $this->uid);
  346. } else {
  347. $data = [
  348. 'uid' => $this->uid,
  349. 'fee' => $fee,
  350. 'cid' => $chapter_id,
  351. 'bid' => $book_info->bid,
  352. 'distribution_channel_id' => $this->distribution_channel_id,
  353. 'book_name' => $book_info->book_name,
  354. 'chapter_name' => $chapter_name,
  355. 'send_order_id' => $this->send_order_id,
  356. 'is_remind' => $is_remind
  357. ];
  358. if ($is_remind) {
  359. $this->addOrderRemind($book_info->bid);
  360. }
  361. return ChapterOrderService::addOrderAndDecrUserBalance($data, $this->uid);
  362. }
  363. }
  364. protected function addOrderRemind($bid)
  365. {
  366. if (ChapterReminderService::checkIsNoReminder($this->uid, $bid)) {
  367. return true;
  368. } else {
  369. ChapterReminderService::add($this->uid, $bid);
  370. return true;
  371. }
  372. }
  373. /**
  374. * 是否订购提醒
  375. * @param $chapter_id
  376. * @return bool
  377. */
  378. protected function isOrderRemind($bid)
  379. {
  380. $is_no_reminder = ChapterReminderService::checkIsNoReminder($this->uid, $bid) ? 1 : 0;
  381. return $is_no_reminder == 0;
  382. }
  383. /**
  384. * 用户是否关注
  385. * @param $uid
  386. * @return bool
  387. */
  388. protected function getSubscribe()
  389. {
  390. $res = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $this->uid]);
  391. if ($res) return true;
  392. return false;
  393. }
  394. /**
  395. * 获取订购记录
  396. * @param $book_info
  397. * @param $chapter_id
  398. * @return bool
  399. */
  400. protected function getOrderRecord($bid, $chapter_id)
  401. {
  402. //包年记录
  403. $uid = $this->uid;
  404. $res = YearOrderService::getRecord($uid);
  405. if ($res) return true;
  406. $res = null;
  407. //单本订购记录
  408. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  409. if ($res) return true;
  410. $res = null;
  411. //章节订购记录
  412. $chapterOrder = new ChapterOrderService();
  413. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  414. return false;
  415. }
  416. /**
  417. * 计算价格
  418. * @param $book_info
  419. * @param $chapter_size
  420. * @return float
  421. */
  422. private function getPrice($book_info, $chapter_size = 0)
  423. {
  424. if ($book_info->charge_type == 'BOOK') {
  425. if (BookOrderService::isHasBookOrder($this->uid)) {
  426. $this->is_first_book_order = 0;
  427. return 1399;
  428. } else {
  429. $this->is_first_book_order = 1;
  430. return 899;
  431. }
  432. } else {
  433. $fee = BookService::getPrice($book_info, $this->distribution_channel_id, $chapter_size);
  434. return $fee;
  435. }
  436. }
  437. /**
  438. * 用户添加标签
  439. * @param $book_info
  440. */
  441. protected function addTag($book_info)
  442. {
  443. if (!UserDeepReadTagService::isAddTag($this->uid, $book_info->bid)) {
  444. try {
  445. UserDeepReadTagService::addTag([
  446. 'uid' => $this->uid,
  447. 'bid' => $book_info->bid,
  448. 'book_name' => $book_info->book_name,
  449. 'category_id' => $book_info->category_id,
  450. 'category_name' => $book_info->category_name,
  451. 'sex_preference' => $book_info->channel_name ? $book_info->channel_name : '',
  452. 'distribution_channel_id' => $this->distribution_channel_id ? $this->distribution_channel_id : '0',
  453. 'send_order_id' => $this->send_order_id,
  454. ]);
  455. } catch (\Exception $e) {
  456. }
  457. }
  458. }
  459. protected function isBookOrdered($bid)
  460. {
  461. $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $bid);
  462. if ($chapter_order) return true;
  463. $res = BookOrderService::getRecordByuidBid($this->uid, $bid);
  464. if ($res) return true;
  465. return false;
  466. }
  467. /**
  468. * 判断是否需要充值
  469. */
  470. private function isBookNeedCharge(int $bid, float $price)
  471. {
  472. $book_order = $this->getOrderRecord($bid, 0);
  473. if ($book_order) {
  474. return false;
  475. } else {
  476. $user_info = $this->user_info;
  477. return $user_info['balance'] < $price;
  478. }
  479. }
  480. /**
  481. * 判断章节是否需要充值
  482. */
  483. private function isChapterNeedCharge(int $bid, int $cid, float $price)
  484. {
  485. $book_order = $this->getOrderRecord($bid, $cid);
  486. if ($book_order) {
  487. return false;
  488. } else {
  489. $user_info = $this->user_info;
  490. return $user_info['balance'] < $price;
  491. }
  492. }
  493. private function stats()
  494. {
  495. //阅读器统计
  496. WapVisitStatService::recordReaderUvAndPv($this->uid, $this->distribution_channel_id);
  497. }
  498. //加桌类型
  499. private function addDesktopType($bid, $sequence)
  500. {
  501. $force_add_desk_type = 0;
  502. $send_order_id = ReadRecordService::getSendOrderId($this->uid);
  503. if (!$send_order_id) return $force_add_desk_type;
  504. $send_order_info = SendOrderService::getById($send_order_id);
  505. if (!$send_order_info) return $force_add_desk_type;
  506. if ($send_order_info->book_id == $bid) {
  507. if ($send_order_info->force_add_desk_type == 1 && $send_order_info->force_add_desk_seq) {
  508. if ($sequence >= $send_order_info->force_add_desk_seq) {
  509. $force_add_desk_type = $send_order_info->force_add_desk_type;
  510. }
  511. }
  512. if ($send_order_info->force_add_desk_type == 2) {
  513. if ($sequence >= $this->book_info->force_subscribe_chapter_seq && $sequence <= $this->book_info->force_subscribe_chapter_seq + 3) {
  514. $force_add_desk_type = $send_order_info->force_add_desk_type;
  515. }
  516. }
  517. }
  518. return $force_add_desk_type;
  519. }
  520. }