BookController.php 13 KB

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