BookController.php 55 KB

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