ChapterController.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. //需要提箱
  297. if ($this->isOrderRemind($bid)) {
  298. if ($book_info->charge_type == 'BOOK') {
  299. return response()->error('QAPP_BOOK_BALANCE_PAY', $data);
  300. } else
  301. if ($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. } else {
  307. //不需要提醒
  308. if ($book_info->charge_type == 'BOOK') {
  309. return response()->error('QAPP_BOOK_SECOND_BALANCE_PAY', $data);
  310. } elseif ($book_info->charge_type == 'CHAPTER') {
  311. return response()->error('QAPP_CHAPTER_SECOND_BALANCE_PAY', $data);
  312. } else {
  313. return response()->error('QAPP_SYS_ERROR');
  314. }
  315. }
  316. }
  317. if ($this->isOrderRemind($bid)) {
  318. if ($book_info->charge_type == 'BOOK') {
  319. return response()->error('QAPP_BOOK_BUY', $data);
  320. } else
  321. if ($book_info->charge_type == 'CHAPTER') {
  322. return response()->error('QAPP_CHAPTER_BUY', $data);
  323. } else {
  324. return response()->error('QAPP_SYS_ERROR');
  325. }
  326. }
  327. //付费 不提醒
  328. if ($this->balancePay($book_info, $cid, $chapter->size, $chapter->name, 0)) {
  329. ReadRecordService::addReadRecord([
  330. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  331. 'cid' => $cid, 'chapter_name' => $chapter->name
  332. ]);
  333. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  334. } else {
  335. //不需要提醒
  336. if ($book_info->charge_type == 'BOOK') {
  337. return response()->error('QAPP_BOOK_SECOND_BALANCE_PAY', $data);
  338. } elseif ($book_info->charge_type == 'CHAPTER') {
  339. return response()->error('QAPP_CHAPTER_SECOND_BALANCE_PAY', $data);
  340. } else {
  341. return response()->error('QAPP_SYS_ERROR');
  342. }
  343. }
  344. }
  345. /**
  346. * @apiVersion 1.0.0
  347. * @apiDescription 余额支付
  348. * @api {get} books/{bid}/balance/chapterOrders/{cid} 余额支付
  349. * @apiParam {String} [token] token
  350. * @apiHeader {String} [Authorization] token 两个token任选其一
  351. * @apiGroup Chapter
  352. * @apiName pay
  353. * @apiParam (Int) remind 提醒
  354. * @apiSuccess {int} code 状态码
  355. * @apiSuccess {String} msg 信息
  356. * @apiSuccess {object} data 结果集
  357. * @apiSuccess {Int} data.chapter_id 章节id
  358. * @apiSuccess {String} data.chapter_name 章节名称
  359. * @apiSuccess {Int} data.chapter_sequence 序号
  360. * @apiSuccess {Int} data.chapter_is_vip 是否vip
  361. * @apiSuccess {Int} data.chapter_size 章节大小
  362. * @apiSuccess {Int} data.prev_cid 上一章节id
  363. * @apiSuccess {Int} data.next_cid 下一章节
  364. * @apiSuccess {String} data.recent_update_at 更新时间
  365. * @apiSuccess {String} data.chapter_content 章节内容
  366. * @apiSuccess {Int} data.is_need_subscirbe 是否强制关注(删除)
  367. * @apiSuccessExample {json} Success-Response:
  368. * HTTP/1.1 200 OK
  369. * {
  370. * code: 0,
  371. * msg: "",
  372. * data: {
  373. * chapter_id: 5,
  374. * chapter_name: "第1240章 不是我",
  375. * chapter_sequence: 1239,
  376. * chapter_is_vip: 1,
  377. * chapter_size: 2422,
  378. * prev_cid: 0,
  379. * next_cid: 0,
  380. * recent_update_at: 2017-11-20 15:01:56,
  381. * chapter_content: "叶妩被司行霈的阴阳怪气一吓,思路偏得太远了。 她张口结舌,忘记了自己要说什么。",
  382. * }
  383. * }
  384. */
  385. public function pay(Request $request, $bid, $cid)
  386. {
  387. $remind = (int) $request->input('remind');
  388. $oldbid = $bid;
  389. $bid = BookService::decodeBidStatic($bid);
  390. $book_info = BookConfigService::getBookById($bid);;
  391. if (empty($book_info)) response()->error('QAPP_SYS_ERROR');
  392. if ($book_info->is_on_shelf == 0 || $book_info->is_on_shelf == 3) {
  393. if (!$this->isBookOrdered($bid)) {
  394. response()->error('QAPP_OFF_SHELF');
  395. }
  396. }
  397. //获取章节
  398. $chapter = ChapterService::getChapterNameById($cid, $bid);
  399. if (!$chapter) {
  400. return response()->error('QAPP_SYS_ERROR');
  401. }
  402. if ($this->balancePay($book_info, $cid, $chapter->size, $chapter->name, $remind)) {
  403. ReadRecordService::addReadRecord([
  404. 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  405. 'cid' => $cid, 'chapter_name' => $chapter->name
  406. ]);
  407. //用户标签
  408. if ($chapter->sequence >= 20) {
  409. $this->addTag($book_info);
  410. }
  411. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  412. } else {
  413. $fee = $this->getPrice($book_info, $chapter->size);
  414. $data = [
  415. 'book_id' => $oldbid,
  416. 'book_name' => $book_info->book_name,
  417. 'chapter_name' => $chapter->name,
  418. 'chapter_id' => $cid,
  419. 'pay_type' => $book_info->charge_type,
  420. 'fee' => $fee,
  421. 'user_balance' => $this->_user_info['balance'],
  422. 'product_id' => $book_info->product_id,
  423. 'uid' => $this->uid,
  424. 'distribution_channel_id' => $this->distribution_channel_id,
  425. 'is_discount' => 0,
  426. 'discount_fee' => '',
  427. 'discount' => ''
  428. ];
  429. //不需要提醒
  430. if ($book_info->charge_type == 'BOOK') {
  431. return response()->error('QAPP_BOOK_SECOND_BALANCE_PAY', $data);
  432. } elseif ($book_info->charge_type == 'CHAPTER') {
  433. return response()->error('QAPP_CHAPTER_SECOND_BALANCE_PAY', $data);
  434. } else {
  435. return response()->error('QAPP_SYS_ERROR');
  436. }
  437. }
  438. }
  439. /**
  440. * 余额支付
  441. * @param $book_info
  442. * @param $chapter_id
  443. * @param $chapter_size
  444. * @return bool
  445. */
  446. protected function balancePay($book_info, $chapter_id, $chapter_size, $chapter_name, $is_remind)
  447. {
  448. $fee = $this->getPrice($book_info, $chapter_size);
  449. if ((int) $this->_user_info['balance'] >= $fee) {
  450. if ($this->bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)) {
  451. return true;
  452. }
  453. return false;
  454. } else {
  455. return false;
  456. }
  457. }
  458. /**
  459. * 获取章节内容
  460. * @param $bid
  461. * @param $cid
  462. * @return bool|mixed
  463. */
  464. protected function getChapter($bid, $cid, $chapter)
  465. {
  466. $chapter_content = ChapterService::getChapter($bid, $cid);
  467. if (!$chapter_content) return false;
  468. $chapter->content = trim(str_replace($chapter_content->name, '', $chapter_content->content));
  469. //统计点击率
  470. $key = 'book_click_num_bid_' . $bid;
  471. $field = date('Y-m-d');
  472. $old = Redis::hget($key, $field);
  473. if (!$old) $old = 0;
  474. Redis::hset($key, $field, $old + 1);
  475. return $chapter;
  476. }
  477. /**
  478. * 添加订购记录
  479. * @param $book_info
  480. * @param $chapter_id
  481. * @param $fee
  482. * @return bool
  483. */
  484. protected function bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)
  485. {
  486. $send_order_id = 0;
  487. if ($book_info['charge_type'] == 'BOOK') {
  488. $data = [
  489. 'uid' => $this->uid,
  490. 'fee' => $fee,
  491. 'u' => $send_order_id,
  492. 'distribution_channel_id' => $this->distribution_channel_id,
  493. 'bid' => $book_info->bid,
  494. 'book_name' => $book_info->book_name,
  495. 'send_order_id' => $send_order_id,
  496. ];
  497. return BookOrderService::addOrderRecodeAndDecrUserBalance($data, $this->uid);
  498. } else {
  499. $data = [
  500. 'uid' => $this->uid,
  501. 'fee' => $fee,
  502. 'cid' => $chapter_id,
  503. 'bid' => $book_info->bid,
  504. 'distribution_channel_id' => $this->distribution_channel_id,
  505. 'book_name' => $book_info->book_name,
  506. 'chapter_name' => $chapter_name,
  507. 'send_order_id' => $send_order_id,
  508. 'is_remind' => $is_remind
  509. ];
  510. //print_r($data);
  511. if ($is_remind) {
  512. $this->addOrderRemind($book_info->bid);
  513. }
  514. return ChapterOrderService::addOrderAndDecrUserBalance($data, $this->uid);
  515. }
  516. }
  517. protected function addOrderRemind($bid)
  518. {
  519. if (ChapterReminderService::checkIsNoReminder($this->uid, $bid)) {
  520. return true;
  521. } else {
  522. ChapterReminderService::add($this->uid, $bid);
  523. return true;
  524. }
  525. }
  526. /**
  527. * 是否订购提醒
  528. * @param $chapter_id
  529. * @return bool
  530. */
  531. protected function isOrderRemind($bid)
  532. {
  533. $is_no_reminder = ChapterReminderService::checkIsNoReminder($this->uid, $bid) ? 1 : 0;
  534. return $is_no_reminder == 0;
  535. }
  536. /**
  537. * 用户是否关注
  538. * @param $uid
  539. * @return bool
  540. */
  541. protected function getSubscribe()
  542. {
  543. $res = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $this->uid]);
  544. if ($res) return true;
  545. return false;
  546. }
  547. /**
  548. * 获取订购记录
  549. * @param $book_info
  550. * @param $chapter_id
  551. * @return bool
  552. */
  553. protected function getOrderRecord($bid, $chapter_id)
  554. {
  555. //包年记录
  556. $uid = $this->uid;
  557. $res = YearOrderService::getRecord($uid);
  558. if ($res) return true;
  559. $res = null;
  560. //单本订购记录
  561. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  562. if ($res) return true;
  563. $res = null;
  564. //章节订购记录
  565. $chapterOrder = new ChapterOrderService();
  566. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  567. return false;
  568. }
  569. /**
  570. * 计算价格
  571. * @param $book_info
  572. * @param $chapter_size
  573. * @return float
  574. */
  575. protected function getPrice($book_info, $chapter_size)
  576. {
  577. if ($book_info->charge_type == 'BOOK')
  578. return $book_info->price * 100;
  579. return ceil($chapter_size / 100);
  580. }
  581. /**
  582. * 用户添加标签
  583. * @param $book_info
  584. */
  585. protected function addTag($book_info)
  586. {
  587. $send_order_id = 0;
  588. if (!UserDeepReadTagService::isAddTag($this->uid, $book_info->bid)) {
  589. try {
  590. UserDeepReadTagService::addTag([
  591. 'uid' => $this->uid,
  592. 'bid' => $book_info->bid,
  593. 'book_name' => $book_info->book_name,
  594. 'category_id' => $book_info->category_id,
  595. 'category_name' => $book_info->category_name,
  596. 'sex_preference' => $book_info->channel_name ? $book_info->channel_name : '',
  597. 'distribution_channel_id' => $this->distribution_channel_id ? $this->distribution_channel_id : '0',
  598. 'send_order_id' => $send_order_id,
  599. ]);
  600. } catch (\Exception $e) { }
  601. }
  602. }
  603. protected function isBookOrdered($bid)
  604. {
  605. $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $bid);
  606. if ($chapter_order) return true;
  607. $res = BookOrderService::getRecordByuidBid($this->uid, $bid);
  608. if ($res) return true;
  609. return false;
  610. }
  611. private function showChapterPrice($bid, $charge_type)
  612. {
  613. if ($charge_type == 'BOOK') {
  614. return false;
  615. }
  616. $show_chapter_price_bid = env('SHOW_CHAPTER_PRICE_BID', 'all');
  617. if ($show_chapter_price_bid != 'all') {
  618. $show_chapter_price_bid_array = explode(',', $show_chapter_price_bid);
  619. if (!in_array($bid, $show_chapter_price_bid_array)) {
  620. return false;
  621. }
  622. }
  623. $show_chapter_price_channel = env('SHOW_CHAPTER_PRICE_CHANNEL', '');
  624. if (!$show_chapter_price_channel) {
  625. return false;
  626. }
  627. if ($show_chapter_price_channel == 'all') {
  628. return true;
  629. }
  630. $show_chapter_price_channel_array = explode(',', $show_chapter_price_channel);
  631. if (in_array($this->distribution_channel_id, $show_chapter_price_channel_array)) {
  632. return true;
  633. }
  634. return false;
  635. }
  636. }