BookController.php 55 KB

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