BookController.php 37 KB

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