ChapterController.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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,$is_need_query_send_order] = $this->addDesktopType($bid, $chapter->sequence);
  588. $chapter->force_add_desk_type = $force_add_desk_type;
  589. $force_add_work_wecaht_type = 0;
  590. $force_add_work_wecaht_url = "";
  591. if($is_need_query_send_order){
  592. [$force_add_work_wecaht_type, $force_add_work_wecaht_url] = $this->addWorkWechatType($chapter->sequence);
  593. }
  594. $chapter->force_add_work_wecaht_type = $force_add_work_wecaht_type;
  595. $chapter->force_add_work_wecaht_url = $force_add_work_wecaht_url;
  596. //统计
  597. $this->stats();
  598. $next_chapter_order_status = $this->nextChapterOrderStatus($bid, $chapter->next_cid);
  599. $chapter->next_chapter_status = $next_chapter_order_status['next_chapter_status'];
  600. $chapter->next_price = $next_chapter_order_status['next_price'];
  601. $chapter->charge_type = $this->book_info->charge_type;
  602. $this->userBookCombination($chapter);
  603. return $chapter;
  604. }
  605. private function nextChapterOrderStatus($bid, $cid)
  606. {
  607. $chapter = ChapterService::getChapterNameById($cid, $bid);
  608. if (!$chapter || $chapter->is_vip == 0 || $this->book_info->charge_type == 'BOOK') {
  609. return ['next_chapter_status' => 0, 'next_price' => 0];
  610. }
  611. $is_paid = $this->getOrderRecord($bid, $cid);
  612. if ($is_paid) {
  613. return ['next_chapter_status' => 1, 'next_price' => 0];
  614. }
  615. $fee = $this->getPrice($this->book_info, $chapter->size);
  616. return ['next_chapter_status' => 2, 'next_price' => $fee];
  617. }
  618. //短续长
  619. private function userBookCombination($chapter)
  620. {
  621. if ($chapter->prev_cid && $chapter->next_cid) {
  622. return;
  623. }
  624. if ($chapter->next_cid == 0 && $this->book_info->charge_type == 'BOOK') {
  625. $bid = UserBookCombinationConfigService::selectAndSave($this->uid, $this->book_info->bid);
  626. if ($bid) {
  627. $bookInfo = BookConfigService::getBookById($bid);
  628. $chapter->next_cid = $bookInfo->first_cid;
  629. }
  630. }
  631. if ($chapter->prev_cid == 0 && $this->book_info->charge_type == 'CHAPTER') {
  632. $bid = UserBookCombinationConfigService::getShortBookFromLongBid($this->uid, $this->book_info->bid);
  633. if ($bid) {
  634. $bookInfo = BookConfigService::getBookById($bid);
  635. $chapter->prev_cid = $bookInfo->last_cid;
  636. }
  637. }
  638. }
  639. private function chapterNotExists($bid, $cid)
  640. {
  641. //1.全局图书组合配置
  642. $combination = BookConfigService::getCombinationInfo($bid);
  643. if ($combination) {
  644. $chapter = ChapterService::getChapterNameByIdNoCheck($cid);
  645. if ($chapter) {
  646. return $chapter;
  647. }
  648. return false;
  649. }
  650. //2.用户图书组合配置
  651. if ($this->book_info->charge_type == 'CHAPTER') {
  652. $user_combination = UserBookCombinationConfigService::getShortBookFromLongBook($this->uid, $bid);
  653. } else {
  654. $user_combination = UserBookCombinationConfigService::getLongBookFromShortBook($this->uid, $bid);
  655. }
  656. if ($user_combination) {
  657. $chapter = ChapterService::getChapterNameByIdNoCheck($cid);
  658. if ($chapter) {
  659. return $chapter;
  660. }
  661. return false;
  662. }
  663. return false;
  664. }
  665. /**
  666. * 添加订购记录
  667. * @param $book_info
  668. * @param $chapter_id
  669. * @param $fee
  670. * @return bool
  671. */
  672. protected function bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)
  673. {
  674. if ($book_info['charge_type'] == 'BOOK') {
  675. $data = [
  676. 'uid' => $this->uid,
  677. 'fee' => $fee,
  678. 'u' => $this->send_order_id,
  679. 'distribution_channel_id' => $this->distribution_channel_id,
  680. 'bid' => $book_info->bid,
  681. 'book_name' => $book_info->book_name,
  682. 'send_order_id' => $this->send_order_id,
  683. ];
  684. return BookOrderService::addOrderRecodeAndDecrUserBalance($data, $this->uid);
  685. } else {
  686. $data = [
  687. 'uid' => $this->uid,
  688. 'fee' => $fee,
  689. 'cid' => $chapter_id,
  690. 'bid' => $book_info->bid,
  691. 'distribution_channel_id' => $this->distribution_channel_id,
  692. 'book_name' => $book_info->book_name,
  693. 'chapter_name' => $chapter_name,
  694. 'send_order_id' => $this->send_order_id,
  695. 'is_remind' => $is_remind
  696. ];
  697. if ($is_remind) {
  698. $this->addOrderRemind($book_info->bid);
  699. }
  700. return ChapterOrderService::addOrderAndDecrUserBalance($data, $this->uid);
  701. }
  702. }
  703. protected function addOrderRemind($bid)
  704. {
  705. if (ChapterReminderService::checkIsNoReminder($this->uid, $bid)) {
  706. return true;
  707. } else {
  708. ChapterReminderService::add($this->uid, $bid);
  709. return true;
  710. }
  711. }
  712. /**
  713. * 是否订购提醒
  714. * @param $chapter_id
  715. * @return bool
  716. */
  717. protected function isOrderRemind($bid)
  718. {
  719. $is_no_reminder = ChapterReminderService::checkIsNoReminder($this->uid, $bid) ? 1 : 0;
  720. return $is_no_reminder == 0;
  721. }
  722. /**
  723. * 用户是否关注
  724. * @param $uid
  725. * @return bool
  726. */
  727. protected function getSubscribe()
  728. {
  729. $res = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $this->uid]);
  730. if ($res) return true;
  731. return false;
  732. }
  733. /**
  734. * 获取订购记录
  735. * @param $book_info
  736. * @param $chapter_id
  737. * @return bool
  738. */
  739. protected function getOrderRecord($bid, $chapter_id)
  740. {
  741. //包年记录
  742. $uid = $this->uid;
  743. $res = YearOrderService::getRecord($uid);
  744. if ($res) return true;
  745. $res = null;
  746. //单本订购记录
  747. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  748. if ($res) return true;
  749. $res = null;
  750. //章节订购记录
  751. $chapterOrder = new ChapterOrderService();
  752. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  753. return false;
  754. }
  755. /**
  756. * 计算价格
  757. * @param $book_info
  758. * @param $chapter_size
  759. * @return float
  760. */
  761. private function getPrice($book_info, $chapter_size = 0)
  762. {
  763. if ($book_info->charge_type == 'BOOK') {
  764. if (BookOrderService::isHasBookOrder($this->uid)) {
  765. $this->is_first_book_order = 0;
  766. return 1399;
  767. } else {
  768. $this->is_first_book_order = 1;
  769. return 899;
  770. }
  771. } else {
  772. // 获取投放后台账号,
  773. $account = '';
  774. $account_send_order = $this->getNowSendOrderInfo();
  775. if ($account_send_order) {
  776. $account = isset($account_send_order['account']) ? $account_send_order['account'] : '';
  777. }
  778. $fee = BookService::getPrice($book_info, $this->distribution_channel_id, $chapter_size, $account);
  779. return $fee;
  780. }
  781. }
  782. /**
  783. * 计算价格
  784. * @param $book_info
  785. * @param $chapter_size
  786. * @return float
  787. */
  788. private function getPriceNew($book_info, $chapter_size = 0, $account = "")
  789. {
  790. if ($book_info->charge_type == 'BOOK') {
  791. if (BookOrderService::isHasBookOrder($this->uid)) {
  792. $this->is_first_book_order = 0;
  793. return 1399;
  794. } else {
  795. $this->is_first_book_order = 1;
  796. return 899;
  797. }
  798. } else {
  799. return BookService::getPrice($book_info, $this->distribution_channel_id, $chapter_size, $account);
  800. }
  801. }
  802. /**
  803. * 获取投放后台账号,
  804. * name: getAccount
  805. * @return mixed|string
  806. * date 2022/10/20 11:29
  807. */
  808. protected function getAccount()
  809. {
  810. $account = '';
  811. $account_send_order = $this->getNowSendOrderInfo();
  812. if ($account_send_order) {
  813. $account = isset($account_send_order['account']) ? $account_send_order['account'] : '';
  814. }
  815. return $account;
  816. }
  817. /**
  818. * 用户添加标签
  819. * @param $book_info
  820. */
  821. protected function addTag($book_info)
  822. {
  823. if (!UserDeepReadTagService::isAddTag($this->uid, $book_info->bid)) {
  824. try {
  825. UserDeepReadTagService::addTag([
  826. 'uid' => $this->uid,
  827. 'bid' => $book_info->bid,
  828. 'book_name' => $book_info->book_name,
  829. 'category_id' => $book_info->category_id,
  830. 'category_name' => $book_info->category_name,
  831. 'sex_preference' => $book_info->channel_name ? $book_info->channel_name : '',
  832. 'distribution_channel_id' => $this->distribution_channel_id ? $this->distribution_channel_id : '0',
  833. 'send_order_id' => $this->send_order_id,
  834. ]);
  835. } catch (\Exception $e) {
  836. }
  837. }
  838. }
  839. protected function isBookOrdered($bid)
  840. {
  841. $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $bid);
  842. if ($chapter_order) return true;
  843. $res = BookOrderService::getRecordByuidBid($this->uid, $bid);
  844. if ($res) return true;
  845. return false;
  846. }
  847. /**
  848. * 判断是否需要充值
  849. */
  850. private function isBookNeedCharge(int $bid, float $price)
  851. {
  852. $book_order = $this->getOrderRecord($bid, 0);
  853. if ($book_order) {
  854. return false;
  855. } else {
  856. $user_info = $this->user_info;
  857. return $user_info['balance'] < $price;
  858. }
  859. }
  860. /**
  861. * 判断章节是否需要充值
  862. */
  863. private function isChapterNeedCharge(int $bid, int $cid, float $price)
  864. {
  865. $book_order = $this->getOrderRecord($bid, $cid);
  866. if ($book_order) {
  867. return false;
  868. } else {
  869. $user_info = $this->user_info;
  870. return $user_info['balance'] < $price;
  871. }
  872. }
  873. private function stats()
  874. {
  875. //阅读器统计
  876. WapVisitStatService::recordReaderUvAndPv($this->uid, $this->distribution_channel_id);
  877. }
  878. //加桌类型
  879. private function addDesktopType($bid, $sequence)
  880. {
  881. $deault_force_add_desk_type = 0;
  882. $send_order_id = $this->GetBindSendOrderId($this->uid);
  883. $is_need_query_order = 0;
  884. if ($send_order_id) {
  885. $send_order_info = SendOrderService::getById($send_order_id);
  886. if (!$send_order_info) return [$deault_force_add_desk_type,$is_need_query_order];
  887. if ($send_order_info->book_id == $bid) {
  888. $is_need_query_order = 1;
  889. // 派单书籍和观看书籍一致,并设置了强加桌,判断当前章节和设置的强加桌章节
  890. if ($send_order_info->force_add_desk_type == 1 && $send_order_info->force_add_desk_seq) {
  891. //在设置的强加桌章节前一章开始
  892. if ($sequence >= ($send_order_info->force_add_desk_seq - 1)) {
  893. $force_add_desk_type = $send_order_info->force_add_desk_type;
  894. return [$force_add_desk_type,$is_need_query_order];
  895. }
  896. }
  897. if ($send_order_info->force_add_desk_type == 2) {
  898. if ($sequence >= $this->book_info->force_subscribe_chapter_seq && $sequence <= $this->book_info->force_subscribe_chapter_seq + 3) {
  899. $force_add_desk_type = $send_order_info->force_add_desk_type;
  900. return [$force_add_desk_type,$is_need_query_order];
  901. }
  902. }
  903. if ($send_order_info->force_add_desk_type == 3) {
  904. //弱加桌+强加桌 ,即强加桌章节之前的章节均弱加桌,之后的章节显示强加桌:
  905. $need_sequence = $send_order_info->force_add_desk_seq;
  906. if (!$need_sequence) {
  907. $need_sequence = $this->book_info->force_subscribe_chapter_seq;
  908. }
  909. if ($sequence >= ($need_sequence - 1)) {
  910. //之后的章节显示强加桌
  911. return [1,$is_need_query_order];
  912. } else {
  913. ////强加桌章节之前的章节均弱加桌
  914. return [2,$is_need_query_order];
  915. }
  916. }
  917. return [$deault_force_add_desk_type,$is_need_query_order];
  918. }
  919. }
  920. //无派单,或者派单书籍与 此次书籍不一致,直接使用 原书籍的默认强关章节
  921. $book_info = BookConfigService::getBookById($bid);
  922. if ($book_info && $book_info->force_subscribe_chapter_seq) {
  923. if ($sequence >= ($book_info->force_subscribe_chapter_seq - 1)) {
  924. return [2,$is_need_query_order];
  925. }
  926. }
  927. return [$deault_force_add_desk_type,$is_need_query_order];
  928. }
  929. private function addWorkWechatType($sequence)
  930. {
  931. $deault_force_add_desk_type = 0;
  932. $work_wechat_url = "";
  933. $send_order_id = $this->GetBindSendOrderId($this->uid);
  934. if ($send_order_id) {
  935. $send_order_info = QappSendOrder::getSendOrderById($send_order_id);
  936. if (!$send_order_info) return [$deault_force_add_desk_type, $work_wechat_url];
  937. if(in_array($send_order_info['force_add_work_wecaht_type'],[1,2]) && $send_order_info['force_add_work_wecaht_seq'] <= ($sequence+1)){
  938. $deault_force_add_desk_type =$send_order_info['force_add_work_wecaht_type'];
  939. $work_wechat_url =$send_order_info['force_add_work_wecaht_url'];
  940. }
  941. }
  942. return [$deault_force_add_desk_type, $work_wechat_url];
  943. }
  944. /**
  945. * [bindSendOrderId description]
  946. * @param [type] $uid [description]
  947. * @param [type] $send_order_id [description]
  948. * @return [type] [description]
  949. */
  950. public function bindSendOrderId($uid, $send_order_id)
  951. {
  952. if ($send_order_id) {
  953. $res = Redis::hset('book_read_chapter:' . $uid, 'send_order_id', $send_order_id);
  954. }
  955. }
  956. /**
  957. * [bindSendOrderId description]
  958. * @param [type] $uid [description]
  959. * @param [type] $send_order_id [description]
  960. * @return [type] [description]
  961. */
  962. public function GetBindSendOrderId($uid)
  963. {
  964. try {
  965. $send_order_id = Redis::hget('book_read_chapter:' . $uid, 'send_order_id');
  966. if ($send_order_id)
  967. return (int)$send_order_id;
  968. } catch (\Exception $e) {
  969. }
  970. }
  971. private function getChapterIds($chapters): array
  972. {
  973. $list = [];
  974. if ($chapters) {
  975. foreach ($chapters as $v) {
  976. $list[] = $v->id;
  977. }
  978. }
  979. return $list;
  980. }
  981. private function getHandelData(int $bid, array $chapterIds): array
  982. {
  983. $uid = $this->uid;
  984. $res = YearOrderService::getRecord($uid);
  985. if ($res) {
  986. return $handle = ['check' => 0, 'paycid' => []];
  987. }
  988. $res = null;
  989. //单本订购记录
  990. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  991. if ($res) {
  992. return $handle = ['check' => 0, 'paycid' => []];
  993. }
  994. //章节订购记录
  995. $chapterOrder = new ChapterOrderService();
  996. $res = $chapterOrder->getOrdersByChapterIds($uid, $bid, $chapterIds);
  997. return $handle = ['check' => 1, 'paycid' => $res];
  998. }
  999. /**
  1000. * 判断是否需要充值
  1001. * name: isChapterNeedChargeNew
  1002. * @param $cid
  1003. * @param $rules
  1004. * @return bool
  1005. * date 2022/10/20 16:08
  1006. */
  1007. private function isChapterNeedChargeNew($cid, $rules, $price = 0): bool
  1008. {
  1009. if ($rules['check'] != 1) {
  1010. return false;
  1011. }
  1012. if (empty($rules['paycid']) || !in_array($cid, $rules['paycid'])) {
  1013. $user_info = $this->user_info;
  1014. return $user_info['balance'] < $price;
  1015. } else {
  1016. return false;
  1017. }
  1018. }
  1019. /*****
  1020. * 是否订阅
  1021. * name: isSubscribe
  1022. * @param $cid
  1023. * @param $rules
  1024. * @return int
  1025. * date 2022/11/02 17:19
  1026. */
  1027. private function isSubscribe($cid, $rules): int
  1028. {
  1029. if ($rules['check'] != 1) {
  1030. return 1;
  1031. }
  1032. if (empty($rules['paycid']) || !in_array($cid, $rules['paycid'])) {
  1033. return 2;
  1034. } else {
  1035. return 1;
  1036. }
  1037. }
  1038. }