BookController.php 45 KB

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