ChapterController.php 32 KB

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