ChapterController.php 31 KB

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