ChapterController.php 34 KB

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