ChapterController.php 25 KB

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