ChapterController.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\Book;
  3. use App\Modules\SendOrder\Services\SendOrderService;
  4. use App\Modules\Statistic\Services\WapVisitStatService;
  5. use App\Modules\User\Services\UserBookCombinationConfigService;
  6. use Illuminate\Http\Request;
  7. use App\Http\Controllers\QuickApp\BaseController;
  8. use Redis;
  9. use App\Modules\Book\Services\ChapterService;
  10. use App\Modules\User\Services\ReadRecordService;
  11. use App\Http\Controllers\QuickApp\Book\Transformers\ChapterTransformer;
  12. use App\Modules\Book\Services\BookConfigService;
  13. use App\Http\Controllers\QuickApp\Book\Transformers\ChapterListTransformer;
  14. use App\Jobs\UserRententionJob;
  15. use App\Modules\Book\Services\BookService;
  16. use App\Modules\Subscribe\Services\BookOrderService;
  17. use App\Modules\Subscribe\Services\ChapterOrderService;
  18. use App\Modules\Subscribe\Services\YearOrderService;
  19. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  20. use App\Modules\Subscribe\Services\ChapterReminderService;
  21. use App\Modules\User\Services\UserDeepReadTagService;
  22. use App\Modules\UserTask\Services\BaseTask;
  23. use App\Modules\UserTask\Services\UserTaskService;
  24. use App\Modules\SendOrder\Models\QappSendOrder;
  25. class ChapterController extends BaseController
  26. {
  27. private $book_info;
  28. public function getCatalog(Request $request, $bid)
  29. {
  30. $bid = BookService::decodeBidStatic($bid);
  31. $lists = ChapterService::getChapterLists($bid);
  32. $book_info = BookConfigService::getBookById($bid);
  33. if (!$book_info) {
  34. return response()->error('PARAM_ERROR');
  35. }
  36. $this->book_info = $book_info;
  37. $lists = $this->getChapterCatalog($bid, $lists, $book_info);
  38. return response()->collection(new ChapterListTransformer, $lists);
  39. }
  40. public function getCatalogPerPage(Request $request, $bid)
  41. {
  42. $bid = BookService::decodeBidStatic($bid);
  43. $book_info = BookConfigService::getBookById($bid);
  44. if (!$book_info) {
  45. return response()->error('PARAM_ERROR');
  46. }
  47. $this->book_info = $book_info;
  48. $page_size = $request->input('page_size', 15);
  49. if ($page_size >= 100) $page_size = 100;
  50. $page_size = 15;
  51. $res = ChapterService::getChapterListsPage($bid, $page_size);
  52. $lists = $this->getChapterCatalog($bid, $res, $book_info);
  53. return response()->pagination(new ChapterListTransformer, $lists);
  54. }
  55. private function getChapterCatalog(int $bid, $chapters, $book_info)
  56. {
  57. // 查询书籍是否限免
  58. $isFree = BookConfigService::judgeBookIsFree($bid);
  59. //渠道自定义vip章节
  60. //$vip_sequence = Redis::hget('channel:chapterfee:setting:' . $this->distribution_channel_id, $bid);
  61. $vip_sequence = BookService::getVipSequence($bid,$this->distribution_channel_id,$this->send_order_id);
  62. list($is_split,$is_change_chapter_name) = BookService::splitContent($bid);
  63. $change_chapter_name = 0;
  64. if($is_split && ($book_info->channel_name == '男频' || $is_change_chapter_name) ){
  65. $change_chapter_name = 1;
  66. }
  67. switch ($book_info->charge_type) {
  68. case 'BOOK':
  69. $price = $this->getPrice($book_info);
  70. $is_need_charge = $this->isBookNeedCharge($bid, $price);
  71. foreach ($chapters as $v) {
  72. $v->next_chapter_status = 0;
  73. $v->next_price = 0;
  74. $v->is_need_charge = $v->is_vip ? $is_need_charge : false;
  75. $v->price = $price;
  76. // 限免判断
  77. if ($isFree) {
  78. $v->is_need_charge = false;
  79. $v->price = 0;
  80. }
  81. if($vip_sequence){
  82. if($v->sequence >= $vip_sequence){
  83. $v->is_vip = 1;
  84. }else{
  85. $v->is_vip = 0;
  86. }
  87. }
  88. //拆章
  89. if($change_chapter_name){
  90. $v->name = '第'.$v->sequence.'章';
  91. }
  92. }
  93. break;
  94. default:
  95. foreach ($chapters as $v) {
  96. if($vip_sequence){
  97. if($v->sequence >= $vip_sequence){
  98. $v->is_vip = 1;
  99. }else{
  100. $v->is_vip = 0;
  101. }
  102. }
  103. // 限免判断
  104. if ($isFree) {
  105. $v->is_need_charge = false;
  106. $v->price = 0;
  107. } else {
  108. $v->price = $v->is_vip ? $this->getPrice($book_info, $v->size) : 0;
  109. $v->is_need_charge = $v->is_vip ? $this->isChapterNeedCharge($bid, $v->id, $v->price) : false;
  110. }
  111. //下一章付费信息
  112. $v->next_chapter_status = 0;
  113. $v->next_price = 0;
  114. if($v->is_vip){
  115. $next_chapter_order_status = $this->nextChapterOrderStatus($bid,$v->next_cid);
  116. $v->next_chapter_status = $next_chapter_order_status['next_chapter_status'];
  117. $v->next_price = $next_chapter_order_status['next_price'];
  118. }
  119. //拆章
  120. if($change_chapter_name){
  121. $v->name = '第'.$v->sequence.'章';
  122. }
  123. }
  124. break;
  125. }
  126. return $chapters;
  127. }
  128. private function getChapterCatalogOld(int $bid, $chapters, $book_info)
  129. {
  130. // 查询书籍是否限免
  131. $isFree = BookConfigService::judgeBookIsFree($bid);
  132. //渠道自定义vip章节
  133. //$vip_sequence = Redis::hget('channel:chapterfee:setting:' . $this->distribution_channel_id, $bid);
  134. $vip_sequence = BookService::getVipSequence($bid,$this->distribution_channel_id,$this->send_order_id);
  135. list($is_split,$is_change_chapter_name) = BookService::splitContent($bid);
  136. $change_chapter_name = 0;
  137. if($is_split && ($book_info->channel_name == '男频' || $is_change_chapter_name) ){
  138. $change_chapter_name = 1;
  139. }
  140. switch ($book_info->charge_type) {
  141. case 'BOOK':
  142. $price = $this->getPrice($book_info);
  143. $is_need_charge = $this->isBookNeedCharge($bid, $price);
  144. foreach ($chapters as $v) {
  145. $v->next_chapter_status = 0;
  146. $v->next_price = 0;
  147. $v->is_need_charge = $v->is_vip ? $is_need_charge : false;
  148. $v->price = $price;
  149. // 限免判断
  150. if ($isFree) {
  151. $v->is_need_charge = false;
  152. $v->price = 0;
  153. }
  154. if($vip_sequence){
  155. if($v->sequence >= $vip_sequence){
  156. $v->is_vip = 1;
  157. }else{
  158. $v->is_vip = 0;
  159. }
  160. }
  161. //拆章
  162. if($change_chapter_name){
  163. $v->name = '第'.$v->sequence.'章';
  164. }
  165. }
  166. break;
  167. default:
  168. foreach ($chapters as $v) {
  169. if($vip_sequence){
  170. if($v->sequence >= $vip_sequence){
  171. $v->is_vip = 1;
  172. }else{
  173. $v->is_vip = 0;
  174. }
  175. }
  176. // 限免判断
  177. if ($isFree) {
  178. $v->is_need_charge = false;
  179. $v->price = 0;
  180. } else {
  181. $v->price = $v->is_vip ? $this->getPrice($book_info, $v->size) : 0;
  182. $v->is_need_charge = $v->is_vip ? $this->isChapterNeedCharge($bid, $v->id, $v->price) : false;
  183. }
  184. //下一章付费信息
  185. $v->next_chapter_status = 0;
  186. $v->next_price = 0;
  187. if($v->is_vip){
  188. $next_chapter_order_status = $this->nextChapterOrderStatus($bid,$v->next_cid);
  189. $v->next_chapter_status = $next_chapter_order_status['next_chapter_status'];
  190. $v->next_price = $next_chapter_order_status['next_price'];
  191. }
  192. //拆章
  193. if($change_chapter_name){
  194. $v->name = '第'.$v->sequence.'章';
  195. }
  196. }
  197. break;
  198. }
  199. return $chapters;
  200. }
  201. public function index(Request $request, $bid, $cid)
  202. {
  203. $send_order_id = $request->header('send_order_id', '');
  204. //每次绑定用户和派单的关系
  205. $this->bindSendOrderId($this->uid,$send_order_id);
  206. $oldbid = $bid;
  207. $bid = BookService::decodeBidStatic($bid);
  208. //获取图书信息
  209. $book_info = BookConfigService::getBookById($bid);
  210. if (empty($book_info))
  211. return response()->error('QAPP_SYS_ERROR');
  212. $this->book_info = $book_info;
  213. //yuyuedu、xinghe 快应用这两个cp的书屏蔽下 wutong,wutong2,wutong3下所有内容都不放快应用
  214. if(in_array($book_info->cp_source,getHiddenCp())){
  215. return response()->error('QAPP_SYS_ERROR');
  216. }
  217. if($this->distribution_channel_id == 7477 && $bid == 13765){
  218. $book_info->is_on_shelf = 2;
  219. }
  220. $special = get_special_bid();
  221. if (in_array($this->distribution_channel_id,[9487,9390]) && in_array($book_info->bid,$special)){
  222. $book_info->is_on_shelf = 2;
  223. }
  224. if($bid == 58886){
  225. $book_info->is_on_shelf = 0;
  226. }
  227. if (!in_array($book_info->is_on_shelf, [1,2])) {
  228. if ($book_info->is_on_shelf != 4) {
  229. return response()->error('QAPP_OFF_SHELF');
  230. }
  231. //补充操作:如果该用户未订阅该下架的书籍则删除其阅读记录(书架不予显示)
  232. //判断是否属于包年用户
  233. $year_account = YearOrderService::getRecord($this->uid);
  234. if(!$year_account){
  235. //获取书籍充值类型
  236. $charge_type = $book_info->charge_type;
  237. if($charge_type == 'BOOK'){
  238. //是否购买过该书,购买过则不删除
  239. $res = BookOrderService::getRecordByuidBid($this->uid,$bid);
  240. }elseif($charge_type == 'CHAPTER'){
  241. //是否购买过该书章节,购买过则不删除
  242. $res = ChapterOrderService::checkBookIsOrdered($this->uid,$bid);
  243. }else{
  244. $res = false;
  245. }
  246. if (!$res) {
  247. ReadRecordService::delReadRecordStatic($this->uid,[$bid]);
  248. return response()->error('QAPP_OFF_SHELF');
  249. }
  250. }
  251. }
  252. //wutong,wutong2,wutong3下所有内容都不放快应用
  253. // if(in_array($book_info->cp_source,['wutong','wutong2','wutong3','youyan2'])){
  254. // return response()->error('QAPP_OFF_SHELF');
  255. // }
  256. $this->book_info = $book_info;
  257. //获取章节信息
  258. $chapter = ChapterService::getChapterNameById($cid, $bid);
  259. if (!$chapter) {
  260. //短推长
  261. $combination_chapter = $this->chapterNotExists($bid,$cid);
  262. if($combination_chapter){
  263. $chapter = $combination_chapter;
  264. $bid = $chapter->bid;
  265. $book_info = BookConfigService::getBookById($bid);
  266. $oldbid = \Hashids::encode($bid);
  267. $this->book_info = $book_info;
  268. }else{
  269. return response()->error('QAPP_SYS_ERROR');
  270. }
  271. }
  272. list($is_split,$is_change_chapter_name) = BookService::splitContent($bid);
  273. if($is_split && ($book_info->channel_name == '男频' || $is_change_chapter_name) ){
  274. $chapter->name = '第'.$chapter->sequence.'章';
  275. }
  276. $is_next_day = date('Y-m-d', strtotime($this->user_info->created_at)) == date('Y-m-d', strtotime('-1 days'));
  277. if ($is_next_day) {
  278. $job = new UserRententionJob($this->uid, now(), $this->user_info->created_at);
  279. dispatch($job)->onConnection('rabbitmq')->onQueue('user_rentention_queue');
  280. }
  281. //自定义vip章节
  282. //$vip_sequence = Redis::hget('channel:chapterfee:setting:' . $this->distribution_channel_id, $bid);
  283. $vip_sequence = BookService::getVipSequence($bid,$this->distribution_channel_id,$this->send_order_id);
  284. if($vip_sequence){
  285. if($chapter->sequence >= $vip_sequence){
  286. $chapter->is_vip = 1;
  287. }else{
  288. $chapter->is_vip = 0;
  289. }
  290. }
  291. if ($chapter->is_vip == 0) {
  292. ReadRecordService::addReadLog($this->uid, [
  293. 'distribution_channel_id' => $this->distribution_channel_id,
  294. 'bid' => $bid,
  295. 'cid' => $chapter->id,
  296. 'uid' => $this->uid,
  297. 'send_order_id' => $this->send_order_id,
  298. 'sequence' => $chapter->sequence,
  299. ]);
  300. ReadRecordService::addReadRecord([
  301. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  302. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  303. ]);
  304. return response()->item(new ChapterTransformer, $this->getChapter($bid, $chapter->id, $chapter));
  305. }
  306. // 书籍是否限免
  307. $free = BookConfigService::judgeBookIsFree($bid);
  308. if ($free) {
  309. ReadRecordService::addReadLog($this->uid, [
  310. 'distribution_channel_id' => $this->distribution_channel_id,
  311. 'bid' => $bid,
  312. 'cid' => $chapter->id,
  313. 'uid' => $this->uid,
  314. 'send_order_id' => $this->send_order_id,
  315. 'sequence' => $chapter->sequence,
  316. ]);
  317. ReadRecordService::addReadRecord([
  318. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  319. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  320. ]);
  321. if ($chapter->is_vip == 1) {
  322. $fee = $this->getPrice($book_info, $chapter->size);
  323. $now = date('Y-m-d');
  324. Redis::hincrby('qapp:book:free:virtual:' . $free->id, $now, $fee);
  325. Redis::sadd('qapp:free:virtual' . $now, $free->id);
  326. Redis::sadd('qapp:free:virtual:uids'.$now.$free->id,$this->uid);
  327. Redis::sadd('qapp:free:virtual:uids'.$free->id,$this->uid);
  328. }
  329. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  330. }
  331. //已经付费
  332. if ($this->getOrderRecord($bid, $cid)) {
  333. ReadRecordService::addReadLog($this->uid, [
  334. 'distribution_channel_id' => $this->distribution_channel_id,
  335. 'bid' => $bid,
  336. 'cid' => $chapter->id,
  337. 'uid' => $this->uid,
  338. 'send_order_id' => $this->send_order_id,
  339. 'sequence' => $chapter->sequence,
  340. ]);
  341. ReadRecordService::addReadRecord([
  342. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  343. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  344. ]);
  345. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  346. }
  347. //未付费 要提醒
  348. $user_info = $this->user_info;
  349. //未付费 余额不足
  350. $fee = $this->getPrice($book_info, $chapter->size);
  351. $data = [
  352. 'book_id' => $oldbid,
  353. 'book_name' => $book_info->book_name,
  354. 'chapter_name' => $chapter->name,
  355. 'chapter_id' => $cid,
  356. 'pay_type' => $book_info->charge_type,
  357. 'fee' => $fee,
  358. 'user_balance' => $user_info->balance,
  359. 'product_id' => $book_info->product_id,
  360. 'uid' => $this->uid,
  361. 'distribution_channel_id' => $this->distribution_channel_id,
  362. 'is_discount' => 0,
  363. 'discount_fee' => '',
  364. 'discount' => ''
  365. ];
  366. if ($user_info['balance'] < $fee) {
  367. if ($book_info->charge_type == 'BOOK') {
  368. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  369. } elseif ($book_info->charge_type == 'CHAPTER') {
  370. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  371. } else {
  372. return response()->error('QAPP_SYS_ERROR');
  373. }
  374. }
  375. if(!$this->send_order_id){
  376. if($book_info->charge_type == 'BOOK'){
  377. return response()->error('QAPP_BOOK_BUY', $data);
  378. }/*else{
  379. return response()->error('QAPP_CHAPTER_BUY', $data);
  380. }*/
  381. }
  382. //付费 不提醒
  383. if ($this->balancePay($book_info, $cid, $chapter->size, $chapter->name, 0)) {
  384. UserTaskService::addUserTaskQueue($this->uid, BaseTask::read, UserTaskService::judge_trigger);
  385. ReadRecordService::addReadLog($this->uid, [
  386. 'distribution_channel_id' => $this->distribution_channel_id,
  387. 'bid' => $bid,
  388. 'cid' => $chapter->id,
  389. 'uid' => $this->uid,
  390. 'send_order_id' => $this->send_order_id,
  391. 'sequence' => $chapter->sequence,
  392. ]);
  393. ReadRecordService::addReadRecord([
  394. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  395. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  396. ]);
  397. //限免统计
  398. $free_book = BookConfigService::getByBidNoFilter($bid);
  399. if($free_book) {
  400. if(strtotime($free_book->end_time)+7*86400 >= strtotime(date('Y-m-d'))) {
  401. if(Redis::Sismember('qapp:free:virtual:uids'.$free_book->id,$this->uid)){
  402. $now = date('Y-m-d');
  403. Redis::hincrby('qapp:book:free:actuality:' . $free_book->id, $now, $fee);
  404. Redis::sadd('qapp:free:actuality' . $now, $free_book->id);
  405. Redis::sadd('qapp:free:actuality:uids'.$now.$free_book->id,$this->uid);
  406. }
  407. }
  408. }
  409. if($this->uid == 247081369){
  410. $item = itemTransform(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  411. myLog('xueqi')->info($item);
  412. }
  413. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  414. } else {
  415. if ($book_info->charge_type == 'BOOK') {
  416. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  417. } elseif ($book_info->charge_type == 'CHAPTER') {
  418. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  419. } else {
  420. return response()->error('QAPP_SYS_ERROR');
  421. }
  422. }
  423. }
  424. public function pay(Request $request, $bid, $cid)
  425. {
  426. $remind = (int)$request->input('remind');
  427. $oldbid = $bid;
  428. $bid = BookService::decodeBidStatic($bid);
  429. $book_info = BookConfigService::getBookById($bid);;
  430. if (empty($book_info)) response()->error('QAPP_SYS_ERROR');
  431. $this->book_info = $book_info;
  432. if($this->distribution_channel_id == 7477 && $bid == 13765){
  433. $book_info->is_on_shelf = 2;
  434. }
  435. if ($book_info->is_on_shelf == 0 || $book_info->is_on_shelf == 3) {
  436. if (!$this->isBookOrdered($bid)) {
  437. response()->error('QAPP_OFF_SHELF');
  438. }
  439. }
  440. //获取章节
  441. $chapter = ChapterService::getChapterNameById($cid, $bid);
  442. if (!$chapter) {
  443. $combination_chapter = $this->chapterNotExists($bid,$cid);
  444. if($combination_chapter){
  445. $chapter = $combination_chapter;
  446. $bid = $chapter->bid;
  447. $book_info = BookConfigService::getBookById($bid);
  448. $oldbid = \Hashids::encode($bid);
  449. $this->book_info = $book_info;
  450. }else{
  451. return response()->error('QAPP_SYS_ERROR');
  452. }
  453. }
  454. if ($this->getOrderRecord($bid, $cid)) {
  455. ReadRecordService::addReadLog($this->uid, [
  456. 'distribution_channel_id' => $this->distribution_channel_id,
  457. 'bid' => $bid,
  458. 'cid' => $chapter->id,
  459. 'uid' => $this->uid,
  460. 'send_order_id' => $this->send_order_id,
  461. 'sequence' => $chapter->sequence,
  462. ]);
  463. ReadRecordService::addReadRecord([
  464. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  465. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  466. ]);
  467. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  468. }
  469. if ($this->balancePay($book_info, $cid, $chapter->size, $chapter->name, $remind)) {
  470. UserTaskService::addUserTaskQueue($this->uid, BaseTask::read, UserTaskService::judge_trigger);
  471. ReadRecordService::addReadLog($this->uid, [
  472. 'distribution_channel_id' => $this->distribution_channel_id,
  473. 'bid' => $bid,
  474. 'cid' => $chapter->id,
  475. 'uid' => $this->uid,
  476. 'send_order_id' => $this->send_order_id,
  477. 'sequence' => $chapter->sequence,
  478. ]);
  479. ReadRecordService::addReadRecord([
  480. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  481. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  482. ]);
  483. $free_book = BookConfigService::getByBidNoFilter($bid);
  484. if($free_book) {
  485. if(strtotime($free_book->end_time)+7*86400 >= strtotime(date('Y-m-d'))) {
  486. if(Redis::Sismember('qapp:free:virtual:uids'.$free_book->id,$this->uid)){
  487. $now = date('Y-m-d');
  488. $fee = $this->getPrice($book_info, $chapter->size);
  489. Redis::hincrby('qapp:book:free:actuality:' . $free_book->id, $now, $fee);
  490. Redis::sadd('qapp:free:actuality' . $now, $free_book->id);
  491. Redis::sadd('qapp:free:actuality:uids'.$now.$free_book->id,$this->uid);
  492. }
  493. }
  494. }
  495. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  496. } else {
  497. $fee = $this->getPrice($book_info, $chapter->size);
  498. $data = [
  499. 'book_id' => $oldbid,
  500. 'book_name' => $book_info->book_name,
  501. 'chapter_name' => $chapter->name,
  502. 'chapter_id' => $cid,
  503. 'pay_type' => $book_info->charge_type,
  504. 'fee' => $fee,
  505. 'user_balance' => $this->user_info['balance'],
  506. 'product_id' => $book_info->product_id,
  507. 'uid' => $this->uid,
  508. 'distribution_channel_id' => $this->distribution_channel_id,
  509. 'is_discount' => 0,
  510. 'discount_fee' => '',
  511. 'discount' => ''
  512. ];
  513. if ($book_info->charge_type == 'BOOK') {
  514. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  515. } elseif ($book_info->charge_type == 'CHAPTER') {
  516. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  517. } else {
  518. return response()->error('QAPP_SYS_ERROR');
  519. }
  520. }
  521. }
  522. /**
  523. * 余额支付
  524. * @param $book_info
  525. * @param $chapter_id
  526. * @param $chapter_size
  527. * @return bool
  528. */
  529. protected function balancePay($book_info, $chapter_id, $chapter_size, $chapter_name, $is_remind)
  530. {
  531. $fee = $this->getPrice($book_info, $chapter_size);
  532. if ((int)$this->user_info['balance'] >= $fee) {
  533. if ($this->bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)) {
  534. return true;
  535. }
  536. return false;
  537. } else {
  538. return false;
  539. }
  540. }
  541. /**
  542. * 获取章节内容
  543. * @param $bid
  544. * @param $cid
  545. * @return bool|mixed
  546. */
  547. protected function getChapter($bid, $cid, $chapter)
  548. {
  549. $chapter_content = ChapterService::getChapter($bid, $cid);
  550. if (!$chapter_content) return false;
  551. $chapter->content = trim(str_replace($chapter_content->name, '', $chapter_content->content));
  552. // 格式特殊处理
  553. $qian=array(" "," ","\t");
  554. $hou=array("","","");
  555. // 去掉所有空格
  556. $chapter->content = str_replace($qian,$hou,$chapter->content);
  557. // 2换行变1换行
  558. $chapter->content = str_replace("\n\n", "\n", $chapter->content);
  559. $chapter->content = str_replace("\r\n\r\n", "\r\n", $chapter->content);
  560. // \Log::info('getChapter_chapter:'.$bid.' cid:'.$cid);
  561. // \Log::info($chapter->content);
  562. //统计点击率
  563. $key = 'book_click_num_bid_' . $bid;
  564. $field = date('Y-m-d');
  565. $old = Redis::hget($key, $field);
  566. if (!$old) $old = 0;
  567. Redis::hset($key, $field, $old + 1);
  568. $force_add_desk_type = $this->addDesktopType($bid, $chapter->sequence);
  569. $chapter->force_add_desk_type = $force_add_desk_type;
  570. //统计
  571. $this->stats();
  572. $next_chapter_order_status = $this->nextChapterOrderStatus($bid,$chapter->next_cid);
  573. $chapter->next_chapter_status = $next_chapter_order_status['next_chapter_status'];
  574. $chapter->next_price = $next_chapter_order_status['next_price'];
  575. $chapter->charge_type = $this->book_info->charge_type;
  576. $this->userBookCombination($chapter);
  577. return $chapter;
  578. }
  579. private function nextChapterOrderStatus($bid,$cid){
  580. $chapter = ChapterService::getChapterNameById($cid, $bid);
  581. if(!$chapter || $chapter->is_vip == 0 || $this->book_info->charge_type == 'BOOK'){
  582. return ['next_chapter_status'=>0,'next_price'=>0];
  583. }
  584. $is_paid = $this->getOrderRecord($bid,$cid);
  585. if($is_paid){
  586. return ['next_chapter_status'=>1,'next_price'=>0];
  587. }
  588. $fee = $this->getPrice($this->book_info, $chapter->size);
  589. return ['next_chapter_status'=>2,'next_price'=>$fee];
  590. }
  591. //短续长
  592. private function userBookCombination($chapter){
  593. if($chapter->prev_cid && $chapter->next_cid){
  594. return ;
  595. }
  596. if($chapter->next_cid == 0 && $this->book_info->charge_type == 'BOOK'){
  597. $bid = UserBookCombinationConfigService::selectAndSave($this->uid,$this->book_info->bid);
  598. if($bid){
  599. $bookInfo = BookConfigService::getBookById($bid);
  600. $chapter->next_cid = $bookInfo->first_cid;
  601. }
  602. }
  603. if($chapter->prev_cid == 0 && $this->book_info->charge_type == 'CHAPTER'){
  604. $bid = UserBookCombinationConfigService::getShortBookFromLongBid($this->uid,$this->book_info->bid);
  605. if($bid){
  606. $bookInfo = BookConfigService::getBookById($bid);
  607. $chapter->prev_cid = $bookInfo->last_cid;
  608. }
  609. }
  610. }
  611. private function chapterNotExists($bid,$cid)
  612. {
  613. //1.全局图书组合配置
  614. $combination = BookConfigService::getCombinationInfo($bid);
  615. if($combination){
  616. $chapter = ChapterService::getChapterNameByIdNoCheck($cid);
  617. if($chapter){
  618. return $chapter;
  619. }
  620. return false;
  621. }
  622. //2.用户图书组合配置
  623. if($this->book_info->charge_type == 'CHAPTER'){
  624. $user_combination = UserBookCombinationConfigService::getShortBookFromLongBook($this->uid,$bid);
  625. }else{
  626. $user_combination = UserBookCombinationConfigService::getLongBookFromShortBook($this->uid,$bid);
  627. }
  628. if($user_combination){
  629. $chapter = ChapterService::getChapterNameByIdNoCheck($cid);
  630. if($chapter){
  631. return $chapter;
  632. }
  633. return false;
  634. }
  635. return false;
  636. }
  637. /**
  638. * 添加订购记录
  639. * @param $book_info
  640. * @param $chapter_id
  641. * @param $fee
  642. * @return bool
  643. */
  644. protected function bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)
  645. {
  646. if ($book_info['charge_type'] == 'BOOK') {
  647. $data = [
  648. 'uid' => $this->uid,
  649. 'fee' => $fee,
  650. 'u' => $this->send_order_id,
  651. 'distribution_channel_id' => $this->distribution_channel_id,
  652. 'bid' => $book_info->bid,
  653. 'book_name' => $book_info->book_name,
  654. 'send_order_id' => $this->send_order_id,
  655. ];
  656. return BookOrderService::addOrderRecodeAndDecrUserBalance($data, $this->uid);
  657. } else {
  658. $data = [
  659. 'uid' => $this->uid,
  660. 'fee' => $fee,
  661. 'cid' => $chapter_id,
  662. 'bid' => $book_info->bid,
  663. 'distribution_channel_id' => $this->distribution_channel_id,
  664. 'book_name' => $book_info->book_name,
  665. 'chapter_name' => $chapter_name,
  666. 'send_order_id' => $this->send_order_id,
  667. 'is_remind' => $is_remind
  668. ];
  669. if ($is_remind) {
  670. $this->addOrderRemind($book_info->bid);
  671. }
  672. return ChapterOrderService::addOrderAndDecrUserBalance($data, $this->uid);
  673. }
  674. }
  675. protected function addOrderRemind($bid)
  676. {
  677. if (ChapterReminderService::checkIsNoReminder($this->uid, $bid)) {
  678. return true;
  679. } else {
  680. ChapterReminderService::add($this->uid, $bid);
  681. return true;
  682. }
  683. }
  684. /**
  685. * 是否订购提醒
  686. * @param $chapter_id
  687. * @return bool
  688. */
  689. protected function isOrderRemind($bid)
  690. {
  691. $is_no_reminder = ChapterReminderService::checkIsNoReminder($this->uid, $bid) ? 1 : 0;
  692. return $is_no_reminder == 0;
  693. }
  694. /**
  695. * 用户是否关注
  696. * @param $uid
  697. * @return bool
  698. */
  699. protected function getSubscribe()
  700. {
  701. $res = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $this->uid]);
  702. if ($res) return true;
  703. return false;
  704. }
  705. /**
  706. * 获取订购记录
  707. * @param $book_info
  708. * @param $chapter_id
  709. * @return bool
  710. */
  711. protected function getOrderRecord($bid, $chapter_id)
  712. {
  713. //包年记录
  714. $uid = $this->uid;
  715. $res = YearOrderService::getRecord($uid);
  716. if ($res) return true;
  717. $res = null;
  718. //单本订购记录
  719. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  720. if ($res) return true;
  721. $res = null;
  722. //章节订购记录
  723. $chapterOrder = new ChapterOrderService();
  724. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  725. return false;
  726. }
  727. /**
  728. * 计算价格
  729. * @param $book_info
  730. * @param $chapter_size
  731. * @return float
  732. */
  733. private function getPrice($book_info, $chapter_size = 0)
  734. {
  735. if ($book_info->charge_type == 'BOOK') {
  736. if (BookOrderService::isHasBookOrder($this->uid)) {
  737. $this->is_first_book_order = 0;
  738. return 1399;
  739. } else {
  740. $this->is_first_book_order = 1;
  741. return 899;
  742. }
  743. } else {
  744. // 获取投放后台账号,
  745. $account = '';
  746. $account_send_order = $this->getNowSendOrderInfo();
  747. if($account_send_order){
  748. $account = isset($account_send_order['account'])?$account_send_order['account']:'';
  749. }
  750. $fee = BookService::getPrice($book_info, $this->distribution_channel_id, $chapter_size,$account);
  751. return $fee;
  752. }
  753. }
  754. /**
  755. * 用户添加标签
  756. * @param $book_info
  757. */
  758. protected function addTag($book_info)
  759. {
  760. if (!UserDeepReadTagService::isAddTag($this->uid, $book_info->bid)) {
  761. try {
  762. UserDeepReadTagService::addTag([
  763. 'uid' => $this->uid,
  764. 'bid' => $book_info->bid,
  765. 'book_name' => $book_info->book_name,
  766. 'category_id' => $book_info->category_id,
  767. 'category_name' => $book_info->category_name,
  768. 'sex_preference' => $book_info->channel_name ? $book_info->channel_name : '',
  769. 'distribution_channel_id' => $this->distribution_channel_id ? $this->distribution_channel_id : '0',
  770. 'send_order_id' => $this->send_order_id,
  771. ]);
  772. } catch (\Exception $e) {
  773. }
  774. }
  775. }
  776. protected function isBookOrdered($bid)
  777. {
  778. $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $bid);
  779. if ($chapter_order) return true;
  780. $res = BookOrderService::getRecordByuidBid($this->uid, $bid);
  781. if ($res) return true;
  782. return false;
  783. }
  784. /**
  785. * 判断是否需要充值
  786. */
  787. private function isBookNeedCharge(int $bid, float $price)
  788. {
  789. $book_order = $this->getOrderRecord($bid, 0);
  790. if ($book_order) {
  791. return false;
  792. } else {
  793. $user_info = $this->user_info;
  794. return $user_info['balance'] < $price;
  795. }
  796. }
  797. /**
  798. * 判断章节是否需要充值
  799. */
  800. private function isChapterNeedCharge(int $bid, int $cid, float $price)
  801. {
  802. $book_order = $this->getOrderRecord($bid, $cid);
  803. if ($book_order) {
  804. return false;
  805. } else {
  806. $user_info = $this->user_info;
  807. return $user_info['balance'] < $price;
  808. }
  809. }
  810. private function stats()
  811. {
  812. //阅读器统计
  813. WapVisitStatService::recordReaderUvAndPv($this->uid, $this->distribution_channel_id);
  814. }
  815. //加桌类型
  816. private function addDesktopType($bid, $sequence)
  817. {
  818. $deault_force_add_desk_type = 0;
  819. $send_order_id = $this->GetBindSendOrderId($this->uid);
  820. if ($send_order_id) {
  821. $send_order_info = SendOrderService::getById($send_order_id);
  822. if (!$send_order_info) return $deault_force_add_desk_type;
  823. if ($send_order_info->book_id == $bid) {
  824. // 派单书籍和观看书籍一致,并设置了强加桌,判断当前章节和设置的强加桌章节
  825. if ($send_order_info->force_add_desk_type == 1 && $send_order_info->force_add_desk_seq) {
  826. //在设置的强加桌章节前一章开始
  827. if ($sequence >= ($send_order_info->force_add_desk_seq - 1) ) {
  828. $force_add_desk_type = $send_order_info->force_add_desk_type;
  829. return $force_add_desk_type;
  830. }
  831. }
  832. if ($send_order_info->force_add_desk_type == 2) {
  833. if ($sequence >= $this->book_info->force_subscribe_chapter_seq && $sequence <= $this->book_info->force_subscribe_chapter_seq + 3) {
  834. $force_add_desk_type = $send_order_info->force_add_desk_type;
  835. return $force_add_desk_type;
  836. }
  837. }
  838. if($send_order_info->force_add_desk_type == 3){
  839. //弱加桌+强加桌 ,即强加桌章节之前的章节均弱加桌,之后的章节显示强加桌:
  840. $need_sequence = $send_order_info->force_add_desk_seq;
  841. if(!$need_sequence){
  842. $need_sequence = $this->book_info->force_subscribe_chapter_seq;
  843. }
  844. if( $sequence >= ( $need_sequence-1 ) ){
  845. //之后的章节显示强加桌
  846. return 1;
  847. }else{
  848. ////强加桌章节之前的章节均弱加桌
  849. return 2;
  850. }
  851. }
  852. return $deault_force_add_desk_type;
  853. }
  854. }
  855. //无派单,或者派单书籍与 此次书籍不一致,直接使用 原书籍的默认强关章节
  856. $book_info = BookConfigService::getBookById($bid);
  857. if ($book_info && $book_info->force_subscribe_chapter_seq) {
  858. if ($sequence >= ($book_info->force_subscribe_chapter_seq - 1) ) {
  859. return 2;
  860. }
  861. }
  862. return $deault_force_add_desk_type;
  863. }
  864. /**
  865. * [bindSendOrderId description]
  866. * @param [type] $uid [description]
  867. * @param [type] $send_order_id [description]
  868. * @return [type] [description]
  869. */
  870. public function bindSendOrderId($uid, $send_order_id)
  871. {
  872. if ($send_order_id) {
  873. $res = Redis::hset('book_read_chapter:' . $uid,'send_order_id', $send_order_id);
  874. }
  875. }
  876. /**
  877. * [bindSendOrderId description]
  878. * @param [type] $uid [description]
  879. * @param [type] $send_order_id [description]
  880. * @return [type] [description]
  881. */
  882. public function GetBindSendOrderId($uid)
  883. {
  884. try {
  885. $send_order_id = Redis::hget('book_read_chapter:' . $uid, 'send_order_id');
  886. if ($send_order_id)
  887. return (int)$send_order_id;
  888. } catch (\Exception $e) {
  889. }
  890. }
  891. }