BookController.php 52 KB

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