BookController.php 56 KB

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