ChapterController.php 19 KB

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