BookController.php 32 KB

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