BookController.php 41 KB

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