ChapterController.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. <?php
  2. namespace App\Http\Controllers\Wap\Book;
  3. use App\Modules\Book\Models\DataAnalysisBookConfig;
  4. use App\Modules\Book\Services\BookService;
  5. use App\Modules\Book\Services\ChapterCommentsService;
  6. use App\Modules\Book\Services\ChapterShareWechatConfigService;
  7. use App\Modules\Statistic\Models\DataAnalysisSelectUser;
  8. use App\Modules\Statistic\Services\AdVisitStatService;
  9. use App\Modules\Statistic\Services\DataAnalysisChapterService;
  10. use App\Modules\Statistic\Services\DataAnalysisSelectUserService;
  11. use App\Modules\Statistic\Services\WapVisitStatService;
  12. use App\Modules\Subscribe\Services\CrmChapterOrderDetailService;
  13. use App\Modules\Subscribe\Services\CustomChapterOrderService;
  14. use App\Modules\Subscribe\Services\OrderService;
  15. use App\Modules\Subscribe\Models\Order;
  16. use App\Modules\User\Services\CrmUserService;
  17. use App\Modules\User\Services\ForceGuidePersonAccountService;
  18. use App\Modules\User\Services\ForceSubscribeUserIService;
  19. use App\Modules\User\Services\ReadRecordStatsService;
  20. use App\Modules\User\Services\UserBindHkWelfareService;
  21. use App\Modules\User\Services\UserService;
  22. use App\Modules\YunQi\Services\BookUserService;
  23. use Illuminate\Http\Request;
  24. use App\Http\Controllers\Wap\BaseController;
  25. use Cookie;
  26. use Redis;
  27. use App\Modules\Book\Services\ChapterService;
  28. use App\Modules\User\Services\ReadRecordService;
  29. use App\Http\Controllers\Wap\Book\Transformers\ChapterTransformer;
  30. use App\Modules\Book\Services\BookConfigService;
  31. use App\Http\Controllers\Wap\Book\Transformers\ChapterListTransformer;
  32. use App\Modules\Subscribe\Services\BookOrderService;
  33. use App\Modules\Subscribe\Services\ChapterOrderService;
  34. use App\Modules\Subscribe\Services\YearOrderService;
  35. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  36. use App\Modules\Subscribe\Services\ChapterReminderService;
  37. use App\Modules\OfficialAccount\Services\OfficialAccountService;
  38. use Hashids;
  39. use GuzzleHttp\Client;
  40. use Log;
  41. use DB;
  42. use App\Modules\User\Services\UserSignService;
  43. use App\Modules\SendOrder\Services\SendOrderService;
  44. use App\Modules\User\Services\UserDeepReadTagService;
  45. use App\Modules\Book\Services\BookSubscribleChapterService;
  46. use App\Modules\Channel\Services\ChannelSubscribeSettingService;
  47. class ChapterController extends BaseController
  48. {
  49. /**
  50. * @apiDefine Chapter 章节
  51. */
  52. //用户是否强关
  53. private $is_had_subscribe = false;
  54. //章节是否需要强关
  55. private $is_need_subscribe;
  56. //用户信息
  57. private $user;
  58. //图书信息
  59. private $book_info;
  60. //cid
  61. private $cid;
  62. //加密的bid
  63. private $en_bid;
  64. //章节信息
  65. private $chapter;
  66. private $is_first_book_order = 0;
  67. private $domain;
  68. private $crm_order = '';
  69. /**
  70. * @apiVersion 1.0.0
  71. * @apiDescription 章节列表不分页
  72. * @api {get} books/{bid}/allcatalog 章节列表不分页
  73. * @apiGroup Chapter
  74. * @apiName getCatalog
  75. * @apiSuccess {int} code 状态码
  76. * @apiSuccess {String} msg 信息
  77. * @apiSuccess {object} data 结果集
  78. * @apiSuccess {Array} data.list 分页结果集
  79. * @apiSuccess {Int} data.list.bid bid
  80. * @apiSuccess {Int} data.list.chapter_id 章节id
  81. * @apiSuccess {String} data.list.chapter_name 章节名称
  82. * @apiSuccess {Int} data.list.chapter_sequence 序号
  83. * @apiSuccess {Int} data.list.chapter_is_vip 是否vip
  84. * @apiSuccess {Int} data.list.chapter_size 章节大小
  85. * @apiSuccess {Int} data.list.prev_cid 上一章节id
  86. * @apiSuccess {Int} data.list.next_cid 下一章节
  87. * @apiSuccess {String} data.list.recent_update_at 更新时间
  88. * @apiSuccess {String} data.list.is_need_subscirbe 是否强制关注
  89. * @apiSuccess {String} data.list.is_show_price 是否显示价格
  90. * @apiSuccess {String} data.list.price 价格
  91. * @apiSuccess {object} data.meta 分页信息
  92. * @apiSuccess {Int} data.meta.total 总条数
  93. * @apiSuccess {Int} data.meta.per_page 每页条数
  94. * @apiSuccess {Int} data.meta.current_page 当前页
  95. * @apiSuccess {Int} data.meta.last_page 最后页
  96. * @apiSuccess {String} data.meta.next_page_url 下一页
  97. * @apiSuccess {String} data.meta.prev_page_url 上一页
  98. * @apiSuccessExample {json} Success-Response:
  99. * HTTP/1.1 200 OK
  100. * {
  101. * code: 0,
  102. * msg: "",
  103. * data:
  104. * [
  105. * {
  106. * bid: 5,
  107. * chapter_id: 5,
  108. * chapter_name: "第1240章 不是我",
  109. * chapter_sequence: 1239,
  110. * chapter_is_vip: 1,
  111. * chapter_size: 2422,
  112. * prev_cid: 0,
  113. * next_cid: 0,
  114. * recent_update_at: 2017-11-20 15:01:56,
  115. * is_need_subscirbe: 1,
  116. * },
  117. * {
  118. * bid: 5,
  119. * chapter_id: 5,
  120. * chapter_name: "第1240章 不是我",
  121. * chapter_sequence: 1239,
  122. * chapter_is_vip: 1,
  123. * chapter_size: 2422,
  124. * prev_cid: 0,
  125. * next_cid: 0,
  126. * recent_update_at: 2017-11-20 15:01:56,
  127. * is_need_subscirbe: 1,
  128. * },
  129. * ]
  130. * }
  131. */
  132. public function getCatalog(Request $request, $t, $domain, $bid)
  133. {
  134. if (!$this->checkUid()) {
  135. return response()->error('WAP_NOT_LOGIN');
  136. }
  137. $this->en_bid = $bid;
  138. $bid = Hashids::decode($bid)[0];
  139. $lists = ChapterService::getChapterLists($bid);
  140. $book_info = BookConfigService::getBookById($bid);
  141. if (!$book_info) {
  142. return response()->error('PARAM_ERROR');
  143. }
  144. $this->book_info = $book_info;
  145. $is_show_price = $this->showChapterPrice($bid, $book_info->charge_type);
  146. foreach ($lists as $v) {
  147. $v->is_show_price = $is_show_price;
  148. $v->price = '';
  149. if ($is_show_price) {
  150. $v->price = $this->getPrice($book_info, $v);
  151. }
  152. }
  153. return response()->collection(new ChapterListTransformer, $lists);
  154. }
  155. /**
  156. * @apiVersion 1.0.0
  157. * @apiDescription 章节列表分页
  158. * @api {get} books/{bid}/catalog 章节列表分页
  159. * @apiGroup Chapter
  160. * @apiName getCatalogPerPage
  161. * @apiParam {Int} page_size 分页大小(默认15)
  162. * @apiParam {Int} page 页码(默认1)
  163. * @apiSuccess {int} code 状态码
  164. * @apiSuccess {String} msg 信息
  165. * @apiSuccess {object} data 结果集
  166. * @apiSuccess {Array} data.list 分页结果集
  167. * @apiSuccess {Int} data.list.bid bid
  168. * @apiSuccess {Int} data.list.chapter_id 章节id
  169. * @apiSuccess {String} data.list.chapter_name 章节名称
  170. * @apiSuccess {Int} data.list.chapter_sequence 序号
  171. * @apiSuccess {Int} data.list.chapter_is_vip 是否vip
  172. * @apiSuccess {Int} data.list.chapter_size 章节大小
  173. * @apiSuccess {Int} data.list.prev_cid 上一章节id
  174. * @apiSuccess {Int} data.list.next_cid 下一章节
  175. * @apiSuccess {String} data.list.recent_update_at 更新时间
  176. * @apiSuccess {String} data.list.is_need_subscirbe 是否强制关注
  177. * @apiSuccess {String} data.list.is_show_price 是否显示价格
  178. * @apiSuccess {String} data.list.price 价格
  179. * @apiSuccess {object} data.meta 分页信息
  180. * @apiSuccess {Int} data.meta.total 总条数
  181. * @apiSuccess {Int} data.meta.per_page 每页条数
  182. * @apiSuccess {Int} data.meta.current_page 当前页
  183. * @apiSuccess {Int} data.meta.last_page 最后页
  184. * @apiSuccess {String} data.meta.next_page_url 下一页
  185. * @apiSuccess {String} data.meta.prev_page_url 上一页
  186. * @apiSuccessExample {json} Success-Response:
  187. * HTTP/1.1 200 OK
  188. * {
  189. * code: 0,
  190. * msg: "",
  191. * data:
  192. * list:[
  193. * {
  194. * bid: 5,
  195. * chapter_id: 5,
  196. * chapter_name: "第1240章 不是我",
  197. * chapter_sequence: 1239,
  198. * chapter_is_vip: 1,
  199. * chapter_size: 2422,
  200. * prev_cid: 0,
  201. * next_cid: 0,
  202. * recent_update_at: 2017-11-20 15:01:56,
  203. * is_need_subscirbe: 1,
  204. * },
  205. * {
  206. * bid: 5,
  207. * chapter_id: 5,
  208. * chapter_name: "第1240章 不是我",
  209. * chapter_sequence: 1239,
  210. * chapter_is_vip: 1,
  211. * chapter_size: 2422,
  212. * prev_cid: 0,
  213. * next_cid: 0,
  214. * recent_update_at: 2017-11-20 15:01:56,
  215. * is_need_subscirbe: 1,
  216. * },
  217. * ]
  218. * meta:{
  219. * total: 1253,
  220. * per_page: 15,
  221. * current_page: 1,
  222. * last_page: 84,
  223. * next_page_url: "http://myapi.cn/api/books/1/chapter?page=2",
  224. * prev_page_url: ""
  225. * }
  226. * }
  227. */
  228. public function getCatalogPerPage(Request $request, $t, $domain, $bid)
  229. {
  230. if (!$this->checkUid()) {
  231. return response()->error('WAP_NOT_LOGIN');
  232. }
  233. $this->en_bid = $bid;
  234. $bid_array = Hashids::decode($bid);
  235. if (isset($bid_array[0])) {
  236. $bid = $bid_array[0];
  237. } else {
  238. return response()->error('PARAM_ERROR');
  239. }
  240. $book_info = BookConfigService::getBookById($bid);
  241. if (!$book_info) {
  242. return response()->error('PARAM_ERROR');
  243. }
  244. $this->book_info = $book_info;
  245. $page_size = $request->input('page_size', 15);
  246. if($page_size>=100) $page_size=100;
  247. $res = ChapterService::getChapterListsPage($bid, $page_size);
  248. $is_show_price = $this->showChapterPrice($bid, $book_info->charge_type);
  249. foreach ($res as $v) {
  250. $v->is_show_price = $is_show_price;
  251. $v->price = '';
  252. if ($is_show_price) {
  253. $v->price = $this->getPrice($book_info, $v);
  254. }
  255. }
  256. return response()->pagination(new ChapterListTransformer, $res);
  257. }
  258. /**
  259. * @apiVersion 1.0.0
  260. * @apiDescription 章节内容
  261. * @api {get} books/{bid}/chapters/{chapter_id} 章节内容
  262. * @apiGroup Chapter
  263. * @apiName index
  264. * @apiSuccess {int} code 状态码
  265. * @apiSuccess {String} msg 信息
  266. * @apiSuccess {object} data 结果集
  267. * @apiSuccess {Int} data.chapter_id 章节id
  268. * @apiSuccess {String} data.chapter_name 章节名称
  269. * @apiSuccess {Int} data.chapter_sequence 序号
  270. * @apiSuccess {Int} data.chapter_is_vip 是否vip
  271. * @apiSuccess {Int} data.chapter_size 章节大小
  272. * @apiSuccess {Int} data.prev_cid 上一章节id
  273. * @apiSuccess {Int} data.next_cid 下一章节
  274. * @apiSuccess {String} data.recent_update_at 更新时间
  275. * @apiSuccess {String} data.chapter_content 章节内容
  276. * @apiSuccess {Int} data.is_need_subscirbe 是否强制关注(删除)
  277. * @apiSuccess {Int} data.is_show_subscribe_link 是否显示底部强关链接
  278. * @apiSuccessExample {json} Success-Response:
  279. * HTTP/1.1 200 OK
  280. * {
  281. * code: 0,
  282. * msg: "",
  283. * data: {
  284. * chapter_id: 5,
  285. * chapter_name: "第1240章 不是我",
  286. * chapter_sequence: 1239,
  287. * chapter_is_vip: 1,
  288. * chapter_size: 2422,
  289. * prev_cid: 0,
  290. * next_cid: 0,
  291. * recent_update_at: 2017-11-20 15:01:56,
  292. * chapter_content: "叶妩被司行霈的阴阳怪气一吓,思路偏得太远了。 她张口结舌,忘记了自己要说什么。",
  293. * }
  294. * }
  295. */
  296. public function index(Request $request, $t, $domain, $bid, $cid)
  297. {
  298. $this->domain = $domain;
  299. $special_user_log = env('SPECIAL_USER_LOG');
  300. //注意:内部约定用 有此参数不需要强关
  301. Cookie::queue('force_show_qrcode', 1, -1);
  302. if (!$this->checkUid()) {
  303. myLog('special')->info('chapter index controller---$this->checkUid() fail-------');
  304. return response()->error('WAP_NOT_LOGIN');
  305. }
  306. $this->en_bid = $bid;
  307. $bid = explode(',',$bid)[0];
  308. $bid = Hashids::decode($bid)[0];
  309. //获取图书信息
  310. $book_info = BookConfigService::getBookById($bid);
  311. //是否开启全站按章模式
  312. $global_charge_by_chapter_channels = explode(',',env('GLOBAL_CHARGE_BY_CHAPTER_CHANNEL'));
  313. if(in_array($this->distribution_channel_id,$global_charge_by_chapter_channels))
  314. {
  315. $book_info->charge_type = 'CHAPTER';
  316. }
  317. if (empty($book_info)) return response()->error('WAP_SYS_ERROR');
  318. $this->book_info = $book_info;
  319. $this->cid = $cid;
  320. //下架图书不能看
  321. if ($this->isOffShelf()) {
  322. return response()->error('WAP_OFF_SHELF');
  323. }
  324. //获取章节信息
  325. $chapter = ChapterService::getChapterNameById($cid, $bid);
  326. $this->chapter = $chapter;
  327. //章节被删除
  328. if (($is_delete = $this->chapterNotExists())) {
  329. return response()->error('WAP_DOMAIN_NOT_MATCH', [
  330. 'url' => $is_delete
  331. ]);
  332. }
  333. $user_info = $this->_user_info;
  334. $this->user = $user_info;
  335. //用户是否强关
  336. $this->is_had_subscribe = true;
  337. //需要强制关注 不是vip章节 已经关注了
  338. if ($chapter->is_vip == 0) {
  339. $this->getChapter();
  340. return response()->item(new ChapterTransformer, $this->chapter);
  341. }
  342. //已经付费
  343. if ($this->getOrderRecord($bid, $cid)) {
  344. $this->getChapter();
  345. if($special_user_log && $special_user_log == $this->uid){
  346. $my_log = myLog('special');
  347. $my_log->info('had order order record--------------------------');
  348. }
  349. return response()->item(new ChapterTransformer, $this->chapter);
  350. }
  351. //获取价格
  352. $fee = $this->getPrice($book_info, $chapter);
  353. //以上都是不用付费的情况******************************************************
  354. //未付费
  355. //返回的价格信息
  356. $data = [
  357. 'book_id' => $this->en_bid,
  358. 'book_name' => $book_info->book_name,
  359. 'chapter_name' => $chapter->name,
  360. 'chapter_id' => $cid,
  361. 'pay_type' => $book_info->charge_type,
  362. 'fee' => $fee,
  363. 'user_balance' => $user_info->balance,
  364. 'product_id' => $book_info->product_id,
  365. 'uid' => $this->uid,
  366. 'distribution_channel_id' => $this->distribution_channel_id,
  367. 'is_discount' => 0,
  368. 'discount_fee' => '',
  369. 'discount' => '',
  370. 'is_first_book_order'=>$this->is_first_book_order
  371. ];
  372. if($special_user_log && $special_user_log == $this->uid){
  373. $my_log = myLog('special');
  374. $my_log->info('data is:');
  375. $my_log->info($data);
  376. }
  377. //余额不足 弹窗提醒
  378. if ($user_info['balance'] < $fee) {
  379. //除了单本订购 其他的都不提醒,直接跳转到充值页面
  380. if ($book_info->charge_type == 'CHAPTER') {
  381. if($special_user_log && $special_user_log == $this->uid){
  382. $my_log = myLog('special');
  383. $my_log->info('chapter order balance lack');
  384. }
  385. return response()->error('WAP_CHAPTER_SECOND_BALANCE_PAY', $data);
  386. }
  387. //需要提箱
  388. if ($this->isOrderRemind($bid)) {
  389. return response()->error('WAP_BOOK_BALANCE_PAY', $data);
  390. } else {
  391. return response()->error('WAP_BOOK_SECOND_BALANCE_PAY', $data);
  392. }
  393. }
  394. //余额充足 单本弹窗提醒,章节直接扣费
  395. if ($this->isOrderRemind($bid) && $book_info->charge_type == 'BOOK') {
  396. return response()->error('WAP_BOOK_BUY', $data);
  397. }
  398. //付费 不提醒
  399. if ($this->balancePay($book_info, $chapter, 0)) {
  400. if($special_user_log && $special_user_log == $this->uid){
  401. $my_log = myLog('special');
  402. $my_log->info('balance pay ok-----');
  403. }
  404. $this->getChapter();
  405. return response()->item(new ChapterTransformer, $this->chapter);
  406. } else {
  407. if($special_user_log && $special_user_log == $this->uid){
  408. $my_log = myLog('special');
  409. $my_log->info('balance pay fails!!!!!!!!!!!!!!!!');
  410. }
  411. //付费 异常
  412. return response()->error('WAP_SYS_ERROR');
  413. }
  414. }
  415. /**
  416. * @apiVersion 1.0.0
  417. * @apiDescription 余额支付
  418. * @api {get} books/{bid}/balance/chapterOrders/{cid} 余额支付
  419. * @apiGroup Chapter
  420. * @apiName pay
  421. * @apiParam (Int) remind 提醒
  422. * @apiSuccess {int} code 状态码
  423. * @apiSuccess {String} msg 信息
  424. * @apiSuccess {object} data 结果集
  425. * @apiSuccess {Int} data.chapter_id 章节id
  426. * @apiSuccess {String} data.chapter_name 章节名称
  427. * @apiSuccess {Int} data.chapter_sequence 序号
  428. * @apiSuccess {Int} data.chapter_is_vip 是否vip
  429. * @apiSuccess {Int} data.chapter_size 章节大小
  430. * @apiSuccess {Int} data.prev_cid 上一章节id
  431. * @apiSuccess {Int} data.next_cid 下一章节
  432. * @apiSuccess {String} data.recent_update_at 更新时间
  433. * @apiSuccess {String} data.chapter_content 章节内容
  434. * @apiSuccess {Int} data.is_need_subscirbe 是否强制关注(删除)
  435. * @apiSuccessExample {json} Success-Response:
  436. * HTTP/1.1 200 OK
  437. * {
  438. * code: 0,
  439. * msg: "",
  440. * data: {
  441. * chapter_id: 5,
  442. * chapter_name: "第1240章 不是我",
  443. * chapter_sequence: 1239,
  444. * chapter_is_vip: 1,
  445. * chapter_size: 2422,
  446. * prev_cid: 0,
  447. * next_cid: 0,
  448. * recent_update_at: 2017-11-20 15:01:56,
  449. * chapter_content: "叶妩被司行霈的阴阳怪气一吓,思路偏得太远了。 她张口结舌,忘记了自己要说什么。",
  450. * }
  451. * }
  452. */
  453. public function pay(Request $request, $t, $domain, $bid, $cid)
  454. {
  455. if (!$this->checkUid()) {
  456. return response()->error('NOT_LOGIN');
  457. }
  458. $remind = (int)$request->input('remind');
  459. $this->en_bid = $bid;
  460. $oldbid = $bid;
  461. $bid = Hashids::decode($bid)[0];
  462. $book_info = BookConfigService::getBookById($bid);;
  463. if (empty($book_info)) response()->error('WAP_SYS_ERROR');
  464. //获取章节
  465. $chapter = ChapterService::getChapterNameById($cid, $bid);
  466. if (!$chapter) {
  467. return response()->error('WAP_SYS_ERROR');
  468. }
  469. $this->chapter = $chapter;
  470. $this->book_info = $book_info;
  471. $this->cid = $cid;
  472. if ($this->balancePay($book_info, $chapter, $remind)) {
  473. ReadRecordService::addReadRecord(['uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  474. 'cid' => $cid, 'chapter_name' => $chapter->name]);
  475. $this->getChapter();
  476. return response()->item(new ChapterTransformer, $this->chapter);
  477. } else {
  478. $fee = $this->getPrice($book_info, $chapter);
  479. $data = [
  480. 'book_id' => $oldbid,
  481. 'book_name' => $book_info->book_name,
  482. 'chapter_name' => $chapter->name,
  483. 'chapter_id' => $cid,
  484. 'pay_type' => $book_info->charge_type,
  485. 'fee' => $fee,
  486. 'user_balance' => $this->_user_info['balance'],
  487. 'product_id' => $book_info->product_id,
  488. 'uid' => $this->uid,
  489. 'distribution_channel_id' => $this->distribution_channel_id,
  490. 'is_discount' => 0,
  491. 'discount_fee' => '',
  492. 'discount' => '',
  493. 'is_first_book_order'=>$this->is_first_book_order
  494. ];
  495. //不需要提醒
  496. if ($book_info->charge_type == 'BOOK') {
  497. return response()->error('WAP_BOOK_SECOND_BALANCE_PAY', $data);
  498. } elseif ($book_info->charge_type == 'CHAPTER') {
  499. return response()->error('WAP_CHAPTER_SECOND_BALANCE_PAY', $data);
  500. } else {
  501. return response()->error('WAP_SYS_ERROR');
  502. }
  503. }
  504. }
  505. /**
  506. * 余额支付
  507. * @param $book_info
  508. * @param $chapter_id
  509. * @param $chapter_size
  510. * @return bool
  511. */
  512. protected function balancePay($book_info, $chapter, $is_remind)
  513. {
  514. $fee = $this->getPrice($book_info, $chapter);
  515. if ((int)$this->_user_info['balance'] >= $fee) {
  516. if ($this->bookOrderOrChapterOrder($book_info, $chapter->id, $fee, $chapter->name, $is_remind)) {
  517. $this->chapterOrderStats($fee);
  518. if($this->user){
  519. $this->user->balance -= $fee;
  520. }
  521. $this->customChapterOrderStats($fee);
  522. return true;
  523. }
  524. }
  525. return false;
  526. }
  527. /**
  528. * 获取章节内容
  529. * @param $bid
  530. * @param $cid
  531. * @return bool|mixed
  532. */
  533. private function getChapter()
  534. {
  535. //初始化 默认字段
  536. $this->initChapterDefaultField();
  537. //章节内容
  538. $chapter_content = ChapterService::getChapter($this->book_info->bid, $this->cid);
  539. //$this->chapter->content = str_replace($chapter_content->name, '', $chapter_content->content);
  540. $this->chapter->content = $chapter_content->content;
  541. //阅读记录
  542. if($this->chapter->sequence >= 2){
  543. ReadRecordService::addReadRecord(['uid' => $this->uid, 'bid' => $this->book_info->bid, 'book_name' => $this->book_info->book_name,
  544. 'cid' => $this->cid, 'chapter_name' => $this->chapter->name]);
  545. }
  546. //统计
  547. $this->chapterStats();
  548. //阅读器分享
  549. $this->share();
  550. //完整阅读纪录 阅读纪录
  551. $this->readRecordOther();
  552. //crm订阅
  553. $this->crmChapterStats();
  554. }
  555. /**
  556. * 章节阅读接口需要配置返回的字段
  557. */
  558. private function initChapterDefaultField()
  559. {
  560. if (!$this->chapter) {
  561. return;
  562. }
  563. //内容
  564. $this->chapter->content = '';
  565. //签到默认字段
  566. $this->chapter->sign_days = 1;
  567. $this->chapter->sign_status = 0;
  568. $this->chapter->sign_reard = 0;
  569. $this->chapter->sign_version = 'v1';
  570. $this->chapter->sign_items = [];//只有v2签到才会用到的字段
  571. //#短篇推送长篇需求#
  572. $this->chapter->next_jump = 0;
  573. $this->chapter->next_jump_url = '';
  574. $this->chapter->show_push_title = 0;
  575. $this->chapter->push_title_url = '';
  576. $this->chapter->push_title = [];
  577. //分享
  578. $this->chapter->is_show_share_button = 0;
  579. //6985的分享配置
  580. $this->chapter->share_config = '';
  581. //阅读底部强关图片 只有123有
  582. $this->chapter->is_had_subscribe = 0;
  583. //广告
  584. $this->chapter->is_show_ad = 0;//可不可点击
  585. $this->chapter->is_visiable_ad = 0;//可不可显示
  586. }
  587. private function share()
  588. {
  589. if ($this->is_had_subscribe && in_array($this->distribution_channel_id, [2, 14, 211,4025,4147,691,123]) && $this->user) {//,4147,691,123,4025
  590. $this->chapter->is_show_share_button = 1;
  591. }
  592. }
  593. //订阅统计
  594. private function chapterOrderStats($fee)
  595. {
  596. $day = date('Y--m-d');
  597. try {
  598. Redis::hincrby('wap:chapterandbookorder:bid:' . $this->book_info->bid, $day, $fee);
  599. } catch (\Exception $e) {
  600. }
  601. $this->smartPushTestBook($this->book_info->bid, $fee);
  602. $specialChannelIdStats = env('SPECIAL_CHANNEL_STATS', 211);
  603. if (in_array($this->distribution_channel_id, explode(',', $specialChannelIdStats))) {
  604. WapVisitStatService::specialChannelIdStatsSave($this->uid, $this->book_info->bid, $fee);
  605. }
  606. }
  607. /**
  608. * 添加订购记录
  609. * @param $book_info
  610. * @param $chapter_id
  611. * @param $fee
  612. * @return bool
  613. */
  614. protected function bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)
  615. {
  616. if ($book_info['charge_type'] == 'BOOK') {
  617. $data = [
  618. 'uid' => $this->uid,
  619. 'fee' => $fee,
  620. 'u' => Cookie::get('send_order_id'),
  621. 'distribution_channel_id' => $this->distribution_channel_id,
  622. 'bid' => $book_info->bid,
  623. 'book_name' => $book_info->book_name,
  624. 'send_order_id' => Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0,
  625. ];
  626. return BookOrderService::addOrderRecodeAndDecrUserBalance($data, $this->uid);
  627. } else {
  628. $data = [
  629. 'uid' => $this->uid,
  630. 'fee' => $fee,
  631. 'cid' => $chapter_id,
  632. 'bid' => $book_info->bid,
  633. 'distribution_channel_id' => $this->distribution_channel_id,
  634. 'book_name' => $book_info->book_name,
  635. 'chapter_name' => $chapter_name,
  636. 'send_order_id' => Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0,
  637. 'is_remind' => $is_remind
  638. ];
  639. //print_r($data);
  640. if ($is_remind) {
  641. $this->addOrderRemind($book_info->bid);
  642. }
  643. return ChapterOrderService::addOrderAndDecrUserBalance($data, $this->uid);
  644. }
  645. }
  646. protected function addOrderRemind($bid)
  647. {
  648. if (ChapterReminderService::checkIsNoReminder($this->uid, $bid)) {
  649. return true;
  650. } else {
  651. ChapterReminderService::add($this->uid, $bid);
  652. return true;
  653. }
  654. }
  655. /**
  656. * 是否订购提醒
  657. * @param $chapter_id
  658. * @return bool
  659. */
  660. protected function isOrderRemind($bid)
  661. {
  662. $is_no_reminder = ChapterReminderService::checkIsNoReminder($this->uid, $bid) ? 1 : 0;
  663. return $is_no_reminder == 0;
  664. }
  665. /**
  666. * 获取订购记录
  667. * @param $book_info
  668. * @param $chapter_id
  669. * @return bool
  670. */
  671. private function getOrderRecord($bid, $chapter_id)
  672. {
  673. //包年记录
  674. $uid = $this->uid;
  675. $res = YearOrderService::getRecord($uid);
  676. if ($res) {
  677. $this->crm_order = 'YEAR_ORDER';
  678. return true;
  679. }
  680. $res = null;
  681. //单本订购记录
  682. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  683. if ($res) {
  684. $this->crm_order = 'BOOK_ORDER';
  685. return true;
  686. }
  687. $res = null;
  688. //章节订购记录
  689. $chapterOrder = new ChapterOrderService();
  690. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) {
  691. return true;
  692. }
  693. $this->crm_order = 'CHAPTER_ORDER';
  694. return false;
  695. }
  696. /**
  697. * 计算价格
  698. * @param $book_info
  699. * @param $chapter_size
  700. * @return float
  701. */
  702. private function getPrice($book_info, $chapter)
  703. {
  704. if ($book_info->charge_type == 'BOOK') {
  705. if(BookOrderService::isHasBookOrder($this->uid)){
  706. $this->is_first_book_order = 0;
  707. return 1399;
  708. }else{
  709. $this->is_first_book_order = 1;
  710. return 899;
  711. }
  712. } else {
  713. $fee = BookService::getPrice($book_info,$this->distribution_channel_id,$chapter->size);
  714. return $fee;
  715. }
  716. }
  717. private function readRecordOther()
  718. {
  719. $bid = $this->book_info->bid;
  720. $cid = $this->cid;
  721. $distribution_channel_id = $this->distribution_channel_id;
  722. $send_order_id = $this->send_order_id;
  723. $from = $this->from_type;
  724. $sequence = $this->chapter->sequence;
  725. ReadRecordStatsService::record($this->uid,$bid,$cid,$distribution_channel_id,$send_order_id,$from,$sequence);
  726. }
  727. /**
  728. * 下架图书 true:下架不能阅读了, false:可以正常阅读
  729. * @return bool
  730. */
  731. private function isOffShelf()
  732. {
  733. $book_info = $this->book_info;
  734. if ($book_info->is_on_shelf == 0 || $book_info->is_on_shelf == 3) {
  735. return true;
  736. }
  737. if ($book_info->is_on_shelf == 4) {
  738. //版权到期,有包年可以继续阅读
  739. $year_order = YearOrderService::getRecord($this->uid);
  740. if($year_order){
  741. return false;
  742. }
  743. //版权到期,有订购记录还可以继续阅读,订购
  744. $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $book_info->bid);
  745. if (!$chapter_order) return true;
  746. }
  747. return false;
  748. }
  749. //章节被删除的情况
  750. private function chapterNotExists()
  751. {
  752. $chapter = $this->chapter;
  753. $cid = $this->cid;
  754. if ($chapter) return false;
  755. //章节不存在的情况
  756. $no_check = ChapterService::getChapterNameByIdNoCheck($cid);
  757. $h5_scheme = 'https';
  758. if (!$no_check) {
  759. //章节真正不存在的情况
  760. $url = $h5_scheme . '://' . _domain() . '/catalog?id=' . $this->en_bid;
  761. return $url;
  762. }
  763. $url = $h5_scheme . '://' . _domain() . '/catalog?id=' . $this->en_bid;// . '&sequence=' . $no_check->sequence;
  764. return $url;
  765. }
  766. private function showChapterPrice($bid, $charge_type)
  767. {
  768. if ($charge_type == 'BOOK') {
  769. return false;
  770. }
  771. $show_chapter_price_bid = env('SHOW_CHAPTER_PRICE_BID', 'all');
  772. if ($show_chapter_price_bid != 'all') {
  773. $show_chapter_price_bid_array = explode(',', $show_chapter_price_bid);
  774. if (!in_array($bid, $show_chapter_price_bid_array)) {
  775. return false;
  776. }
  777. }
  778. $show_chapter_price_channel = env('SHOW_CHAPTER_PRICE_CHANNEL', '');
  779. if (!$show_chapter_price_channel) {
  780. return false;
  781. }
  782. if ($show_chapter_price_channel == 'all') {
  783. return true;
  784. }
  785. $show_chapter_price_channel_array = explode(',', $show_chapter_price_channel);
  786. if (in_array($this->distribution_channel_id, $show_chapter_price_channel_array)) {
  787. return true;
  788. }
  789. return false;
  790. }
  791. /**
  792. * 阅读统计
  793. */
  794. private function chapterStats()
  795. {
  796. $chapter = $this->chapter;
  797. $bid = $this->book_info->bid;
  798. //派单精确统计
  799. if ($chapter->sequence <= 30) {
  800. //$this->sendOrderChapterUv($bid, $chapter->sequence);
  801. }
  802. //智能推送 测书统计章节uv
  803. if ($chapter->sequence <= 60) {
  804. //测书数据 由30改到60 2018-11-26 18:00
  805. if($this->book_info->test_status == 1){
  806. $this->smartPushTestBookChapterUv($bid, $chapter->sequence); //2019-07-09 10:30 cancel
  807. }
  808. }
  809. //智能推送 测书统计第二章 uv
  810. if ($chapter->sequence == 2) {
  811. $this->smartPushTestBookSecondChapterUv($bid);
  812. }
  813. // 统计前5张uv 会放在book_five_chapter_uv表中,脚本在 \App\Console\Commands\BookTest::data1里面
  814. // 作用早忘记了
  815. $field = date('Y-m-d');
  816. //统计点击率
  817. $key = 'book_click_num_bid_' . $bid;
  818. try {
  819. Redis::HINCRBY($key, $field, 1);
  820. } catch (\Exception $e) {
  821. }
  822. //阅读页面pvuv
  823. $this->recordReaderUvAndPv();
  824. }
  825. private function smartPushTestBook($bid, $balance)
  826. {
  827. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  828. if ($smart_bid && in_array($bid, $smart_bid)) {
  829. $field = sprintf('%s_amount', $bid);
  830. try {
  831. Redis::HINCRBY('smart_push_test_book', $field, $balance);
  832. Redis::sadd('smart_push_test_book_user_count' . $bid, $this->uid);
  833. } catch (\Exception $e) {
  834. }
  835. }
  836. }
  837. private function smartPushTestBookSecondChapterUv($bid)
  838. {
  839. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  840. if ($smart_bid && in_array($bid, $smart_bid)) {
  841. try {
  842. Redis::sadd('smart_push_test_book_second_uv' . $bid, $this->uid);
  843. } catch (\Exception $e) {
  844. }
  845. }
  846. }
  847. private function smartPushTestBookChapterUv($bid, $sequence)
  848. {
  849. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  850. if ($smart_bid && in_array($bid, $smart_bid)) {
  851. try {
  852. $key = 'smartPushTestBookChapterUv:bid:%s:seq:%s';
  853. Redis::sadd(sprintf($key, $bid, $sequence), $this->uid);
  854. } catch (\Exception $e) {
  855. }
  856. }
  857. }
  858. private function recordReaderUvAndPv()
  859. {
  860. WapVisitStatService::recordReaderUvAndPv($this->uid, $this->distribution_channel_id);
  861. }
  862. private function crmChapterStats(){
  863. $crm = Cookie::get('crm');
  864. if(!$crm)return ;
  865. if(!$this->crm_order) return ;
  866. $price_rate = env('DEFAULT_CHAPTER_PRICE', 0.015);
  867. $fee = ceil($this->chapter->size * $price_rate);
  868. if($fee <37) $fee = 37;
  869. if($fee >60) $fee = 60;
  870. try{
  871. CrmChapterOrderDetailService::create($this->uid,$fee,$this->book_info->bid,$crm,$this->cid,$this->crm_order);
  872. }catch (\Exception $e){}
  873. }
  874. private function customChapterOrderStats($fee)
  875. {
  876. //custom_290824
  877. Log::info('customChapterOrderStats start');
  878. Log::info($this->from_type);
  879. if(strpos($this->from_type,'custom') === false){
  880. return ;
  881. }
  882. $customer_id= str_ireplace('custom_','',$this->from_type);
  883. Log::info($customer_id);
  884. if(!$customer_id || !is_numeric($customer_id)) return ;
  885. $info = Redis::hget('latestcustomerinfo',$customer_id);
  886. if(!$info) return ;
  887. Log::info($info);
  888. $info_array = json_decode($info,1);
  889. Log::info($info_array);
  890. $customer_bid = isset($info_array['bid'])?$info_array['bid']:0;
  891. $customer_send_time = isset($info_array['send_time'])?$info_array['send_time']:0;
  892. if(!$customer_bid || !$customer_send_time) return ;
  893. CustomChapterOrderService::create([
  894. 'distribution_channel_id'=>$this->distribution_channel_id,
  895. 'bid'=>$this->book_info->bid,
  896. 'cid'=>$this->cid,
  897. 'book_name'=>$this->book_info->book_name,
  898. 'chapter_name'=>$this->chapter->name,
  899. 'uid'=>$this->uid,
  900. 'send_order_id'=>$this->send_order_id,
  901. 'fee'=>$fee,
  902. 'charge_balance'=>0,
  903. 'reward_balance'=>0,
  904. 'custom_id'=>$customer_id,
  905. 'flag'=>$customer_bid == $this->book_info->bid?1:0,
  906. 'send_time'=>$customer_send_time
  907. ]);
  908. }
  909. public function getWechatJsConfig(Request $request)
  910. {
  911. $bid = $request->input('bid', '');
  912. $cid = $request->input('cid', '');
  913. $url = $request->input('url', '');
  914. $url = urldecode($url);
  915. \Log::info('url:' . ($this->uid) . $url);
  916. if (empty($bid) || empty($cid) || empty($url)) {
  917. return response()->error('PARAM_EMPTY');
  918. }
  919. $app_hash = array(
  920. 4025=>['appid'=>'wx80b618c9dcc940f7','appsecret'=>'255a2de8db51e4084b5e9087d26d4ecb'],
  921. 4147=>['appid'=>'wx2a99699cd9572812','appsecret'=>'0e0d57f94eeba50abb3af6883df05a5c'],
  922. 691=>['appid'=>'wx5cefcd251dc40693','appsecret'=>'755f64c85173dad21f20870e38f59d8a'],
  923. 123=>['appid'=>'wx983808610aa9dcc5','appsecret'=>'91002999590b79f3c7be35fd73c04049'],
  924. 211=>['appid'=>'wx39338e374cc2055b','appsecret'=>'1a4c2039be271b3811182be5bb53eb34'],
  925. );
  926. $appId = $app_hash[$this->distribution_channel_id]['appid'];
  927. $appSecret = $app_hash[$this->distribution_channel_id]['appsecret'];
  928. $js_config = (new ChapterShareWechatConfigService($appId,$appSecret))->getSignPackage($url);
  929. $book = BookService::getBookById(Hashids::decode($bid)[0]);
  930. $first_cid = $book->first_cid;
  931. $share_url = 'https://site' . encodeDistributionChannelId($this->distribution_channel_id) . '.' . env('SHARE_DOMAIN') . '/reader?fromtype=readershare&bid=' . $bid . '&cid=' . ($first_cid) . '&fromflag=' . ($this->uid) . '&fromcid=' . $cid;
  932. if (in_array($this->distribution_channel_id, [2, 14])) {
  933. $js_config = ChapterShareWechatConfigService::getConfig($url);
  934. $share_url = 'https://site' . ($this->distribution_channel_id) . '.' . env('SHARE_DOMAIN') . '/reader?fromtype=readershare&bid=' . $bid . '&cid=' . ($first_cid) . '&fromflag=' . ($this->uid) . '&fromcid=' . $cid;
  935. }
  936. $js_config['share_url'] = $share_url;
  937. return response()->success($js_config);
  938. }
  939. }