ChapterController.php 42 KB

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