ChapterController.php 19 KB

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