ChapterController.php 35 KB

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