BookController.php 36 KB

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