BookController.php 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  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. //
  283. // ->where('book_categories.pid',$channel)
  284. // ->count();
  285. //
  286. // if($count == $book_count){
  287. // return $bid_list;
  288. // }
  289. //获取需要补充的书籍数量
  290. // $supplement_count = (($count - $book_count) > 0) ? $count - $book_count : 0;
  291. // if($supplement_count <= 0){
  292. // return $bid_list;
  293. // }
  294. //获取书籍交集bid,过滤掉不符合要求的书
  295. $bids = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  296. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  297. ->whereIn('book_configs.bid',$bid_list)
  298. ->where('book_configs.is_on_shelf',2)
  299. ->where($where)
  300. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  301. ->where('book_categories.pid',$channel)
  302. ->pluck('book_configs.bid')->all();
  303. // $bid_list = array_intersect($bid_list,$bids);
  304. $book_count = count($bids);
  305. if ($book_count === $count){
  306. return $bid_list;
  307. }
  308. $supplement_count = $count - $book_count;
  309. //获取随机的有效的书籍bid
  310. $rand_bid = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  311. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  312. ->where('book_configs.is_on_shelf',2)
  313. // ->where('book_configs.charge_type','!=','BOOK')
  314. ->where($where)
  315. ->whereNotIn('book_configs.cp_source',$hidden_cp)
  316. ->where('book_categories.pid',$channel)
  317. ->inRandomOrder()
  318. ->limit($supplement_count)
  319. ->get()->pluck('bid')->toArray();
  320. return array_filter(array_merge($bid_list,$rand_bid));
  321. }
  322. private function getBidCidFromUrl(string $url)
  323. {
  324. if (preg_match('/\?bid=(\w+)\S+cid=(\w+)/', $url, $matches) || preg_match('/\?id=(\w+)/', $url, $matches)) {
  325. return [
  326. 'bid' => $matches[1],
  327. 'cid' => isset($matches[2]) ? $matches[2] : 0,
  328. ];
  329. } else {
  330. return [
  331. 'bid' => '',
  332. 'cid' => 0,
  333. ];
  334. }
  335. }
  336. public function library(Request $request)
  337. {
  338. $package = $request->header('x-package', '');
  339. if (in_array($package ,["com.app.kyy.dmzyd","com.app.kyy.tths"])){
  340. // return $this->getSpecialLibrary($request);
  341. }
  342. $where = [];
  343. $order = [];
  344. $where['is_on_shelf'] = [2];
  345. $category_id = $request->input('category_id');
  346. if ($category_id) {
  347. if ($category_id == 1) {
  348. $where['channel_name'] = '男频';
  349. } elseif ($category_id == 2) {
  350. $where['channel_name'] = '女频';
  351. } else {
  352. $where['category_id'] = $category_id;
  353. }
  354. }
  355. $key = $request->input('key');
  356. $uid = $request->input('uid', 0);
  357. if ($key && $uid && is_numeric($uid)) {
  358. BookConfigService::saveUserSearchLog($key, $uid);
  359. }
  360. $where['key'] = $key;
  361. $order_field = $request->input('order_field');
  362. $order_seq = $request->input('order_seq');
  363. if ($order_field != '' && in_array($order_field, ['recommend_index', 'click_count', 'update', 'size', 'create'])) {
  364. if ($order_field == 'update') {
  365. $order = ['book_configs.updated_at', 'desc'];
  366. } elseif ($order_field == 'create') {
  367. $order = ['book_configs.created_at', 'desc'];
  368. } else {
  369. $order = [$order_field, 'desc'];
  370. }
  371. if ($order_seq == 'asc') {
  372. $order = [$order_field, 'asc'];
  373. }
  374. if ($order_seq == 'desc') {
  375. $order = [$order_field, 'desc'];
  376. }
  377. }
  378. // 审核状态默认值
  379. $package = $request->header('x-package', '');
  380. $brand = $request->header('x-nbrand', '');
  381. $codeVersion = $request->header('x-codeversion', '');
  382. if ($order_field === 'recommend_index' && Utils::checkIsAudit($package, $brand, $codeVersion)) {
  383. $order = ['book_configs.bid', 'desc'];
  384. }
  385. // 是否只使用原创书殿的书
  386. $isAuth = check_qapp_auth($package ,0);
  387. if (!$isAuth){
  388. $where['cp_source'] = "ycsd";
  389. }
  390. $status = $request->input('status');
  391. if ($status != '') {
  392. $where['status'] = $status;
  393. }
  394. // 搜索关键词的情况下,屏蔽书籍完本状态
  395. if ($key && isset($where['status'])) {
  396. unset($where['status']);
  397. }
  398. $page_size = $request->input('page_size', 15);
  399. $where['channel_id'] = $request->input('distribution_channel_id',0);
  400. $books = BookConfigService::getBooks($where, $order, $page_size);
  401. return response()->pagination(new BookTransformer, $books);
  402. }
  403. public function hotWords(Request $request)
  404. {
  405. $result = BookConfigService::findBookKeywords();
  406. return response()->pagination(new KeywordTransformer, $result);
  407. }
  408. public function similarRecom(Request $request)
  409. {
  410. $package = $request->header('x-package', '');
  411. $category_id = $request->input('category_id');
  412. $bid = $request->input('bid');
  413. $package = $request->header('x-package', '');
  414. if (empty($bid) || (empty($category_id) && $category_id != 0)) {
  415. return response()->error('PARAM_ERROR');
  416. }
  417. $isAuth = check_qapp_auth($package ,0);
  418. $bid = BookService::decodeBidStatic($bid);
  419. $where = ['category_id' => $category_id, 'is_on_shelf' => [2]];
  420. if (!$isAuth){
  421. $where['cp_source'] = "ycsd";
  422. }
  423. $where['channel_id'] = $request->input('distribution_channel_id',0);
  424. $books = BookConfigService::getBooks($where, [], 4);
  425. $data = [];
  426. foreach ($books as $v) {
  427. if ($v->bid != $bid && count($data) < 3) {
  428. $data[] = $v;
  429. }
  430. }
  431. return response()->collection(new BookTransformer(), $data);
  432. }
  433. public function readOverRecommend(Request $request)
  434. {
  435. $bid = $request->input('bid');
  436. if (empty($bid)) {
  437. return response()->error('PARAM_ERROR');
  438. }
  439. $bid = BookService::decodeBidStatic($bid);
  440. $book_info = BookConfigService::getBookById($bid);
  441. $res = BookConfigService::getRecommendBooks($bid, $book_info->channel_name);
  442. $urge_status = 0;
  443. if ($book_info->status == 0 && !BookUrgeUpdateService::isHadUrged($this->uid, $bid)) {
  444. $urge_status = 1;
  445. }
  446. $recommend_result = collectionTransform(new BookTransformer(), $res);
  447. $book_status = [
  448. 'status' => $book_info->status,
  449. 'urge_status' => $urge_status
  450. ];
  451. $data = [
  452. 'recommend_result' => $recommend_result,
  453. 'book_status' => $book_status
  454. ];
  455. return response()->success($data);
  456. }
  457. public function rank(Request $request)
  458. {
  459. // 1:男频,2:女频
  460. $sex = (int)$request->input('sex');
  461. if (!in_array($sex, [1, 2], true)) {
  462. return response()->error('PARAM_ERROR');
  463. }
  464. // 默认
  465. $bids = [11529, 11941, 12720, 11990, 11988, 11976, 11977, 4183, 12717, 11833,
  466. 7287,14297,12716,14312,14000,13577,16712,13002,12717,15103,13928,14793,
  467. 12708,13286];
  468. if ($sex === 2) {
  469. $bids = [8469, 11660, 9117, 7891, 12281, 12470, 8167, 11661, 11670, 8476, 8557, 11662,
  470. 11680, 11926, 12462, 7836, 11681, 11664, 11928, 8631];
  471. }
  472. /**
  473. * pid:1为男频 2为女频
  474. SELECT
  475. CONCAT( books.id, ',' )
  476. FROM
  477. books
  478. LEFT JOIN book_configs ON books.id = book_configs.bid
  479. WHERE
  480. book_configs.is_on_shelf = 2
  481. AND books.category_id IN ( SELECT id FROM book_categories WHERE pid = 2 )
  482. ORDER BY
  483. book_configs.recommend_index DESC
  484. LIMIT 10;
  485. */
  486. // 根据包名、平台、版本号判断是否审核
  487. $package = $request->header('x-package', '');
  488. $brand = $request->header('x-nbrand', '');
  489. $codeVersion = $request->header('x-codeversion', '');
  490. if (Utils::checkIsAudit($package, $brand, $codeVersion)) {
  491. $bids = [2266, 3838, 9700, 10175, 10301, 3422, 1166, 4546, 9163, 2509,
  492. 7287,14297,12716,14312,14000,13577,16712,13002,12717,15103,13928,
  493. 14793,12708,13286,13336,13275,13073,15121,13929,12693,13254,3526,
  494. 10313,3483,13278,14004,4098,10378,14072,21376,21139,21757,19449];
  495. if ($sex === 2) {
  496. $bids = [159, 2439, 6276, 10074, 5409, 9379, 10323, 9078, 3603, 487];
  497. }
  498. }
  499. $isAuth = check_qapp_auth($package,0);
  500. if (!$isAuth){
  501. $rank = config('home.rank');
  502. $bids = $rank['male'];
  503. if ($sex === 2) {
  504. $bids = $rank['female'];;
  505. }
  506. }
  507. $channel_id = $request->input('distribution_channel_id',0);
  508. $books = collectionTransform(new BookTransformer, BookConfigService::getBookLists(compact('bids','channel_id')));
  509. return response()->success($books);
  510. }
  511. /**
  512. * 推荐书
  513. */
  514. public function recommen()
  515. {
  516. $books = $this->getDefaultBanner();
  517. return response()->success($books);
  518. }
  519. /**
  520. * 阅爱小说任务轮播图
  521. */
  522. public function recommenYueAi()
  523. {
  524. $user = (new QappUserService)->getGolableUser();
  525. if(isset($user->uid) && !empty($user->uid) && $user->send_order_id > 0){
  526. $orderRecord = ChapterOrderService::hasUserRecord($user->uid);
  527. $data = config('home.yueai');
  528. $books = $data['task_banner'];
  529. $bids = BookConfigService::getAvailableBIdsbyBids(array_column($books,'bid'),$this->distribution_channel_id,false);
  530. if($orderRecord && !empty($bids)){
  531. foreach ($books as &$value){
  532. $value['bid'] =Hashids::encode($value['bid']);
  533. $value['redirect_url '] = empty($value['cid']) ? "views/Detail" : "views/Reader";
  534. }
  535. unset($value);
  536. return response()->success($books);
  537. }
  538. }
  539. $books = $this->getDefaultBanner();
  540. return response()->success($books);
  541. }
  542. /**
  543. * 新获取各种广告列表
  544. * @param Request $request
  545. * @return mixed
  546. */
  547. public function getRecommendBanners(Request $request)
  548. {
  549. $release_type = $request->get('release_type','');
  550. $distribution_id = $this->distribution_channel_id;
  551. if(empty($release_type)){
  552. //默认原banner
  553. $banner = $this->getDefaultBanner();
  554. return response()->success($banner);
  555. }
  556. if($release_type == '4' || $release_type == '5'){
  557. //弹窗和充值页返回需要先判断频率跟权限
  558. $advert = ChannelAdvert::select('id','photo as banner_url','activity_id','type','content','person','frequency')
  559. ->where('distribution_id',$distribution_id)
  560. ->where('release_type',$release_type)
  561. ->where('status',1)
  562. ->first();
  563. if(!$advert){
  564. return response()->success([]);
  565. }
  566. $advert = $advert->toArray();
  567. $advert['ids'] = Hashids::encode($advert['id']);
  568. if($advert['type'] == 1){
  569. $advert['redirect_url'] = 'views/Reader';
  570. $content = explode(';',$advert['content']);
  571. $advert['bid'] = isset($content[2]) ? $content[2] : '';
  572. $advert['cid'] = isset($content[3]) ? $content[3] : '';
  573. }else{
  574. $advert['redirect_url'] = 'views/Detail';
  575. }
  576. if($release_type == '4'){
  577. //弹窗需要判断频率
  578. if($advert['frequency'] == 'always'){
  579. $advert = self::getBackFormat($advert);
  580. return response()->success($advert);
  581. }
  582. $day = strtotime(date('Y-m-d H:i:s'));
  583. $nextDay = strtotime( date('Y-m-d'). ' +1 day');
  584. $nextWeek = strtotime(date('Y-m-d',strtotime('+1 week last monday')));
  585. if($advert['frequency'] == 'day'){
  586. if(!Redis::exists('banner:'.$distribution_id.':'.$this->uid)){
  587. Redis::setex('banner:'.$distribution_id.':'.$this->uid,($nextDay-$day),1);
  588. $advert = self::getBackFormat($advert);
  589. return response()->success($advert);
  590. }
  591. }
  592. if($advert['frequency'] == 'week'){
  593. if(!Redis::exists('banner:'.$distribution_id.':'.$this->uid)){
  594. Redis::setex('banner:'.$distribution_id.':'.$this->uid,($nextWeek-$day),1);
  595. $advert = self::getBackFormat($advert);
  596. return response()->success($advert);
  597. }
  598. }
  599. return response()->success([]);
  600. }
  601. if($release_type == '5'){
  602. //充值页返回需要判断用户权限
  603. $check_user = $this->checkUsers($advert['person']);
  604. if(!$check_user){
  605. return response()->success([]);
  606. }
  607. $activity = ActivityService::getById($advert['activity_id'] ?? 0);
  608. if ($activity){
  609. if ($activity['permanent'] == 1 || ($activity['start_time'] < date("Y-m-d H:i:s") && $activity['end_time'] > date("Y-m-d H:i:s") )){
  610. $advert['redirect_url'] = "/views/Activity";
  611. $advert = self::getBackFormat($advert);
  612. $advert['param'] = ['token' => $activity['token']];
  613. return response()->success($advert);
  614. }else{
  615. return response()->success([]);
  616. }
  617. }else{
  618. return response()->success([]);
  619. }
  620. }
  621. }
  622. //男女频,书架列表
  623. $banner = ChannelAdvertService::getAdvertList($distribution_id,$release_type);
  624. if($banner->isEmpty()){
  625. $banner = $this->getDefaultBanner();
  626. return response()->success($banner);
  627. }
  628. $banner->transform(function ($item) {
  629. $item->ids = Hashids::encode($item->id);
  630. if($item->type == 1){
  631. $content = explode(';',$item->content);
  632. $item->bid = isset($content[2]) ? $content[2] : '';
  633. $item->cid = isset($content[3]) ? $content[3] : '';
  634. if ($item->cid){
  635. $item->redirect_url = "views/Reader";
  636. }else{
  637. $item->redirect_url = "views/Detail";
  638. }
  639. $item->redirect_type = "book";
  640. }else{
  641. $activity = ActivityService::getById($item->activity_id);
  642. $item->redirect_url = "/views/Activity";
  643. $item->redirect_type = "activity";
  644. if($activity && !empty($activity['token'])){
  645. $item['param'] = ['token' => $activity['token']];
  646. }else{
  647. $item->redirect_url = "#";
  648. }
  649. }
  650. self::getBackFormat($item);
  651. return $item;
  652. });
  653. return response()->success($banner);
  654. }
  655. static function getBackFormat($data)
  656. {
  657. if(isset($data['id']) || $data->id) unset($data['id'],$data->id);
  658. if(isset($data['type']) || $data->type) unset($data['type'],$data->type);
  659. if(isset($data['person']) || $data->person) unset($data['person'],$data->person);
  660. if(isset($data['frequency']) || $data->frequency) unset($data['frequency'],$data->frequency);
  661. return $data;
  662. }
  663. /**
  664. * 默认获取banner
  665. * @return mixed
  666. */
  667. protected function getDefaultBanner()
  668. {
  669. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  670. $banner = (new RecoBannerService)->getByType($reco_banner_type, 2);
  671. $banner->transform(function ($item) {
  672. $result = $this->getBidCidFromUrl($item->redirect_url);
  673. $item->bid = $result['bid'];
  674. $item->cid = $result['cid'];
  675. $item->ids = Hashids::encode($item->id);
  676. $item->type = 'default';
  677. if ($result['cid']) {
  678. $item->redirect_url = "views/Reader";
  679. } else {
  680. $item->redirect_url = "views/Detail";
  681. }
  682. $item->redirect_type = "book";
  683. unset($item->id);
  684. return $item;
  685. });
  686. return $banner;
  687. }
  688. /**
  689. * 限免
  690. */
  691. public function free(int $sex)
  692. {
  693. $result = BookConfigService::findFreeBooks($sex);
  694. return response()->success($result);
  695. }
  696. public function yueaiBackRecom(Request $request)
  697. {
  698. $package = $request->header('x-package', '');
  699. if (empty($package) || $package != 'com.beidao.kuaiying.yueai') {
  700. return response()->success([]);
  701. }
  702. $user = (new QappUserService)->getGolableUser();
  703. if(isset($user->uid) && !empty($user->uid) && $user->send_order_id > 0){
  704. $bid = BookConfigService::getAvailableBIdsbyBids([58238,60534,63220,14500,13254,63221,63548,14022,59334,58888,63417,61701],$this->distribution_channel_id,false);
  705. if (!empty($bid)){
  706. $bid = array_random($bid,4);
  707. }
  708. $orderRecord = ChapterOrderService::hasUserRecord($user->uid);
  709. if($orderRecord && count($bid) >= 4){
  710. $where = ['is_on_shelf' => [1,2],'bids' => $bid];
  711. // $books = BookConfigService::getBooksByIds($bid,[],false);
  712. $books = BookConfigService::getBookLists($where,[],false);
  713. return response()->collection(new BookTransformer(), $books);
  714. }
  715. }
  716. return response()->success([]);
  717. $where = ['is_on_shelf' => [2]];
  718. $where['channel_id'] = $request->input('distribution_channel_id',0);
  719. $books = BookConfigService::getBooks($where, [], 4);
  720. return response()->collection(new BookTransformer(), $books);
  721. }
  722. public function shelfRecom(Request $request)
  723. {
  724. $category_id = $request->input('category_id');
  725. $bid = $request->input('bid');
  726. $package = $request->header('x-package', '');
  727. if (empty($package) || $package != 'com.beidao.kuaiying.yueai') {
  728. return response()->success([]);
  729. }
  730. return response()->success([]);
  731. $user = (new QappUserService)->getGolableUser();
  732. if(isset($user->uid) && !empty($user->uid) && $user->send_order_id > 0){
  733. $bid = BookConfigService::getAvailableBIdsbyBids([58238,60534,63220,14500,13254,63221,63548,14022,59334,58888,63417,61701],$this->distribution_channel_id,false);
  734. if (!empty($bid)){
  735. $bid = array_random($bid,4);
  736. }
  737. $orderRecord = ChapterOrderService::hasUserRecord($user->uid);
  738. if($orderRecord && count($bid) > 1){
  739. $where = ['is_on_shelf' => [1,2],'bids' => $bid];
  740. // $books = BookConfigService::getBooksByIds($bid,[],false);
  741. $books = BookConfigService::getBookLists($where,[],false);
  742. return response()->collection(new BookTransformer(), $books);
  743. }
  744. }
  745. return response()->success([]);
  746. $where = ['is_on_shelf' => [2]];
  747. $where['channel_id'] = $request->input('distribution_channel_id',0);;
  748. $books = BookConfigService::getBooks($where, [], 4);
  749. return response()->collection(new BookTransformer(), $books);
  750. }
  751. /**
  752. * 新推荐书单
  753. * @param Request $request
  754. * @return mixed
  755. */
  756. public function recommendBooks(Request $request)
  757. {
  758. $distribution_id = $this->distribution_channel_id;
  759. $bid = $request->get('bid',0);
  760. if(empty($distribution_id)){
  761. \Log::info('recommendBooks:1');
  762. return response()->success([]);
  763. }
  764. if(!empty($bid)){
  765. \Log::info('recommendBooks:2');
  766. $bid = str_decode($bid);
  767. }
  768. //判断包是否存在
  769. $package_info = QappPackage::getPackage($distribution_id);
  770. if(empty($package_info) || !isset($package_info->channel_id)){
  771. \Log::info('recommendBooks:3');
  772. return response()->success([]);
  773. }
  774. //包对应有没有配置开启推荐书单
  775. $config = ChannelRecommendBookConfigService::getRecommendConfigs($distribution_id);
  776. if(empty($config) || !isset($config->status) || $config->status == 0){
  777. \Log::info('recommendBooks:4');
  778. return response()->success([]);
  779. }
  780. //根据频率和用户属性决定是否需要返回
  781. $res = $this->checkUsersAuth($config);
  782. if(!$res){
  783. \Log::info('recommendBooks:5');
  784. return response()->success([]);
  785. }
  786. $list = ChannelRecommendBooksService::getRecommendBooks($distribution_id,$bid);
  787. if(!$list->isEmpty()){
  788. foreach($list as $key => $item){
  789. $this->incrRecommendNum($distribution_id,$item->bid);
  790. }
  791. \Log::info('recommendBooks:6');
  792. return response()->collection(new BookTransformer(), $list);
  793. }
  794. \Log::info('recommendBooks:7');
  795. return response()->success([]);
  796. }
  797. /**
  798. * 点击推荐书籍记录点击次数
  799. * @param Request $request
  800. * @return mixed
  801. */
  802. public function clickRecommendBooks(Request $request)
  803. {
  804. $distribution_id = $this->distribution_channel_id;
  805. $bid = $request->get('bid','');
  806. if(empty($bid)){
  807. return response()->success();
  808. }
  809. //判断包是否存在
  810. $package_info = QappPackage::getPackage($distribution_id);
  811. if(empty($package_info) || !isset($package_info->channel_id)){
  812. return response()->success([]);
  813. }
  814. $date = date('Ymd');
  815. $bid = str_decode($bid);
  816. $cacheKey = 'recommend:click:'.$date.':'.$distribution_id.$bid;
  817. $this->incrRedisKey($cacheKey);
  818. return response()->success();
  819. }
  820. /**
  821. * 判断用户是否需要推荐
  822. * @param $config
  823. * @return bool
  824. */
  825. protected function checkUsersAuth($config)
  826. {
  827. \Log::info($config);
  828. \Log::info('$config->person:'.$config->person);
  829. \Log::info($this->uid);
  830. $res = $this->checkUsers($config->person);
  831. if($res === false){
  832. return false;
  833. }
  834. //频率判断
  835. if($config->frequency == 'back'){
  836. //返回即推送
  837. return true;
  838. }
  839. $day = strtotime(date('Y-m-d H:i:s'));
  840. $nextDay = strtotime( date('Y-m-d'). ' +1 day');
  841. $nextWeek = strtotime(date('Y-m-d',strtotime('+1 week last monday')));
  842. if($config->frequency == 'day'){
  843. //每日推送
  844. if(!Redis::exists('recommend:'.$config->channel_id.':'.$this->uid)){
  845. Redis::setex('recommend:'.$config->channel_id.':'.$this->uid,($nextDay-$day),1);
  846. return true;
  847. }
  848. \Log::info('当天已经推送过了');
  849. return false;
  850. }
  851. if($config->frequency == 'week'){
  852. //每周推送
  853. if(!Redis::exists('recommend:'.$config->channel_id.':'.$this->uid)){
  854. Redis::setex('recommend:'.$config->channel_id.':'.$this->uid,($nextWeek-$day),1);
  855. return true;
  856. }
  857. \Log::info('该周已经推送过了');
  858. return false;
  859. }
  860. }
  861. /**
  862. * 判断用户是否有权限
  863. * @param $type
  864. * @return bool
  865. */
  866. protected function checkUsers($type)
  867. {
  868. $res = false;
  869. switch($type)
  870. {
  871. case 'pay':
  872. //付费用户
  873. $orders = OrderService::getUserLastestOrder($this->uid);
  874. if($orders){
  875. $res = true;
  876. }
  877. \Log::info('用户是否付费:');
  878. \Log::info($orders);
  879. break;
  880. case 'send_order':
  881. //派单用户
  882. if($this->send_order_id != 0){
  883. $res = true;
  884. }
  885. \Log::info('用户是否是派单用户:$this->send_order_id:'.$this->send_order_id);
  886. break;
  887. case 'pay_send_order':
  888. //付费派单用户
  889. $orders = OrderService::getUserLastestOrder($this->uid);
  890. if($orders && $this->send_order_id != 0){
  891. $res = true;
  892. }
  893. \Log::info('用户是否是付费派单用户:$this->send_order_id:'.$this->send_order_id);
  894. \Log::info($orders);
  895. break;
  896. case 'unpaid':
  897. $orders = OrderService::getUserLastestOrder($this->uid);
  898. if(!$orders){
  899. $res = true;
  900. }
  901. break;
  902. case 'all':
  903. $res = true;
  904. \Log::info('所有用户返回');
  905. break;
  906. }
  907. return $res;
  908. }
  909. /**
  910. * 推广书籍推荐次数
  911. * @param $channel_id
  912. * @param $bid
  913. */
  914. protected function incrRecommendNum($channel_id,$bid)
  915. {
  916. $date = date('Ymd');
  917. $cacheKey = 'recommend:sum:'.$date.':'.$channel_id.$bid;
  918. $this->incrRedisKey($cacheKey);
  919. }
  920. /**
  921. * 点击广告统计
  922. * @param Request $request
  923. * @return mixed
  924. */
  925. public function getCheckAdvertisement(Request $request)
  926. {
  927. $type = $request->get('type','');
  928. $id = $request->get('ids','');
  929. $distribution_id = $this->distribution_channel_id;
  930. \Log::info('getCheckAdvertisement:type:'.$type.' id:'.$id.' $distribution_id:'.$distribution_id);
  931. if(empty($id) || empty($distribution_id)){
  932. return response()->success();
  933. }
  934. $id = str_decode($id);
  935. if(!$id){
  936. return response()->success();
  937. }
  938. if($type == 'default'){
  939. //默认原表reco_banners广告,区分跟新表channel_advert的id
  940. $id = -$id;
  941. }else{
  942. $advert = ChannelAdvert::find($id);
  943. if(!$advert){
  944. \Log::info('getCheckAdvertisement:error:不存在该广告');
  945. \Log::info(json_encode($request->all()));
  946. return response()->success();
  947. }
  948. }
  949. $where = ['channel_ad_id' => $id, 'uid' => $this->uid, 'distribution_id' => $distribution_id];
  950. \Log::info('insert_where:');
  951. \Log::info(json_encode($where));
  952. ChannelAdUser::firstOrCreate($where);
  953. $date = date('Ymd');
  954. $cacheKey = 'advertisement:pv:'.$date.':'.$distribution_id.$id;
  955. $this->incrRedisKey($cacheKey);
  956. //设置当前用户的所属广告有效期一周
  957. $key = 'advertisement:uid:'.$this->uid.':id';
  958. Redis::set($key,$id);
  959. Redis::expire($key,BaseConst::ONE_WEEK_SECONDS);
  960. return response()->success();
  961. }
  962. /**
  963. * redis新增
  964. * @param $cacheKey
  965. */
  966. protected function incrRedisKey($cacheKey)
  967. {
  968. if(!Redis::exists($cacheKey)){
  969. Redis::set($cacheKey,1);
  970. }else{
  971. Redis::incrBy($cacheKey, 1);
  972. }
  973. }
  974. /**
  975. * 绑定广告和订单
  976. * @param Request $request
  977. * @return mixed
  978. */
  979. public function getAdvertOrders(Request $request)
  980. {
  981. $order_id = $request->get('trade_no','');
  982. $uid = $this->uid;
  983. $distribution_id = $this->distribution_channel_id;
  984. $cacheKey = 'advertisement:uid:'.$uid.':id';
  985. if(empty($order_id)){
  986. return response()->success();
  987. }
  988. $channel_ad_id = 0;
  989. \Log::info('getAdvertOrders:trade_no'.$order_id.' uid:'.$uid);
  990. if(Redis::exists($cacheKey)){
  991. try {
  992. //获取广告id
  993. $channel_ad_id = Redis::get($cacheKey);
  994. //判断订单跟广告是否有关系
  995. $order = OrderService::getByTradeNo($order_id);
  996. if(!$order){
  997. return response()->success();
  998. }
  999. if($channel_ad_id > 0){
  1000. //新版
  1001. $type = 1;
  1002. $advert = ChannelAdvert::find($channel_ad_id);
  1003. if(!$advert){
  1004. return response()->success();
  1005. }
  1006. if($advert->type == '2' && $advert->activity_id != 0){
  1007. //广告类型为活动
  1008. if($order->activity_id != $advert->activity_id){
  1009. return response()->success();
  1010. }
  1011. }else{
  1012. //广告类型为书籍
  1013. $content = explode(';',$advert->content);
  1014. if(!isset($content[2])){
  1015. return response()->success();
  1016. }
  1017. if($order->from_bid == 0 || $order->from_bid != str_decode($content[2])){
  1018. return response()->success();
  1019. }
  1020. }
  1021. }else{
  1022. //旧版
  1023. $type = 0;
  1024. $advert = RecoBanner::find(abs($channel_ad_id));
  1025. if(!$advert){
  1026. return response()->success();
  1027. }
  1028. $result = $this->getBidCidFromUrl($advert->redirect_url);
  1029. $bid = $result['bid'];
  1030. $cid = $result['cid'];
  1031. if($cid){
  1032. if($order->from_bid == 0 || $order->from_bid != str_decode($bid)){
  1033. return response()->success();
  1034. }
  1035. }else{
  1036. if($order->activity_id == 0 || $order->from_bid != str_decode($bid)){
  1037. return response()->success();
  1038. }
  1039. }
  1040. }
  1041. $created_at = $updated_at = date('Y-m-d H:i:s');
  1042. DB::table('channel_advert_orders')->insert(compact('order_id','uid','distribution_id','channel_ad_id','type','created_at','updated_at'));
  1043. } catch (\Exception $e) {
  1044. \Log::info('绑定广告和订单失败'.$e->getMessage());
  1045. \Log::info('order_id:'.$order_id.' uid:'.$uid.' distribution_id:'.$distribution_id.' channel_ad_id:'.$channel_ad_id);
  1046. }
  1047. }
  1048. return response()->success();
  1049. }
  1050. private function getSpecialLibrary(Request $request)
  1051. {
  1052. $where = [];
  1053. $order = [];
  1054. $where['is_on_shelf'] = [2,4];
  1055. $category_id = $request->input('category_id');
  1056. if ($category_id) {
  1057. if ($category_id == 1) {
  1058. $where['channel_name'] = '男频';
  1059. } elseif ($category_id == 2) {
  1060. $where['channel_name'] = '女频';
  1061. } else {
  1062. $where['category_id'] = $category_id;
  1063. }
  1064. }
  1065. $key = $request->input('key');
  1066. $uid = $request->input('uid', 0);
  1067. if ($key && $uid && is_numeric($uid)) {
  1068. BookConfigService::saveUserSearchLog($key, $uid);
  1069. }
  1070. $where['key'] = $key;
  1071. $order_field = $request->input('order_field');
  1072. $order_seq = $request->input('order_seq');
  1073. if ($order_field != '' && in_array($order_field, ['recommend_index', 'click_count', 'update', 'size', 'create'])) {
  1074. if ($order_field == 'update') {
  1075. $order = ['book_configs.updated_at', 'desc'];
  1076. } elseif ($order_field == 'create') {
  1077. $order = ['book_configs.created_at', 'desc'];
  1078. } else {
  1079. $order = [$order_field, 'desc'];
  1080. }
  1081. if ($order_seq == 'asc') {
  1082. $order = [$order_field, 'asc'];
  1083. }
  1084. if ($order_seq == 'desc') {
  1085. $order = [$order_field, 'desc'];
  1086. }
  1087. }
  1088. // 审核状态默认值
  1089. $package = $request->header('x-package', '');
  1090. $brand = $request->header('x-nbrand', '');
  1091. $codeVersion = $request->header('x-codeversion', '');
  1092. if ($order_field === 'recommend_index' && Utils::checkIsAudit($package, $brand, $codeVersion)) {
  1093. $order = ['book_configs.bid', 'desc'];
  1094. }
  1095. $status = $request->input('status');
  1096. if ($status != '') {
  1097. $where['status'] = $status;
  1098. }
  1099. // 搜索关键词的情况下,屏蔽书籍完本状态
  1100. if ($key && isset($where['status'])) {
  1101. unset($where['status']);
  1102. }
  1103. $page_size = $request->input('page_size', 15);
  1104. $where['channel_id'] = ($package === 'com.beidao.kuaiying.zsy') ? 7477 : 0;
  1105. $books = BookConfigService::getBooks($where, $order, $page_size);
  1106. $special = get_special_bid();
  1107. $list = [];
  1108. foreach ($books as $ke => $val){
  1109. if ($val->is_on_shelf != 2 && !in_array($val->bid,$special)){
  1110. unset($books[$ke]);
  1111. }
  1112. }
  1113. return response()->pagination(new BookTransformer,$books);
  1114. }
  1115. }