BookController.php 53 KB

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