ChapterController.php 31 KB

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