ChapterController.php 33 KB

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