ChapterController.php 26 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 Illuminate\Http\Request;
  6. use App\Http\Controllers\QuickApp\BaseController;
  7. use Redis;
  8. use App\Modules\Book\Services\ChapterService;
  9. use App\Modules\User\Services\ReadRecordService;
  10. use App\Http\Controllers\QuickApp\Book\Transformers\ChapterTransformer;
  11. use App\Modules\Book\Services\BookConfigService;
  12. use App\Http\Controllers\QuickApp\Book\Transformers\ChapterListTransformer;
  13. use App\Jobs\UserRententionJob;
  14. use App\Modules\Book\Services\BookService;
  15. use App\Modules\Subscribe\Services\BookOrderService;
  16. use App\Modules\Subscribe\Services\ChapterOrderService;
  17. use App\Modules\Subscribe\Services\YearOrderService;
  18. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  19. use App\Modules\Subscribe\Services\ChapterReminderService;
  20. use App\Modules\User\Services\UserDeepReadTagService;
  21. use App\Modules\UserTask\Services\BaseTask;
  22. use App\Modules\UserTask\Services\UserTaskService;
  23. class ChapterController extends BaseController
  24. {
  25. /**
  26. * @apiDefine Chapter 章节
  27. */
  28. /**
  29. * @apiVersion 1.0.0
  30. * @apiDescription 章节列表不分页
  31. * @api {get} books/{bid}/allcatalog 章节列表不分页
  32. * @apiParam {String} [token] token
  33. * @apiHeader {String} [Authorization] token 两个token任选其一
  34. * @apiGroup Chapter
  35. * @apiName getCatalog
  36. * @apiSuccess {int} code 状态码
  37. * @apiSuccess {String} msg 信息
  38. * @apiSuccess {object} data 结果集
  39. * @apiSuccess {Array} data.list 分页结果集
  40. * @apiSuccess {Int} data.list.bid bid
  41. * @apiSuccess {Int} data.list.chapter_id 章节id
  42. * @apiSuccess {String} data.list.chapter_name 章节名称
  43. * @apiSuccess {Int} data.list.chapter_sequence 序号
  44. * @apiSuccess {Int} data.list.chapter_is_vip 是否vip
  45. * @apiSuccess {Int} data.list.chapter_size 章节大小
  46. * @apiSuccess {Int} data.list.prev_cid 上一章节id
  47. * @apiSuccess {Int} data.list.next_cid 下一章节
  48. * @apiSuccess {String} data.list.recent_update_at 更新时间
  49. * @apiSuccess {Int} data.list.is_need_charge 是否需要充值
  50. * @apiSuccess {object} data.meta 分页信息
  51. * @apiSuccess {Int} data.meta.total 总条数
  52. * @apiSuccess {Int} data.meta.per_page 每页条数
  53. * @apiSuccess {Int} data.meta.current_page 当前页
  54. * @apiSuccess {Int} data.meta.last_page 最后页
  55. * @apiSuccess {String} data.meta.next_page_url 下一页
  56. * @apiSuccess {String} data.meta.prev_page_url 上一页
  57. * @apiSuccessExample {json} Success-Response:
  58. * HTTP/1.1 200 OK
  59. * {
  60. * code: 0,
  61. * msg: "",
  62. * data:
  63. * [
  64. * {
  65. * bid: 5,
  66. * chapter_id: 5,
  67. * chapter_name: "第1240章 不是我",
  68. * chapter_sequence: 1239,
  69. * chapter_is_vip: 1,
  70. * chapter_size: 2422,
  71. * prev_cid: 0,
  72. * next_cid: 0,
  73. * recent_update_at: 2017-11-20 15:01:56,
  74. * is_need_charge: 0,
  75. * },
  76. * {
  77. * bid: 5,
  78. * chapter_id: 5,
  79. * chapter_name: "第1240章 不是我",
  80. * chapter_sequence: 1239,
  81. * chapter_is_vip: 1,
  82. * chapter_size: 2422,
  83. * prev_cid: 0,
  84. * next_cid: 0,
  85. * recent_update_at: 2017-11-20 15:01:56,
  86. * is_need_charge: 0,
  87. * },
  88. * ]
  89. * }
  90. */
  91. public function getCatalog(Request $request, $bid)
  92. {
  93. $bid = BookService::decodeBidStatic($bid);
  94. $lists = ChapterService::getChapterLists($bid);
  95. $book_info = BookConfigService::getBookById($bid);
  96. if (!$book_info) {
  97. return response()->error('PARAM_ERROR');
  98. }
  99. $lists = $this->getChapterCatalog($bid, $lists, $book_info);
  100. return response()->collection(new ChapterListTransformer, $lists);
  101. }
  102. /**
  103. * @apiVersion 1.0.0
  104. * @apiDescription 章节列表分页
  105. * @api {get} books/{bid}/catalog 章节列表分页
  106. * @apiParam {String} [token] token
  107. * @apiHeader {String} [Authorization] token 两个token任选其一
  108. * @apiGroup Chapter
  109. * @apiName getCatalogPerPage
  110. * @apiParam {Int} page_size 分页大小(默认15)
  111. * @apiParam {Int} page 页码(默认1)
  112. * @apiSuccess {int} code 状态码
  113. * @apiSuccess {String} msg 信息
  114. * @apiSuccess {object} data 结果集
  115. * @apiSuccess {Array} data.list 分页结果集
  116. * @apiSuccess {Int} data.list.bid bid
  117. * @apiSuccess {Int} data.list.chapter_id 章节id
  118. * @apiSuccess {String} data.list.chapter_name 章节名称
  119. * @apiSuccess {Int} data.list.chapter_sequence 序号
  120. * @apiSuccess {Int} data.list.chapter_is_vip 是否vip
  121. * @apiSuccess {Int} data.list.chapter_size 章节大小
  122. * @apiSuccess {Int} data.list.prev_cid 上一章节id
  123. * @apiSuccess {Int} data.list.next_cid 下一章节
  124. * @apiSuccess {String} data.list.recent_update_at 更新时间
  125. * @apiSuccess {Int} data.list.is_need_charge 是否需要充值
  126. * @apiSuccess {object} data.meta 分页信息
  127. * @apiSuccess {Int} data.meta.total 总条数
  128. * @apiSuccess {Int} data.meta.per_page 每页条数
  129. * @apiSuccess {Int} data.meta.current_page 当前页
  130. * @apiSuccess {Int} data.meta.last_page 最后页
  131. * @apiSuccess {String} data.meta.next_page_url 下一页
  132. * @apiSuccess {String} data.meta.prev_page_url 上一页
  133. * @apiSuccessExample {json} Success-Response:
  134. * HTTP/1.1 200 OK
  135. * {
  136. * code: 0,
  137. * msg: "",
  138. * data:
  139. * list:[
  140. * {
  141. * bid: 5,
  142. * chapter_id: 5,
  143. * chapter_name: "第1240章 不是我",
  144. * chapter_sequence: 1239,
  145. * chapter_is_vip: 1,
  146. * chapter_size: 2422,
  147. * prev_cid: 0,
  148. * next_cid: 0,
  149. * recent_update_at: 2017-11-20 15:01:56,
  150. * is_need_charge: 0,
  151. * },
  152. * {
  153. * bid: 5,
  154. * chapter_id: 5,
  155. * chapter_name: "第1240章 不是我",
  156. * chapter_sequence: 1239,
  157. * chapter_is_vip: 1,
  158. * chapter_size: 2422,
  159. * prev_cid: 0,
  160. * next_cid: 0,
  161. * recent_update_at: 2017-11-20 15:01:56,
  162. * is_need_charge: 0,
  163. * },
  164. * ]
  165. * meta:{
  166. * total: 1253,
  167. * per_page: 15,
  168. * current_page: 1,
  169. * last_page: 84,
  170. * next_page_url: "http://myapi.cn/api/books/1/chapter?page=2",
  171. * prev_page_url: ""
  172. * }
  173. * }
  174. */
  175. public function getCatalogPerPage(Request $request, $bid)
  176. {
  177. $bid = BookService::decodeBidStatic($bid);
  178. $book_info = BookConfigService::getBookById($bid);
  179. if (!$book_info) {
  180. return response()->error('PARAM_ERROR');
  181. }
  182. $page_size = $request->input('page_size', 15);
  183. if ($page_size >= 100) $page_size = 100;
  184. $res = ChapterService::getChapterListsPage($bid, $page_size);
  185. $lists = $this->getChapterCatalog($bid, $res, $book_info);
  186. return response()->pagination(new ChapterListTransformer, $lists);
  187. }
  188. private function getChapterCatalog(int $bid, $chapters, $book_info)
  189. {
  190. switch ($book_info->charge_type) {
  191. case 'BOOK':
  192. $price = $this->getPrice($book_info);
  193. $is_need_charge = $this->isBookNeedCharge($bid, $price);
  194. foreach ($chapters as $v) {
  195. $v->is_need_charge = $v->is_vip ? $is_need_charge : false;
  196. $v->price = $price;
  197. }
  198. break;
  199. default:
  200. foreach ($chapters as $v) {
  201. $v->price = $v->is_vip ? $this->getPrice($book_info, $v->size) : 0;
  202. $v->is_need_charge = $v->is_vip ? $this->isChapterNeedCharge($bid, $v->id, $v->price) : false;
  203. }
  204. break;
  205. }
  206. return $chapters;
  207. }
  208. /**
  209. * @apiVersion 1.0.0
  210. * @apiDescription 章节内容
  211. * @api {get} books/{bid}/chapters/{chapter_id} 章节内容
  212. * @apiParam {String} [token] token
  213. * @apiHeader {String} [Authorization] token 两个token任选其一
  214. * @apiGroup Chapter
  215. * @apiName index
  216. * @apiSuccess {int} code 状态码
  217. * @apiSuccess {String} msg 信息
  218. * @apiSuccess {object} data 结果集
  219. * @apiSuccess {Int} data.chapter_id 章节id
  220. * @apiSuccess {String} data.chapter_name 章节名称
  221. * @apiSuccess {Int} data.chapter_sequence 序号
  222. * @apiSuccess {Int} data.chapter_is_vip 是否vip
  223. * @apiSuccess {Int} data.chapter_size 章节大小
  224. * @apiSuccess {Int} data.prev_cid 上一章节id
  225. * @apiSuccess {Int} data.next_cid 下一章节
  226. * @apiSuccess {String} data.recent_update_at 更新时间
  227. * @apiSuccess {String} data.chapter_content 章节内容
  228. * @apiSuccessExample {json} Success-Response:
  229. * HTTP/1.1 200 OK
  230. * {
  231. * code: 0,
  232. * msg: "",
  233. * data: {
  234. * chapter_id: 5,
  235. * chapter_name: "第1240章 不是我",
  236. * chapter_sequence: 1239,
  237. * chapter_is_vip: 1,
  238. * chapter_size: 2422,
  239. * prev_cid: 0,
  240. * next_cid: 0,
  241. * recent_update_at: 2017-11-20 15:01:56,
  242. * chapter_content: "叶妩被司行霈的阴阳怪气一吓,思路偏得太远了。 她张口结舌,忘记了自己要说什么。",
  243. * }
  244. * }
  245. */
  246. public function index(Request $request, $bid, $cid)
  247. {
  248. $oldbid = $bid;
  249. $bid = BookService::decodeBidStatic($bid);
  250. //获取图书信息
  251. $book_info = BookConfigService::getBookById($bid);
  252. if (empty($book_info))
  253. return response()->error('QAPP_SYS_ERROR');
  254. //获取章节信息
  255. $chapter = ChapterService::getChapterNameById($cid, $bid);
  256. if (!$chapter) {
  257. return response()->error('QAPP_SYS_ERROR');
  258. }
  259. $is_next_day = date('Y-m-d', strtotime($this->user_info->created_at)) == date('Y-m-d', strtotime('-1 days'));
  260. if ($is_next_day) {
  261. $job = new UserRententionJob($this->uid, now(), $this->user_info->created_at);
  262. dispatch($job)->onConnection('rabbitmq')->onQueue('user_rentention_queue');
  263. }
  264. if ($chapter->is_vip == 0) {
  265. ReadRecordService::addReadRecord([
  266. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  267. 'cid' => $cid, 'chapter_name' => $chapter->name
  268. ]);
  269. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  270. }
  271. //已经付费
  272. if ($this->getOrderRecord($bid, $cid)) {
  273. ReadRecordService::addReadRecord([
  274. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  275. 'cid' => $cid, 'chapter_name' => $chapter->name
  276. ]);
  277. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  278. }
  279. //未付费 要提醒
  280. $user_info = $this->user_info;
  281. //未付费 余额不足
  282. $fee = $this->getPrice($book_info, $chapter->size);
  283. $data = [
  284. 'book_id' => $oldbid,
  285. 'book_name' => $book_info->book_name,
  286. 'chapter_name' => $chapter->name,
  287. 'chapter_id' => $cid,
  288. 'pay_type' => $book_info->charge_type,
  289. 'fee' => $fee,
  290. 'user_balance' => $user_info->balance,
  291. 'product_id' => $book_info->product_id,
  292. 'uid' => $this->uid,
  293. 'distribution_channel_id' => $this->distribution_channel_id,
  294. 'is_discount' => 0,
  295. 'discount_fee' => '',
  296. 'discount' => ''
  297. ];
  298. if ($user_info['balance'] < $fee) {
  299. if ($book_info->charge_type == 'BOOK') {
  300. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  301. } elseif ($book_info->charge_type == 'CHAPTER') {
  302. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  303. } else {
  304. return response()->error('QAPP_SYS_ERROR');
  305. }
  306. }
  307. //付费 不提醒
  308. if ($this->balancePay($book_info, $cid, $chapter->size, $chapter->name, 0)) {
  309. UserTaskService::addUserTaskQueue($this->uid, BaseTask::read, UserTaskService::judge_trigger);
  310. ReadRecordService::addReadRecord([
  311. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  312. 'cid' => $cid, 'chapter_name' => $chapter->name
  313. ]);
  314. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  315. } else {
  316. if ($book_info->charge_type == 'BOOK') {
  317. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  318. } elseif ($book_info->charge_type == 'CHAPTER') {
  319. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  320. } else {
  321. return response()->error('QAPP_SYS_ERROR');
  322. }
  323. }
  324. }
  325. /**
  326. * @apiVersion 1.0.0
  327. * @apiDescription 余额支付
  328. * @api {get} books/{bid}/balance/chapterOrders/{cid} 余额支付
  329. * @apiParam {String} [token] token
  330. * @apiHeader {String} [Authorization] token 两个token任选其一
  331. * @apiGroup Chapter
  332. * @apiName pay
  333. * @apiParam (Int) remind 提醒
  334. * @apiSuccess {int} code 状态码
  335. * @apiSuccess {String} msg 信息
  336. * @apiSuccess {object} data 结果集
  337. * @apiSuccess {Int} data.chapter_id 章节id
  338. * @apiSuccess {String} data.chapter_name 章节名称
  339. * @apiSuccess {Int} data.chapter_sequence 序号
  340. * @apiSuccess {Int} data.chapter_is_vip 是否vip
  341. * @apiSuccess {Int} data.chapter_size 章节大小
  342. * @apiSuccess {Int} data.prev_cid 上一章节id
  343. * @apiSuccess {Int} data.next_cid 下一章节
  344. * @apiSuccess {String} data.recent_update_at 更新时间
  345. * @apiSuccess {String} data.chapter_content 章节内容
  346. * @apiSuccessExample {json} Success-Response:
  347. * HTTP/1.1 200 OK
  348. * {
  349. * code: 0,
  350. * msg: "",
  351. * data: {
  352. * chapter_id: 5,
  353. * chapter_name: "第1240章 不是我",
  354. * chapter_sequence: 1239,
  355. * chapter_is_vip: 1,
  356. * chapter_size: 2422,
  357. * prev_cid: 0,
  358. * next_cid: 0,
  359. * recent_update_at: 2017-11-20 15:01:56,
  360. * chapter_content: "叶妩被司行霈的阴阳怪气一吓,思路偏得太远了。 她张口结舌,忘记了自己要说什么。",
  361. * }
  362. * }
  363. */
  364. public function pay(Request $request, $bid, $cid)
  365. {
  366. $remind = (int) $request->input('remind');
  367. $oldbid = $bid;
  368. $bid = BookService::decodeBidStatic($bid);
  369. $book_info = BookConfigService::getBookById($bid);;
  370. if (empty($book_info)) response()->error('QAPP_SYS_ERROR');
  371. if ($book_info->is_on_shelf == 0 || $book_info->is_on_shelf == 3) {
  372. if (!$this->isBookOrdered($bid)) {
  373. response()->error('QAPP_OFF_SHELF');
  374. }
  375. }
  376. //获取章节
  377. $chapter = ChapterService::getChapterNameById($cid, $bid);
  378. if (!$chapter) {
  379. return response()->error('QAPP_SYS_ERROR');
  380. }
  381. if ($this->balancePay($book_info, $cid, $chapter->size, $chapter->name, $remind)) {
  382. ReadRecordService::addReadRecord([
  383. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  384. 'cid' => $cid, 'chapter_name' => $chapter->name
  385. ]);
  386. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  387. } else {
  388. $fee = $this->getPrice($book_info, $chapter->size);
  389. $data = [
  390. 'book_id' => $oldbid,
  391. 'book_name' => $book_info->book_name,
  392. 'chapter_name' => $chapter->name,
  393. 'chapter_id' => $cid,
  394. 'pay_type' => $book_info->charge_type,
  395. 'fee' => $fee,
  396. 'user_balance' => $this->user_info['balance'],
  397. 'product_id' => $book_info->product_id,
  398. 'uid' => $this->uid,
  399. 'distribution_channel_id' => $this->distribution_channel_id,
  400. 'is_discount' => 0,
  401. 'discount_fee' => '',
  402. 'discount' => ''
  403. ];
  404. if ($book_info->charge_type == 'BOOK') {
  405. return response()->error('QAPP_BOOK_INSUFFICIENT_BALANCE', $data);
  406. } elseif ($book_info->charge_type == 'CHAPTER') {
  407. return response()->error('QAPP_CHAPTER_INSUFFICIENT_BALANCE', $data);
  408. } else {
  409. return response()->error('QAPP_SYS_ERROR');
  410. }
  411. }
  412. }
  413. /**
  414. * 余额支付
  415. * @param $book_info
  416. * @param $chapter_id
  417. * @param $chapter_size
  418. * @return bool
  419. */
  420. protected function balancePay($book_info, $chapter_id, $chapter_size, $chapter_name, $is_remind)
  421. {
  422. $fee = $this->getPrice($book_info, $chapter_size);
  423. if ((int) $this->user_info['balance'] >= $fee) {
  424. if ($this->bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)) {
  425. return true;
  426. }
  427. return false;
  428. } else {
  429. return false;
  430. }
  431. }
  432. /**
  433. * 获取章节内容
  434. * @param $bid
  435. * @param $cid
  436. * @return bool|mixed
  437. */
  438. protected function getChapter($bid, $cid, $chapter)
  439. {
  440. $chapter_content = ChapterService::getChapter($bid, $cid);
  441. if (!$chapter_content) return false;
  442. $chapter->content = trim(str_replace($chapter_content->name, '', $chapter_content->content));
  443. //统计点击率
  444. $key = 'book_click_num_bid_' . $bid;
  445. $field = date('Y-m-d');
  446. $old = Redis::hget($key, $field);
  447. if (!$old) $old = 0;
  448. Redis::hset($key, $field, $old + 1);
  449. $force_add_desk_type = $this->addDesktopType($bid,$chapter->sequence);
  450. $chapter->force_add_desk_type = $force_add_desk_type;
  451. //统计
  452. $this->stats();
  453. return $chapter;
  454. }
  455. /**
  456. * 添加订购记录
  457. * @param $book_info
  458. * @param $chapter_id
  459. * @param $fee
  460. * @return bool
  461. */
  462. protected function bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)
  463. {
  464. if ($book_info['charge_type'] == 'BOOK') {
  465. $data = [
  466. 'uid' => $this->uid,
  467. 'fee' => $fee,
  468. 'u' => $this->send_order_id,
  469. 'distribution_channel_id' => $this->distribution_channel_id,
  470. 'bid' => $book_info->bid,
  471. 'book_name' => $book_info->book_name,
  472. 'send_order_id' => $this->send_order_id,
  473. ];
  474. return BookOrderService::addOrderRecodeAndDecrUserBalance($data, $this->uid);
  475. } else {
  476. $data = [
  477. 'uid' => $this->uid,
  478. 'fee' => $fee,
  479. 'cid' => $chapter_id,
  480. 'bid' => $book_info->bid,
  481. 'distribution_channel_id' => $this->distribution_channel_id,
  482. 'book_name' => $book_info->book_name,
  483. 'chapter_name' => $chapter_name,
  484. 'send_order_id' => $this->send_order_id,
  485. 'is_remind' => $is_remind
  486. ];
  487. if ($is_remind) {
  488. $this->addOrderRemind($book_info->bid);
  489. }
  490. return ChapterOrderService::addOrderAndDecrUserBalance($data, $this->uid);
  491. }
  492. }
  493. protected function addOrderRemind($bid)
  494. {
  495. if (ChapterReminderService::checkIsNoReminder($this->uid, $bid)) {
  496. return true;
  497. } else {
  498. ChapterReminderService::add($this->uid, $bid);
  499. return true;
  500. }
  501. }
  502. /**
  503. * 是否订购提醒
  504. * @param $chapter_id
  505. * @return bool
  506. */
  507. protected function isOrderRemind($bid)
  508. {
  509. $is_no_reminder = ChapterReminderService::checkIsNoReminder($this->uid, $bid) ? 1 : 0;
  510. return $is_no_reminder == 0;
  511. }
  512. /**
  513. * 用户是否关注
  514. * @param $uid
  515. * @return bool
  516. */
  517. protected function getSubscribe()
  518. {
  519. $res = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $this->uid]);
  520. if ($res) return true;
  521. return false;
  522. }
  523. /**
  524. * 获取订购记录
  525. * @param $book_info
  526. * @param $chapter_id
  527. * @return bool
  528. */
  529. protected function getOrderRecord($bid, $chapter_id)
  530. {
  531. //包年记录
  532. $uid = $this->uid;
  533. $res = YearOrderService::getRecord($uid);
  534. if ($res) return true;
  535. $res = null;
  536. //单本订购记录
  537. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  538. if ($res) return true;
  539. $res = null;
  540. //章节订购记录
  541. $chapterOrder = new ChapterOrderService();
  542. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  543. return false;
  544. }
  545. /**
  546. * 计算价格
  547. * @param $book_info
  548. * @param $chapter_size
  549. * @return float
  550. */
  551. /*protected function getPrice_($book_info, $chapter_size = 0)
  552. {
  553. if ($book_info->charge_type == 'BOOK')
  554. return $book_info->price * 100;
  555. return ceil($chapter_size / 100);
  556. }*/
  557. private function getPrice($book_info, $chapter_size = 0)
  558. {
  559. if ($book_info->charge_type == 'BOOK') {
  560. if (BookOrderService::isHasBookOrder($this->uid)) {
  561. $this->is_first_book_order = 0;
  562. return 1399;
  563. } else {
  564. $this->is_first_book_order = 1;
  565. return 899;
  566. }
  567. } else {
  568. $fee = BookService::getPrice($book_info, $this->distribution_channel_id, $chapter_size);
  569. return $fee;
  570. }
  571. }
  572. /**
  573. * 用户添加标签
  574. * @param $book_info
  575. */
  576. protected function addTag($book_info)
  577. {
  578. if (!UserDeepReadTagService::isAddTag($this->uid, $book_info->bid)) {
  579. try {
  580. UserDeepReadTagService::addTag([
  581. 'uid' => $this->uid,
  582. 'bid' => $book_info->bid,
  583. 'book_name' => $book_info->book_name,
  584. 'category_id' => $book_info->category_id,
  585. 'category_name' => $book_info->category_name,
  586. 'sex_preference' => $book_info->channel_name ? $book_info->channel_name : '',
  587. 'distribution_channel_id' => $this->distribution_channel_id ? $this->distribution_channel_id : '0',
  588. 'send_order_id' => $this->send_order_id,
  589. ]);
  590. } catch (\Exception $e) {
  591. }
  592. }
  593. }
  594. protected function isBookOrdered($bid)
  595. {
  596. $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $bid);
  597. if ($chapter_order) return true;
  598. $res = BookOrderService::getRecordByuidBid($this->uid, $bid);
  599. if ($res) return true;
  600. return false;
  601. }
  602. /**
  603. * 判断是否需要充值
  604. */
  605. private function isBookNeedCharge(int $bid, float $price)
  606. {
  607. $book_order = $this->getOrderRecord($bid, 0);
  608. if ($book_order) {
  609. return false;
  610. } else {
  611. $user_info = $this->user_info;
  612. return $user_info['balance'] < $price;
  613. }
  614. }
  615. /**
  616. * 判断章节是否需要充值
  617. */
  618. private function isChapterNeedCharge(int $bid, int $cid, float $price)
  619. {
  620. $book_order = $this->getOrderRecord($bid, $cid);
  621. if ($book_order) {
  622. return false;
  623. } else {
  624. $user_info = $this->user_info;
  625. return $user_info['balance'] < $price;
  626. }
  627. }
  628. private function stats()
  629. {
  630. //阅读器统计
  631. WapVisitStatService::recordReaderUvAndPv($this->uid, $this->distribution_channel_id);
  632. }
  633. //加桌类型
  634. private function addDesktopType($bid,$sequence){
  635. $force_add_desk_type = 0;
  636. $send_order_id = ReadRecordService::getSendOrderId($this->uid);
  637. if(!$send_order_id) return $force_add_desk_type;
  638. $send_order_info = SendOrderService::getById($send_order_id);
  639. if(!$send_order_info) return $force_add_desk_type;
  640. if($send_order_info->book_id == $bid && $send_order_info->force_add_desk_type && $send_order_info->force_add_desk_seq){
  641. if($sequence >= $send_order_info->force_add_desk_seq){
  642. $force_add_desk_type = $send_order_info->force_add_desk_type;
  643. }
  644. }
  645. return $force_add_desk_type;
  646. }
  647. }