BookController.php 56 KB

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