BookController.php 42 KB

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