BookController.php 42 KB

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