ChapterController.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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. return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
  408. } else {
  409. $fee = $this->getPrice($book_info, $chapter->size);
  410. $data = [
  411. 'book_id' => $oldbid,
  412. 'book_name' => $book_info->book_name,
  413. 'chapter_name' => $chapter->name,
  414. 'chapter_id' => $cid,
  415. 'pay_type' => $book_info->charge_type,
  416. 'fee' => $fee,
  417. 'user_balance' => $this->_user_info['balance'],
  418. 'product_id' => $book_info->product_id,
  419. 'uid' => $this->uid,
  420. 'distribution_channel_id' => $this->distribution_channel_id,
  421. 'is_discount' => 0,
  422. 'discount_fee' => '',
  423. 'discount' => ''
  424. ];
  425. //不需要提醒
  426. if ($book_info->charge_type == 'BOOK') {
  427. return response()->error('QAPP_BOOK_SECOND_BALANCE_PAY', $data);
  428. } elseif ($book_info->charge_type == 'CHAPTER') {
  429. return response()->error('QAPP_CHAPTER_SECOND_BALANCE_PAY', $data);
  430. } else {
  431. return response()->error('QAPP_SYS_ERROR');
  432. }
  433. }
  434. }
  435. /**
  436. * 余额支付
  437. * @param $book_info
  438. * @param $chapter_id
  439. * @param $chapter_size
  440. * @return bool
  441. */
  442. protected function balancePay($book_info, $chapter_id, $chapter_size, $chapter_name, $is_remind)
  443. {
  444. $fee = $this->getPrice($book_info, $chapter_size);
  445. if ((int) $this->_user_info['balance'] >= $fee) {
  446. if ($this->bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)) {
  447. return true;
  448. }
  449. return false;
  450. } else {
  451. return false;
  452. }
  453. }
  454. /**
  455. * 获取章节内容
  456. * @param $bid
  457. * @param $cid
  458. * @return bool|mixed
  459. */
  460. protected function getChapter($bid, $cid, $chapter)
  461. {
  462. $chapter_content = ChapterService::getChapter($bid, $cid);
  463. if (!$chapter_content) return false;
  464. $chapter->content = trim(str_replace($chapter_content->name, '', $chapter_content->content));
  465. //统计点击率
  466. $key = 'book_click_num_bid_' . $bid;
  467. $field = date('Y-m-d');
  468. $old = Redis::hget($key, $field);
  469. if (!$old) $old = 0;
  470. Redis::hset($key, $field, $old + 1);
  471. return $chapter;
  472. }
  473. /**
  474. * 添加订购记录
  475. * @param $book_info
  476. * @param $chapter_id
  477. * @param $fee
  478. * @return bool
  479. */
  480. protected function bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)
  481. {
  482. $send_order_id = 0;
  483. if ($book_info['charge_type'] == 'BOOK') {
  484. $data = [
  485. 'uid' => $this->uid,
  486. 'fee' => $fee,
  487. 'u' => $send_order_id,
  488. 'distribution_channel_id' => $this->distribution_channel_id,
  489. 'bid' => $book_info->bid,
  490. 'book_name' => $book_info->book_name,
  491. 'send_order_id' => $send_order_id,
  492. ];
  493. return BookOrderService::addOrderRecodeAndDecrUserBalance($data, $this->uid);
  494. } else {
  495. $data = [
  496. 'uid' => $this->uid,
  497. 'fee' => $fee,
  498. 'cid' => $chapter_id,
  499. 'bid' => $book_info->bid,
  500. 'distribution_channel_id' => $this->distribution_channel_id,
  501. 'book_name' => $book_info->book_name,
  502. 'chapter_name' => $chapter_name,
  503. 'send_order_id' => $send_order_id,
  504. 'is_remind' => $is_remind
  505. ];
  506. //print_r($data);
  507. if ($is_remind) {
  508. $this->addOrderRemind($book_info->bid);
  509. }
  510. return ChapterOrderService::addOrderAndDecrUserBalance($data, $this->uid);
  511. }
  512. }
  513. protected function addOrderRemind($bid)
  514. {
  515. if (ChapterReminderService::checkIsNoReminder($this->uid, $bid)) {
  516. return true;
  517. } else {
  518. ChapterReminderService::add($this->uid, $bid);
  519. return true;
  520. }
  521. }
  522. /**
  523. * 是否订购提醒
  524. * @param $chapter_id
  525. * @return bool
  526. */
  527. protected function isOrderRemind($bid)
  528. {
  529. $is_no_reminder = ChapterReminderService::checkIsNoReminder($this->uid, $bid) ? 1 : 0;
  530. return $is_no_reminder == 0;
  531. }
  532. /**
  533. * 用户是否关注
  534. * @param $uid
  535. * @return bool
  536. */
  537. protected function getSubscribe()
  538. {
  539. $res = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $this->uid]);
  540. if ($res) return true;
  541. return false;
  542. }
  543. /**
  544. * 获取订购记录
  545. * @param $book_info
  546. * @param $chapter_id
  547. * @return bool
  548. */
  549. protected function getOrderRecord($bid, $chapter_id)
  550. {
  551. //包年记录
  552. $uid = $this->uid;
  553. $res = YearOrderService::getRecord($uid);
  554. if ($res) return true;
  555. $res = null;
  556. //单本订购记录
  557. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  558. if ($res) return true;
  559. $res = null;
  560. //章节订购记录
  561. $chapterOrder = new ChapterOrderService();
  562. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  563. return false;
  564. }
  565. /**
  566. * 计算价格
  567. * @param $book_info
  568. * @param $chapter_size
  569. * @return float
  570. */
  571. protected function getPrice($book_info, $chapter_size)
  572. {
  573. if ($book_info->charge_type == 'BOOK')
  574. return $book_info->price * 100;
  575. return ceil($chapter_size / 100);
  576. }
  577. /**
  578. * 用户添加标签
  579. * @param $book_info
  580. */
  581. protected function addTag($book_info)
  582. {
  583. $send_order_id = 0;
  584. if (!UserDeepReadTagService::isAddTag($this->uid, $book_info->bid)) {
  585. try {
  586. UserDeepReadTagService::addTag([
  587. 'uid' => $this->uid,
  588. 'bid' => $book_info->bid,
  589. 'book_name' => $book_info->book_name,
  590. 'category_id' => $book_info->category_id,
  591. 'category_name' => $book_info->category_name,
  592. 'sex_preference' => $book_info->channel_name ? $book_info->channel_name : '',
  593. 'distribution_channel_id' => $this->distribution_channel_id ? $this->distribution_channel_id : '0',
  594. 'send_order_id' => $send_order_id,
  595. ]);
  596. } catch (\Exception $e) { }
  597. }
  598. }
  599. protected function isBookOrdered($bid)
  600. {
  601. $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $bid);
  602. if ($chapter_order) return true;
  603. $res = BookOrderService::getRecordByuidBid($this->uid, $bid);
  604. if ($res) return true;
  605. return false;
  606. }
  607. private function showChapterPrice($bid, $charge_type)
  608. {
  609. if ($charge_type == 'BOOK') {
  610. return false;
  611. }
  612. $show_chapter_price_bid = env('SHOW_CHAPTER_PRICE_BID', 'all');
  613. if ($show_chapter_price_bid != 'all') {
  614. $show_chapter_price_bid_array = explode(',', $show_chapter_price_bid);
  615. if (!in_array($bid, $show_chapter_price_bid_array)) {
  616. return false;
  617. }
  618. }
  619. $show_chapter_price_channel = env('SHOW_CHAPTER_PRICE_CHANNEL', '');
  620. if (!$show_chapter_price_channel) {
  621. return false;
  622. }
  623. if ($show_chapter_price_channel == 'all') {
  624. return true;
  625. }
  626. $show_chapter_price_channel_array = explode(',', $show_chapter_price_channel);
  627. if (in_array($this->distribution_channel_id, $show_chapter_price_channel_array)) {
  628. return true;
  629. }
  630. return false;
  631. }
  632. }