ChapterController.php 18 KB

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