BookController.php 45 KB

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