BookController.php 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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','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. $content = explode(';',$item->content);
  604. $item->bid = isset($content[2]) ? $content[2] : '';
  605. $item->cid = isset($content[3]) ? $content[3] : '';
  606. $item->redirect_url = "views/Detail?bid={$item->bid}";
  607. }else{
  608. $activity = ActivityService::getById($item->activity_id);
  609. if($activity && !empty($activity['token'])){
  610. $item->redirect_url = "/views/Activity?token={$activity['token']}";
  611. }else{
  612. $item->redirect_url = "#";
  613. }
  614. }
  615. self::getBackFormat($item);
  616. return $item;
  617. });
  618. return response()->success($banner);
  619. }
  620. static function getBackFormat($data)
  621. {
  622. if(isset($data['id']) || $data->id) unset($data['id'],$data->id);
  623. if(isset($data['type']) || $data->type) unset($data['type'],$data->type);
  624. if(isset($data['person']) || $data->person) unset($data['person'],$data->person);
  625. if(isset($data['frequency']) || $data->frequency) unset($data['frequency'],$data->frequency);
  626. return $data;
  627. }
  628. /**
  629. * 默认获取banner
  630. * @return mixed
  631. */
  632. protected function getDefaultBanner()
  633. {
  634. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  635. $banner = (new RecoBannerService)->getByType($reco_banner_type, 2);
  636. $banner->transform(function ($item) {
  637. $result = $this->getBidCidFromUrl($item->redirect_url);
  638. $item->bid = $result['bid'];
  639. $item->cid = $result['cid'];
  640. $item->ids = Hashids::encode($item->id);
  641. $item->type = 'default';
  642. if ($result['cid']) {
  643. $item->redirect_url = "views/Reader";
  644. } else {
  645. $item->redirect_url = "views/Detail";
  646. }
  647. unset($item->id);
  648. return $item;
  649. });
  650. return $banner;
  651. }
  652. /**
  653. * 限免
  654. */
  655. public function free(int $sex)
  656. {
  657. $result = BookConfigService::findFreeBooks($sex);
  658. return response()->success($result);
  659. }
  660. public function yueaiBackRecom(Request $request)
  661. {
  662. $package = $request->header('x-package', '');
  663. if (empty($package) || $package != 'com.beidao.kuaiying.yueai') {
  664. return response()->success([]);
  665. }
  666. $user = (new QappUserService)->getGolableUser();
  667. if(isset($user->uid) && !empty($user->uid) && $user->send_order_id > 0){
  668. $orderRecord = ChapterOrderService::hasUserRecord($user->uid);
  669. if($orderRecord){
  670. $bid = [58238,60534,63220,14500,13254,63221,63548,14022,59334,58888,63417,61701];
  671. $bid = array_random($bid,4);
  672. $books = BookConfigService::getBooksByIds($bid,[],false);
  673. return response()->collection(new BookTransformer(), $books);
  674. }
  675. }
  676. return response()->success([]);
  677. $where = ['is_on_shelf' => [2]];
  678. $where['channel_id'] = ($package === 'com.beidao.kuaiying.zsy') ? 7477 : 0;
  679. $books = BookConfigService::getBooks($where, [], 4);
  680. return response()->collection(new BookTransformer(), $books);
  681. }
  682. public function shelfRecom(Request $request)
  683. {
  684. $category_id = $request->input('category_id');
  685. $bid = $request->input('bid');
  686. $package = $request->header('x-package', '');
  687. if (empty($package) || $package != 'com.beidao.kuaiying.yueai') {
  688. return response()->success([]);
  689. }
  690. $user = (new QappUserService)->getGolableUser();
  691. if(isset($user->uid) && !empty($user->uid) && $user->send_order_id > 0){
  692. $orderRecord = ChapterOrderService::hasUserRecord($user->uid);
  693. if($orderRecord){
  694. $bid = [58238,60534,63220,14500,13254,63221,63548,14022,59334,58888,63417,61701];
  695. $bid = array_random($bid,4);
  696. $books = BookConfigService::getBooksByIds($bid,[],false);
  697. return response()->collection(new BookTransformer(), $books);
  698. }
  699. }
  700. return response()->success([]);
  701. $where = ['is_on_shelf' => [2]];
  702. $where['channel_id'] = ($package === 'com.beidao.kuaiying.zsy') ? 7477 : 0;
  703. $books = BookConfigService::getBooks($where, [], 4);
  704. return response()->collection(new BookTransformer(), $books);
  705. }
  706. /**
  707. * 新推荐书单
  708. * @param Request $request
  709. * @return mixed
  710. */
  711. public function recommendBooks(Request $request)
  712. {
  713. $distribution_id = $this->distribution_channel_id;
  714. $bid = $request->get('bid',0);
  715. if(empty($distribution_id)){
  716. \Log::info('recommendBooks:1');
  717. return response()->success([]);
  718. }
  719. if(!empty($bid)){
  720. \Log::info('recommendBooks:2');
  721. $bid = Hashids::decode($bid)[0];
  722. }
  723. //判断包是否存在
  724. $package_info = QappPackage::getPackage($distribution_id);
  725. if(empty($package_info) || !isset($package_info->channel_id)){
  726. \Log::info('recommendBooks:3');
  727. return response()->success([]);
  728. }
  729. //包对应有没有配置开启推荐书单
  730. $config = ChannelRecommendBookConfigService::getRecommendConfigs($distribution_id);
  731. if(empty($config) || !isset($config->status) || $config->status == 0){
  732. \Log::info('recommendBooks:4');
  733. return response()->success([]);
  734. }
  735. //根据频率和用户属性决定是否需要返回
  736. $res = $this->checkUsersAuth($config);
  737. if(!$res){
  738. \Log::info('recommendBooks:5');
  739. return response()->success([]);
  740. }
  741. $list = ChannelRecommendBooksService::getRecommendBooks($distribution_id,$bid);
  742. if(!$list->isEmpty()){
  743. foreach($list as $key => $item){
  744. $this->incrRecommendNum($distribution_id,$item->bid);
  745. }
  746. \Log::info('recommendBooks:6');
  747. return response()->collection(new BookTransformer(), $list);
  748. }
  749. \Log::info('recommendBooks:7');
  750. return response()->success([]);
  751. }
  752. /**
  753. * 点击推荐书籍记录点击次数
  754. * @param Request $request
  755. * @return mixed
  756. */
  757. public function clickRecommendBooks(Request $request)
  758. {
  759. $distribution_id = $this->distribution_channel_id;
  760. $bid = $request->get('bid','');
  761. if(empty($bid)){
  762. return response()->success();
  763. }
  764. //判断包是否存在
  765. $package_info = QappPackage::getPackage($distribution_id);
  766. if(empty($package_info) || !isset($package_info->channel_id)){
  767. return response()->success([]);
  768. }
  769. $date = date('Ymd');
  770. $bid = Hashids::decode($bid)[0];
  771. $cacheKey = 'recommend:click:'.$date.':'.$distribution_id.$bid;
  772. $this->incrRedisKey($cacheKey);
  773. return response()->success();
  774. }
  775. /**
  776. * 判断用户是否需要推荐
  777. * @param $config
  778. * @return bool
  779. */
  780. protected function checkUsersAuth($config)
  781. {
  782. \Log::info($config);
  783. \Log::info('$config->person:'.$config->person);
  784. \Log::info($this->uid);
  785. $res = $this->checkUsers($config->person);
  786. if($res === false){
  787. return false;
  788. }
  789. //频率判断
  790. if($config->frequency == 'back'){
  791. //返回即推送
  792. return true;
  793. }
  794. $day = strtotime(date('Y-m-d H:i:s'));
  795. $nextDay = strtotime( date('Y-m-d'). ' +1 day');
  796. $nextWeek = strtotime(date('Y-m-d',strtotime('+1 week last monday')));
  797. if($config->frequency == 'day'){
  798. //每日推送
  799. if(!Redis::exists('recommend:'.$config->channel_id.':'.$this->uid)){
  800. Redis::setex('recommend:'.$config->channel_id.':'.$this->uid,($nextDay-$day),1);
  801. return true;
  802. }
  803. \Log::info('当天已经推送过了');
  804. return false;
  805. }
  806. if($config->frequency == 'week'){
  807. //每周推送
  808. if(!Redis::exists('recommend:'.$config->channel_id.':'.$this->uid)){
  809. Redis::setex('recommend:'.$config->channel_id.':'.$this->uid,($nextWeek-$day),1);
  810. return true;
  811. }
  812. \Log::info('该周已经推送过了');
  813. return false;
  814. }
  815. }
  816. /**
  817. * 判断用户是否有权限
  818. * @param $type
  819. * @return bool
  820. */
  821. protected function checkUsers($type)
  822. {
  823. $res = false;
  824. switch($type)
  825. {
  826. case 'pay':
  827. //付费用户
  828. $orders = OrderService::getUserLastestOrder($this->uid);
  829. if($orders){
  830. $res = true;
  831. }
  832. \Log::info('用户是否付费:');
  833. \Log::info($orders);
  834. break;
  835. case 'send_order':
  836. //派单用户
  837. if($this->send_order_id != 0){
  838. $res = true;
  839. }
  840. \Log::info('用户是否是派单用户:$this->send_order_id:'.$this->send_order_id);
  841. break;
  842. case 'pay_send_order':
  843. //付费派单用户
  844. $orders = OrderService::getUserLastestOrder($this->uid);
  845. if($orders && $this->send_order_id != 0){
  846. $res = true;
  847. }
  848. \Log::info('用户是否是付费派单用户:$this->send_order_id:'.$this->send_order_id);
  849. \Log::info($orders);
  850. break;
  851. case 'unpaid':
  852. $orders = OrderService::getUserLastestOrder($this->uid);
  853. if(!$orders){
  854. $res = true;
  855. }
  856. break;
  857. case 'all':
  858. $res = true;
  859. \Log::info('所有用户返回');
  860. break;
  861. }
  862. return $res;
  863. }
  864. /**
  865. * 推广书籍推荐次数
  866. * @param $channel_id
  867. * @param $bid
  868. */
  869. protected function incrRecommendNum($channel_id,$bid)
  870. {
  871. $date = date('Ymd');
  872. $cacheKey = 'recommend:sum:'.$date.':'.$channel_id.$bid;
  873. $this->incrRedisKey($cacheKey);
  874. }
  875. /**
  876. * 点击广告统计
  877. * @param Request $request
  878. * @return mixed
  879. */
  880. public function getCheckAdvertisement(Request $request)
  881. {
  882. $type = $request->get('type','');
  883. $id = $request->get('ids','');
  884. $distribution_id = $this->distribution_channel_id;
  885. if(empty($type) || empty($id) || empty($distribution_id)){
  886. return response()->success();
  887. }
  888. $id = Hashids::decode($id)[0];
  889. if(!$id){
  890. return response()->success();
  891. }
  892. if($type == 'default'){
  893. //默认原表reco_banners广告,区分跟新表channel_advert的id
  894. $id = -$id;
  895. }else{
  896. $advert = ChannelAdvert::find($id);
  897. if(!$advert){
  898. \Log::info('getCheckAdvertisement:error:不存在该广告');
  899. \Log::info(json_encode($request->all()));
  900. return response()->success();
  901. }
  902. }
  903. $where = ['channel_ad_id' => $id, 'uid' => $this->uid, 'distribution_id' => $distribution_id];
  904. ChannelAdUser::firstOrCreate($where);
  905. $date = date('Ymd');
  906. $cacheKey = 'advertisement:pv:'.$date.':'.$distribution_id.$id;
  907. $this->incrRedisKey($cacheKey);
  908. //设置当前用户的所属广告有效期一周
  909. $key = 'advertisement:uid:'.$this->uid.':id';
  910. Redis::set($key,$id);
  911. Redis::expire($key,BaseConst::ONE_WEEK_SECONDS);
  912. return response()->success();
  913. }
  914. /**
  915. * redis新增
  916. * @param $cacheKey
  917. */
  918. protected function incrRedisKey($cacheKey)
  919. {
  920. if(!Redis::exists($cacheKey)){
  921. Redis::set($cacheKey,1);
  922. }else{
  923. Redis::incrBy($cacheKey, 1);
  924. }
  925. }
  926. /**
  927. * 绑定广告和订单
  928. * @param Request $request
  929. * @return mixed
  930. */
  931. public function getAdvertOrders(Request $request)
  932. {
  933. $trade_no = $request->get('trade_no','');
  934. $uid = $this->uid;
  935. $distribution_id = $this->distribution_channel_id;
  936. $cacheKey = 'advertisement:uid:'.$uid.':id';
  937. if(empty($trade_no)){
  938. return response()->success();
  939. }
  940. $channel_ad_id = 0;
  941. if(Redis::exists($cacheKey)){
  942. try {
  943. //获取广告id
  944. $channel_ad_id = Redis::get($cacheKey);
  945. //判断订单跟广告是否有关系
  946. $order = OrderService::getByTradeNo($trade_no);
  947. if(!$order){
  948. return response()->success();
  949. }
  950. if($channel_ad_id > 0){
  951. //新版
  952. $type = 1;
  953. $advert = ChannelAdvert::find($channel_ad_id);
  954. if(!$advert){
  955. return response()->success();
  956. }
  957. if($advert->type == '2' && $advert->activity_id != 0){
  958. //广告类型为活动
  959. if($order->activity_id != $advert->activity_id){
  960. return response()->success();
  961. }
  962. }else{
  963. //广告类型为书籍
  964. $content = explode(';',$advert->content);
  965. if(!isset($content[2])){
  966. return response()->success();
  967. }
  968. if($order->from_bid == 0 || $order->from_bid != Hashids::decode($content[2])[0]){
  969. return response()->success();
  970. }
  971. }
  972. }else{
  973. //旧版
  974. $type = 0;
  975. $advert = RecoBanner::find(abs($channel_ad_id));
  976. if(!$advert){
  977. return response()->success();
  978. }
  979. $result = $this->getBidCidFromUrl($advert->redirect_url);
  980. $bid = $result['bid'];
  981. $cid = $result['cid'];
  982. if($cid){
  983. if($order->from_bid == 0 || $order->from_bid != Hashids::decode($bid)[0]){
  984. return response()->success();
  985. }
  986. }else{
  987. if($order->activity_id == 0 || $order->from_bid != Hashids::decode($bid)[0]){
  988. return response()->success();
  989. }
  990. }
  991. }
  992. $created_at = $updated_at = date('Y-m-d H:i:s');
  993. DB::table('channel_advert_orders')->insert(compact('order_id','uid','distribution_id','channel_ad_id','type','created_at','updated_at'));
  994. } catch (\Exception $e) {
  995. \Log::info('绑定广告和订单失败'.$e->getMessage());
  996. \Log::info('order_id:'.$trade_no.' uid:'.$uid.' distribution_id:'.$distribution_id.' channel_ad_id:'.$channel_ad_id);
  997. }
  998. }
  999. return response()->success();
  1000. }
  1001. }