BookController.php 41 KB

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