ChapterController.php 35 KB

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