ChapterController.php 27 KB

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