ChapterController.php 35 KB

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