ChapterController.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. if($book_info->charge_type == 'BOOK'){
  255. return response()->error('QAPP_BOOK_BUY', $data);
  256. }else{
  257. return response()->error('QAPP_CHAPTER_BUY', $data);
  258. }
  259. //付费 不提醒
  260. if ($this->balancePay($book_info, $cid, $chapter->size, $chapter->name, 0)) {
  261. UserTaskService::addUserTaskQueue($this->uid, BaseTask::read, UserTaskService::judge_trigger);
  262. ReadRecordService::addReadLog($this->uid, [
  263. 'distribution_channel_id' => $this->distribution_channel_id,
  264. 'bid' => $bid,
  265. 'cid' => $chapter->id,
  266. 'uid' => $this->uid,
  267. 'send_order_id' => $this->send_order_id,
  268. 'sequence' => $chapter->sequence,
  269. ]);
  270. ReadRecordService::addReadRecord([
  271. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  272. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  273. ]);
  274. //限免统计
  275. $free_book = BookConfigService::getByBidNoFilter($bid);
  276. if($free_book) {
  277. if(strtotime($free_book->end_time)+7*86400 >= strtotime(date('Y-m-d'))) {
  278. if(Redis::Sismember('qapp:free:virtual:uids'.$free_book->id,$this->uid)){
  279. $now = date('Y-m-d');
  280. Redis::hincrby('qapp:book:free:actuality:' . $free_book->id, $now, $fee);
  281. Redis::sadd('qapp:free:actuality' . $now, $free_book->id);
  282. Redis::sadd('qapp:free:actuality:uids'.$now.$free_book->id,$this->uid);
  283. }
  284. }
  285. }
  286. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  287. } else {
  288. if ($book_info->charge_type == 'BOOK') {
  289. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  290. } elseif ($book_info->charge_type == 'CHAPTER') {
  291. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  292. } else {
  293. return response()->error('QAPP_SYS_ERROR');
  294. }
  295. }
  296. }
  297. public function pay(Request $request, $bid, $cid)
  298. {
  299. $remind = (int)$request->input('remind');
  300. $oldbid = $bid;
  301. $bid = BookService::decodeBidStatic($bid);
  302. $book_info = BookConfigService::getBookById($bid);;
  303. if (empty($book_info)) response()->error('QAPP_SYS_ERROR');
  304. if($this->distribution_channel_id == 7477 && $bid == 13765){
  305. $book_info->is_on_shelf = 2;
  306. }
  307. if ($book_info->is_on_shelf == 0 || $book_info->is_on_shelf == 3) {
  308. if (!$this->isBookOrdered($bid)) {
  309. response()->error('QAPP_OFF_SHELF');
  310. }
  311. }
  312. //获取章节
  313. $chapter = ChapterService::getChapterNameById($cid, $bid);
  314. if (!$chapter) {
  315. $combination_chapter = $this->chapterNotExists($bid,$cid);
  316. if($combination_chapter){
  317. $chapter = $combination_chapter;
  318. $bid = $chapter->bid;
  319. $book_info = BookConfigService::getBookById($bid);
  320. $oldbid = \Hashids::encode($bid);
  321. $this->book_info = $book_info;
  322. }else{
  323. return response()->error('QAPP_SYS_ERROR');
  324. }
  325. }
  326. if ($this->getOrderRecord($bid, $cid)) {
  327. ReadRecordService::addReadLog($this->uid, [
  328. 'distribution_channel_id' => $this->distribution_channel_id,
  329. 'bid' => $bid,
  330. 'cid' => $chapter->id,
  331. 'uid' => $this->uid,
  332. 'send_order_id' => $this->send_order_id,
  333. 'sequence' => $chapter->sequence,
  334. ]);
  335. ReadRecordService::addReadRecord([
  336. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  337. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  338. ]);
  339. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  340. }
  341. if ($this->balancePay($book_info, $cid, $chapter->size, $chapter->name, $remind)) {
  342. UserTaskService::addUserTaskQueue($this->uid, BaseTask::read, UserTaskService::judge_trigger);
  343. ReadRecordService::addReadLog($this->uid, [
  344. 'distribution_channel_id' => $this->distribution_channel_id,
  345. 'bid' => $bid,
  346. 'cid' => $chapter->id,
  347. 'uid' => $this->uid,
  348. 'send_order_id' => $this->send_order_id,
  349. 'sequence' => $chapter->sequence,
  350. ]);
  351. ReadRecordService::addReadRecord([
  352. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  353. 'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
  354. ]);
  355. $free_book = BookConfigService::getByBidNoFilter($bid);
  356. if($free_book) {
  357. if(strtotime($free_book->end_time)+7*86400 >= strtotime(date('Y-m-d'))) {
  358. if(Redis::Sismember('qapp:free:virtual:uids'.$free_book->id,$this->uid)){
  359. $now = date('Y-m-d');
  360. $fee = $this->getPrice($book_info, $chapter->size);
  361. Redis::hincrby('qapp:book:free:actuality:' . $free_book->id, $now, $fee);
  362. Redis::sadd('qapp:free:actuality' . $now, $free_book->id);
  363. Redis::sadd('qapp:free:actuality:uids'.$now.$free_book->id,$this->uid);
  364. }
  365. }
  366. }
  367. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  368. } else {
  369. $fee = $this->getPrice($book_info, $chapter->size);
  370. $data = [
  371. 'book_id' => $oldbid,
  372. 'book_name' => $book_info->book_name,
  373. 'chapter_name' => $chapter->name,
  374. 'chapter_id' => $cid,
  375. 'pay_type' => $book_info->charge_type,
  376. 'fee' => $fee,
  377. 'user_balance' => $this->user_info['balance'],
  378. 'product_id' => $book_info->product_id,
  379. 'uid' => $this->uid,
  380. 'distribution_channel_id' => $this->distribution_channel_id,
  381. 'is_discount' => 0,
  382. 'discount_fee' => '',
  383. 'discount' => ''
  384. ];
  385. if ($book_info->charge_type == 'BOOK') {
  386. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  387. } elseif ($book_info->charge_type == 'CHAPTER') {
  388. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  389. } else {
  390. return response()->error('QAPP_SYS_ERROR');
  391. }
  392. }
  393. }
  394. /**
  395. * 余额支付
  396. * @param $book_info
  397. * @param $chapter_id
  398. * @param $chapter_size
  399. * @return bool
  400. */
  401. protected function balancePay($book_info, $chapter_id, $chapter_size, $chapter_name, $is_remind)
  402. {
  403. $fee = $this->getPrice($book_info, $chapter_size);
  404. if ((int)$this->user_info['balance'] >= $fee) {
  405. if ($this->bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)) {
  406. return true;
  407. }
  408. return false;
  409. } else {
  410. return false;
  411. }
  412. }
  413. /**
  414. * 获取章节内容
  415. * @param $bid
  416. * @param $cid
  417. * @return bool|mixed
  418. */
  419. protected function getChapter($bid, $cid, $chapter)
  420. {
  421. $chapter_content = ChapterService::getChapter($bid, $cid);
  422. if (!$chapter_content) return false;
  423. $chapter->content = trim(str_replace($chapter_content->name, '', $chapter_content->content));
  424. //统计点击率
  425. $key = 'book_click_num_bid_' . $bid;
  426. $field = date('Y-m-d');
  427. $old = Redis::hget($key, $field);
  428. if (!$old) $old = 0;
  429. Redis::hset($key, $field, $old + 1);
  430. $force_add_desk_type = $this->addDesktopType($bid, $chapter->sequence);
  431. $chapter->force_add_desk_type = $force_add_desk_type;
  432. //统计
  433. $this->stats();
  434. $this->userBookCombination($chapter);
  435. return $chapter;
  436. }
  437. //短续长
  438. private function userBookCombination($chapter){
  439. if($chapter->prev_cid && $chapter->next_cid){
  440. return ;
  441. }
  442. if($chapter->next_cid == 0 && $this->book_info->charge_type == 'BOOK'){
  443. $bid = UserBookCombinationConfigService::selectAndSave($this->uid,$this->book_info->bid);
  444. if($bid){
  445. $bookInfo = BookConfigService::getBookById($bid);
  446. $chapter->next_cid = $bookInfo->first_cid;
  447. }
  448. }
  449. if($chapter->prev_cid == 0 && $this->book_info->charge_type == 'CHAPTER'){
  450. $bid = UserBookCombinationConfigService::getShortBookFromLongBid($this->uid,$this->book_info->bid);
  451. if($bid){
  452. $bookInfo = BookConfigService::getBookById($bid);
  453. $chapter->prev_cid = $bookInfo->last_cid;
  454. }
  455. }
  456. }
  457. private function chapterNotExists($bid,$cid)
  458. {
  459. //1.全局图书组合配置
  460. $combination = BookConfigService::getCombinationInfo($bid);
  461. if($combination){
  462. $chapter = ChapterService::getChapterNameByIdNoCheck($cid);
  463. if($chapter){
  464. return $chapter;
  465. }
  466. return false;
  467. }
  468. //2.用户图书组合配置
  469. if($this->book_info->charge_type == 'CHAPTER'){
  470. $user_combination = UserBookCombinationConfigService::getShortBookFromLongBook($this->uid,$bid);
  471. }else{
  472. $user_combination = UserBookCombinationConfigService::getLongBookFromShortBook($this->uid,$bid);
  473. }
  474. if($user_combination){
  475. $chapter = ChapterService::getChapterNameByIdNoCheck($cid);
  476. if($chapter){
  477. return $chapter;
  478. }
  479. return false;
  480. }
  481. return false;
  482. }
  483. /**
  484. * 添加订购记录
  485. * @param $book_info
  486. * @param $chapter_id
  487. * @param $fee
  488. * @return bool
  489. */
  490. protected function bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)
  491. {
  492. if ($book_info['charge_type'] == 'BOOK') {
  493. $data = [
  494. 'uid' => $this->uid,
  495. 'fee' => $fee,
  496. 'u' => $this->send_order_id,
  497. 'distribution_channel_id' => $this->distribution_channel_id,
  498. 'bid' => $book_info->bid,
  499. 'book_name' => $book_info->book_name,
  500. 'send_order_id' => $this->send_order_id,
  501. ];
  502. return BookOrderService::addOrderRecodeAndDecrUserBalance($data, $this->uid);
  503. } else {
  504. $data = [
  505. 'uid' => $this->uid,
  506. 'fee' => $fee,
  507. 'cid' => $chapter_id,
  508. 'bid' => $book_info->bid,
  509. 'distribution_channel_id' => $this->distribution_channel_id,
  510. 'book_name' => $book_info->book_name,
  511. 'chapter_name' => $chapter_name,
  512. 'send_order_id' => $this->send_order_id,
  513. 'is_remind' => $is_remind
  514. ];
  515. if ($is_remind) {
  516. $this->addOrderRemind($book_info->bid);
  517. }
  518. return ChapterOrderService::addOrderAndDecrUserBalance($data, $this->uid);
  519. }
  520. }
  521. protected function addOrderRemind($bid)
  522. {
  523. if (ChapterReminderService::checkIsNoReminder($this->uid, $bid)) {
  524. return true;
  525. } else {
  526. ChapterReminderService::add($this->uid, $bid);
  527. return true;
  528. }
  529. }
  530. /**
  531. * 是否订购提醒
  532. * @param $chapter_id
  533. * @return bool
  534. */
  535. protected function isOrderRemind($bid)
  536. {
  537. $is_no_reminder = ChapterReminderService::checkIsNoReminder($this->uid, $bid) ? 1 : 0;
  538. return $is_no_reminder == 0;
  539. }
  540. /**
  541. * 用户是否关注
  542. * @param $uid
  543. * @return bool
  544. */
  545. protected function getSubscribe()
  546. {
  547. $res = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $this->uid]);
  548. if ($res) return true;
  549. return false;
  550. }
  551. /**
  552. * 获取订购记录
  553. * @param $book_info
  554. * @param $chapter_id
  555. * @return bool
  556. */
  557. protected function getOrderRecord($bid, $chapter_id)
  558. {
  559. //包年记录
  560. $uid = $this->uid;
  561. $res = YearOrderService::getRecord($uid);
  562. if ($res) return true;
  563. $res = null;
  564. //单本订购记录
  565. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  566. if ($res) return true;
  567. $res = null;
  568. //章节订购记录
  569. $chapterOrder = new ChapterOrderService();
  570. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  571. return false;
  572. }
  573. /**
  574. * 计算价格
  575. * @param $book_info
  576. * @param $chapter_size
  577. * @return float
  578. */
  579. private function getPrice($book_info, $chapter_size = 0)
  580. {
  581. if ($book_info->charge_type == 'BOOK') {
  582. if (BookOrderService::isHasBookOrder($this->uid)) {
  583. $this->is_first_book_order = 0;
  584. return 1399;
  585. } else {
  586. $this->is_first_book_order = 1;
  587. return 899;
  588. }
  589. } else {
  590. // 获取投放后台账号,
  591. $account = '';
  592. if(isset($this->user_info->send_order_id) && $this->user_info->send_order_id){
  593. $account_send_order = QappSendOrder::getSendOrderById($this->user_info->send_order_id);
  594. $account = isset($account_send_order['account'])?$account_send_order['account']:'';
  595. \Log::info('getPrice:'.$this->uid.' account:'.$account.' send_order_id:'.$this->user_info->send_order_id);
  596. }
  597. $fee = BookService::getPrice($book_info, $this->distribution_channel_id, $chapter_size,$account);
  598. return $fee;
  599. }
  600. }
  601. /**
  602. * 用户添加标签
  603. * @param $book_info
  604. */
  605. protected function addTag($book_info)
  606. {
  607. if (!UserDeepReadTagService::isAddTag($this->uid, $book_info->bid)) {
  608. try {
  609. UserDeepReadTagService::addTag([
  610. 'uid' => $this->uid,
  611. 'bid' => $book_info->bid,
  612. 'book_name' => $book_info->book_name,
  613. 'category_id' => $book_info->category_id,
  614. 'category_name' => $book_info->category_name,
  615. 'sex_preference' => $book_info->channel_name ? $book_info->channel_name : '',
  616. 'distribution_channel_id' => $this->distribution_channel_id ? $this->distribution_channel_id : '0',
  617. 'send_order_id' => $this->send_order_id,
  618. ]);
  619. } catch (\Exception $e) {
  620. }
  621. }
  622. }
  623. protected function isBookOrdered($bid)
  624. {
  625. $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $bid);
  626. if ($chapter_order) return true;
  627. $res = BookOrderService::getRecordByuidBid($this->uid, $bid);
  628. if ($res) return true;
  629. return false;
  630. }
  631. /**
  632. * 判断是否需要充值
  633. */
  634. private function isBookNeedCharge(int $bid, float $price)
  635. {
  636. $book_order = $this->getOrderRecord($bid, 0);
  637. if ($book_order) {
  638. return false;
  639. } else {
  640. $user_info = $this->user_info;
  641. return $user_info['balance'] < $price;
  642. }
  643. }
  644. /**
  645. * 判断章节是否需要充值
  646. */
  647. private function isChapterNeedCharge(int $bid, int $cid, float $price)
  648. {
  649. $book_order = $this->getOrderRecord($bid, $cid);
  650. if ($book_order) {
  651. return false;
  652. } else {
  653. $user_info = $this->user_info;
  654. return $user_info['balance'] < $price;
  655. }
  656. }
  657. private function stats()
  658. {
  659. //阅读器统计
  660. WapVisitStatService::recordReaderUvAndPv($this->uid, $this->distribution_channel_id);
  661. }
  662. //加桌类型
  663. private function addDesktopType($bid, $sequence)
  664. {
  665. $force_add_desk_type = 0;
  666. \Log::info('force_add_desk_log:uid:'.$this->uid);
  667. $send_order_id = ReadRecordService::getSendOrderId($this->uid);
  668. if (!$send_order_id) return $force_add_desk_type;
  669. \Log::info('force_add_desk_log:send_order_id:'.$send_order_id);
  670. $send_order_info = SendOrderService::getById($send_order_id);
  671. if (!$send_order_info) return $force_add_desk_type;
  672. \Log::info('force_add_desk_log:book_id:'.$send_order_info->book_id.' bid:'.$bid.' sequence:'.$sequence);
  673. if ($send_order_info->book_id == $bid) {
  674. \Log::info(' force_add_desk_type:'.$send_order_info->force_add_desk_type);
  675. if ($send_order_info->force_add_desk_type == 1 && $send_order_info->force_add_desk_seq) {
  676. \Log::info('force_add_desk_seq:'.$send_order_info->force_add_desk_seq);
  677. if ($sequence >= $send_order_info->force_add_desk_seq) {
  678. $force_add_desk_type = $send_order_info->force_add_desk_type;
  679. }
  680. }
  681. if ($send_order_info->force_add_desk_type == 2) {
  682. if ($sequence >= $this->book_info->force_subscribe_chapter_seq && $sequence <= $this->book_info->force_subscribe_chapter_seq + 3) {
  683. $force_add_desk_type = $send_order_info->force_add_desk_type;
  684. }
  685. }
  686. }
  687. \Log::info('force_add_desk_type_bid:'.$bid.' return_force_add_desk_type:'.$force_add_desk_type);
  688. return $force_add_desk_type;
  689. }
  690. }