ChapterController.php 26 KB

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