ChapterController.php 28 KB

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