ChapterController.php 26 KB

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