ChapterController.php 42 KB

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