ChapterController.php 20 KB

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