ChapterController.php 27 KB

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