BookController.php 51 KB

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