ChapterController.php 22 KB

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