BookController.php 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\Book;
  3. use App\Consts\BaseConst;
  4. use App\Libs\Utils;
  5. use App\Modules\Activity\Services\ActivityService;
  6. use App\Modules\Book\Models\RecoBanner;
  7. use App\Modules\Book\Services\BookAuditService;
  8. use App\Modules\Channel\Models\ChannelAdvert;
  9. use App\Modules\Channel\Services\ChannelAdvertService;
  10. use App\Modules\Channel\Services\ChannelRecommendBookConfigService;
  11. use App\Modules\Channel\Services\ChannelRecommendBooksService;
  12. use App\Modules\RecommendBook\Services\QappRecommendService;
  13. use App\Modules\RecommendBook\Services\RecommendService;
  14. use App\Modules\Book\Services\RecoBannerService;
  15. use App\Modules\Subscribe\Models\Order;
  16. use App\Modules\User\Models\ChannelAdUser;
  17. use App\Modules\User\Models\QappPackage;
  18. use App\Modules\User\Services\QappUserService;
  19. use Illuminate\Http\Request;
  20. use App\Http\Controllers\QuickApp\BaseController;
  21. use App\Http\Controllers\QuickApp\Book\Transformers\BookTransformer;
  22. use App\Http\Controllers\QuickApp\Book\Transformers\KeywordTransformer;
  23. use App\Modules\Book\Models\BookConfig;
  24. use App\Modules\Book\Services\BookConfigService;
  25. use App\Modules\Book\Services\BookService;
  26. use App\Modules\Book\Services\BookUrgeUpdateService;
  27. use App\Modules\Book\Services\UserShelfBooksService;
  28. use App\Modules\Book\Services\ChapterService;
  29. use App\Modules\Subscribe\Services\BookOrderService;
  30. use App\Modules\Subscribe\Services\ChapterOrderService;
  31. use App\Modules\Subscribe\Services\YearOrderService;
  32. use App\Modules\Subscribe\Services\OrderService;
  33. use App\Modules\User\Services\ReadRecordService;
  34. use Hashids;
  35. use Illuminate\Support\Facades\DB;
  36. use Log;
  37. use Illuminate\Support\Facades\Redis;
  38. class BookController extends BaseController
  39. {
  40. public function index(Request $request, $bid)
  41. {
  42. $bid = str_decode($bid);
  43. $book_info = BookConfigService::getBookById($bid);
  44. $package = $request->header('x-package', '');
  45. if (!$book_info) {
  46. return response()->error('QAPP_SYS_ERROR');
  47. }
  48. $book_auth = BookConfigService::bookCopyright($bid,$this->distribution_channel_id);
  49. if($book_auth == 2){
  50. return response()->error('QAPP_OFF_SHELF');
  51. }
  52. //yuyuedu、xinghe 快应用这两个cp的书屏蔽下
  53. if(in_array($book_info->cp_source,getHiddenCp($package)) && $book_auth != 1){
  54. return response()->error('QAPP_OFF_SHELF');
  55. }
  56. if($this->distribution_channel_id == 7477 && $bid == 13765){
  57. $book_info->is_on_shelf = 2;
  58. }
  59. $special = get_special_bid();
  60. if (in_array($this->distribution_channel_id,[9487,9390]) && in_array($book_info->bid,$special)){
  61. $book_info->is_on_shelf = 2;
  62. }
  63. if($bid == 58886){
  64. $book_info->is_on_shelf = 0;
  65. }
  66. if (!in_array($book_info->is_on_shelf, [1,2])) {
  67. return response()->error('QAPP_OFF_SHELF');
  68. }
  69. $is_on_shelf = UserShelfBooksService::getUserShelfBooksListByUidAndBid($this->uid, $bid);
  70. $book_info['is_on_user_shelf'] = 0;
  71. if ($is_on_shelf) {
  72. $book_info['is_on_user_shelf'] = 1;
  73. }
  74. $last_chapter = ChapterService::getChapterNameById($book_info['last_cid'], $bid);
  75. $book_info->last_chapter = $last_chapter['name'];
  76. list($is_split,$is_change_chapter_name) = BookService::splitContent($bid);
  77. if($is_split && ($book_info->channel_name == '男频' || $is_change_chapter_name) ){
  78. $book_info->last_chapter = '第'.$book_info->chapter_count.'章';
  79. }
  80. $book_info['last_chapter_is_vip'] = $last_chapter['is_vip'];
  81. $book_info['is_need_charge'] = $this->isNeedCharge($bid, $last_chapter, $book_info);
  82. $record = ReadRecordService::getBookReadRecordStatic($this->uid, $bid);
  83. if ($record) {
  84. $book_info['record_chapter_id'] = $record['record_chapter_id'];
  85. $book_info['record_chapter_name'] = $record['record_chapter_name'];
  86. }
  87. return response()->item(new BookTransformer(), $book_info);
  88. }
  89. /**
  90. * 获取订购记录
  91. * @param $book_info
  92. * @param $chapter_id
  93. * @return bool
  94. */
  95. protected function getOrderRecord($bid, $chapter_id)
  96. {
  97. //包年记录
  98. $uid = $this->uid;
  99. $res = YearOrderService::getRecord($uid);
  100. if ($res) return true;
  101. $res = null;
  102. //单本订购记录
  103. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  104. if ($res) return true;
  105. $res = null;
  106. //章节订购记录
  107. $chapterOrder = new ChapterOrderService();
  108. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  109. return false;
  110. }
  111. /**
  112. * 判断是否需要充值
  113. */
  114. private function isBookNeedCharge(int $bid, float $price)
  115. {
  116. $book_order = $this->getOrderRecord($bid, 0);
  117. if ($book_order) {
  118. return false;
  119. } else {
  120. $user_info = $this->user_info;
  121. return $user_info['balance'] < $price;
  122. }
  123. }
  124. /**
  125. * 判断章节是否需要充值
  126. */
  127. private function isChapterNeedCharge(int $bid, int $cid, float $price)
  128. {
  129. $book_order = $this->getOrderRecord($bid, $cid);
  130. if ($book_order) {
  131. return false;
  132. } else {
  133. $user_info = $this->user_info;
  134. return $user_info['balance'] < $price;
  135. }
  136. }
  137. /**
  138. * 判断是否需要充值
  139. */
  140. private function isNeedCharge(int $bid, $last_chapter, $book_info)
  141. {
  142. $is_free = BookConfigService::judgeBookIsFree($bid);
  143. if ($is_free) {
  144. return false;
  145. }
  146. switch ($book_info->charge_type) {
  147. case 'BOOK':
  148. $price = $this->getPrice($book_info);
  149. return $this->isBookNeedCharge($bid, $price);
  150. default:
  151. $price = isset($last_chapter->is_vip) ? $this->getPrice($book_info, $last_chapter->size) : 0;
  152. return isset($last_chapter->is_vip) ? $this->isChapterNeedCharge($bid, $last_chapter->id, $price) : false;
  153. }
  154. }
  155. /**
  156. * 计算价格
  157. * @param $book_info
  158. * @param $chapter_size
  159. * @return float
  160. */
  161. protected function getPrice($book_info, $chapter_size = 0)
  162. {
  163. if ($book_info->charge_type == 'BOOK')
  164. return $book_info->price * 100;
  165. return ceil($chapter_size / 100);
  166. }
  167. /**
  168. * 首页
  169. */
  170. public function getBookLists(Request $request, $sex)
  171. {
  172. // 获取基本数据
  173. $package = $request->header('x-package', '');
  174. $brand = $request->header('x-nbrand', '');
  175. $codeVersion = $request->header('x-codeversion', '');
  176. $isAuth = check_qapp_auth($package ,0);
  177. // 根据包名、平台、版本号判断是否审核
  178. if (Utils::checkIsAudit($package, $brand, $codeVersion) || $isAuth == false) {
  179. $result = BookAuditService::getHomeBooksData($sex, $package,$isAuth);
  180. return response()->success($result);
  181. }
  182. $user = (new QappUserService)->getGolableUser();
  183. if($package == "com.beidao.kuaiying.yueai" && $sex == "male" && isset($user->uid) && !empty($user->uid) && $user->send_order_id > 0){
  184. $orderRecord = ChapterOrderService::hasUserRecord($user->uid);
  185. if($orderRecord){
  186. $result = BookAuditService::getYueaiHomeBooksData($sex, $package,$isAuth,1);
  187. return response()->success($result);
  188. }
  189. }
  190. if ($sex == 'male') {
  191. $channel = 1;
  192. $reco_banner_type = ['MALE', 'PUBLIC'];
  193. } else {
  194. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  195. $channel = 2;
  196. }
  197. if($isAuth){
  198. $books = $this->getDefaultBanner($reco_banner_type);
  199. }else{
  200. $home = config('home.ycsd');
  201. $banner = $home['reco_banner'];
  202. $books = $banner[$sex];
  203. }
  204. //新判断: 根据包名来获取对应所需的bid
  205. $qapp_package = QappPackage::getPackageByPackage($package);
  206. if($qapp_package){
  207. $package_id = $qapp_package->id;
  208. }else{
  209. $package_id = 0;
  210. }
  211. \Log::info('un_send_order_book:package_id1:'.$package_id.' $package:'.$package);
  212. if(isset($user->uid) && !empty($user->uid)){
  213. if(!$this->send_order_id || $this->send_order_id == 0 ){
  214. \Log::info('un_send_order_book:uid:'.$this->uid.' $package:'.$package);
  215. $result = $this->getCheckBids($channel,$books,$package_id,$package);
  216. if(isset($result[1]['books']) && count((array)$result[1]['books']) > 1){
  217. return response()->success($result);
  218. }
  219. $result = $this->getCheckBids($channel,$books,0,$package);
  220. return response()->success($result);
  221. }else{
  222. $package_id = 0;
  223. }
  224. \Log::info('un_send_order_book:package_id2:'.$package_id.' $package:'.$package);
  225. }else{
  226. $package_id = 0;
  227. }
  228. \Log::info('un_send_order_book:package_id3:'.$package_id.' $package:'.$package);
  229. $result = $this->getCheckBids($channel,$books,$package_id,$package);
  230. return response()->success($result);
  231. }
  232. /**
  233. * 根据包名
  234. * @param $channel
  235. * @param $books
  236. * @param $package_id
  237. * @return array
  238. */
  239. private function getCheckBids($channel,$books,$package_id,$package)
  240. {
  241. $isAuthor = check_qapp_auth($package_id,1);
  242. $hotBids = BookConfigService::getCheckBooks(QappRecommendService::getRecommendByPacketId($channel, 'hot',$package_id),$channel,$package,$isAuthor);
  243. $liveBids = BookConfigService::getCheckBooks(QappRecommendService::getRecommendByPacketId($channel, 'live',$package_id),$channel,$package,$isAuthor);
  244. $recomBids = BookConfigService::getCheckBooks(QappRecommendService::getRecommendByPacketId($channel, 'recom',$package_id),$channel,$package,$isAuthor);
  245. $newBids = BookConfigService::getCheckBooks(QappRecommendService::getRecommendByPacketId($channel, 'new_recom',$package_id),$channel,$package,$isAuthor);
  246. // myLog("Qapp_home_data")->info(['liveBids' =>$liveBids,'hotbids'=>$hotBids,'new' => $newBids,'recom'=> $recomBids]);
  247. return array_filter([
  248. ['type' => 'reco_banner', 'lable' => '首页banner', 'books' => $books],
  249. ['type' => 'hot', 'lable' => '热门书单', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($hotBids))],
  250. ['type' => 'zhibo', 'lable' => '神书直播', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($liveBids))],
  251. ['type' => 'recom', 'lable' => '小编精选', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($recomBids))],
  252. ['type' => 'new_recom', 'lable' => '人气新书', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($newBids))]
  253. ]);
  254. }
  255. /**
  256. * 检测并补充不满足条件的书籍id
  257. * @param $bid_list
  258. * @param $channel : 频道
  259. * @param $package : 包名
  260. * @return array
  261. */
  262. private function getCheckBooks($bid_list,$channel,$package,$is_author)
  263. {
  264. $hidden_cp = getHiddenCp($package);
  265. // if(!is_public_package($package)){
  266. // $hidden_cp = array_merge($hidden_cp,['lianshang']);
  267. // }
  268. //获取书本数量
  269. $count = count($bid_list);
  270. if (!$is_author){
  271. $where = [
  272. ['book_configs.charge_type','!=','BOOK'],
  273. ['book_configs.cp_source','=','ycsd'],
  274. ];
  275. }else{
  276. $where = [
  277. ['book_configs.charge_type','!=','BOOK'],
  278. ];
  279. }
  280. //获取当前有效书本数量
  281. $book_count = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  282. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  283. ->whereIn('book_configs.bid',$bid_list)
  284. ->where('book_configs.is_on_shelf',2)
  285. ->where('book_configs.charge_type','!=','BOOK')
  286. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  287. ->where($where)
  288. ->where('book_categories.pid',$channel)
  289. ->count();
  290. if($count == $book_count){
  291. return $bid_list;
  292. }
  293. //获取需要补充的书籍数量
  294. $supplement_count = (($count - $book_count) > 0) ? $count - $book_count : 0;
  295. if($supplement_count <= 0){
  296. return $bid_list;
  297. }
  298. //获取书籍交集bid,过滤掉不符合要求的书
  299. $bids = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  300. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  301. ->whereIn('book_configs.bid',$bid_list)
  302. ->where('book_configs.is_on_shelf',2)
  303. ->where($where)
  304. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  305. ->where('book_categories.pid',$channel)
  306. ->pluck('book_configs.bid')->all();
  307. $bid_list = array_intersect($bid_list,$bids);
  308. //获取随机的有效的书籍bid
  309. $rand_bid = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  310. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  311. ->where('book_configs.is_on_shelf',2)
  312. // ->where('book_configs.charge_type','!=','BOOK')
  313. ->where($where)
  314. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  315. ->where('book_categories.pid',$channel)
  316. ->inRandomOrder()
  317. ->limit($supplement_count)
  318. ->get()->pluck('bid')->toArray();
  319. return array_filter(array_merge($bid_list,$rand_bid));
  320. }
  321. private function getBidCidFromUrl(string $url)
  322. {
  323. if (preg_match('/\?bid=(\w+)\S+cid=(\w+)/', $url, $matches) || preg_match('/\?id=(\w+)/', $url, $matches)) {
  324. return [
  325. 'bid' => $matches[1],
  326. 'cid' => isset($matches[2]) ? $matches[2] : 0,
  327. ];
  328. } else {
  329. return [
  330. 'bid' => '',
  331. 'cid' => 0,
  332. ];
  333. }
  334. }
  335. public function library(Request $request)
  336. {
  337. $package = $request->header('x-package', '');
  338. if (in_array($package ,["com.app.kyy.dmzyd","com.app.kyy.tths"])){
  339. // return $this->getSpecialLibrary($request);
  340. }
  341. $where = [];
  342. $order = [];
  343. $where['is_on_shelf'] = [2];
  344. $category_id = $request->input('category_id');
  345. if ($category_id) {
  346. if ($category_id == 1) {
  347. $where['channel_name'] = '男频';
  348. } elseif ($category_id == 2) {
  349. $where['channel_name'] = '女频';
  350. } else {
  351. $where['category_id'] = $category_id;
  352. }
  353. }
  354. $key = $request->input('key');
  355. $uid = $request->input('uid', 0);
  356. if ($key && $uid && is_numeric($uid)) {
  357. BookConfigService::saveUserSearchLog($key, $uid);
  358. }
  359. $where['key'] = $key;
  360. $order_field = $request->input('order_field');
  361. $order_seq = $request->input('order_seq');
  362. if ($order_field != '' && in_array($order_field, ['recommend_index', 'click_count', 'update', 'size', 'create'])) {
  363. if ($order_field == 'update') {
  364. $order = ['book_configs.updated_at', 'desc'];
  365. } elseif ($order_field == 'create') {
  366. $order = ['book_configs.created_at', 'desc'];
  367. } else {
  368. $order = [$order_field, 'desc'];
  369. }
  370. if ($order_seq == 'asc') {
  371. $order = [$order_field, 'asc'];
  372. }
  373. if ($order_seq == 'desc') {
  374. $order = [$order_field, 'desc'];
  375. }
  376. }
  377. // 审核状态默认值
  378. $package = $request->header('x-package', '');
  379. $brand = $request->header('x-nbrand', '');
  380. $codeVersion = $request->header('x-codeversion', '');
  381. if ($order_field === 'recommend_index' && Utils::checkIsAudit($package, $brand, $codeVersion)) {
  382. $order = ['book_configs.bid', 'desc'];
  383. }
  384. // 是否只使用原创书殿的书
  385. $isAuth = check_qapp_auth($package ,0);
  386. if (!$isAuth){
  387. $where['cp_source'] = "ycsd";
  388. }
  389. $status = $request->input('status');
  390. if ($status != '') {
  391. $where['status'] = $status;
  392. }
  393. // 搜索关键词的情况下,屏蔽书籍完本状态
  394. if ($key && isset($where['status'])) {
  395. unset($where['status']);
  396. }
  397. $page_size = $request->input('page_size', 15);
  398. $where['channel_id'] = $request->input('distribution_channel_id',0);
  399. $books = BookConfigService::getBooks($where, $order, $page_size);
  400. return response()->pagination(new BookTransformer, $books);
  401. }
  402. public function hotWords(Request $request)
  403. {
  404. $result = BookConfigService::findBookKeywords();
  405. return response()->pagination(new KeywordTransformer, $result);
  406. }
  407. public function similarRecom(Request $request)
  408. {
  409. $package = $request->header('x-package', '');
  410. $category_id = $request->input('category_id');
  411. $bid = $request->input('bid');
  412. $package = $request->header('x-package', '');
  413. if (empty($bid) || (empty($category_id) && $category_id != 0)) {
  414. return response()->error('PARAM_ERROR');
  415. }
  416. $isAuth = check_qapp_auth($package ,0);
  417. $bid = BookService::decodeBidStatic($bid);
  418. $where = ['category_id' => $category_id, 'is_on_shelf' => [2]];
  419. if (!$isAuth){
  420. $where['cp_source'] = "ycsd";
  421. }
  422. $where['channel_id'] = $request->input('distribution_channel_id',0);
  423. $books = BookConfigService::getBooks($where, [], 4);
  424. $data = [];
  425. foreach ($books as $v) {
  426. if ($v->bid != $bid && count($data) < 3) {
  427. $data[] = $v;
  428. }
  429. }
  430. return response()->collection(new BookTransformer(), $data);
  431. }
  432. public function readOverRecommend(Request $request)
  433. {
  434. $bid = $request->input('bid');
  435. if (empty($bid)) {
  436. return response()->error('PARAM_ERROR');
  437. }
  438. $bid = BookService::decodeBidStatic($bid);
  439. $book_info = BookConfigService::getBookById($bid);
  440. $res = BookConfigService::getRecommendBooks($bid, $book_info->channel_name);
  441. $urge_status = 0;
  442. if ($book_info->status == 0 && !BookUrgeUpdateService::isHadUrged($this->uid, $bid)) {
  443. $urge_status = 1;
  444. }
  445. $recommend_result = collectionTransform(new BookTransformer(), $res);
  446. $book_status = [
  447. 'status' => $book_info->status,
  448. 'urge_status' => $urge_status
  449. ];
  450. $data = [
  451. 'recommend_result' => $recommend_result,
  452. 'book_status' => $book_status
  453. ];
  454. return response()->success($data);
  455. }
  456. public function rank(Request $request)
  457. {
  458. // 1:男频,2:女频
  459. $sex = (int)$request->input('sex');
  460. $package = $request->header('x-package', '');
  461. $channel_id = $request->input('distribution_channel_id',0);
  462. if (!in_array($sex, [1, 2], true)) {
  463. return response()->error('PARAM_ERROR');
  464. }
  465. $bids = BookConfigService::getAvailableBIdsbyBids( BookConfigService::getRankList($package,$sex),$channel_id,false);
  466. if (count($bids) > 30){
  467. $bids = array_chunk($bids,30)[0];
  468. }else{
  469. $bids = [11529, 11941, 12720, 11990, 11988, 11976, 11977, 4183, 12717, 11833,
  470. 7287,14297,12716,14312,14000,13577,16712,13002,12717,15103,13928,14793,
  471. 12708,13286];
  472. if ($sex === 2) {
  473. $bids = [8469, 11660, 9117, 7891, 12281, 12470, 8167, 11661, 11670, 8476, 8557, 11662,
  474. 11680, 11926, 12462, 7836, 11681, 11664, 11928, 8631];
  475. }
  476. }
  477. $books = collectionTransform(new BookTransformer, BookConfigService::getBookLists(compact('bids','channel_id')));
  478. return response()->success($books);
  479. }
  480. public function rankOld(Request $request)
  481. {
  482. // 1:男频,2:女频
  483. $sex = (int)$request->input('sex');
  484. if (!in_array($sex, [1, 2], true)) {
  485. return response()->error('PARAM_ERROR');
  486. }
  487. // 默认
  488. $bids = [11529, 11941, 12720, 11990, 11988, 11976, 11977, 4183, 12717, 11833,
  489. 7287,14297,12716,14312,14000,13577,16712,13002,12717,15103,13928,14793,
  490. 12708,13286];
  491. if ($sex === 2) {
  492. $bids = [8469, 11660, 9117, 7891, 12281, 12470, 8167, 11661, 11670, 8476, 8557, 11662,
  493. 11680, 11926, 12462, 7836, 11681, 11664, 11928, 8631];
  494. }
  495. /**
  496. * pid:1为男频 2为女频
  497. SELECT
  498. CONCAT( books.id, ',' )
  499. FROM
  500. books
  501. LEFT JOIN book_configs ON books.id = book_configs.bid
  502. WHERE
  503. book_configs.is_on_shelf = 2
  504. AND books.category_id IN ( SELECT id FROM book_categories WHERE pid = 2 )
  505. ORDER BY
  506. book_configs.recommend_index DESC
  507. LIMIT 10;
  508. */
  509. // 根据包名、平台、版本号判断是否审核
  510. $package = $request->header('x-package', '');
  511. $brand = $request->header('x-nbrand', '');
  512. $codeVersion = $request->header('x-codeversion', '');
  513. if (Utils::checkIsAudit($package, $brand, $codeVersion)) {
  514. $bids = [2266, 3838, 9700, 10175, 10301, 3422, 1166, 4546, 9163, 2509,
  515. 7287,14297,12716,14312,14000,13577,16712,13002,12717,15103,13928,
  516. 14793,12708,13286,13336,13275,13073,15121,13929,12693,13254,3526,
  517. 10313,3483,13278,14004,4098,10378,14072,21376,21139,21757,19449];
  518. if ($sex === 2) {
  519. $bids = [159, 2439, 6276, 10074, 5409, 9379, 10323, 9078, 3603, 487];
  520. }
  521. }
  522. $isAuth = check_qapp_auth($package,0);
  523. if (!$isAuth){
  524. $rank = config('home.rank');
  525. $bids = $rank['male'];
  526. if ($sex === 2) {
  527. $bids = $rank['female'];;
  528. }
  529. }
  530. $channel_id = $request->input('distribution_channel_id',0);
  531. $books = collectionTransform(new BookTransformer, BookConfigService::getBookLists(compact('bids','channel_id')));
  532. return response()->success($books);
  533. }
  534. /**
  535. * 推荐书
  536. */
  537. public function recommen()
  538. {
  539. $books = $this->getDefaultBanner();
  540. return response()->success($books);
  541. }
  542. /**
  543. * 阅爱小说任务轮播图
  544. */
  545. public function recommenYueAi()
  546. {
  547. $user = (new QappUserService)->getGolableUser();
  548. if(isset($user->uid) && !empty($user->uid) && $user->send_order_id > 0){
  549. $orderRecord = ChapterOrderService::hasUserRecord($user->uid);
  550. $data = config('home.yueai');
  551. $books = $data['task_banner'];
  552. $bids = BookConfigService::getAvailableBIdsbyBids(array_column($books,'bid'),$this->distribution_channel_id,false);
  553. if($orderRecord && !empty($bids)){
  554. foreach ($books as &$value){
  555. $value['bid'] =Hashids::encode($value['bid']);
  556. $value['redirect_url '] = empty($value['cid']) ? "views/Detail" : "views/Reader";
  557. }
  558. unset($value);
  559. return response()->success($books);
  560. }
  561. }
  562. $books = $this->getDefaultBanner( ['FEMALE', 'PUBLIC']);
  563. return response()->success($books);
  564. }
  565. /**
  566. * 新获取各种广告列表
  567. * @param Request $request
  568. * @return mixed
  569. */
  570. public function getRecommendBanners(Request $request)
  571. {
  572. $release_type = $request->get('release_type','');
  573. $distribution_id = $this->distribution_channel_id;
  574. if(is_empty($release_type)){
  575. //默认原banner
  576. // $recom_banner_type = ['FEMALE', 'PUBLIC'] : ['FEMALE', 'PUBLIC'];
  577. $banner = $this->getDefaultBanner(['FEMALE','PUBLIC']);
  578. return response()->success($banner);
  579. }
  580. if($release_type == '4' || $release_type == '5'){
  581. //弹窗和充值页返回需要先判断频率跟权限
  582. $advert = ChannelAdvert::select('id','photo as banner_url','activity_id','type','content','person','frequency')
  583. ->where('distribution_id',$distribution_id)
  584. ->where('release_type',$release_type)
  585. ->where('status',1)
  586. ->first();
  587. if(!$advert){
  588. return response()->success([]);
  589. }
  590. $advert = $advert->toArray();
  591. $advert['ids'] = Hashids::encode($advert['id']);
  592. if($advert['type'] == 1){
  593. $advert['redirect_url'] = 'views/Reader';
  594. $content = explode(';',$advert['content']);
  595. $advert['bid'] = isset($content[2]) ? $content[2] : '';
  596. $advert['cid'] = isset($content[3]) ? $content[3] : '';
  597. }else{
  598. $advert['redirect_url'] = 'views/Detail';
  599. }
  600. if($release_type == '4'){
  601. //弹窗需要判断频率
  602. if($advert['frequency'] == 'always'){
  603. $advert = self::getBackFormat($advert);
  604. return response()->success($advert);
  605. }
  606. $day = strtotime(date('Y-m-d H:i:s'));
  607. $nextDay = strtotime( date('Y-m-d'). ' +1 day');
  608. $nextWeek = strtotime(date('Y-m-d',strtotime('+1 week last monday')));
  609. if($advert['frequency'] == 'day'){
  610. if(!Redis::exists('banner:'.$distribution_id.':'.$this->uid)){
  611. Redis::setex('banner:'.$distribution_id.':'.$this->uid,($nextDay-$day),1);
  612. $advert = self::getBackFormat($advert);
  613. return response()->success($advert);
  614. }
  615. }
  616. if($advert['frequency'] == 'week'){
  617. if(!Redis::exists('banner:'.$distribution_id.':'.$this->uid)){
  618. Redis::setex('banner:'.$distribution_id.':'.$this->uid,($nextWeek-$day),1);
  619. $advert = self::getBackFormat($advert);
  620. return response()->success($advert);
  621. }
  622. }
  623. return response()->success([]);
  624. }
  625. if($release_type == '5'){
  626. //充值页返回需要判断用户权限
  627. $check_user = $this->checkUsers($advert['person']);
  628. if(!$check_user){
  629. return response()->success([]);
  630. }
  631. $activity = ActivityService::getById($advert['activity_id'] ?? 0);
  632. if ($activity){
  633. if ($activity['permanent'] == 1 || ($activity['start_time'] < date("Y-m-d H:i:s") && $activity['end_time'] > date("Y-m-d H:i:s") )){
  634. $advert['redirect_url'] = "/views/Activity";
  635. $advert = self::getBackFormat($advert);
  636. $advert['param'] = ['token' => $activity['token']];
  637. return response()->success($advert);
  638. }else{
  639. return response()->success([]);
  640. }
  641. }else{
  642. return response()->success([]);
  643. }
  644. }
  645. }
  646. //男女频,书架列表
  647. $banner = ChannelAdvertService::getAdvertList($distribution_id,$release_type);
  648. if($banner->isEmpty()){
  649. $recom_banner_type = ["PUBLIC",$release_type ==1 ? "MALE" : "FEMALE"];
  650. $banner = $this->getDefaultBanner($recom_banner_type);
  651. return response()->success($banner);
  652. }
  653. $banner->transform(function ($item) {
  654. $item->ids = Hashids::encode($item->id);
  655. if($item->type == 1){
  656. $content = explode(';',$item->content);
  657. $item->bid = isset($content[2]) ? $content[2] : '';
  658. $item->cid = isset($content[3]) ? $content[3] : '';
  659. if ($item->cid){
  660. $item->redirect_url = "views/Reader";
  661. }else{
  662. $item->redirect_url = "views/Detail";
  663. }
  664. $item->redirect_type = "book";
  665. }else{
  666. $activity = ActivityService::getById($item->activity_id);
  667. $item->redirect_url = "/views/Activity";
  668. $item->redirect_type = "activity";
  669. if($activity && !empty($activity['token'])){
  670. $item['param'] = ['token' => $activity['token']];
  671. }else{
  672. $item->redirect_url = "#";
  673. }
  674. }
  675. self::getBackFormat($item);
  676. return $item;
  677. });
  678. return response()->success($banner);
  679. }
  680. static function getBackFormat($data)
  681. {
  682. if(isset($data['id']) || $data->id) unset($data['id'],$data->id);
  683. if(isset($data['type']) || $data->type) unset($data['type'],$data->type);
  684. if(isset($data['person']) || $data->person) unset($data['person'],$data->person);
  685. if(isset($data['frequency']) || $data->frequency) unset($data['frequency'],$data->frequency);
  686. return $data;
  687. }
  688. /**
  689. * 默认获取banner
  690. * @return mixed
  691. */
  692. protected function getDefaultBanner( $reco_banner_type = ['FEMALE', 'PUBLIC'])
  693. {
  694. $banner = (new RecoBannerService)->getByType($reco_banner_type, 2);
  695. $banner->transform(function ($item) {
  696. $result = $this->getBidCidFromUrl($item->redirect_url);
  697. $item->bid = $result['bid'];
  698. $item->cid = $result['cid'];
  699. $item->ids = Hashids::encode($item->id);
  700. $item->type = 'default';
  701. if ($result['cid']) {
  702. $item->redirect_url = "views/Reader";
  703. } else {
  704. $item->redirect_url = "views/Detail";
  705. }
  706. $item->redirect_type = "book";
  707. unset($item->id);
  708. return $item;
  709. });
  710. return $banner;
  711. }
  712. /**
  713. * 限免
  714. */
  715. public function free(int $sex)
  716. {
  717. $result = BookConfigService::findFreeBooks($sex);
  718. return response()->success($result);
  719. }
  720. public function yueaiBackRecom(Request $request)
  721. {
  722. $package = $request->header('x-package', '');
  723. if (empty($package) || $package != 'com.beidao.kuaiying.yueai') {
  724. return response()->success([]);
  725. }
  726. $user = (new QappUserService)->getGolableUser();
  727. if(isset($user->uid) && !empty($user->uid) && $user->send_order_id > 0){
  728. $bid = BookConfigService::getAvailableBIdsbyBids([58238,60534,63220,14500,13254,63221,63548,14022,59334,58888,63417,61701],$this->distribution_channel_id,false);
  729. if (!empty($bid)){
  730. $bid = array_random($bid,4);
  731. }
  732. $orderRecord = ChapterOrderService::hasUserRecord($user->uid);
  733. if($orderRecord && count($bid) >= 4){
  734. $where = ['is_on_shelf' => [1,2],'bids' => $bid];
  735. // $books = BookConfigService::getBooksByIds($bid,[],false);
  736. $books = BookConfigService::getBookLists($where,[],false);
  737. return response()->collection(new BookTransformer(), $books);
  738. }
  739. }
  740. return response()->success([]);
  741. $where = ['is_on_shelf' => [2]];
  742. $where['channel_id'] = $request->input('distribution_channel_id',0);
  743. $books = BookConfigService::getBooks($where, [], 4);
  744. return response()->collection(new BookTransformer(), $books);
  745. }
  746. public function shelfRecom(Request $request)
  747. {
  748. $package = $request->header('x-package', '');
  749. $user = (new QappUserService)->getGolableUser();
  750. if (!empty($package) && $package == 'com.beidao.kuaiying.yueai') {
  751. if(isset($user->uid) && !empty($user->uid) && $user->send_order_id > 0){
  752. $bid = BookConfigService::getAvailableBIdsbyBids([58238,60534,63220,14500,13254,63221,63548,14022,59334,58888,63417,61701],$this->distribution_channel_id,false);
  753. if (!empty($bid)){
  754. $bid = array_random($bid,4);
  755. }
  756. $orderRecord = ChapterOrderService::hasUserRecord($user->uid);
  757. if($orderRecord && count($bid) > 1){
  758. $where = ['is_on_shelf' => [1,2],'bids' => $bid];
  759. // $books = BookConfigService::getBooksByIds($bid,[],false);
  760. $books = BookConfigService::getBookLists($where,[],false);
  761. return response()->collection(new BookTransformer(), $books);
  762. }
  763. }
  764. return response()->success([]);
  765. }
  766. $where = ['is_on_shelf' => [2],'channel_id' => $this->distribution_channel_id,'is_high_quality' => 1];
  767. if (isset($user->uid) && $user->user->sex == 1){
  768. $where['channel_name'] = "男频";
  769. }else{
  770. $where['channel_name'] = "女频";
  771. }
  772. $books = BookConfigService::getBooks($where, [], 3);
  773. return response()->collection(new BookTransformer(), $books);
  774. }
  775. /**
  776. * 新推荐书单
  777. * @param Request $request
  778. * @return mixed
  779. */
  780. public function recommendBooks(Request $request)
  781. {
  782. $distribution_id = $this->distribution_channel_id;
  783. $bid = $request->get('bid',0);
  784. if(empty($distribution_id)){
  785. \Log::info('recommendBooks:1');
  786. return response()->success([]);
  787. }
  788. if(!empty($bid)){
  789. \Log::info('recommendBooks:2');
  790. $bid = str_decode($bid);
  791. }
  792. //判断包是否存在
  793. $package_info = QappPackage::getPackage($distribution_id);
  794. if(empty($package_info) || !isset($package_info->channel_id)){
  795. \Log::info('recommendBooks:3');
  796. return response()->success([]);
  797. }
  798. //包对应有没有配置开启推荐书单
  799. $config = ChannelRecommendBookConfigService::getRecommendConfigs($distribution_id);
  800. if(empty($config) || !isset($config->status) || $config->status == 0){
  801. \Log::info('recommendBooks:4');
  802. return response()->success([]);
  803. }
  804. //根据频率和用户属性决定是否需要返回
  805. $res = $this->checkUsersAuth($config);
  806. if(!$res){
  807. \Log::info('recommendBooks:5');
  808. return response()->success([]);
  809. }
  810. $list = ChannelRecommendBooksService::getRecommendBooks($distribution_id,$bid);
  811. if(!is_empty($list)){
  812. foreach($list as $key => $item){
  813. $this->incrRecommendNum($distribution_id,$item->bid);
  814. }
  815. \Log::info('recommendBooks:6');
  816. return response()->collection(new BookTransformer(), $list);
  817. }
  818. \Log::info('recommendBooks:7');
  819. return response()->success([]);
  820. }
  821. /**
  822. * 点击推荐书籍记录点击次数
  823. * @param Request $request
  824. * @return mixed
  825. */
  826. public function clickRecommendBooks(Request $request)
  827. {
  828. $distribution_id = $this->distribution_channel_id;
  829. $bid = $request->get('bid','');
  830. if(empty($bid)){
  831. return response()->success();
  832. }
  833. //判断包是否存在
  834. $package_info = QappPackage::getPackage($distribution_id);
  835. if(empty($package_info) || !isset($package_info->channel_id)){
  836. return response()->success([]);
  837. }
  838. $date = date('Ymd');
  839. $bid = str_decode($bid);
  840. $cacheKey = 'recommend:click:'.$date.':'.$distribution_id.$bid;
  841. $this->incrRedisKey($cacheKey);
  842. return response()->success();
  843. }
  844. /**
  845. * 判断用户是否需要推荐
  846. * @param $config
  847. * @return bool
  848. */
  849. protected function checkUsersAuth($config)
  850. {
  851. \Log::info($config);
  852. \Log::info('$config->person:'.$config->person);
  853. \Log::info($this->uid);
  854. $res = $this->checkUsers($config->person);
  855. if($res === false){
  856. return false;
  857. }
  858. //频率判断
  859. if($config->frequency == 'back'){
  860. //返回即推送
  861. return true;
  862. }
  863. $day = strtotime(date('Y-m-d H:i:s'));
  864. $nextDay = strtotime( date('Y-m-d'). ' +1 day');
  865. $nextWeek = strtotime(date('Y-m-d',strtotime('+1 week last monday')));
  866. if($config->frequency == 'day'){
  867. //每日推送
  868. if(!Redis::exists('recommend:'.$config->channel_id.':'.$this->uid)){
  869. Redis::setex('recommend:'.$config->channel_id.':'.$this->uid,($nextDay-$day),1);
  870. return true;
  871. }
  872. \Log::info('当天已经推送过了');
  873. return false;
  874. }
  875. if($config->frequency == 'week'){
  876. //每周推送
  877. if(!Redis::exists('recommend:'.$config->channel_id.':'.$this->uid)){
  878. Redis::setex('recommend:'.$config->channel_id.':'.$this->uid,($nextWeek-$day),1);
  879. return true;
  880. }
  881. \Log::info('该周已经推送过了');
  882. return false;
  883. }
  884. }
  885. /**
  886. * 判断用户是否有权限
  887. * @param $type
  888. * @return bool
  889. */
  890. protected function checkUsers($type)
  891. {
  892. $res = false;
  893. switch($type)
  894. {
  895. case 'pay':
  896. //付费用户
  897. $orders = OrderService::getUserLastestOrder($this->uid);
  898. if($orders){
  899. $res = true;
  900. }
  901. \Log::info('用户是否付费:');
  902. \Log::info($orders);
  903. break;
  904. case 'send_order':
  905. //派单用户
  906. if($this->send_order_id != 0){
  907. $res = true;
  908. }
  909. \Log::info('用户是否是派单用户:$this->send_order_id:'.$this->send_order_id);
  910. break;
  911. case 'pay_send_order':
  912. //付费派单用户
  913. $orders = OrderService::getUserLastestOrder($this->uid);
  914. if($orders && $this->send_order_id != 0){
  915. $res = true;
  916. }
  917. \Log::info('用户是否是付费派单用户:$this->send_order_id:'.$this->send_order_id);
  918. \Log::info($orders);
  919. break;
  920. case 'unpaid':
  921. $orders = OrderService::getUserLastestOrder($this->uid);
  922. if(!$orders){
  923. $res = true;
  924. }
  925. break;
  926. case 'all':
  927. $res = true;
  928. \Log::info('所有用户返回');
  929. break;
  930. }
  931. return $res;
  932. }
  933. /**
  934. * 推广书籍推荐次数
  935. * @param $channel_id
  936. * @param $bid
  937. */
  938. protected function incrRecommendNum($channel_id,$bid)
  939. {
  940. $date = date('Ymd');
  941. $cacheKey = 'recommend:sum:'.$date.':'.$channel_id.$bid;
  942. $this->incrRedisKey($cacheKey);
  943. }
  944. /**
  945. * 点击广告统计
  946. * @param Request $request
  947. * @return mixed
  948. */
  949. public function getCheckAdvertisement(Request $request)
  950. {
  951. $type = $request->get('type','');
  952. $id = $request->get('ids','');
  953. $distribution_id = $this->distribution_channel_id;
  954. \Log::info('getCheckAdvertisement:type:'.$type.' id:'.$id.' $distribution_id:'.$distribution_id);
  955. if(empty($id) || empty($distribution_id)){
  956. return response()->success();
  957. }
  958. $id = str_decode($id);
  959. if(!$id){
  960. return response()->success();
  961. }
  962. if($type == 'default'){
  963. //默认原表reco_banners广告,区分跟新表channel_advert的id
  964. $id = -$id;
  965. }else{
  966. $advert = ChannelAdvert::find($id);
  967. if(!$advert){
  968. \Log::info('getCheckAdvertisement:error:不存在该广告');
  969. \Log::info(json_encode($request->all()));
  970. return response()->success();
  971. }
  972. }
  973. $where = ['channel_ad_id' => $id, 'uid' => $this->uid, 'distribution_id' => $distribution_id];
  974. \Log::info('insert_where:');
  975. \Log::info(json_encode($where));
  976. ChannelAdUser::firstOrCreate($where);
  977. $date = date('Ymd');
  978. $cacheKey = 'advertisement:pv:'.$date.':'.$distribution_id.$id;
  979. $this->incrRedisKey($cacheKey);
  980. //设置当前用户的所属广告有效期一周
  981. $key = 'advertisement:uid:'.$this->uid.':id';
  982. Redis::set($key,$id);
  983. Redis::expire($key,BaseConst::ONE_WEEK_SECONDS);
  984. return response()->success();
  985. }
  986. /**
  987. * redis新增
  988. * @param $cacheKey
  989. */
  990. protected function incrRedisKey($cacheKey)
  991. {
  992. if(!Redis::exists($cacheKey)){
  993. Redis::set($cacheKey,1);
  994. }else{
  995. Redis::incrBy($cacheKey, 1);
  996. }
  997. }
  998. /**
  999. * 绑定广告和订单
  1000. * @param Request $request
  1001. * @return mixed
  1002. */
  1003. public function getAdvertOrders(Request $request)
  1004. {
  1005. $order_id = $request->get('trade_no','');
  1006. $uid = $this->uid;
  1007. $distribution_id = $this->distribution_channel_id;
  1008. $cacheKey = 'advertisement:uid:'.$uid.':id';
  1009. if(empty($order_id)){
  1010. return response()->success();
  1011. }
  1012. $channel_ad_id = 0;
  1013. \Log::info('getAdvertOrders:trade_no'.$order_id.' uid:'.$uid);
  1014. if(Redis::exists($cacheKey)){
  1015. try {
  1016. //获取广告id
  1017. $channel_ad_id = Redis::get($cacheKey);
  1018. //判断订单跟广告是否有关系
  1019. $order = OrderService::getByTradeNo($order_id);
  1020. if(!$order){
  1021. return response()->success();
  1022. }
  1023. if($channel_ad_id > 0){
  1024. //新版
  1025. $type = 1;
  1026. $advert = ChannelAdvert::find($channel_ad_id);
  1027. if(!$advert){
  1028. return response()->success();
  1029. }
  1030. if($advert->type == '2' && $advert->activity_id != 0){
  1031. //广告类型为活动
  1032. if($order->activity_id != $advert->activity_id){
  1033. return response()->success();
  1034. }
  1035. }else{
  1036. //广告类型为书籍
  1037. $content = explode(';',$advert->content);
  1038. if(!isset($content[2])){
  1039. return response()->success();
  1040. }
  1041. if($order->from_bid == 0 || $order->from_bid != str_decode($content[2])){
  1042. return response()->success();
  1043. }
  1044. }
  1045. }else{
  1046. //旧版
  1047. $type = 0;
  1048. $advert = RecoBanner::find(abs($channel_ad_id));
  1049. if(!$advert){
  1050. return response()->success();
  1051. }
  1052. $result = $this->getBidCidFromUrl($advert->redirect_url);
  1053. $bid = $result['bid'];
  1054. $cid = $result['cid'];
  1055. if($cid){
  1056. if($order->from_bid == 0 || $order->from_bid != str_decode($bid)){
  1057. return response()->success();
  1058. }
  1059. }else{
  1060. if($order->activity_id == 0 || $order->from_bid != str_decode($bid)){
  1061. return response()->success();
  1062. }
  1063. }
  1064. }
  1065. $created_at = $updated_at = date('Y-m-d H:i:s');
  1066. DB::table('channel_advert_orders')->insert(compact('order_id','uid','distribution_id','channel_ad_id','type','created_at','updated_at'));
  1067. } catch (\Exception $e) {
  1068. \Log::info('绑定广告和订单失败'.$e->getMessage());
  1069. \Log::info('order_id:'.$order_id.' uid:'.$uid.' distribution_id:'.$distribution_id.' channel_ad_id:'.$channel_ad_id);
  1070. }
  1071. }
  1072. return response()->success();
  1073. }
  1074. private function getSpecialLibrary(Request $request)
  1075. {
  1076. $where = [];
  1077. $order = [];
  1078. $where['is_on_shelf'] = [2,4];
  1079. $category_id = $request->input('category_id');
  1080. if ($category_id) {
  1081. if ($category_id == 1) {
  1082. $where['channel_name'] = '男频';
  1083. } elseif ($category_id == 2) {
  1084. $where['channel_name'] = '女频';
  1085. } else {
  1086. $where['category_id'] = $category_id;
  1087. }
  1088. }
  1089. $key = $request->input('key');
  1090. $uid = $request->input('uid', 0);
  1091. if ($key && $uid && is_numeric($uid)) {
  1092. BookConfigService::saveUserSearchLog($key, $uid);
  1093. }
  1094. $where['key'] = $key;
  1095. $order_field = $request->input('order_field');
  1096. $order_seq = $request->input('order_seq');
  1097. if ($order_field != '' && in_array($order_field, ['recommend_index', 'click_count', 'update', 'size', 'create'])) {
  1098. if ($order_field == 'update') {
  1099. $order = ['book_configs.updated_at', 'desc'];
  1100. } elseif ($order_field == 'create') {
  1101. $order = ['book_configs.created_at', 'desc'];
  1102. } else {
  1103. $order = [$order_field, 'desc'];
  1104. }
  1105. if ($order_seq == 'asc') {
  1106. $order = [$order_field, 'asc'];
  1107. }
  1108. if ($order_seq == 'desc') {
  1109. $order = [$order_field, 'desc'];
  1110. }
  1111. }
  1112. // 审核状态默认值
  1113. $package = $request->header('x-package', '');
  1114. $brand = $request->header('x-nbrand', '');
  1115. $codeVersion = $request->header('x-codeversion', '');
  1116. if ($order_field === 'recommend_index' && Utils::checkIsAudit($package, $brand, $codeVersion)) {
  1117. $order = ['book_configs.bid', 'desc'];
  1118. }
  1119. $status = $request->input('status');
  1120. if ($status != '') {
  1121. $where['status'] = $status;
  1122. }
  1123. // 搜索关键词的情况下,屏蔽书籍完本状态
  1124. if ($key && isset($where['status'])) {
  1125. unset($where['status']);
  1126. }
  1127. $page_size = $request->input('page_size', 15);
  1128. $where['channel_id'] = ($package === 'com.beidao.kuaiying.zsy') ? 7477 : 0;
  1129. $books = BookConfigService::getBooks($where, $order, $page_size);
  1130. $special = get_special_bid();
  1131. $list = [];
  1132. foreach ($books as $ke => $val){
  1133. if ($val->is_on_shelf != 2 && !in_array($val->bid,$special)){
  1134. unset($books[$ke]);
  1135. }
  1136. }
  1137. return response()->pagination(new BookTransformer,$books);
  1138. }
  1139. /**
  1140. * 首页
  1141. */
  1142. public function getBookListsNew(Request $request, $sex)
  1143. {
  1144. // 获取基本数据
  1145. $package = $request->header('x-package', '');
  1146. $brand = $request->header('x-nbrand', '');
  1147. $codeVersion = $request->header('x-codeversion', '');
  1148. $isAuth = check_qapp_auth($package ,0);
  1149. //新判断: 根据包名来获取对应所需的bid
  1150. $qapp_package = QappPackage::getPackageByPackage($package);
  1151. if($qapp_package){
  1152. $package_id = $qapp_package->id;
  1153. }else{
  1154. $package_id = 0;
  1155. }
  1156. $channel = $sex == 'male' ? 1 : 2;
  1157. $result = $this->getRecommendBooks($package_id,$package,$channel, $isAuth);
  1158. return response()->success($result);
  1159. }
  1160. private function getRecommendBooks($package_id,$package, $channel, $is_auth)
  1161. {
  1162. $data = BookConfigService::getRecommendBids($package,$channel,[],48);
  1163. $bids = array_chunk($data,12);
  1164. $guss_like = BookConfigService::getRecommendBids($package,$channel,$data,20);
  1165. $new_recom = BookConfigService::getNewRecommendBids($package,$channel,$data);
  1166. $default = [1,1,1,1,1,1,1,1,1,1,1,1];
  1167. return array_filter([
  1168. ["has_more"=>false,'type' => 'hot', 'lable' => '热门书单', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($bids[0] ?? BookConfigService::getCheckBooks($default,$channel,$package,$is_auth)))],
  1169. ["has_more"=>false,'type' => 'zhibo', 'lable' => '神书直播', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($bids[2] ?? BookConfigService::getCheckBooks($default,$channel,$package,$is_auth)))],
  1170. ["has_more"=>false,'type' => 'recom', 'lable' => '小编精选', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($bids[3] ?? BookConfigService::getCheckBooks($default,$channel,$package,$is_auth)))],
  1171. ["has_more"=>false,'type' => 'new_recom', 'lable' => '人气新书', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($bids[4] ?? BookConfigService::getCheckBooks($default,$channel,$package,$is_auth)))],
  1172. ["has_more"=>false,'type' => 'guss_like', 'lable' => '猜你喜欢', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($guss_like))],
  1173. ]);
  1174. return [];
  1175. }
  1176. }