BookController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\Book;
  3. use App\Libs\Utils;
  4. use App\Modules\Book\Services\BookAuditService;
  5. use App\Modules\RecommendBook\Services\RecommendService;
  6. use App\Modules\Book\Services\RecoBannerService;
  7. use Illuminate\Http\Request;
  8. use App\Http\Controllers\QuickApp\BaseController;
  9. use App\Http\Controllers\QuickApp\Book\Transformers\BookTransformer;
  10. use App\Http\Controllers\QuickApp\Book\Transformers\KeywordTransformer;
  11. use App\Modules\Book\Models\BookConfig;
  12. use App\Modules\Book\Services\BookConfigService;
  13. use App\Modules\Book\Services\BookService;
  14. use App\Modules\Book\Services\BookUrgeUpdateService;
  15. use App\Modules\Book\Services\UserShelfBooksService;
  16. use App\Modules\Book\Services\ChapterService;
  17. use App\Modules\Subscribe\Services\BookOrderService;
  18. use App\Modules\Subscribe\Services\ChapterOrderService;
  19. use App\Modules\Subscribe\Services\YearOrderService;
  20. use App\Modules\User\Services\ReadRecordService;
  21. class BookController extends BaseController
  22. {
  23. public function index(Request $request, $bid)
  24. {
  25. $bid = BookService::decodeBidStatic($bid);
  26. $book_info = BookConfigService::getBookById($bid);
  27. if (!$book_info) {
  28. return response()->error('QAPP_SYS_ERROR');
  29. }
  30. if (!in_array($book_info->is_on_shelf, [2])) {
  31. return response()->error('QAPP_OFF_SHELF');
  32. }
  33. $is_on_shelf = UserShelfBooksService::getUserShelfBooksListByUidAndBid($this->uid, $bid);
  34. $book_info['is_on_user_shelf'] = 0;
  35. if ($is_on_shelf) {
  36. $book_info['is_on_user_shelf'] = 1;
  37. }
  38. $last_chapter = ChapterService::getChapterNameById($book_info['last_cid'], $bid);
  39. $book_info['last_chapter_is_vip'] = $last_chapter['is_vip'];
  40. $book_info['is_need_charge'] = $this->isNeedCharge($bid, $last_chapter, $book_info);
  41. $record = ReadRecordService::getBookReadRecordStatic($this->uid, $bid);
  42. if ($record) {
  43. $book_info['record_chapter_id'] = $record['record_chapter_id'];
  44. $book_info['record_chapter_name'] = $record['record_chapter_name'];
  45. }
  46. return response()->item(new BookTransformer(), $book_info);
  47. }
  48. /**
  49. * 获取订购记录
  50. * @param $book_info
  51. * @param $chapter_id
  52. * @return bool
  53. */
  54. protected function getOrderRecord($bid, $chapter_id)
  55. {
  56. //包年记录
  57. $uid = $this->uid;
  58. $res = YearOrderService::getRecord($uid);
  59. if ($res) return true;
  60. $res = null;
  61. //单本订购记录
  62. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  63. if ($res) return true;
  64. $res = null;
  65. //章节订购记录
  66. $chapterOrder = new ChapterOrderService();
  67. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  68. return false;
  69. }
  70. /**
  71. * 判断是否需要充值
  72. */
  73. private function isBookNeedCharge(int $bid, float $price)
  74. {
  75. $book_order = $this->getOrderRecord($bid, 0);
  76. if ($book_order) {
  77. return false;
  78. } else {
  79. $user_info = $this->user_info;
  80. return $user_info['balance'] < $price;
  81. }
  82. }
  83. /**
  84. * 判断章节是否需要充值
  85. */
  86. private function isChapterNeedCharge(int $bid, int $cid, float $price)
  87. {
  88. $book_order = $this->getOrderRecord($bid, $cid);
  89. if ($book_order) {
  90. return false;
  91. } else {
  92. $user_info = $this->user_info;
  93. return $user_info['balance'] < $price;
  94. }
  95. }
  96. /**
  97. * 判断是否需要充值
  98. */
  99. private function isNeedCharge(int $bid, $last_chapter, $book_info)
  100. {
  101. switch ($book_info->charge_type) {
  102. case 'BOOK':
  103. $price = $this->getPrice($book_info);
  104. return $this->isBookNeedCharge($bid, $price);
  105. default:
  106. $price = $last_chapter->is_vip ? $this->getPrice($book_info, $last_chapter->size) : 0;
  107. return $last_chapter->is_vip ? $this->isChapterNeedCharge($bid, $last_chapter->id, $price) : false;
  108. }
  109. }
  110. /**
  111. * 计算价格
  112. * @param $book_info
  113. * @param $chapter_size
  114. * @return float
  115. */
  116. protected function getPrice($book_info, $chapter_size = 0)
  117. {
  118. if ($book_info->charge_type == 'BOOK')
  119. return $book_info->price * 100;
  120. return ceil($chapter_size / 100);
  121. }
  122. /**
  123. * 首页
  124. */
  125. public function getBookLists(Request $request, $sex)
  126. {
  127. // 获取基本数据
  128. $package = $request->header('x-package', '');
  129. $brand = $request->header('x-nbrand', '');
  130. $codeVersion = $request->header('x-codeversion', '');
  131. // 根据包名、平台、版本号判断是否审核
  132. if (Utils::checkIsAudit($package, $brand, $codeVersion)) {
  133. $result = BookAuditService::getHomeBooksData($sex);
  134. return response()->success($result);
  135. }
  136. if ($sex == 'male') {
  137. $channel = 1;
  138. $reco_banner_type = ['MALE', 'PUBLIC'];
  139. } else {
  140. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  141. $channel = 2;
  142. }
  143. $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
  144. $books->transform(function ($item) {
  145. $result = $this->getBidCidFromUrl($item->redirect_url);
  146. $item->bid = $result['bid'];
  147. $item->cid = $result['cid'];
  148. if ($result['cid']) {
  149. $item->redirect_url = "views/Reader";
  150. } else {
  151. $item->redirect_url = "views/Detail";
  152. }
  153. return $item;
  154. });
  155. $hotBids = RecommendService::getRecommendIdsStatic($channel, 'hot');
  156. $liveBids = RecommendService::getRecommendIdsStatic($channel, 'live');
  157. $recomBids = RecommendService::getRecommendIdsStatic($channel, 'recom');
  158. $newBids = RecommendService::getRecommendIdsStatic($channel, 'new_recom');
  159. $result = [
  160. ['type' => 'reco_banner', 'lable' => '首页banner', 'books' => $books],
  161. ['type' => 'hot', 'lable' => '热门推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($hotBids))],
  162. ['type' => 'zhibo', 'lable' => '神书直播', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($liveBids))],
  163. ['type' => 'recom', 'lable' => '编辑推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($recomBids))],
  164. ['type' => 'new_recom', 'lable' => '新书推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($newBids))],
  165. ];
  166. return response()->success($result);
  167. }
  168. private function getBidCidFromUrl(string $url)
  169. {
  170. if (preg_match('/\?bid=(\w+)\S+cid=(\w+)/', $url, $matches) || preg_match('/\?id=(\w+)/', $url, $matches)) {
  171. return [
  172. 'bid' => $matches[1],
  173. 'cid' => isset($matches[2]) ? $matches[2] : 0,
  174. ];
  175. } else {
  176. return [
  177. 'bid' => '',
  178. 'cid' => 0,
  179. ];
  180. }
  181. }
  182. public function library(Request $request)
  183. {
  184. $where = [];
  185. $order = [];
  186. $where['is_on_shelf'] = [2];
  187. $category_id = $request->input('category_id');
  188. if ($category_id) {
  189. if ($category_id == 1) {
  190. $where['channel_name'] = '男频';
  191. } elseif ($category_id == 2) {
  192. $where['channel_name'] = '女频';
  193. } else {
  194. $where['category_id'] = $category_id;
  195. }
  196. }
  197. $key = $request->input('key');
  198. $uid = $request->input('uid', 0);
  199. if ($key && $uid && is_numeric($uid)) {
  200. BookConfigService::saveUserSearchLog($key, $uid);
  201. }
  202. $where['key'] = $key;
  203. $order_field = $request->input('order_field');
  204. $order_seq = $request->input('order_seq');
  205. if ($order_field != '' && in_array($order_field, ['recommend_index', 'click_count', 'update', 'size', 'create'])) {
  206. if ($order_field == 'update') {
  207. $order = ['book_configs.updated_at', 'desc'];
  208. } elseif ($order_field == 'create') {
  209. $order = ['book_configs.created_at', 'desc'];
  210. } else {
  211. $order = [$order_field, 'desc'];
  212. }
  213. if ($order_seq == 'asc') {
  214. $order = [$order_field, 'asc'];
  215. }
  216. if ($order_seq == 'desc') {
  217. $order = [$order_field, 'desc'];
  218. }
  219. }
  220. // 审核状态默认值
  221. $package = $request->header('x-package', '');
  222. $brand = $request->header('x-nbrand', '');
  223. $codeVersion = $request->header('x-codeversion', '');
  224. if ($order_field === 'recommend_index' && Utils::checkIsAudit($package, $brand, $codeVersion)) {
  225. $order = ['book_configs.bid', 'desc'];
  226. }
  227. $status = $request->input('status');
  228. if ($status != '') {
  229. $where['status'] = $status;
  230. }
  231. $page_size = $request->input('page_size', 15);
  232. $books = BookConfigService::getBooks($where, $order, $page_size);
  233. return response()->pagination(new BookTransformer, $books);
  234. }
  235. public function hotWords(Request $request)
  236. {
  237. $result = BookConfigService::findBookKeywords();
  238. return response()->pagination(new KeywordTransformer, $result);
  239. }
  240. public function similarRecom(Request $request)
  241. {
  242. $category_id = $request->input('category_id');
  243. $bid = $request->input('bid');
  244. if (empty($bid) || empty($category_id)) {
  245. return response()->error('PARAM_ERROR');
  246. }
  247. $bid = BookService::decodeBidStatic($bid);
  248. $where = ['category_id' => $category_id, 'is_on_shelf' => [2]];
  249. $books = BookConfigService::getBooks($where, [], 4);
  250. $data = [];
  251. foreach ($books as $v) {
  252. if ($v->bid != $bid && count($data) < 3) {
  253. $data[] = $v;
  254. }
  255. }
  256. return response()->collection(new BookTransformer(), $data);
  257. }
  258. public function readOverRecommend(Request $request)
  259. {
  260. $bid = $request->input('bid');
  261. if (empty($bid)) {
  262. return response()->error('PARAM_ERROR');
  263. }
  264. $bid = BookService::decodeBidStatic($bid);
  265. $book_info = BookConfigService::getBookById($bid);
  266. $res = BookConfigService::getRecommendBooks($bid, $book_info->channel_name);
  267. $urge_status = 0;
  268. if ($book_info->status == 0 && !BookUrgeUpdateService::isHadUrged($this->uid, $bid)) {
  269. $urge_status = 1;
  270. }
  271. $recommend_result = collectionTransform(new BookTransformer(), $res);
  272. $book_status = [
  273. 'status' => $book_info->status,
  274. 'urge_status' => $urge_status
  275. ];
  276. $data = [
  277. 'recommend_result' => $recommend_result,
  278. 'book_status' => $book_status
  279. ];
  280. return response()->success($data);
  281. }
  282. public function rank(Request $request)
  283. {
  284. $sex = $request->input('sex');
  285. if ($sex == 1) {
  286. $books = collectionTransform(new BookTransformer, BookConfigService::getBooksByIds([11601, 11529, 3365, 10377, 11457, 8102, 6464, 7287, 2563, 10419]));
  287. } elseif ($sex == 2) {
  288. $books = collectionTransform(new BookTransformer, BookConfigService::getBooksByIds([10823, 10479, 10467, 10139, 9990, 9973, 9479, 9423, 1148, 8693, 8497, 8148, 8129, 7857, 7854, 7629, 7362, 5748, 5362, 4811, 4470, 4135, 3759, 3696, 3418, 3401, 3369, 2698, 1634, 1479]));
  289. } else {
  290. return response()->error('PARAM_ERROR');
  291. }
  292. return response()->success($books);
  293. }
  294. /**
  295. * 推荐书
  296. */
  297. public function recommen()
  298. {
  299. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  300. $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
  301. $books->transform(function ($item) {
  302. $result = $this->getBidCidFromUrl($item->redirect_url);
  303. $item->bid = $result['bid'];
  304. $item->cid = $result['cid'];
  305. if ($result['cid']) {
  306. $item->redirect_url = "views/Reader";
  307. } else {
  308. $item->redirect_url = "views/Detail";
  309. }
  310. return $item;
  311. });
  312. return response()->success($books);
  313. }
  314. /**
  315. * 限免
  316. */
  317. public function free(int $sex)
  318. {
  319. $result = BookConfigService::findFreeBooks($sex);
  320. return response()->success($result);
  321. }
  322. }