ChapterController.php 26 KB

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