BookController.php 42 KB

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