ChapterController.php 35 KB

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