ChapterController.php 19 KB

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