ChapterController.php 42 KB

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