BookController.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\Book;
  3. use App\Consts\BaseConst;
  4. use App\Libs\Utils;
  5. use App\Modules\Book\Services\BookAuditService;
  6. use App\Modules\Channel\Services\ChannelRecommendBookConfigService;
  7. use App\Modules\Channel\Services\ChannelRecommendBooksService;
  8. use App\Modules\RecommendBook\Services\QappRecommendService;
  9. use App\Modules\RecommendBook\Services\RecommendService;
  10. use App\Modules\Book\Services\RecoBannerService;
  11. use App\Modules\Trade\Services\OrderService;
  12. use App\Modules\User\Models\QappPackage;
  13. use App\Modules\User\Services\QappUserService;
  14. use Illuminate\Http\Request;
  15. use App\Http\Controllers\QuickApp\BaseController;
  16. use App\Http\Controllers\QuickApp\Book\Transformers\BookTransformer;
  17. use App\Http\Controllers\QuickApp\Book\Transformers\KeywordTransformer;
  18. use App\Modules\Book\Models\BookConfig;
  19. use App\Modules\Book\Services\BookConfigService;
  20. use App\Modules\Book\Services\BookService;
  21. use App\Modules\Book\Services\BookUrgeUpdateService;
  22. use App\Modules\Book\Services\UserShelfBooksService;
  23. use App\Modules\Book\Services\ChapterService;
  24. use App\Modules\Subscribe\Services\BookOrderService;
  25. use App\Modules\Subscribe\Services\ChapterOrderService;
  26. use App\Modules\Subscribe\Services\YearOrderService;
  27. use App\Modules\User\Services\ReadRecordService;
  28. use Hashids;
  29. use Illuminate\Support\Facades\Redis;
  30. class BookController extends BaseController
  31. {
  32. public function index(Request $request, $bid)
  33. {
  34. $bid = BookService::decodeBidStatic($bid);
  35. $book_info = BookConfigService::getBookById($bid);
  36. if (!$book_info) {
  37. return response()->error('QAPP_SYS_ERROR');
  38. }
  39. //yuyuedu、xinghe 快应用这两个cp的书屏蔽下
  40. if(in_array($book_info->cp_source,getHiddenCp())){
  41. return response()->error('QAPP_SYS_ERROR');
  42. }
  43. if($this->distribution_channel_id == 7477 && $bid == 13765){
  44. $book_info->is_on_shelf = 2;
  45. }
  46. if($bid == 58886){
  47. $book_info->is_on_shelf = 0;
  48. }
  49. if (!in_array($book_info->is_on_shelf, [1,2])) {
  50. return response()->error('QAPP_OFF_SHELF');
  51. }
  52. $is_on_shelf = UserShelfBooksService::getUserShelfBooksListByUidAndBid($this->uid, $bid);
  53. $book_info['is_on_user_shelf'] = 0;
  54. if ($is_on_shelf) {
  55. $book_info['is_on_user_shelf'] = 1;
  56. }
  57. $last_chapter = ChapterService::getChapterNameById($book_info['last_cid'], $bid);
  58. $book_info->last_chapter = $last_chapter['name'];
  59. list($is_split,$is_change_chapter_name) = BookService::splitContent($bid);
  60. if($is_split && ($book_info->channel_name == '男频' || $is_change_chapter_name) ){
  61. $book_info->last_chapter = '第'.$book_info->chapter_count.'章';
  62. }
  63. $book_info['last_chapter_is_vip'] = $last_chapter['is_vip'];
  64. $book_info['is_need_charge'] = $this->isNeedCharge($bid, $last_chapter, $book_info);
  65. $record = ReadRecordService::getBookReadRecordStatic($this->uid, $bid);
  66. if ($record) {
  67. $book_info['record_chapter_id'] = $record['record_chapter_id'];
  68. $book_info['record_chapter_name'] = $record['record_chapter_name'];
  69. }
  70. return response()->item(new BookTransformer(), $book_info);
  71. }
  72. /**
  73. * 获取订购记录
  74. * @param $book_info
  75. * @param $chapter_id
  76. * @return bool
  77. */
  78. protected function getOrderRecord($bid, $chapter_id)
  79. {
  80. //包年记录
  81. $uid = $this->uid;
  82. $res = YearOrderService::getRecord($uid);
  83. if ($res) return true;
  84. $res = null;
  85. //单本订购记录
  86. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  87. if ($res) return true;
  88. $res = null;
  89. //章节订购记录
  90. $chapterOrder = new ChapterOrderService();
  91. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  92. return false;
  93. }
  94. /**
  95. * 判断是否需要充值
  96. */
  97. private function isBookNeedCharge(int $bid, float $price)
  98. {
  99. $book_order = $this->getOrderRecord($bid, 0);
  100. if ($book_order) {
  101. return false;
  102. } else {
  103. $user_info = $this->user_info;
  104. return $user_info['balance'] < $price;
  105. }
  106. }
  107. /**
  108. * 判断章节是否需要充值
  109. */
  110. private function isChapterNeedCharge(int $bid, int $cid, float $price)
  111. {
  112. $book_order = $this->getOrderRecord($bid, $cid);
  113. if ($book_order) {
  114. return false;
  115. } else {
  116. $user_info = $this->user_info;
  117. return $user_info['balance'] < $price;
  118. }
  119. }
  120. /**
  121. * 判断是否需要充值
  122. */
  123. private function isNeedCharge(int $bid, $last_chapter, $book_info)
  124. {
  125. $is_free = BookConfigService::judgeBookIsFree($bid);
  126. if ($is_free) {
  127. return false;
  128. }
  129. switch ($book_info->charge_type) {
  130. case 'BOOK':
  131. $price = $this->getPrice($book_info);
  132. return $this->isBookNeedCharge($bid, $price);
  133. default:
  134. $price = isset($last_chapter->is_vip) ? $this->getPrice($book_info, $last_chapter->size) : 0;
  135. return isset($last_chapter->is_vip) ? $this->isChapterNeedCharge($bid, $last_chapter->id, $price) : false;
  136. }
  137. }
  138. /**
  139. * 计算价格
  140. * @param $book_info
  141. * @param $chapter_size
  142. * @return float
  143. */
  144. protected function getPrice($book_info, $chapter_size = 0)
  145. {
  146. if ($book_info->charge_type == 'BOOK')
  147. return $book_info->price * 100;
  148. return ceil($chapter_size / 100);
  149. }
  150. /**
  151. * 首页
  152. */
  153. public function getBookLists(Request $request, $sex)
  154. {
  155. // 获取基本数据
  156. $package = $request->header('x-package', '');
  157. $brand = $request->header('x-nbrand', '');
  158. $codeVersion = $request->header('x-codeversion', '');
  159. $isAuth = check_qapp_auth($package ,0);
  160. // 根据包名、平台、版本号判断是否审核
  161. if (Utils::checkIsAudit($package, $brand, $codeVersion) || $isAuth == false) {
  162. $result = BookAuditService::getHomeBooksData($sex, $package,$isAuth);
  163. return response()->success($result);
  164. }
  165. $user = (new QappUserService)->getGolableUser();
  166. if($package == "com.beidao.kuaiying.yueai" && $sex == "male" && isset($user->uid) && !empty($user->uid) && $user->send_order_id > 0){
  167. $orderRecord = ChapterOrderService::hasUserRecord($user->uid);
  168. if($orderRecord){
  169. $result = BookAuditService::getYueaiHomeBooksData($sex, $package,$isAuth,1);
  170. return response()->success($result);
  171. }
  172. }
  173. if ($sex == 'male') {
  174. $channel = 1;
  175. $reco_banner_type = ['MALE', 'PUBLIC'];
  176. } else {
  177. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  178. $channel = 2;
  179. }
  180. if($isAuth){
  181. $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
  182. $books->transform(function ($item) {
  183. $result = $this->getBidCidFromUrl($item->redirect_url);
  184. $item->bid = $result['bid'];
  185. $item->cid = $result['cid'];
  186. if ($result['cid']) {
  187. $item->redirect_url = "views/Reader";
  188. } else {
  189. $item->redirect_url = "views/Detail";
  190. }
  191. return $item;
  192. });
  193. }else{
  194. $home = config('home.ycsd');
  195. $banner = $home['reco_banner'];
  196. $books = $banner[$sex];
  197. }
  198. //新判断: 根据包名来获取对应所需的bid
  199. $qapp_package = QappPackage::getPackageByPackage($package);
  200. if($qapp_package){
  201. $package_id = $qapp_package->id;
  202. }else{
  203. $package_id = 0;
  204. }
  205. \Log::info('un_send_order_book:package_id1:'.$package_id.' $package:'.$package);
  206. if(isset($user->uid) && !empty($user->uid)){
  207. if(!$this->send_order_id || $this->send_order_id == 0 ){
  208. \Log::info('un_send_order_book:uid:'.$this->uid.' $package:'.$package);
  209. $result = $this->getCheckBids($channel,$books,$package_id,$package);
  210. if(isset($result[1]['books']) && count((array)$result[1]['books']) > 1){
  211. return response()->success($result);
  212. }
  213. $result = $this->getCheckBids($channel,$books,0,$package);
  214. return response()->success($result);
  215. }else{
  216. $package_id = 0;
  217. }
  218. \Log::info('un_send_order_book:package_id2:'.$package_id.' $package:'.$package);
  219. }else{
  220. $package_id = 0;
  221. }
  222. \Log::info('un_send_order_book:package_id3:'.$package_id.' $package:'.$package);
  223. $result = $this->getCheckBids($channel,$books,$package_id,$package);
  224. return response()->success($result);
  225. }
  226. /**
  227. * 根据包名
  228. * @param $channel
  229. * @param $books
  230. * @param $package_id
  231. * @return array
  232. */
  233. private function getCheckBids($channel,$books,$package_id,$package)
  234. {
  235. $isAuthor = check_qapp_auth($package_id,1);
  236. $hotBids = $this->getCheckBooks(QappRecommendService::getRecommendByPacketId($channel, 'hot',$package_id),$channel,$package,$isAuthor);
  237. $liveBids = $this->getCheckBooks(QappRecommendService::getRecommendByPacketId($channel, 'live',$package_id),$channel,$package,$isAuthor);
  238. $recomBids = $this->getCheckBooks(QappRecommendService::getRecommendByPacketId($channel, 'recom',$package_id),$channel,$package,$isAuthor);
  239. $newBids = $this->getCheckBooks(QappRecommendService::getRecommendByPacketId($channel, 'new_recom',$package_id),$channel,$package,$isAuthor);
  240. return array_filter([
  241. ['type' => 'reco_banner', 'lable' => '首页banner', 'books' => $books],
  242. ['type' => 'hot', 'lable' => '热门书单', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($hotBids))],
  243. ['type' => 'zhibo', 'lable' => '神书直播', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($liveBids))],
  244. ['type' => 'recom', 'lable' => '小编精选', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($recomBids))],
  245. ['type' => 'new_recom', 'lable' => '人气新书', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($newBids))]
  246. ]);
  247. }
  248. /**
  249. * 检测并补充不满足条件的书籍id
  250. * @param $bid_list
  251. * @param $channel : 频道
  252. * @param $package : 频道
  253. * @return array
  254. */
  255. private function getCheckBooks($bid_list,$channel,$package,$is_author)
  256. {
  257. $hidden_cp = getHiddenCp();
  258. if($package !== 'com.beidao.kuaiying.zsy'){
  259. $hidden_cp = array_merge($hidden_cp,['lianshang']);
  260. }
  261. //获取书本数量
  262. $count = count($bid_list);
  263. if (!$is_author){
  264. $where = [
  265. ['book_configs.charge_type','!=','BOOK'],
  266. ['book_configs.cp_source','=','ycsd'],
  267. ];
  268. }else{
  269. $where = [
  270. ['book_configs.charge_type','!=','BOOK'],
  271. ];
  272. }
  273. //获取当前有效书本数量
  274. $book_count = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  275. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  276. ->whereIn('book_configs.bid',$bid_list)
  277. ->where('book_configs.is_on_shelf',2)
  278. ->where('book_configs.charge_type','!=','BOOK')
  279. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  280. ->where($where)
  281. ->where('book_categories.pid',$channel)
  282. ->count();
  283. if($count == $book_count){
  284. return $bid_list;
  285. }
  286. //获取需要补充的书籍数量
  287. $supplement_count = (($count - $book_count) > 0) ? $count - $book_count : 0;
  288. if($supplement_count <= 0){
  289. return $bid_list;
  290. }
  291. //获取书籍交集bid,过滤掉不符合要求的书
  292. $bids = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  293. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  294. ->whereIn('book_configs.bid',$bid_list)
  295. ->where('book_configs.is_on_shelf',2)
  296. ->where($where)
  297. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  298. ->where('book_categories.pid',$channel)
  299. ->pluck('book_configs.bid')->all();
  300. $bid_list = array_intersect($bid_list,$bids);
  301. //获取随机的有效的书籍bid
  302. $rand_bid = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  303. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  304. ->where('book_configs.is_on_shelf',2)
  305. // ->where('book_configs.charge_type','!=','BOOK')
  306. ->where($where)
  307. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  308. ->where('book_categories.pid',$channel)
  309. ->inRandomOrder()
  310. ->limit($supplement_count)
  311. ->get()->pluck('bid')->toArray();
  312. return array_filter(array_merge($bid_list,$rand_bid));
  313. }
  314. private function getBidCidFromUrl(string $url)
  315. {
  316. if (preg_match('/\?bid=(\w+)\S+cid=(\w+)/', $url, $matches) || preg_match('/\?id=(\w+)/', $url, $matches)) {
  317. return [
  318. 'bid' => $matches[1],
  319. 'cid' => isset($matches[2]) ? $matches[2] : 0,
  320. ];
  321. } else {
  322. return [
  323. 'bid' => '',
  324. 'cid' => 0,
  325. ];
  326. }
  327. }
  328. public function library(Request $request)
  329. {
  330. $where = [];
  331. $order = [];
  332. $where['is_on_shelf'] = [2];
  333. $category_id = $request->input('category_id');
  334. if ($category_id) {
  335. if ($category_id == 1) {
  336. $where['channel_name'] = '男频';
  337. } elseif ($category_id == 2) {
  338. $where['channel_name'] = '女频';
  339. } else {
  340. $where['category_id'] = $category_id;
  341. }
  342. }
  343. $key = $request->input('key');
  344. $uid = $request->input('uid', 0);
  345. if ($key && $uid && is_numeric($uid)) {
  346. BookConfigService::saveUserSearchLog($key, $uid);
  347. }
  348. $where['key'] = $key;
  349. $order_field = $request->input('order_field');
  350. $order_seq = $request->input('order_seq');
  351. if ($order_field != '' && in_array($order_field, ['recommend_index', 'click_count', 'update', 'size', 'create'])) {
  352. if ($order_field == 'update') {
  353. $order = ['book_configs.updated_at', 'desc'];
  354. } elseif ($order_field == 'create') {
  355. $order = ['book_configs.created_at', 'desc'];
  356. } else {
  357. $order = [$order_field, 'desc'];
  358. }
  359. if ($order_seq == 'asc') {
  360. $order = [$order_field, 'asc'];
  361. }
  362. if ($order_seq == 'desc') {
  363. $order = [$order_field, 'desc'];
  364. }
  365. }
  366. // 审核状态默认值
  367. $package = $request->header('x-package', '');
  368. $brand = $request->header('x-nbrand', '');
  369. $codeVersion = $request->header('x-codeversion', '');
  370. if ($order_field === 'recommend_index' && Utils::checkIsAudit($package, $brand, $codeVersion)) {
  371. $order = ['book_configs.bid', 'desc'];
  372. }
  373. // 是否只使用原创书殿的书
  374. $isAuth = check_qapp_auth($package ,0);
  375. if (!$isAuth){
  376. $where['cp_source'] = "ycsd";
  377. }
  378. $status = $request->input('status');
  379. if ($status != '') {
  380. $where['status'] = $status;
  381. }
  382. // 搜索关键词的情况下,屏蔽书籍完本状态
  383. if ($key && isset($where['status'])) {
  384. unset($where['status']);
  385. }
  386. $page_size = $request->input('page_size', 15);
  387. $where['channel_id'] = ($package === 'com.beidao.kuaiying.zsy') ? 7477 : 0;
  388. $books = BookConfigService::getBooks($where, $order, $page_size);
  389. return response()->pagination(new BookTransformer, $books);
  390. }
  391. public function hotWords(Request $request)
  392. {
  393. $result = BookConfigService::findBookKeywords();
  394. return response()->pagination(new KeywordTransformer, $result);
  395. }
  396. public function similarRecom(Request $request)
  397. {
  398. $package = $request->header('x-package', '');
  399. $category_id = $request->input('category_id');
  400. $bid = $request->input('bid');
  401. $package = $request->header('x-package', '');
  402. if (empty($bid) || (empty($category_id) && $category_id != 0)) {
  403. return response()->error('PARAM_ERROR');
  404. }
  405. $isAuth = check_qapp_auth($package ,0);
  406. $bid = BookService::decodeBidStatic($bid);
  407. $where = ['category_id' => $category_id, 'is_on_shelf' => [2]];
  408. if (!$isAuth){
  409. $where['cp_source'] = "ycsd";
  410. }
  411. $where['channel_id'] = ($package === 'com.beidao.kuaiying.zsy') ? 7477 : 0;
  412. $books = BookConfigService::getBooks($where, [], 4);
  413. $data = [];
  414. foreach ($books as $v) {
  415. if ($v->bid != $bid && count($data) < 3) {
  416. $data[] = $v;
  417. }
  418. }
  419. return response()->collection(new BookTransformer(), $data);
  420. }
  421. public function readOverRecommend(Request $request)
  422. {
  423. $bid = $request->input('bid');
  424. if (empty($bid)) {
  425. return response()->error('PARAM_ERROR');
  426. }
  427. $bid = BookService::decodeBidStatic($bid);
  428. $book_info = BookConfigService::getBookById($bid);
  429. $res = BookConfigService::getRecommendBooks($bid, $book_info->channel_name);
  430. $urge_status = 0;
  431. if ($book_info->status == 0 && !BookUrgeUpdateService::isHadUrged($this->uid, $bid)) {
  432. $urge_status = 1;
  433. }
  434. $recommend_result = collectionTransform(new BookTransformer(), $res);
  435. $book_status = [
  436. 'status' => $book_info->status,
  437. 'urge_status' => $urge_status
  438. ];
  439. $data = [
  440. 'recommend_result' => $recommend_result,
  441. 'book_status' => $book_status
  442. ];
  443. return response()->success($data);
  444. }
  445. public function rank(Request $request)
  446. {
  447. // 1:男频,2:女频
  448. $sex = (int)$request->input('sex');
  449. if (!in_array($sex, [1, 2], true)) {
  450. return response()->error('PARAM_ERROR');
  451. }
  452. // 默认
  453. $bids = [11529, 11941, 12720, 11990, 11988, 11976, 11977, 4183, 12717, 11833,
  454. 7287,14297,12716,14312,14000,13577,16712,13002,12717,15103,13928,14793,
  455. 12708,13286];
  456. if ($sex === 2) {
  457. $bids = [8469, 11660, 9117, 7891, 12281, 12470, 8167, 11661, 11670, 8476, 8557, 11662,
  458. 11680, 11926, 12462, 7836, 11681, 11664, 11928, 8631];
  459. }
  460. /**
  461. * pid:1为男频 2为女频
  462. SELECT
  463. CONCAT( books.id, ',' )
  464. FROM
  465. books
  466. LEFT JOIN book_configs ON books.id = book_configs.bid
  467. WHERE
  468. book_configs.is_on_shelf = 2
  469. AND books.category_id IN ( SELECT id FROM book_categories WHERE pid = 2 )
  470. ORDER BY
  471. book_configs.recommend_index DESC
  472. LIMIT 10;
  473. */
  474. // 根据包名、平台、版本号判断是否审核
  475. $package = $request->header('x-package', '');
  476. $brand = $request->header('x-nbrand', '');
  477. $codeVersion = $request->header('x-codeversion', '');
  478. if (Utils::checkIsAudit($package, $brand, $codeVersion)) {
  479. $bids = [2266, 3838, 9700, 10175, 10301, 3422, 1166, 4546, 9163, 2509,
  480. 7287,14297,12716,14312,14000,13577,16712,13002,12717,15103,13928,
  481. 14793,12708,13286,13336,13275,13073,15121,13929,12693,13254,3526,
  482. 10313,3483,13278,14004,4098,10378,14072,21376,21139,21757,19449];
  483. if ($sex === 2) {
  484. $bids = [159, 2439, 6276, 10074, 5409, 9379, 10323, 9078, 3603, 487];
  485. }
  486. }
  487. $isAuth = check_qapp_auth($package,0);
  488. if (!$isAuth){
  489. $rank = config('home.rank');
  490. $bids = $rank['male'];
  491. if ($sex === 2) {
  492. $bids = $rank['female'];;
  493. }
  494. }
  495. $channel_id = ($package === 'com.beidao.kuaiying.zsy') ? 7477 : 0;
  496. $books = collectionTransform(new BookTransformer, BookConfigService::getBookLists(compact('bids','channel_id')));
  497. return response()->success($books);
  498. }
  499. /**
  500. * 推荐书
  501. */
  502. public function recommen()
  503. {
  504. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  505. $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
  506. $books->transform(function ($item) {
  507. $result = $this->getBidCidFromUrl($item->redirect_url);
  508. $item->bid = $result['bid'];
  509. $item->cid = $result['cid'];
  510. if ($result['cid']) {
  511. $item->redirect_url = "views/Reader";
  512. } else {
  513. $item->redirect_url = "views/Detail";
  514. }
  515. return $item;
  516. });
  517. return response()->success($books);
  518. }
  519. /**
  520. * 阅爱小说任务轮播图
  521. */
  522. public function recommenYueAi()
  523. {
  524. $user = (new QappUserService)->getGolableUser();
  525. if(isset($user->uid) && !empty($user->uid) && $user->send_order_id > 0){
  526. $orderRecord = ChapterOrderService::hasUserRecord($user->uid);
  527. if($orderRecord){
  528. $data = config('home.yueai');
  529. $books = $data['task_banner'];
  530. foreach ($books as &$value){
  531. $value['bid'] =Hashids::encode($value['bid']);
  532. $value['redirect_url '] = empty($value['cid']) ? "views/Detail" : "views/Reader";
  533. }
  534. unset($value);
  535. return response()->success($books);
  536. }
  537. }
  538. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  539. $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
  540. $books->transform(function ($item) {
  541. $result = $this->getBidCidFromUrl($item->redirect_url);
  542. $item->bid = $result['bid'];
  543. $item->cid = $result['cid'];
  544. if ($result['cid']) {
  545. $item->redirect_url = "views/Reader";
  546. } else {
  547. $item->redirect_url = "views/Detail";
  548. }
  549. return $item;
  550. });
  551. return response()->success($books);
  552. }
  553. /**
  554. * 限免
  555. */
  556. public function free(int $sex)
  557. {
  558. $result = BookConfigService::findFreeBooks($sex);
  559. return response()->success($result);
  560. }
  561. public function yueaiBackRecom(Request $request)
  562. {
  563. $package = $request->header('x-package', '');
  564. if (empty($package) || $package != 'com.beidao.kuaiying.yueai') {
  565. return response()->success([]);
  566. }
  567. $user = (new QappUserService)->getGolableUser();
  568. if(isset($user->uid) && !empty($user->uid) && $user->send_order_id > 0){
  569. $orderRecord = ChapterOrderService::hasUserRecord($user->uid);
  570. if($orderRecord){
  571. $bid = [58238,60534,63220,14500,13254,63221,63548,14022,59334,58888,63417,61701];
  572. $bid = array_random($bid,4);
  573. $books = BookConfigService::getBooksByIds($bid,[],false);
  574. return response()->collection(new BookTransformer(), $books);
  575. }
  576. }
  577. return response()->success([]);
  578. $where = ['is_on_shelf' => [2]];
  579. $where['channel_id'] = ($package === 'com.beidao.kuaiying.zsy') ? 7477 : 0;
  580. $books = BookConfigService::getBooks($where, [], 4);
  581. return response()->collection(new BookTransformer(), $books);
  582. }
  583. public function shelfRecom(Request $request)
  584. {
  585. $category_id = $request->input('category_id');
  586. $bid = $request->input('bid');
  587. $package = $request->header('x-package', '');
  588. if (empty($package) || $package != 'com.beidao.kuaiying.yueai') {
  589. return response()->success([]);
  590. }
  591. $user = (new QappUserService)->getGolableUser();
  592. if(isset($user->uid) && !empty($user->uid) && $user->send_order_id > 0){
  593. $orderRecord = ChapterOrderService::hasUserRecord($user->uid);
  594. if($orderRecord){
  595. $bid = [58238,60534,63220,14500,13254,63221,63548,14022,59334,58888,63417,61701];
  596. $bid = array_random($bid,4);
  597. $books = BookConfigService::getBooksByIds($bid,[],false);
  598. return response()->collection(new BookTransformer(), $books);
  599. }
  600. }
  601. return response()->success([]);
  602. $where = ['is_on_shelf' => [2]];
  603. $where['channel_id'] = ($package === 'com.beidao.kuaiying.zsy') ? 7477 : 0;
  604. $books = BookConfigService::getBooks($where, [], 4);
  605. return response()->collection(new BookTransformer(), $books);
  606. }
  607. /**
  608. * 新推荐书单
  609. * @param Request $request
  610. * @return mixed
  611. */
  612. public function recommend_books(Request $request)
  613. {
  614. $package = $request->header('x-package','');
  615. $id = $request->get('id',0);
  616. if(empty($package)){
  617. return response()->success([]);
  618. }
  619. //判断包是否存在
  620. $package_info = QappPackage::getPackageByPackage($package);
  621. if(empty($package_info) || !isset($package_info->channel_id)){
  622. return response()->success([]);
  623. }
  624. //包对应有没有配置开启推荐书单
  625. $config = ChannelRecommendBookConfigService::getRecommendConfigs($package_info->channel_id);
  626. if(empty($config) || !isset($config->status) || $config->status == 0){
  627. return response()->success([]);
  628. }
  629. //根据频率和用户属性决定是否需要返回
  630. $res = $this->checkUsersAuth($config);
  631. if(!$res){
  632. return response()->success([]);
  633. }
  634. $list = ChannelRecommendBooksService::getRecommendBooks($package_info->channel_id,$id);
  635. \Log::info($list);
  636. foreach($list as $item){
  637. \Log::info($item);
  638. $this->incrRecommendNum($package_info->channel_id,$item['bid']);
  639. }
  640. return response()->success($list);
  641. }
  642. /**
  643. * 判断用户是否需要推荐
  644. * @param $config
  645. * @return bool
  646. */
  647. protected function checkUsersAuth($config)
  648. {
  649. $res = false;
  650. switch($config->person)
  651. {
  652. case 'pay':
  653. //付费用户
  654. $orders = OrderService::getUserLastestOrder($this->uid);
  655. if($orders){
  656. $res = true;
  657. }
  658. break;
  659. case 'send_order':
  660. //派单用户
  661. if($this->send_order_id != 0){
  662. $res = true;
  663. }
  664. break;
  665. case 'pay_send_order':
  666. //付费派单用户
  667. $orders = OrderService::getUserLastestOrder($this->uid);
  668. if($orders && $this->send_order_id != 0){
  669. $res = true;
  670. }
  671. break;
  672. case 'all':
  673. $res = true;
  674. break;
  675. }
  676. if($res === false){
  677. return false;
  678. }
  679. //频率判断
  680. if($config->frequency == 'back'){
  681. //返回即推送
  682. return true;
  683. }
  684. $day = strtotime(date('Y-m-d H:i:s'));
  685. $nextDay = strtotime( date('Y-m-d'). ' +1 day');
  686. $nextWeek = strtotime(date('Y-m-d',strtotime('+1 week last monday')));
  687. if($config->frequency == 'day'){
  688. //每日推送
  689. if(!Redis::exists('recommend:'.$config->channel_id.':'.$this->uid)){
  690. Redis::setex('recommend:'.$config->channel_id.':'.$this->uid,($nextDay-$day),1);
  691. return true;
  692. }
  693. return false;
  694. }
  695. if($config->frequency == 'week'){
  696. //每周推送
  697. if(!Redis::exists('recommend:'.$config->channel_id.':'.$this->uid)){
  698. Redis::setex('recommend:'.$config->channel_id.':'.$this->uid,($nextWeek-$day),1);
  699. return true;
  700. }
  701. return false;
  702. }
  703. }
  704. /**
  705. * 推广书籍推荐次数
  706. * @param $channel_id
  707. * @param $bid
  708. */
  709. protected function incrRecommendNum($channel_id,$bid)
  710. {
  711. $date = date('Ymd');
  712. $cacheKey = 'recommend:sum:'.$date.':'.$channel_id.$bid;
  713. if(!Redis::exists($cacheKey)){
  714. Redis::set($cacheKey,1);
  715. }else{
  716. Redis::incrBy($cacheKey, 1);
  717. }
  718. }
  719. }