BookController.php 56 KB

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