BookController.php 40 KB

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