BookController.php 53 KB

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