BookController.php 56 KB

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