ChapterController.php 42 KB

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