ChapterController.php 30 KB

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