BookController.php 57 KB

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