ChapterController.php 17 KB

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