BookController.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  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. // $result = null;
  495. if(is_empty($result)){
  496. $result = $this->getRankData($package,$sex,$channel_id);
  497. if(!is_empty($result)) {
  498. HomeCache::setRankPageInfo($package, $sex, $result, 86400 + rand(0, 100));
  499. }
  500. }
  501. return response()->success($result);
  502. }
  503. protected function getRankData($package,$sex,$channel_id){
  504. $bids = BookConfigService::getAvailableBIdsbyBids( BookConfigService::getRankList($package,$sex),$channel_id,false);
  505. if (count($bids) > 30){
  506. $bids = array_chunk($bids,30)[0];
  507. }else{
  508. $bids = [11529, 11941, 12720, 11990, 11988, 11976, 11977, 4183, 12717, 11833,
  509. 7287,14297,12716,14312,14000,13577,16712,13002,12717,15103,13928,14793,
  510. 12708,13286];
  511. if ($sex === 2) {
  512. $bids = [8469, 11660, 9117, 7891, 12281, 12470, 8167, 11661, 11670, 8476, 8557, 11662,
  513. 11680, 11926, 12462, 7836, 11681, 11664, 11928, 8631];
  514. }
  515. }
  516. return collectionTransform(new BookTransformer, BookConfigService::getBookLists(compact('bids','channel_id')));
  517. }
  518. public function rankOld(Request $request)
  519. {
  520. // 1:男频,2:女频
  521. $sex = (int)$request->input('sex');
  522. if (!in_array($sex, [1, 2], true)) {
  523. return response()->error('PARAM_ERROR');
  524. }
  525. // 默认
  526. $bids = [11529, 11941, 12720, 11990, 11988, 11976, 11977, 4183, 12717, 11833,
  527. 7287,14297,12716,14312,14000,13577,16712,13002,12717,15103,13928,14793,
  528. 12708,13286];
  529. if ($sex === 2) {
  530. $bids = [8469, 11660, 9117, 7891, 12281, 12470, 8167, 11661, 11670, 8476, 8557, 11662,
  531. 11680, 11926, 12462, 7836, 11681, 11664, 11928, 8631];
  532. }
  533. /**
  534. * pid:1为男频 2为女频
  535. SELECT
  536. CONCAT( books.id, ',' )
  537. FROM
  538. books
  539. LEFT JOIN book_configs ON books.id = book_configs.bid
  540. WHERE
  541. book_configs.is_on_shelf = 2
  542. AND books.category_id IN ( SELECT id FROM book_categories WHERE pid = 2 )
  543. ORDER BY
  544. book_configs.recommend_index DESC
  545. LIMIT 10;
  546. */
  547. // 根据包名、平台、版本号判断是否审核
  548. $package = $request->header('x-package', '');
  549. $brand = $request->header('x-nbrand', '');
  550. $codeVersion = $request->header('x-codeversion', '');
  551. if (Utils::checkIsAudit($package, $brand, $codeVersion)) {
  552. $bids = [2266, 3838, 9700, 10175, 10301, 3422, 1166, 4546, 9163, 2509,
  553. 7287,14297,12716,14312,14000,13577,16712,13002,12717,15103,13928,
  554. 14793,12708,13286,13336,13275,13073,15121,13929,12693,13254,3526,
  555. 10313,3483,13278,14004,4098,10378,14072,21376,21139,21757,19449];
  556. if ($sex === 2) {
  557. $bids = [159, 2439, 6276, 10074, 5409, 9379, 10323, 9078, 3603, 487];
  558. }
  559. }
  560. $isAuth = check_qapp_auth($package,0);
  561. if (!$isAuth){
  562. $rank = config('home.rank');
  563. $bids = $rank['male'];
  564. if ($sex === 2) {
  565. $bids = $rank['female'];;
  566. }
  567. }
  568. $channel_id = $request->input('distribution_channel_id',0);
  569. $books = collectionTransform(new BookTransformer, BookConfigService::getBookLists(compact('bids','channel_id')));
  570. return response()->success($books);
  571. }
  572. /**
  573. * 推荐书
  574. */
  575. public function recommen()
  576. {
  577. $books = $this->getDefaultBanner();
  578. return response()->success($books);
  579. }
  580. /**
  581. * 阅爱小说任务轮播图
  582. */
  583. public function recommenYueAi()
  584. {
  585. $user = (new QappUserService)->getGolableUser();
  586. if(isset($user->uid) && !empty($user->uid) && $user->send_order_id > 0){
  587. $orderRecord = ChapterOrderService::hasUserRecord($user->uid);
  588. $data = config('home.yueai');
  589. $books = $data['task_banner'];
  590. $bids = BookConfigService::getAvailableBIdsbyBids(array_column($books,'bid'),$this->distribution_channel_id,false);
  591. if($orderRecord && !empty($bids)){
  592. foreach ($books as &$value){
  593. $value['bid'] =Hashids::encode($value['bid']);
  594. $value['redirect_url '] = empty($value['cid']) ? "views/Detail" : "views/Reader";
  595. }
  596. unset($value);
  597. return response()->success($books);
  598. }
  599. }
  600. $books = $this->getDefaultBanner( ['FEMALE', 'PUBLIC']);
  601. return response()->success($books);
  602. }
  603. /**
  604. * 新获取各种广告列表
  605. * @param Request $request
  606. * @return mixed
  607. */
  608. public function getRecommendBanners(Request $request)
  609. {
  610. $release_type = $request->get('release_type','');
  611. $distribution_id = $this->distribution_channel_id;
  612. if(is_empty($release_type)){
  613. //默认原banner
  614. // $recom_banner_type = ['FEMALE', 'PUBLIC'] : ['FEMALE', 'PUBLIC'];
  615. $banner = $this->getDefaultBanner(['FEMALE','PUBLIC']);
  616. return response()->success($banner);
  617. }
  618. if($release_type == '4' || $release_type == '5'){
  619. //弹窗和充值页返回需要先判断频率跟权限
  620. $advert = ChannelAdvert::select('id','photo as banner_url','activity_id','type','content','person','frequency')
  621. ->where('distribution_id',$distribution_id)
  622. ->where('release_type',$release_type)
  623. ->where('status',1)
  624. ->first();
  625. if(!$advert){
  626. return response()->success([]);
  627. }
  628. $advert = $advert->toArray();
  629. $advert['ids'] = Hashids::encode($advert['id']);
  630. if($advert['type'] == 1){
  631. $advert['redirect_url'] = 'views/Reader';
  632. $content = explode(';',$advert['content']);
  633. $advert['bid'] = isset($content[2]) ? $content[2] : '';
  634. $advert['cid'] = isset($content[3]) ? $content[3] : '';
  635. }else{
  636. $advert['redirect_url'] = 'views/Detail';
  637. }
  638. if($release_type == '4'){
  639. //弹窗需要判断频率
  640. if($advert['frequency'] == 'always'){
  641. $advert = self::getBackFormat($advert);
  642. return response()->success($advert);
  643. }
  644. $day = strtotime(date('Y-m-d H:i:s'));
  645. $nextDay = strtotime( date('Y-m-d'). ' +1 day');
  646. $nextWeek = strtotime(date('Y-m-d',strtotime('+1 week last monday')));
  647. if($advert['frequency'] == 'day'){
  648. if(!Redis::exists('banner:'.$distribution_id.':'.$this->uid)){
  649. Redis::setex('banner:'.$distribution_id.':'.$this->uid,($nextDay-$day),1);
  650. $advert = self::getBackFormat($advert);
  651. return response()->success($advert);
  652. }
  653. }
  654. if($advert['frequency'] == 'week'){
  655. if(!Redis::exists('banner:'.$distribution_id.':'.$this->uid)){
  656. Redis::setex('banner:'.$distribution_id.':'.$this->uid,($nextWeek-$day),1);
  657. $advert = self::getBackFormat($advert);
  658. return response()->success($advert);
  659. }
  660. }
  661. return response()->success([]);
  662. }
  663. if($release_type == '5'){
  664. //充值页返回需要判断用户权限
  665. $check_user = $this->checkUsers($advert['person']);
  666. if(!$check_user){
  667. return response()->success([]);
  668. }
  669. $activity = ActivityService::getById($advert['activity_id'] ?? 0);
  670. if ($activity){
  671. if ($activity['permanent'] == 1 || ($activity['start_time'] < date("Y-m-d H:i:s") && $activity['end_time'] > date("Y-m-d H:i:s") )){
  672. $advert['redirect_url'] = "/views/Activity";
  673. $advert = self::getBackFormat($advert);
  674. $advert['param'] = ['token' => $activity['token']];
  675. return response()->success($advert);
  676. }else{
  677. return response()->success([]);
  678. }
  679. }else{
  680. return response()->success([]);
  681. }
  682. }
  683. }
  684. //男女频,书架列表
  685. $banner = ChannelAdvertService::getAdvertList($distribution_id,$release_type);
  686. if($banner->isEmpty()){
  687. $recom_banner_type = ["PUBLIC",$release_type ==1 ? "MALE" : "FEMALE"];
  688. $banner = $this->getDefaultBanner($recom_banner_type);
  689. return response()->success($banner);
  690. }
  691. $banner->transform(function ($item) {
  692. $item->ids = Hashids::encode($item->id);
  693. if($item->type == 1){
  694. $content = explode(';',$item->content);
  695. $item->bid = isset($content[2]) ? $content[2] : '';
  696. $item->cid = isset($content[3]) ? $content[3] : '';
  697. if ($item->cid){
  698. $item->redirect_url = "views/Reader";
  699. }else{
  700. $item->redirect_url = "views/Detail";
  701. }
  702. $item->redirect_type = "book";
  703. }else{
  704. $activity = ActivityService::getById($item->activity_id);
  705. $item->redirect_url = "/views/Activity";
  706. $item->redirect_type = "activity";
  707. if($activity && !empty($activity['token'])){
  708. $item['param'] = ['token' => $activity['token']];
  709. }else{
  710. $item->redirect_url = "#";
  711. }
  712. }
  713. self::getBackFormat($item);
  714. return $item;
  715. });
  716. return response()->success($banner);
  717. }
  718. static function getBackFormat($data)
  719. {
  720. if(isset($data['id']) || $data->id) unset($data['id'],$data->id);
  721. if(isset($data['type']) || $data->type) unset($data['type'],$data->type);
  722. if(isset($data['person']) || $data->person) unset($data['person'],$data->person);
  723. if(isset($data['frequency']) || $data->frequency) unset($data['frequency'],$data->frequency);
  724. return $data;
  725. }
  726. /**
  727. * 默认获取banner
  728. * @return mixed
  729. */
  730. protected function getDefaultBanner( $reco_banner_type = ['FEMALE', 'PUBLIC'])
  731. {
  732. $banner = (new RecoBannerService)->getByType($reco_banner_type, 2);
  733. $banner->transform(function ($item) {
  734. $result = $this->getBidCidFromUrl($item->redirect_url);
  735. $item->bid = $result['bid'];
  736. $item->cid = $result['cid'];
  737. $item->ids = Hashids::encode($item->id);
  738. $item->type = 'default';
  739. if ($result['cid']) {
  740. $item->redirect_url = "views/Reader";
  741. } else {
  742. $item->redirect_url = "views/Detail";
  743. }
  744. $item->redirect_type = "book";
  745. unset($item->id);
  746. return $item;
  747. });
  748. return $banner;
  749. }
  750. /**
  751. * 限免
  752. */
  753. public function free(int $sex)
  754. {
  755. $result = BookConfigService::findFreeBooks($sex);
  756. return response()->success($result);
  757. }
  758. public function yueaiBackRecom(Request $request)
  759. {
  760. $package = $request->header('x-package', '');
  761. if (empty($package) || $package != 'com.beidao.kuaiying.yueai') {
  762. return response()->success([]);
  763. }
  764. $user = (new QappUserService)->getGolableUser();
  765. if(isset($user->uid) && !empty($user->uid) && $user->send_order_id > 0){
  766. $bid = BookConfigService::getAvailableBIdsbyBids([58238,60534,63220,14500,13254,63221,63548,14022,59334,58888,63417,61701],$this->distribution_channel_id,false);
  767. if (!empty($bid)){
  768. $bid = array_random($bid,4);
  769. }
  770. $orderRecord = ChapterOrderService::hasUserRecord($user->uid);
  771. if($orderRecord && count($bid) >= 4){
  772. $where = ['is_on_shelf' => [1,2],'bids' => $bid];
  773. // $books = BookConfigService::getBooksByIds($bid,[],false);
  774. $books = BookConfigService::getBookLists($where,[],false);
  775. return response()->collection(new BookTransformer(), $books);
  776. }
  777. }
  778. return response()->success([]);
  779. $where = ['is_on_shelf' => [2]];
  780. $where['channel_id'] = $request->input('distribution_channel_id',0);
  781. $books = BookConfigService::getBooks($where, [], 4);
  782. return response()->collection(new BookTransformer(), $books);
  783. }
  784. public function shelfRecom(Request $request)
  785. {
  786. $package = $request->header('x-package', '');
  787. $user = (new QappUserService)->getGolableUser();
  788. if (!empty($package) && $package == 'com.beidao.kuaiying.yueai') {
  789. if(isset($user->uid) && !empty($user->uid) && $user->send_order_id > 0){
  790. $bid = BookConfigService::getAvailableBIdsbyBids([58238,60534,63220,14500,13254,63221,63548,14022,59334,58888,63417,61701],$this->distribution_channel_id,false);
  791. if (!empty($bid)){
  792. $bid = array_random($bid,4);
  793. }
  794. $orderRecord = ChapterOrderService::hasUserRecord($user->uid);
  795. if($orderRecord && count($bid) > 1){
  796. $where = ['is_on_shelf' => [1,2],'bids' => $bid];
  797. // $books = BookConfigService::getBooksByIds($bid,[],false);
  798. $books = BookConfigService::getBookLists($where,[],false);
  799. return response()->collection(new BookTransformer(), $books);
  800. }
  801. }
  802. return response()->success([]);
  803. }else if(!empty($package) && $package == 'com.beidao.kuaiying.tiantiankanshu'){
  804. if (isset($user->uid) && !empty($user->uid) && $user->send_order_id > 0){
  805. $bid = [ 61618,14555,60680,58919,58889,58925];
  806. $list = BookConfigService::getBooksByIds($bid,[],false);
  807. return response()->collection(new BookTransformer(), $list);
  808. }
  809. }
  810. $where = ['is_on_shelf' => [2],'channel_id' => $this->distribution_channel_id,'is_high_quality' => 1];
  811. if (isset($user->uid) && $user->user->sex == 1){
  812. $where['channel_name'] = "男频";
  813. }else{
  814. $where['channel_name'] = "女频";
  815. }
  816. $books = BookConfigService::getBooks($where, [], 3);
  817. return response()->collection(new BookTransformer(), $books);
  818. }
  819. /**
  820. * 新推荐书单
  821. * @param Request $request
  822. * @return mixed
  823. */
  824. public function recommendBooks(Request $request)
  825. {
  826. $distribution_id = $this->distribution_channel_id;
  827. $bid = $request->get('bid',0);
  828. if(empty($distribution_id)){
  829. \Log::info('recommendBooks:1');
  830. return response()->success([]);
  831. }
  832. if(!empty($bid)){
  833. \Log::info('recommendBooks:2');
  834. $bid = str_decode($bid);
  835. }
  836. //判断包是否存在
  837. $package_info = QappPackage::getPackage($distribution_id);
  838. if(empty($package_info) || !isset($package_info->channel_id)){
  839. \Log::info('recommendBooks:3');
  840. return response()->success([]);
  841. }
  842. //包对应有没有配置开启推荐书单
  843. $config = ChannelRecommendBookConfigService::getRecommendConfigs($distribution_id);
  844. if(empty($config) || !isset($config->status) || $config->status == 0){
  845. \Log::info('recommendBooks:4');
  846. return response()->success([]);
  847. }
  848. //根据频率和用户属性决定是否需要返回
  849. $res = $this->checkUsersAuth($config);
  850. if(!$res){
  851. \Log::info('recommendBooks:5');
  852. return response()->success([]);
  853. }
  854. $list = ChannelRecommendBooksService::getRecommendBooks($distribution_id,$bid);
  855. if(!is_empty($list)){
  856. foreach($list as $key => $item){
  857. $this->incrRecommendNum($distribution_id,$item->bid);
  858. }
  859. \Log::info('recommendBooks:6');
  860. return response()->collection(new BookTransformer(), $list);
  861. }
  862. \Log::info('recommendBooks:7'.json_encode($list));
  863. return response()->success([]);
  864. }
  865. /**
  866. * 点击推荐书籍记录点击次数
  867. * @param Request $request
  868. * @return mixed
  869. */
  870. public function clickRecommendBooks(Request $request)
  871. {
  872. $distribution_id = $this->distribution_channel_id;
  873. $bid = $request->get('bid','');
  874. if(empty($bid)){
  875. return response()->success();
  876. }
  877. //判断包是否存在
  878. $package_info = QappPackage::getPackage($distribution_id);
  879. if(empty($package_info) || !isset($package_info->channel_id)){
  880. return response()->success([]);
  881. }
  882. $date = date('Ymd');
  883. $bid = str_decode($bid);
  884. $cacheKey = 'recommend:click:'.$date.':'.$distribution_id.$bid;
  885. $this->incrRedisKey($cacheKey);
  886. return response()->success();
  887. }
  888. /**
  889. * 判断用户是否需要推荐
  890. * @param $config
  891. * @return bool
  892. */
  893. protected function checkUsersAuth($config)
  894. {
  895. \Log::info($config);
  896. \Log::info('$config->person:'.$config->person);
  897. \Log::info($this->uid);
  898. $res = $this->checkUsers($config->person);
  899. if($res === false){
  900. return false;
  901. }
  902. //频率判断
  903. if($config->frequency == 'back'){
  904. //返回即推送
  905. return true;
  906. }
  907. $day = strtotime(date('Y-m-d H:i:s'));
  908. $nextDay = strtotime( date('Y-m-d'). ' +1 day');
  909. $nextWeek = strtotime(date('Y-m-d',strtotime('+1 week last monday')));
  910. if($config->frequency == 'day'){
  911. //每日推送
  912. if(!Redis::exists('recommend:'.$config->channel_id.':'.$this->uid)){
  913. Redis::setex('recommend:'.$config->channel_id.':'.$this->uid,($nextDay-$day),1);
  914. return true;
  915. }
  916. \Log::info('当天已经推送过了');
  917. return false;
  918. }
  919. if($config->frequency == 'week'){
  920. //每周推送
  921. if(!Redis::exists('recommend:'.$config->channel_id.':'.$this->uid)){
  922. Redis::setex('recommend:'.$config->channel_id.':'.$this->uid,($nextWeek-$day),1);
  923. return true;
  924. }
  925. \Log::info('该周已经推送过了');
  926. return false;
  927. }
  928. }
  929. /**
  930. * 判断用户是否有权限
  931. * @param $type
  932. * @return bool
  933. */
  934. protected function checkUsers($type)
  935. {
  936. $res = false;
  937. switch($type)
  938. {
  939. case 'pay':
  940. //付费用户
  941. $orders = OrderService::getUserLastestOrder($this->uid);
  942. if($orders){
  943. $res = true;
  944. }
  945. \Log::info('用户是否付费:');
  946. \Log::info($orders);
  947. break;
  948. case 'send_order':
  949. //派单用户
  950. if($this->send_order_id != 0){
  951. $res = true;
  952. }
  953. \Log::info('用户是否是派单用户:$this->send_order_id:'.$this->send_order_id);
  954. break;
  955. case 'pay_send_order':
  956. //付费派单用户
  957. $orders = OrderService::getUserLastestOrder($this->uid);
  958. if($orders && $this->send_order_id != 0){
  959. $res = true;
  960. }
  961. \Log::info('用户是否是付费派单用户:$this->send_order_id:'.$this->send_order_id);
  962. \Log::info($orders);
  963. break;
  964. case 'unpaid':
  965. $orders = OrderService::getUserLastestOrder($this->uid);
  966. if(!$orders){
  967. $res = true;
  968. }
  969. break;
  970. case 'all':
  971. $res = true;
  972. \Log::info('所有用户返回');
  973. break;
  974. }
  975. return $res;
  976. }
  977. /**
  978. * 推广书籍推荐次数
  979. * @param $channel_id
  980. * @param $bid
  981. */
  982. protected function incrRecommendNum($channel_id,$bid)
  983. {
  984. $date = date('Ymd');
  985. $cacheKey = 'recommend:sum:'.$date.':'.$channel_id.$bid;
  986. $this->incrRedisKey($cacheKey);
  987. }
  988. /**
  989. * 点击广告统计
  990. * @param Request $request
  991. * @return mixed
  992. */
  993. public function getCheckAdvertisement(Request $request)
  994. {
  995. $type = $request->get('type','');
  996. $id = $request->get('ids','');
  997. $distribution_id = $this->distribution_channel_id;
  998. \Log::info('getCheckAdvertisement:type:'.$type.' id:'.$id.' $distribution_id:'.$distribution_id);
  999. if(empty($id) || empty($distribution_id)){
  1000. return response()->success();
  1001. }
  1002. $id = str_decode($id);
  1003. if(!$id){
  1004. return response()->success();
  1005. }
  1006. if($type == 'default'){
  1007. //默认原表reco_banners广告,区分跟新表channel_advert的id
  1008. $id = -$id;
  1009. }else{
  1010. $advert = ChannelAdvert::find($id);
  1011. if(!$advert){
  1012. \Log::info('getCheckAdvertisement:error:不存在该广告');
  1013. \Log::info(json_encode($request->all()));
  1014. return response()->success();
  1015. }
  1016. }
  1017. $where = ['channel_ad_id' => $id, 'uid' => $this->uid, 'distribution_id' => $distribution_id];
  1018. \Log::info('insert_where:');
  1019. \Log::info(json_encode($where));
  1020. ChannelAdUser::firstOrCreate($where);
  1021. $date = date('Ymd');
  1022. $cacheKey = 'advertisement:pv:'.$date.':'.$distribution_id.$id;
  1023. $this->incrRedisKey($cacheKey);
  1024. //设置当前用户的所属广告有效期一周
  1025. $key = 'advertisement:uid:'.$this->uid.':id';
  1026. Redis::set($key,$id);
  1027. Redis::expire($key,BaseConst::ONE_WEEK_SECONDS);
  1028. return response()->success();
  1029. }
  1030. /**
  1031. * redis新增
  1032. * @param $cacheKey
  1033. */
  1034. protected function incrRedisKey($cacheKey)
  1035. {
  1036. if(!Redis::exists($cacheKey)){
  1037. Redis::set($cacheKey,1);
  1038. }else{
  1039. Redis::incrBy($cacheKey, 1);
  1040. }
  1041. }
  1042. /**
  1043. * 绑定广告和订单
  1044. * @param Request $request
  1045. * @return mixed
  1046. */
  1047. public function getAdvertOrders(Request $request)
  1048. {
  1049. $order_id = $request->get('trade_no','');
  1050. $uid = $this->uid;
  1051. $distribution_id = $this->distribution_channel_id;
  1052. $cacheKey = 'advertisement:uid:'.$uid.':id';
  1053. if(empty($order_id)){
  1054. return response()->success();
  1055. }
  1056. $channel_ad_id = 0;
  1057. \Log::info('getAdvertOrders:trade_no'.$order_id.' uid:'.$uid);
  1058. if(Redis::exists($cacheKey)){
  1059. try {
  1060. //获取广告id
  1061. $channel_ad_id = Redis::get($cacheKey);
  1062. //判断订单跟广告是否有关系
  1063. $order = OrderService::getByTradeNo($order_id);
  1064. if(!$order){
  1065. return response()->success();
  1066. }
  1067. if($channel_ad_id > 0){
  1068. //新版
  1069. $type = 1;
  1070. $advert = ChannelAdvert::find($channel_ad_id);
  1071. if(!$advert){
  1072. return response()->success();
  1073. }
  1074. if($advert->type == '2' && $advert->activity_id != 0){
  1075. //广告类型为活动
  1076. if($order->activity_id != $advert->activity_id){
  1077. return response()->success();
  1078. }
  1079. }else{
  1080. //广告类型为书籍
  1081. $content = explode(';',$advert->content);
  1082. if(!isset($content[2])){
  1083. return response()->success();
  1084. }
  1085. if($order->from_bid == 0 || $order->from_bid != str_decode($content[2])){
  1086. return response()->success();
  1087. }
  1088. }
  1089. }else{
  1090. //旧版
  1091. $type = 0;
  1092. $advert = RecoBanner::find(abs($channel_ad_id));
  1093. if(!$advert){
  1094. return response()->success();
  1095. }
  1096. $result = $this->getBidCidFromUrl($advert->redirect_url);
  1097. $bid = $result['bid'];
  1098. $cid = $result['cid'];
  1099. if($cid){
  1100. if($order->from_bid == 0 || $order->from_bid != str_decode($bid)){
  1101. return response()->success();
  1102. }
  1103. }else{
  1104. if($order->activity_id == 0 || $order->from_bid != str_decode($bid)){
  1105. return response()->success();
  1106. }
  1107. }
  1108. }
  1109. $created_at = $updated_at = date('Y-m-d H:i:s');
  1110. DB::table('channel_advert_orders')->insert(compact('order_id','uid','distribution_id','channel_ad_id','type','created_at','updated_at'));
  1111. } catch (\Exception $e) {
  1112. \Log::info('绑定广告和订单失败'.$e->getMessage());
  1113. \Log::info('order_id:'.$order_id.' uid:'.$uid.' distribution_id:'.$distribution_id.' channel_ad_id:'.$channel_ad_id);
  1114. }
  1115. }
  1116. return response()->success();
  1117. }
  1118. private function getSpecialLibrary(Request $request)
  1119. {
  1120. $where = [];
  1121. $order = [];
  1122. $where['is_on_shelf'] = [2,4];
  1123. $category_id = $request->input('category_id');
  1124. if ($category_id) {
  1125. if ($category_id == 1) {
  1126. $where['channel_name'] = '男频';
  1127. } elseif ($category_id == 2) {
  1128. $where['channel_name'] = '女频';
  1129. } else {
  1130. $where['category_id'] = $category_id;
  1131. }
  1132. }
  1133. $key = $request->input('key');
  1134. $uid = $request->input('uid', 0);
  1135. if ($key && $uid && is_numeric($uid)) {
  1136. BookConfigService::saveUserSearchLog($key, $uid);
  1137. }
  1138. $where['key'] = $key;
  1139. $order_field = $request->input('order_field');
  1140. $order_seq = $request->input('order_seq');
  1141. if ($order_field != '' && in_array($order_field, ['recommend_index', 'click_count', 'update', 'size', 'create'])) {
  1142. if ($order_field == 'update') {
  1143. $order = ['book_configs.updated_at', 'desc'];
  1144. } elseif ($order_field == 'create') {
  1145. $order = ['book_configs.created_at', 'desc'];
  1146. } else {
  1147. $order = [$order_field, 'desc'];
  1148. }
  1149. if ($order_seq == 'asc') {
  1150. $order = [$order_field, 'asc'];
  1151. }
  1152. if ($order_seq == 'desc') {
  1153. $order = [$order_field, 'desc'];
  1154. }
  1155. }
  1156. // 审核状态默认值
  1157. $package = $request->header('x-package', '');
  1158. $brand = $request->header('x-nbrand', '');
  1159. $codeVersion = $request->header('x-codeversion', '');
  1160. if ($order_field === 'recommend_index' && Utils::checkIsAudit($package, $brand, $codeVersion)) {
  1161. $order = ['book_configs.bid', 'desc'];
  1162. }
  1163. $status = $request->input('status');
  1164. if ($status != '') {
  1165. $where['status'] = $status;
  1166. }
  1167. // 搜索关键词的情况下,屏蔽书籍完本状态
  1168. if ($key && isset($where['status'])) {
  1169. unset($where['status']);
  1170. }
  1171. $page_size = $request->input('page_size', 15);
  1172. $where['channel_id'] = ($package === 'com.beidao.kuaiying.zsy') ? 7477 : 0;
  1173. $books = BookConfigService::getBooks($where, $order, $page_size);
  1174. $special = get_special_bid();
  1175. $list = [];
  1176. foreach ($books as $ke => $val){
  1177. if ($val->is_on_shelf != 2 && !in_array($val->bid,$special)){
  1178. unset($books[$ke]);
  1179. }
  1180. }
  1181. return response()->pagination(new BookTransformer,$books);
  1182. }
  1183. /**
  1184. * 首页
  1185. */
  1186. public function getBookListsNew(Request $request, $sex)
  1187. {
  1188. // 获取基本数据
  1189. $package = $request->header('x-package', '');
  1190. $brand = $request->header('x-nbrand', '');
  1191. $codeVersion = $request->header('x-codeversion', '');
  1192. $result = HomeCache::getHomePageInfo($package,$sex."_new");
  1193. if(is_empty($result)){
  1194. $isAuth = check_qapp_auth($package ,0);
  1195. //新判断: 根据包名来获取对应所需的bid
  1196. $qapp_package = QappPackage::getPackageByPackage($package);
  1197. if($qapp_package){
  1198. $package_id = $qapp_package->id;
  1199. }else{
  1200. $package_id = 0;
  1201. }
  1202. $channel = $sex == 'male' ? 1 : 2;
  1203. $result = $this->getRecommendBooks($package_id,$package,$channel, $isAuth);
  1204. HomeCache::setHomePageInfo($package,$sex."_new",$result,86400+rand(0,100));
  1205. }
  1206. return response()->success($result);
  1207. }
  1208. private function getRecommendBooks($package_id,$package, $channel, $is_auth)
  1209. {
  1210. $data = BookConfigService::getRecommendBids($package,$channel,[],48);
  1211. $bids = array_chunk($data,12);
  1212. $guss_like = BookConfigService::getRecommendBids($package,$channel,$data,20);
  1213. // $new_recom = BookConfigService::getNewRecommendBids($package,$channel,$data);
  1214. $default = [1,1,1,1,1,1,1,1,1,1,1,1];
  1215. return array_filter([
  1216. ["has_more"=>false,'type' => 'hot', 'lable' => '热门书单', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($bids[0] ?? BookConfigService::getCheckBooks($default,$channel,$package,$is_auth)))],
  1217. ["has_more"=>false,'type' => 'zhibo', 'lable' => '神书直播', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($bids[2] ?? BookConfigService::getCheckBooks($default,$channel,$package,$is_auth)))],
  1218. ["has_more"=>false,'type' => 'recom', 'lable' => '小编精选', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($bids[3] ?? BookConfigService::getCheckBooks($default,$channel,$package,$is_auth)))],
  1219. ["has_more"=>false,'type' => 'new_recom', 'lable' => '人气新书', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($bids[4] ?? BookConfigService::getCheckBooks($default,$channel,$package,$is_auth)))],
  1220. ["has_more"=>false,'type' => 'guss_like', 'lable' => '猜你喜欢', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($guss_like))],
  1221. ]);
  1222. return [];
  1223. }
  1224. }