ChapterController.php 32 KB

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