BookController.php 53 KB

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