ChapterController.php 24 KB

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