BookController.php 41 KB

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