ChapterController.php 18 KB

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