BookController.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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. if($orderRecord){
  512. $data = config('home.yueai');
  513. $books = $data['task_banner'];
  514. foreach ($books as &$value){
  515. $value['bid'] =Hashids::encode($value['bid']);
  516. $value['redirect_url '] = empty($value['cid']) ? "views/Detail" : "views/Reader";
  517. }
  518. unset($value);
  519. return response()->success($books);
  520. }
  521. }
  522. $books = $this->getDefaultBanner();
  523. return response()->success($books);
  524. }
  525. /**
  526. * 新获取各种广告列表
  527. * @param Request $request
  528. * @return mixed
  529. */
  530. public function getRecommendBanners(Request $request)
  531. {
  532. $release_type = $request->get('release_type','');
  533. $distribution_id = $this->distribution_channel_id;
  534. if(empty($release_type)){
  535. //默认原banner
  536. $banner = $this->getDefaultBanner();
  537. return response()->success($banner);
  538. }
  539. if($release_type == '4' || $release_type == '5'){
  540. //弹窗和充值页返回需要先判断频率跟权限
  541. $advert = ChannelAdvert::select('id','photo as banner_url','type','content','person','frequency')
  542. ->where('distribution_id',$distribution_id)
  543. ->where('release_type',$release_type)
  544. ->where('status',1)
  545. ->first();
  546. if(!$advert){
  547. return response()->success([]);
  548. }
  549. $advert = $advert->toArray();
  550. $advert['ids'] = Hashids::encode($advert['id']);
  551. if($advert['type'] == 1){
  552. $advert['redirect_url'] = 'views/Reader';
  553. $content = explode(';',$advert['content']);
  554. $advert['bid'] = isset($content[2]) ? $content[2] : '';
  555. $advert['cid'] = isset($content[3]) ? $content[3] : '';
  556. }else{
  557. $advert['redirect_url'] = 'views/Detail';
  558. }
  559. if($release_type == '4'){
  560. //弹窗需要判断频率
  561. if($advert['frequency'] == 'always'){
  562. $advert = self::getBackFormat($advert);
  563. return response()->success($advert);
  564. }
  565. $day = strtotime(date('Y-m-d H:i:s'));
  566. $nextDay = strtotime( date('Y-m-d'). ' +1 day');
  567. $nextWeek = strtotime(date('Y-m-d',strtotime('+1 week last monday')));
  568. if($advert['frequency'] == 'day'){
  569. if(!Redis::exists('banner:'.$distribution_id.':'.$this->uid)){
  570. Redis::setex('banner:'.$distribution_id.':'.$this->uid,($nextDay-$day),1);
  571. $advert = self::getBackFormat($advert);
  572. return response()->success($advert);
  573. }
  574. }
  575. if($advert['frequency'] == 'week'){
  576. if(!Redis::exists('banner:'.$distribution_id.':'.$this->uid)){
  577. Redis::setex('banner:'.$distribution_id.':'.$this->uid,($nextWeek-$day),1);
  578. $advert = self::getBackFormat($advert);
  579. return response()->success($advert);
  580. }
  581. }
  582. return response()->success([]);
  583. }
  584. if($release_type == '5'){
  585. //充值页返回需要判断用户权限
  586. $check_user = $this->checkUsers($advert['person']);
  587. if(!$check_user){
  588. return response()->success([]);
  589. }
  590. $activity = ActivityService::getById($advert['activity_id'] ?? 0);
  591. if ($activity){
  592. $advert = self::getBackFormat($advert);
  593. return response()->success($advert);
  594. }else{
  595. return response()->success([]);
  596. }
  597. }
  598. }
  599. //男女频,书架列表
  600. $banner = ChannelAdvertService::getAdvertList($distribution_id,$release_type);
  601. if($banner->isEmpty()){
  602. $banner = $this->getDefaultBanner();
  603. return response()->success($banner);
  604. }
  605. $banner->transform(function ($item) {
  606. $item->ids = Hashids::encode($item->id);
  607. if($item->type == 1){
  608. $content = explode(';',$item->content);
  609. $item->bid = isset($content[2]) ? $content[2] : '';
  610. $item->cid = isset($content[3]) ? $content[3] : '';
  611. if ($item->cid){
  612. $item->redirect_url = "views/Reader";
  613. }else{
  614. $item->redirect_url = "views/Detail";
  615. }
  616. $item->redirect_type = "book";
  617. }else{
  618. $activity = ActivityService::getById($item->activity_id);
  619. $item->redirect_url = "/views/Activity";
  620. $item->redirect_type = "activity";
  621. if($activity && !empty($activity['token'])){
  622. $item['param'] = ['token' => $activity['token']];
  623. }else{
  624. $item->redirect_url = "#";
  625. }
  626. }
  627. self::getBackFormat($item);
  628. return $item;
  629. });
  630. return response()->success($banner);
  631. }
  632. static function getBackFormat($data)
  633. {
  634. if(isset($data['id']) || $data->id) unset($data['id'],$data->id);
  635. if(isset($data['type']) || $data->type) unset($data['type'],$data->type);
  636. if(isset($data['person']) || $data->person) unset($data['person'],$data->person);
  637. if(isset($data['frequency']) || $data->frequency) unset($data['frequency'],$data->frequency);
  638. return $data;
  639. }
  640. /**
  641. * 默认获取banner
  642. * @return mixed
  643. */
  644. protected function getDefaultBanner()
  645. {
  646. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  647. $banner = (new RecoBannerService)->getByType($reco_banner_type, 2);
  648. $banner->transform(function ($item) {
  649. $result = $this->getBidCidFromUrl($item->redirect_url);
  650. $item->bid = $result['bid'];
  651. $item->cid = $result['cid'];
  652. $item->ids = Hashids::encode($item->id);
  653. $item->type = 'default';
  654. if ($result['cid']) {
  655. $item->redirect_url = "views/Reader";
  656. } else {
  657. $item->redirect_url = "views/Detail";
  658. }
  659. $item->redirect_type = "book";
  660. unset($item->id);
  661. return $item;
  662. });
  663. return $banner;
  664. }
  665. /**
  666. * 限免
  667. */
  668. public function free(int $sex)
  669. {
  670. $result = BookConfigService::findFreeBooks($sex);
  671. return response()->success($result);
  672. }
  673. public function yueaiBackRecom(Request $request)
  674. {
  675. $package = $request->header('x-package', '');
  676. if (empty($package) || $package != 'com.beidao.kuaiying.yueai') {
  677. return response()->success([]);
  678. }
  679. $user = (new QappUserService)->getGolableUser();
  680. if(isset($user->uid) && !empty($user->uid) && $user->send_order_id > 0){
  681. $orderRecord = ChapterOrderService::hasUserRecord($user->uid);
  682. if($orderRecord){
  683. $bid = [58238,60534,63220,14500,13254,63221,63548,14022,59334,58888,63417,61701];
  684. $bid = array_random($bid,4);
  685. $books = BookConfigService::getBooksByIds($bid,[],false);
  686. return response()->collection(new BookTransformer(), $books);
  687. }
  688. }
  689. return response()->success([]);
  690. $where = ['is_on_shelf' => [2]];
  691. $where['channel_id'] = ($package === 'com.beidao.kuaiying.zsy') ? 7477 : 0;
  692. $books = BookConfigService::getBooks($where, [], 4);
  693. return response()->collection(new BookTransformer(), $books);
  694. }
  695. public function shelfRecom(Request $request)
  696. {
  697. $category_id = $request->input('category_id');
  698. $bid = $request->input('bid');
  699. $package = $request->header('x-package', '');
  700. if (empty($package) || $package != 'com.beidao.kuaiying.yueai') {
  701. return response()->success([]);
  702. }
  703. $user = (new QappUserService)->getGolableUser();
  704. if(isset($user->uid) && !empty($user->uid) && $user->send_order_id > 0){
  705. $orderRecord = ChapterOrderService::hasUserRecord($user->uid);
  706. if($orderRecord){
  707. $bid = [58238,60534,63220,14500,13254,63221,63548,14022,59334,58888,63417,61701];
  708. $bid = array_random($bid,4);
  709. $books = BookConfigService::getBooksByIds($bid,[],false);
  710. return response()->collection(new BookTransformer(), $books);
  711. }
  712. }
  713. return response()->success([]);
  714. $where = ['is_on_shelf' => [2]];
  715. $where['channel_id'] = ($package === 'com.beidao.kuaiying.zsy') ? 7477 : 0;
  716. $books = BookConfigService::getBooks($where, [], 4);
  717. return response()->collection(new BookTransformer(), $books);
  718. }
  719. /**
  720. * 新推荐书单
  721. * @param Request $request
  722. * @return mixed
  723. */
  724. public function recommendBooks(Request $request)
  725. {
  726. $distribution_id = $this->distribution_channel_id;
  727. $bid = $request->get('bid',0);
  728. if(empty($distribution_id)){
  729. \Log::info('recommendBooks:1');
  730. return response()->success([]);
  731. }
  732. if(!empty($bid)){
  733. \Log::info('recommendBooks:2');
  734. $bid = Hashids::decode($bid)[0];
  735. }
  736. //判断包是否存在
  737. $package_info = QappPackage::getPackage($distribution_id);
  738. if(empty($package_info) || !isset($package_info->channel_id)){
  739. \Log::info('recommendBooks:3');
  740. return response()->success([]);
  741. }
  742. //包对应有没有配置开启推荐书单
  743. $config = ChannelRecommendBookConfigService::getRecommendConfigs($distribution_id);
  744. if(empty($config) || !isset($config->status) || $config->status == 0){
  745. \Log::info('recommendBooks:4');
  746. return response()->success([]);
  747. }
  748. //根据频率和用户属性决定是否需要返回
  749. $res = $this->checkUsersAuth($config);
  750. if(!$res){
  751. \Log::info('recommendBooks:5');
  752. return response()->success([]);
  753. }
  754. $list = ChannelRecommendBooksService::getRecommendBooks($distribution_id,$bid);
  755. if(!$list->isEmpty()){
  756. foreach($list as $key => $item){
  757. $this->incrRecommendNum($distribution_id,$item->bid);
  758. }
  759. \Log::info('recommendBooks:6');
  760. return response()->collection(new BookTransformer(), $list);
  761. }
  762. \Log::info('recommendBooks:7');
  763. return response()->success([]);
  764. }
  765. /**
  766. * 点击推荐书籍记录点击次数
  767. * @param Request $request
  768. * @return mixed
  769. */
  770. public function clickRecommendBooks(Request $request)
  771. {
  772. $distribution_id = $this->distribution_channel_id;
  773. $bid = $request->get('bid','');
  774. if(empty($bid)){
  775. return response()->success();
  776. }
  777. //判断包是否存在
  778. $package_info = QappPackage::getPackage($distribution_id);
  779. if(empty($package_info) || !isset($package_info->channel_id)){
  780. return response()->success([]);
  781. }
  782. $date = date('Ymd');
  783. $bid = Hashids::decode($bid)[0];
  784. $cacheKey = 'recommend:click:'.$date.':'.$distribution_id.$bid;
  785. $this->incrRedisKey($cacheKey);
  786. return response()->success();
  787. }
  788. /**
  789. * 判断用户是否需要推荐
  790. * @param $config
  791. * @return bool
  792. */
  793. protected function checkUsersAuth($config)
  794. {
  795. \Log::info($config);
  796. \Log::info('$config->person:'.$config->person);
  797. \Log::info($this->uid);
  798. $res = $this->checkUsers($config->person);
  799. if($res === false){
  800. return false;
  801. }
  802. //频率判断
  803. if($config->frequency == 'back'){
  804. //返回即推送
  805. return true;
  806. }
  807. $day = strtotime(date('Y-m-d H:i:s'));
  808. $nextDay = strtotime( date('Y-m-d'). ' +1 day');
  809. $nextWeek = strtotime(date('Y-m-d',strtotime('+1 week last monday')));
  810. if($config->frequency == 'day'){
  811. //每日推送
  812. if(!Redis::exists('recommend:'.$config->channel_id.':'.$this->uid)){
  813. Redis::setex('recommend:'.$config->channel_id.':'.$this->uid,($nextDay-$day),1);
  814. return true;
  815. }
  816. \Log::info('当天已经推送过了');
  817. return false;
  818. }
  819. if($config->frequency == 'week'){
  820. //每周推送
  821. if(!Redis::exists('recommend:'.$config->channel_id.':'.$this->uid)){
  822. Redis::setex('recommend:'.$config->channel_id.':'.$this->uid,($nextWeek-$day),1);
  823. return true;
  824. }
  825. \Log::info('该周已经推送过了');
  826. return false;
  827. }
  828. }
  829. /**
  830. * 判断用户是否有权限
  831. * @param $type
  832. * @return bool
  833. */
  834. protected function checkUsers($type)
  835. {
  836. $res = false;
  837. switch($type)
  838. {
  839. case 'pay':
  840. //付费用户
  841. $orders = OrderService::getUserLastestOrder($this->uid);
  842. if($orders){
  843. $res = true;
  844. }
  845. \Log::info('用户是否付费:');
  846. \Log::info($orders);
  847. break;
  848. case 'send_order':
  849. //派单用户
  850. if($this->send_order_id != 0){
  851. $res = true;
  852. }
  853. \Log::info('用户是否是派单用户:$this->send_order_id:'.$this->send_order_id);
  854. break;
  855. case 'pay_send_order':
  856. //付费派单用户
  857. $orders = OrderService::getUserLastestOrder($this->uid);
  858. if($orders && $this->send_order_id != 0){
  859. $res = true;
  860. }
  861. \Log::info('用户是否是付费派单用户:$this->send_order_id:'.$this->send_order_id);
  862. \Log::info($orders);
  863. break;
  864. case 'unpaid':
  865. $orders = OrderService::getUserLastestOrder($this->uid);
  866. if(!$orders){
  867. $res = true;
  868. }
  869. break;
  870. case 'all':
  871. $res = true;
  872. \Log::info('所有用户返回');
  873. break;
  874. }
  875. return $res;
  876. }
  877. /**
  878. * 推广书籍推荐次数
  879. * @param $channel_id
  880. * @param $bid
  881. */
  882. protected function incrRecommendNum($channel_id,$bid)
  883. {
  884. $date = date('Ymd');
  885. $cacheKey = 'recommend:sum:'.$date.':'.$channel_id.$bid;
  886. $this->incrRedisKey($cacheKey);
  887. }
  888. /**
  889. * 点击广告统计
  890. * @param Request $request
  891. * @return mixed
  892. */
  893. public function getCheckAdvertisement(Request $request)
  894. {
  895. $type = $request->get('type','');
  896. $id = $request->get('ids','');
  897. $distribution_id = $this->distribution_channel_id;
  898. if(empty($type) || empty($id) || empty($distribution_id)){
  899. return response()->success();
  900. }
  901. $id = Hashids::decode($id)[0];
  902. if(!$id){
  903. return response()->success();
  904. }
  905. if($type == 'default'){
  906. //默认原表reco_banners广告,区分跟新表channel_advert的id
  907. $id = -$id;
  908. }else{
  909. $advert = ChannelAdvert::find($id);
  910. if(!$advert){
  911. \Log::info('getCheckAdvertisement:error:不存在该广告');
  912. \Log::info(json_encode($request->all()));
  913. return response()->success();
  914. }
  915. }
  916. $where = ['channel_ad_id' => $id, 'uid' => $this->uid, 'distribution_id' => $distribution_id];
  917. ChannelAdUser::firstOrCreate($where);
  918. $date = date('Ymd');
  919. $cacheKey = 'advertisement:pv:'.$date.':'.$distribution_id.$id;
  920. $this->incrRedisKey($cacheKey);
  921. //设置当前用户的所属广告有效期一周
  922. $key = 'advertisement:uid:'.$this->uid.':id';
  923. Redis::set($key,$id);
  924. Redis::expire($key,BaseConst::ONE_WEEK_SECONDS);
  925. return response()->success();
  926. }
  927. /**
  928. * redis新增
  929. * @param $cacheKey
  930. */
  931. protected function incrRedisKey($cacheKey)
  932. {
  933. if(!Redis::exists($cacheKey)){
  934. Redis::set($cacheKey,1);
  935. }else{
  936. Redis::incrBy($cacheKey, 1);
  937. }
  938. }
  939. /**
  940. * 绑定广告和订单
  941. * @param Request $request
  942. * @return mixed
  943. */
  944. public function getAdvertOrders(Request $request)
  945. {
  946. $trade_no = $request->get('trade_no','');
  947. $uid = $this->uid;
  948. $distribution_id = $this->distribution_channel_id;
  949. $cacheKey = 'advertisement:uid:'.$uid.':id';
  950. if(empty($trade_no)){
  951. return response()->success();
  952. }
  953. $channel_ad_id = 0;
  954. if(Redis::exists($cacheKey)){
  955. try {
  956. //获取广告id
  957. $channel_ad_id = Redis::get($cacheKey);
  958. //判断订单跟广告是否有关系
  959. $order = OrderService::getByTradeNo($trade_no);
  960. if(!$order){
  961. return response()->success();
  962. }
  963. if($channel_ad_id > 0){
  964. //新版
  965. $type = 1;
  966. $advert = ChannelAdvert::find($channel_ad_id);
  967. if(!$advert){
  968. return response()->success();
  969. }
  970. if($advert->type == '2' && $advert->activity_id != 0){
  971. //广告类型为活动
  972. if($order->activity_id != $advert->activity_id){
  973. return response()->success();
  974. }
  975. }else{
  976. //广告类型为书籍
  977. $content = explode(';',$advert->content);
  978. if(!isset($content[2])){
  979. return response()->success();
  980. }
  981. if($order->from_bid == 0 || $order->from_bid != Hashids::decode($content[2])[0]){
  982. return response()->success();
  983. }
  984. }
  985. }else{
  986. //旧版
  987. $type = 0;
  988. $advert = RecoBanner::find(abs($channel_ad_id));
  989. if(!$advert){
  990. return response()->success();
  991. }
  992. $result = $this->getBidCidFromUrl($advert->redirect_url);
  993. $bid = $result['bid'];
  994. $cid = $result['cid'];
  995. if($cid){
  996. if($order->from_bid == 0 || $order->from_bid != Hashids::decode($bid)[0]){
  997. return response()->success();
  998. }
  999. }else{
  1000. if($order->activity_id == 0 || $order->from_bid != Hashids::decode($bid)[0]){
  1001. return response()->success();
  1002. }
  1003. }
  1004. }
  1005. $created_at = $updated_at = date('Y-m-d H:i:s');
  1006. DB::table('channel_advert_orders')->insert(compact('order_id','uid','distribution_id','channel_ad_id','type','created_at','updated_at'));
  1007. } catch (\Exception $e) {
  1008. \Log::info('绑定广告和订单失败'.$e->getMessage());
  1009. \Log::info('order_id:'.$trade_no.' uid:'.$uid.' distribution_id:'.$distribution_id.' channel_ad_id:'.$channel_ad_id);
  1010. }
  1011. }
  1012. return response()->success();
  1013. }
  1014. }