ChapterController.php 25 KB

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