ChapterController.php 26 KB

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