ChapterController.php 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  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\OrderService;
  13. use Illuminate\Http\Request;
  14. use App\Http\Controllers\Wap\BaseController;
  15. use Cookie;
  16. use Redis;
  17. use App\Modules\Book\Services\ChapterService;
  18. use App\Modules\User\Services\ReadRecordService;
  19. use App\Http\Controllers\Wap\Book\Transformers\ChapterTransformer;
  20. use App\Modules\Book\Services\BookConfigService;
  21. use App\Http\Controllers\Wap\Book\Transformers\ChapterListTransformer;
  22. use App\Modules\Subscribe\Services\BookOrderService;
  23. use App\Modules\Subscribe\Services\ChapterOrderService;
  24. use App\Modules\Subscribe\Services\YearOrderService;
  25. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  26. use App\Modules\Subscribe\Services\ChapterReminderService;
  27. use App\Modules\OfficialAccount\Services\OfficialAccountService;
  28. use Hashids;
  29. use GuzzleHttp\Client;
  30. use Log;
  31. use DB;
  32. use App\Modules\User\Services\UserSignService;
  33. use App\Modules\SendOrder\Services\SendOrderService;
  34. use App\Modules\User\Services\UserDeepReadTagService;
  35. use App\Modules\Book\Services\BookSubscribleChapterService;
  36. use App\Modules\Channel\Services\ChannelSubscribeSettingService;
  37. class ChapterController extends BaseController
  38. {
  39. /**
  40. * @apiDefine Chapter 章节
  41. */
  42. //用户是否强关
  43. private $is_had_subscribe = false;
  44. //章节是否需要强关
  45. private $is_need_subscribe;
  46. //用户信息
  47. private $user;
  48. //图书信息
  49. private $book_info;
  50. //cid
  51. private $cid;
  52. //加密的bid
  53. private $en_bid;
  54. //章节信息
  55. private $chapter;
  56. //强关时间
  57. private $force_subscribe_time = null;
  58. /**
  59. * @apiVersion 1.0.0
  60. * @apiDescription 章节列表不分页
  61. * @api {get} books/{bid}/allcatalog 章节列表不分页
  62. * @apiGroup Chapter
  63. * @apiName getCatalog
  64. * @apiSuccess {int} code 状态码
  65. * @apiSuccess {String} msg 信息
  66. * @apiSuccess {object} data 结果集
  67. * @apiSuccess {Array} data.list 分页结果集
  68. * @apiSuccess {Int} data.list.bid bid
  69. * @apiSuccess {Int} data.list.chapter_id 章节id
  70. * @apiSuccess {String} data.list.chapter_name 章节名称
  71. * @apiSuccess {Int} data.list.chapter_sequence 序号
  72. * @apiSuccess {Int} data.list.chapter_is_vip 是否vip
  73. * @apiSuccess {Int} data.list.chapter_size 章节大小
  74. * @apiSuccess {Int} data.list.prev_cid 上一章节id
  75. * @apiSuccess {Int} data.list.next_cid 下一章节
  76. * @apiSuccess {String} data.list.recent_update_at 更新时间
  77. * @apiSuccess {String} data.list.is_need_subscirbe 是否强制关注
  78. * @apiSuccess {String} data.list.is_show_price 是否显示价格
  79. * @apiSuccess {String} data.list.price 价格
  80. * @apiSuccess {object} data.meta 分页信息
  81. * @apiSuccess {Int} data.meta.total 总条数
  82. * @apiSuccess {Int} data.meta.per_page 每页条数
  83. * @apiSuccess {Int} data.meta.current_page 当前页
  84. * @apiSuccess {Int} data.meta.last_page 最后页
  85. * @apiSuccess {String} data.meta.next_page_url 下一页
  86. * @apiSuccess {String} data.meta.prev_page_url 上一页
  87. * @apiSuccessExample {json} Success-Response:
  88. * HTTP/1.1 200 OK
  89. * {
  90. * code: 0,
  91. * msg: "",
  92. * data:
  93. * [
  94. * {
  95. * bid: 5,
  96. * chapter_id: 5,
  97. * chapter_name: "第1240章 不是我",
  98. * chapter_sequence: 1239,
  99. * chapter_is_vip: 1,
  100. * chapter_size: 2422,
  101. * prev_cid: 0,
  102. * next_cid: 0,
  103. * recent_update_at: 2017-11-20 15:01:56,
  104. * is_need_subscirbe: 1,
  105. * },
  106. * {
  107. * bid: 5,
  108. * chapter_id: 5,
  109. * chapter_name: "第1240章 不是我",
  110. * chapter_sequence: 1239,
  111. * chapter_is_vip: 1,
  112. * chapter_size: 2422,
  113. * prev_cid: 0,
  114. * next_cid: 0,
  115. * recent_update_at: 2017-11-20 15:01:56,
  116. * is_need_subscirbe: 1,
  117. * },
  118. * ]
  119. * }
  120. */
  121. public function getCatalog(Request $request, $t, $domain, $bid)
  122. {
  123. $this->en_bid = $bid;
  124. $bid = Hashids::decode($bid)[0];
  125. $lists = ChapterService::getChapterLists($bid);
  126. $book_info = BookConfigService::getBookById($bid);
  127. if (!$book_info) {
  128. return response()->error('PARAM_ERROR');
  129. }
  130. $this->book_info = $book_info;
  131. $is_show_price = $this->showChapterPrice($bid, $book_info->charge_type);
  132. foreach ($lists as $v) {
  133. $v->is_show_price = $is_show_price;
  134. $v->price = '';
  135. if ($is_show_price) {
  136. $v->price = $this->getPrice($book_info, $v);
  137. }
  138. }
  139. return response()->collection(new ChapterListTransformer, $lists);
  140. }
  141. /**
  142. * @apiVersion 1.0.0
  143. * @apiDescription 章节列表分页
  144. * @api {get} books/{bid}/catalog 章节列表分页
  145. * @apiGroup Chapter
  146. * @apiName getCatalogPerPage
  147. * @apiParam {Int} page_size 分页大小(默认15)
  148. * @apiParam {Int} page 页码(默认1)
  149. * @apiSuccess {int} code 状态码
  150. * @apiSuccess {String} msg 信息
  151. * @apiSuccess {object} data 结果集
  152. * @apiSuccess {Array} data.list 分页结果集
  153. * @apiSuccess {Int} data.list.bid bid
  154. * @apiSuccess {Int} data.list.chapter_id 章节id
  155. * @apiSuccess {String} data.list.chapter_name 章节名称
  156. * @apiSuccess {Int} data.list.chapter_sequence 序号
  157. * @apiSuccess {Int} data.list.chapter_is_vip 是否vip
  158. * @apiSuccess {Int} data.list.chapter_size 章节大小
  159. * @apiSuccess {Int} data.list.prev_cid 上一章节id
  160. * @apiSuccess {Int} data.list.next_cid 下一章节
  161. * @apiSuccess {String} data.list.recent_update_at 更新时间
  162. * @apiSuccess {String} data.list.is_need_subscirbe 是否强制关注
  163. * @apiSuccess {String} data.list.is_show_price 是否显示价格
  164. * @apiSuccess {String} data.list.price 价格
  165. * @apiSuccess {object} data.meta 分页信息
  166. * @apiSuccess {Int} data.meta.total 总条数
  167. * @apiSuccess {Int} data.meta.per_page 每页条数
  168. * @apiSuccess {Int} data.meta.current_page 当前页
  169. * @apiSuccess {Int} data.meta.last_page 最后页
  170. * @apiSuccess {String} data.meta.next_page_url 下一页
  171. * @apiSuccess {String} data.meta.prev_page_url 上一页
  172. * @apiSuccessExample {json} Success-Response:
  173. * HTTP/1.1 200 OK
  174. * {
  175. * code: 0,
  176. * msg: "",
  177. * data:
  178. * list:[
  179. * {
  180. * bid: 5,
  181. * chapter_id: 5,
  182. * chapter_name: "第1240章 不是我",
  183. * chapter_sequence: 1239,
  184. * chapter_is_vip: 1,
  185. * chapter_size: 2422,
  186. * prev_cid: 0,
  187. * next_cid: 0,
  188. * recent_update_at: 2017-11-20 15:01:56,
  189. * is_need_subscirbe: 1,
  190. * },
  191. * {
  192. * bid: 5,
  193. * chapter_id: 5,
  194. * chapter_name: "第1240章 不是我",
  195. * chapter_sequence: 1239,
  196. * chapter_is_vip: 1,
  197. * chapter_size: 2422,
  198. * prev_cid: 0,
  199. * next_cid: 0,
  200. * recent_update_at: 2017-11-20 15:01:56,
  201. * is_need_subscirbe: 1,
  202. * },
  203. * ]
  204. * meta:{
  205. * total: 1253,
  206. * per_page: 15,
  207. * current_page: 1,
  208. * last_page: 84,
  209. * next_page_url: "http://myapi.cn/api/books/1/chapter?page=2",
  210. * prev_page_url: ""
  211. * }
  212. * }
  213. */
  214. public function getCatalogPerPage(Request $request, $t, $domain, $bid)
  215. {
  216. $this->en_bid = $bid;
  217. $bid_array = Hashids::decode($bid);
  218. if (isset($bid_array[0])) {
  219. $bid = $bid_array[0];
  220. } else {
  221. return response()->error('PARAM_ERROR');
  222. }
  223. $book_info = BookConfigService::getBookById($bid);
  224. if (!$book_info) {
  225. return response()->error('PARAM_ERROR');
  226. }
  227. $this->book_info = $book_info;
  228. $page_size = $request->input('page_size', 15);
  229. $res = ChapterService::getChapterListsPage($bid, $page_size);
  230. $is_show_price = $this->showChapterPrice($bid, $book_info->charge_type);
  231. foreach ($res as $v) {
  232. $v->is_show_price = $is_show_price;
  233. $v->price = '';
  234. if ($is_show_price) {
  235. $v->price = $this->getPrice($book_info, $v);
  236. }
  237. }
  238. return response()->pagination(new ChapterListTransformer, $res);
  239. }
  240. /**
  241. * @apiVersion 1.0.0
  242. * @apiDescription 章节内容
  243. * @api {get} books/{bid}/chapters/{chapter_id} 章节内容
  244. * @apiGroup Chapter
  245. * @apiName index
  246. * @apiSuccess {int} code 状态码
  247. * @apiSuccess {String} msg 信息
  248. * @apiSuccess {object} data 结果集
  249. * @apiSuccess {Int} data.chapter_id 章节id
  250. * @apiSuccess {String} data.chapter_name 章节名称
  251. * @apiSuccess {Int} data.chapter_sequence 序号
  252. * @apiSuccess {Int} data.chapter_is_vip 是否vip
  253. * @apiSuccess {Int} data.chapter_size 章节大小
  254. * @apiSuccess {Int} data.prev_cid 上一章节id
  255. * @apiSuccess {Int} data.next_cid 下一章节
  256. * @apiSuccess {String} data.recent_update_at 更新时间
  257. * @apiSuccess {String} data.chapter_content 章节内容
  258. * @apiSuccess {Int} data.is_need_subscirbe 是否强制关注(删除)
  259. * @apiSuccess {Int} data.is_show_subscribe_link 是否显示底部强关链接
  260. * @apiSuccessExample {json} Success-Response:
  261. * HTTP/1.1 200 OK
  262. * {
  263. * code: 0,
  264. * msg: "",
  265. * data: {
  266. * chapter_id: 5,
  267. * chapter_name: "第1240章 不是我",
  268. * chapter_sequence: 1239,
  269. * chapter_is_vip: 1,
  270. * chapter_size: 2422,
  271. * prev_cid: 0,
  272. * next_cid: 0,
  273. * recent_update_at: 2017-11-20 15:01:56,
  274. * chapter_content: "叶妩被司行霈的阴阳怪气一吓,思路偏得太远了。 她张口结舌,忘记了自己要说什么。",
  275. * }
  276. * }
  277. */
  278. public function index(Request $request, $t, $domain, $bid, $cid)
  279. {
  280. //注意:内部约定用 有此参数不需要强关
  281. $inter_not_need_sub = $request->has('inter_not_need_sub') ? true : false;
  282. $ad_status = $request->get('ad_status');
  283. if (!$this->checkUid()) {
  284. return response()->error('WAP_NOT_LOGIN');
  285. }
  286. $this->en_bid = $bid;
  287. $bid = Hashids::decode($bid)[0];
  288. //获取图书信息
  289. $book_info = BookConfigService::getBookById($bid);
  290. if (empty($book_info)) return response()->error('WAP_SYS_ERROR');
  291. $this->book_info = $book_info;
  292. $this->cid = $cid;
  293. //图书域名,book_configs表的promotion_domain 要跟当前的主机名匹配
  294. if ($this->isVisitDomainEqualBookDomain($domain)) {
  295. return response()->error('WAP_DOMAIN_NOT_MATCH', [
  296. 'url' => $this->getCorrespondBookUrl()
  297. ]);
  298. }
  299. //下架图书不能看
  300. if ($this->isOffShelf()) {
  301. return response()->error('WAP_OFF_SHELF');
  302. }
  303. //获取章节信息
  304. $chapter = ChapterService::getChapterNameById($cid, $bid);
  305. $this->chapter = $chapter;
  306. //章节被删除
  307. if (($is_delete = $this->chapterNotExists())) {
  308. return response()->error('WAP_DOMAIN_NOT_MATCH', [
  309. 'url' => $is_delete
  310. ]);
  311. }
  312. $user_info = $this->_user_info;
  313. $this->user = $user_info;
  314. //用户是否强关
  315. $this->is_had_subscribe = $is_had_subscribe = $inter_not_need_sub ? true : $this->getSubscribe();
  316. //获取强关章节数
  317. $subscribe_seq = $this->getSubscribeChapterNum($book_info);
  318. //章节是否需要强关 TRUE:不需要,false:需要
  319. $force_subscribe = $subscribe_seq > $chapter->sequence;
  320. $this->is_need_subscribe = !$force_subscribe;
  321. if ($force_subscribe || $chapter->is_vip == 0) {
  322. //vip前一张和枪管前一张的访问统计
  323. $this->beforeForceSubAndBeforeVipUvAndPv($book_info, $subscribe_seq, $chapter);
  324. }
  325. //阅读记录
  326. ReadRecordService::addReadRecord(['uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  327. 'cid' => $cid, 'chapter_name' => $chapter->name]);
  328. //不用关注且不是vip章节
  329. if ($force_subscribe && $chapter->is_vip == 0) {
  330. //获取公共号
  331. $officialAccount = $this->getOfficialAccount();
  332. $this->chapter->is_show_subscribe_link = 0;
  333. //非强关,但是设置了弱关,阅读页底部显示强关链接
  334. if ($officialAccount && isset($officialAccount->appid) && !empty($officialAccount->appid)) {
  335. $sub_type = $this->getSubscribeType($officialAccount->appid, $bid);
  336. if (in_array(2, $sub_type['type']) && !$is_had_subscribe) {
  337. $this->chapter->is_show_subscribe_link = 1;
  338. }
  339. }
  340. $this->getChapter();
  341. return response()->item(new ChapterTransformer, $this->chapter);
  342. }
  343. //需要强制关注 不是vip章节 已经关注了
  344. if (!$force_subscribe && $chapter->is_vip == 0 && $is_had_subscribe) {
  345. $this->getChapter();
  346. return response()->item(new ChapterTransformer, $this->chapter);
  347. }
  348. //需要强制关注 没关注
  349. if ((!$force_subscribe) && !$is_had_subscribe) {
  350. $force_data = $this->forceSubscribe();
  351. if ($force_data) {
  352. $this->subscribeStats();
  353. return response()->error($force_data['error_type'], $force_data['data']);
  354. }
  355. //取不到公众号则不用强关
  356. if ($chapter->is_vip == 0) {
  357. $this->getChapter();
  358. return response()->item(new ChapterTransformer, $this->chapter);
  359. }
  360. }
  361. //已经付费
  362. if ($this->getOrderRecord($bid, $cid)) {
  363. $this->getChapter();
  364. return response()->item(new ChapterTransformer, $this->chapter);
  365. }
  366. //获取价格
  367. $fee = $this->getPrice($book_info, $chapter);
  368. //广告
  369. if($this->isUserInAdSample() && $ad_status && $ad_status == 1 && $user_info->balance < $fee){
  370. $this->getChapter();
  371. $this->bookOrderOrChapterOrder($book_info, $cid, 0, $chapter->name, 1);
  372. return response()->item(new ChapterTransformer, $this->chapter);
  373. }
  374. //以上都是不用付费的情况******************************************************
  375. //未付费
  376. //返回的价格信息
  377. $data = [
  378. 'book_id' => $this->en_bid,
  379. 'book_name' => $book_info->book_name,
  380. 'chapter_name' => $chapter->name,
  381. 'chapter_id' => $cid,
  382. 'pay_type' => $book_info->charge_type,
  383. 'fee' => $fee,
  384. 'user_balance' => $user_info->balance,
  385. 'product_id' => $book_info->product_id,
  386. 'uid' => $this->uid,
  387. 'distribution_channel_id' => $this->distribution_channel_id,
  388. 'is_discount' => 0,
  389. 'discount_fee' => '',
  390. 'discount' => ''
  391. ];
  392. //余额不足 弹窗提醒
  393. if ($user_info['balance'] < $fee) {
  394. $this->smartPushTestBookPayPageUv($bid);
  395. //$this->sendOrderPayPageUv($bid);
  396. //除了单本订购 其他的都不提醒,直接跳转到充值页面
  397. if ($book_info->charge_type == 'CHAPTER') {
  398. return response()->error('WAP_CHAPTER_SECOND_BALANCE_PAY', $data);
  399. }
  400. //需要提箱
  401. if ($this->isOrderRemind($bid)) {
  402. return response()->error('WAP_BOOK_BALANCE_PAY', $data);
  403. } else {
  404. return response()->error('WAP_BOOK_SECOND_BALANCE_PAY', $data);
  405. }
  406. }
  407. //余额充足 单本弹窗提醒,章节直接扣费
  408. if ($this->isOrderRemind($bid) && $book_info->charge_type == 'BOOK') {
  409. return response()->error('WAP_BOOK_BUY', $data);
  410. }
  411. //付费 不提醒
  412. if ($this->balancePay($book_info, $chapter, 0)) {
  413. $this->getChapter();
  414. return response()->item(new ChapterTransformer, $this->chapter);
  415. } else {
  416. //付费 异常
  417. return response()->error('WAP_SYS_ERROR');
  418. }
  419. }
  420. /**
  421. * @apiVersion 1.0.0
  422. * @apiDescription 余额支付
  423. * @api {get} books/{bid}/balance/chapterOrders/{cid} 余额支付
  424. * @apiGroup Chapter
  425. * @apiName pay
  426. * @apiParam (Int) remind 提醒
  427. * @apiSuccess {int} code 状态码
  428. * @apiSuccess {String} msg 信息
  429. * @apiSuccess {object} data 结果集
  430. * @apiSuccess {Int} data.chapter_id 章节id
  431. * @apiSuccess {String} data.chapter_name 章节名称
  432. * @apiSuccess {Int} data.chapter_sequence 序号
  433. * @apiSuccess {Int} data.chapter_is_vip 是否vip
  434. * @apiSuccess {Int} data.chapter_size 章节大小
  435. * @apiSuccess {Int} data.prev_cid 上一章节id
  436. * @apiSuccess {Int} data.next_cid 下一章节
  437. * @apiSuccess {String} data.recent_update_at 更新时间
  438. * @apiSuccess {String} data.chapter_content 章节内容
  439. * @apiSuccess {Int} data.is_need_subscirbe 是否强制关注(删除)
  440. * @apiSuccessExample {json} Success-Response:
  441. * HTTP/1.1 200 OK
  442. * {
  443. * code: 0,
  444. * msg: "",
  445. * data: {
  446. * chapter_id: 5,
  447. * chapter_name: "第1240章 不是我",
  448. * chapter_sequence: 1239,
  449. * chapter_is_vip: 1,
  450. * chapter_size: 2422,
  451. * prev_cid: 0,
  452. * next_cid: 0,
  453. * recent_update_at: 2017-11-20 15:01:56,
  454. * chapter_content: "叶妩被司行霈的阴阳怪气一吓,思路偏得太远了。 她张口结舌,忘记了自己要说什么。",
  455. * }
  456. * }
  457. */
  458. public function pay(Request $request, $t, $domain, $bid, $cid)
  459. {
  460. if (!$this->checkUid()) {
  461. return response()->error('NOT_LOGIN');
  462. }
  463. $remind = (int)$request->input('remind');
  464. $this->en_bid = $bid;
  465. $oldbid = $bid;
  466. $bid = Hashids::decode($bid)[0];
  467. $book_info = BookConfigService::getBookById($bid);;
  468. if (empty($book_info)) response()->error('WAP_SYS_ERROR');
  469. //获取章节
  470. $chapter = ChapterService::getChapterNameById($cid, $bid);
  471. if (!$chapter) {
  472. return response()->error('WAP_SYS_ERROR');
  473. }
  474. $this->chapter = $chapter;
  475. $this->book_info = $book_info;
  476. $this->cid = $cid;
  477. if ($this->balancePay($book_info, $chapter, $remind)) {
  478. ReadRecordService::addReadRecord(['uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
  479. 'cid' => $cid, 'chapter_name' => $chapter->name]);
  480. $this->getChapter();
  481. return response()->item(new ChapterTransformer, $this->chapter);
  482. } else {
  483. $fee = $this->getPrice($book_info, $chapter);
  484. $data = [
  485. 'book_id' => $oldbid,
  486. 'book_name' => $book_info->book_name,
  487. 'chapter_name' => $chapter->name,
  488. 'chapter_id' => $cid,
  489. 'pay_type' => $book_info->charge_type,
  490. 'fee' => $fee,
  491. 'user_balance' => $this->_user_info['balance'],
  492. 'product_id' => $book_info->product_id,
  493. 'uid' => $this->uid,
  494. 'distribution_channel_id' => $this->distribution_channel_id,
  495. 'is_discount' => 0,
  496. 'discount_fee' => '',
  497. 'discount' => ''
  498. ];
  499. //不需要提醒
  500. if ($book_info->charge_type == 'BOOK') {
  501. return response()->error('WAP_BOOK_SECOND_BALANCE_PAY', $data);
  502. } elseif ($book_info->charge_type == 'CHAPTER') {
  503. return response()->error('WAP_CHAPTER_SECOND_BALANCE_PAY', $data);
  504. } else {
  505. return response()->error('WAP_SYS_ERROR');
  506. }
  507. }
  508. }
  509. /**
  510. * 余额支付
  511. * @param $book_info
  512. * @param $chapter_id
  513. * @param $chapter_size
  514. * @return bool
  515. */
  516. protected function balancePay($book_info, $chapter, $is_remind)
  517. {
  518. $fee = $this->getPrice($book_info, $chapter);
  519. if ((int)$this->_user_info['balance'] >= $fee) {
  520. if ($this->bookOrderOrChapterOrder($book_info, $chapter->id, $fee, $chapter->name, $is_remind)) {
  521. $this->chapterOrderStats($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. //统计
  541. $this->chapterStats();
  542. //123特殊强关
  543. $this->specialSubscribeFlag();
  544. //签到奖励
  545. $this->sign();
  546. //#短篇推送长篇需求#
  547. //$this->shortPushLong();
  548. //#短推长、短推短对比
  549. $this->shortPushshort();
  550. //阅读器分享
  551. $this->share();
  552. //广告
  553. $this->isShowAd();
  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. //#短篇推送长篇需求#
  570. $this->chapter->next_jump = 0;
  571. $this->chapter->next_jump_url = '';
  572. $this->chapter->show_push_title = 0;
  573. $this->chapter->push_title_url = '';
  574. $this->chapter->push_title = [];
  575. //分享
  576. $this->chapter->is_show_share_button = 0;
  577. //阅读底部强关图片 只有123有
  578. $this->chapter->is_had_subscribe = 0;
  579. //广告
  580. $this->chapter->is_show_ad = 0;
  581. }
  582. private function sign()
  583. {
  584. //渠道123 未关注的签到放到强关前一章
  585. if ($this->distribution_channel_id == 123 && !$this->is_had_subscribe) {
  586. if ($this->book_info->force_subscribe_chapter_seq - 1 == $this->chapter->sequence) {
  587. $sign_status = $this->userSign($this->uid);
  588. //$sign_status = 0;
  589. $this->chapter->sign_status = $sign_status ? 1 : 0;
  590. $this->chapter->sign_reard = $sign_status;
  591. $this->chapter->sign_days = ReadRecordService::getSignCount($this->uid);
  592. }
  593. } else {
  594. $sign_status = $this->userSign($this->uid);
  595. //$sign_status =0;
  596. $this->chapter->sign_status = $sign_status ? 1 : 0;
  597. $this->chapter->sign_reard = $sign_status;
  598. $this->chapter->sign_days = ReadRecordService::getSignCount($this->uid);
  599. }
  600. }
  601. private function forceSubscribe()
  602. {
  603. //获取公众号信息
  604. $officialAccount = $this->getOfficialAccount();
  605. if (!$officialAccount || empty($officialAccount->appid)) {
  606. return false;
  607. }
  608. //如果是链接强关,则跳转
  609. $sub_type = $this->getSubscribeType($officialAccount->appid, $this->book_info->bid);
  610. if (in_array(3, $sub_type['type']) && $sub_type['url']) {
  611. return ['error_type' => 'WAP_LINK_SUBSCRIBE', 'data' => ['url' => $sub_type['url']]];
  612. }
  613. //普通强关
  614. if (in_array(1, $sub_type['type'])) {
  615. $src = $this->getRcodeInfo(['appid' => $officialAccount->appid], $this->book_info->bid);
  616. if (empty($src)) return false;
  617. if ($officialAccount->head_img) {
  618. $url = '/subscribe/qrcode?img=' . urlencode($src) . '&title=' . $officialAccount->nickname . '&head_img=' . urlencode($officialAccount->head_img) . '&uid=' . $this->uid;
  619. } else {
  620. $url = '/subscribe/qrcode?img=' . urlencode($src) . '&title=' . $officialAccount->nickname . '&uid=' . $this->uid;
  621. }
  622. return ['error_type' => 'WAP_NOT_SUBSCRIBE', 'data' => ['src' => $url, 'head_img' => $officialAccount->head_img]];
  623. }
  624. return false;
  625. }
  626. //强关页面的统计
  627. private function subscribeStats()
  628. {
  629. if ($this->send_order_id) {
  630. //$this->sendOrderSubscribePageUv($this->book_info->bid);
  631. //强关页面的uv
  632. /*try {
  633. Redis::sadd('subscribe_page_uv' . $this->send_order_id, $this->uid);
  634. Redis::sadd('subscribe_page_uv_send_order_ids', $this->send_order_id);
  635. } catch (\Exception $e) {
  636. }*/
  637. }
  638. }
  639. /**
  640. * 短推长
  641. */
  642. private function shortPushLong()
  643. {
  644. if ($this->book_info->charge_type = 'CHAPTER') return;
  645. $is_has_book_order = BookOrderService::isHasBookOrder($this->uid);
  646. if ($this->chapter->is_vip && $is_has_book_order) {
  647. $this->chapter->show_push_title = 1;
  648. $this->chapter->push_title = DataAnalysisBookConfig::getUserReaderPagePushBook($this->uid, $this->distribution_channel_id);
  649. }
  650. if (!$this->chapter->next_cid && $is_has_book_order) {
  651. $link = DataAnalysisBookConfig::getUserReaderPagePushBook($this->uid, $this->distribution_channel_id, 1);
  652. if ($link && isset($link['link']) && !empty($link['link'])) {
  653. $this->chapter->next_jump = 1;
  654. $this->chapter->next_jump_url = $link['link'];
  655. }
  656. }
  657. }
  658. /**
  659. * 短推长、短推短对比需求
  660. * 目标用户:全站短篇进入新注册且订阅短篇的付费用户
  661. * 方式:根据uid奇偶取用,奇:测试用户1 偶:测试用户2
  662. * 测试用户1:底部链接+完本推荐 均使用短篇小说
  663. * 测试用户2:底部链接+完本推荐 均使用长篇小说
  664. * 目标用户:全站短篇进入新注册且订阅短篇的付费用户
  665. */
  666. private function shortPushshort()
  667. {
  668. //新用户 注册时间大于某个值
  669. if(!$this->user){
  670. return ;
  671. }
  672. if(!env('SHORT_PUSH_SHORT_START_TIME') || strtotime($this->user->created_at) < env('SHORT_PUSH_SHORT_START_TIME')){
  673. return ;
  674. }
  675. //没有派单
  676. if(!$this->user->send_order_id) return ;
  677. //只在vip章节底部显示
  678. if(!$this->chapter->is_vip){
  679. return ;
  680. }
  681. //强关
  682. if(!$this->is_had_subscribe){
  683. return ;
  684. }
  685. //短篇进入
  686. $send_order_info = SendOrderService::getById($this->user->send_order_id);
  687. if(!$send_order_info || $send_order_info->charge_type != 'BOOK'){
  688. return ;
  689. }
  690. //订阅短篇的付费用户
  691. $is_has_book_order = BookOrderService::isHasBookOrder($this->uid);
  692. if(!$is_has_book_order) return ;
  693. //是否付费用户
  694. if(!OrderService::isPaidUser($this->uid)) return ;
  695. $type = $this->uid %2 == 1? 'BOOK':'CHAPTER';
  696. $result = DataAnalysisSelectUserService::create($this->uid, $this->distribution_channel_id, $type, 500, $attach = 'short2s2l');
  697. if(!$result) return ;
  698. //底部链接
  699. $this->chapter->show_push_title = 1;
  700. $this->chapter->push_title = DataAnalysisBookConfig::getUserReaderPagePushBookV2($this->uid, $this->distribution_channel_id,$type);
  701. //完本推荐
  702. if (!$this->chapter->next_cid ) {
  703. $link = DataAnalysisBookConfig::getUserReaderPagePushBookV2($this->uid, $this->distribution_channel_id,$type, 1);
  704. if($link && $link['link']){
  705. $this->chapter->next_jump = 1;
  706. $this->chapter->next_jump_url = $link['link'];
  707. }
  708. }
  709. }
  710. private function share()
  711. {
  712. if ($this->is_had_subscribe && in_array($this->distribution_channel_id, [2, 14, 211,4025,4147,691,123]) && $this->user) {//,4147,691,123,4025
  713. $this->chapter->is_show_share_button = 1;
  714. }
  715. }
  716. /**
  717. * 123 渠道阅读底部强关图片
  718. */
  719. private function specialSubscribeFlag()
  720. {
  721. if ($this->distribution_channel_id == 123 && !$this->is_had_subscribe) {
  722. $this->chapter->is_had_subscribe = 1;
  723. }
  724. }
  725. //订阅统计
  726. private function chapterOrderStats($fee)
  727. {
  728. $day = date('Y--m-d');
  729. try {
  730. Redis::hincrby('wap:chapterandbookorder:bid:' . $this->book_info->bid, $day, $fee);
  731. } catch (\Exception $e) {
  732. }
  733. $this->smartPushTestBook($this->book_info->bid, $fee);
  734. $specialChannelIdStats = env('SPECIAL_CHANNEL_STATS', 211);
  735. if (in_array($this->distribution_channel_id, explode(',', $specialChannelIdStats))) {
  736. WapVisitStatService::specialChannelIdStatsSave($this->uid, $this->book_info->bid, $fee);
  737. }
  738. }
  739. /**
  740. * 签到
  741. * @param $uid
  742. * @return int
  743. */
  744. protected function userSign($uid)
  745. {
  746. if (UserSignService::isSign($uid)) {
  747. //签过到了
  748. return 0;
  749. }
  750. if ($res = UserSignService::signToday($uid)) {
  751. //$sign_key = 'leyuee:wap:usersigni';
  752. //Redis::hset($sign_key, $uid, $res);
  753. return $res;
  754. }
  755. return 0;
  756. }
  757. /**
  758. * 添加订购记录
  759. * @param $book_info
  760. * @param $chapter_id
  761. * @param $fee
  762. * @return bool
  763. */
  764. protected function bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)
  765. {
  766. if ($book_info['charge_type'] == 'BOOK') {
  767. $data = [
  768. 'uid' => $this->uid,
  769. 'fee' => $fee,
  770. 'u' => Cookie::get('send_order_id'),
  771. 'distribution_channel_id' => $this->distribution_channel_id,
  772. 'bid' => $book_info->bid,
  773. 'book_name' => $book_info->book_name,
  774. 'send_order_id' => Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0,
  775. ];
  776. return BookOrderService::addOrderRecodeAndDecrUserBalance($data, $this->uid);
  777. } else {
  778. $data = [
  779. 'uid' => $this->uid,
  780. 'fee' => $fee,
  781. 'cid' => $chapter_id,
  782. 'bid' => $book_info->bid,
  783. 'distribution_channel_id' => $this->distribution_channel_id,
  784. 'book_name' => $book_info->book_name,
  785. 'chapter_name' => $chapter_name,
  786. 'send_order_id' => Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0,
  787. 'is_remind' => $is_remind
  788. ];
  789. //print_r($data);
  790. if ($is_remind) {
  791. $this->addOrderRemind($book_info->bid);
  792. }
  793. return ChapterOrderService::addOrderAndDecrUserBalance($data, $this->uid);
  794. }
  795. }
  796. protected function addOrderRemind($bid)
  797. {
  798. if (ChapterReminderService::checkIsNoReminder($this->uid, $bid)) {
  799. return true;
  800. } else {
  801. ChapterReminderService::add($this->uid, $bid);
  802. return true;
  803. }
  804. }
  805. /**
  806. * 是否订购提醒
  807. * @param $chapter_id
  808. * @return bool
  809. */
  810. protected function isOrderRemind($bid)
  811. {
  812. $is_no_reminder = ChapterReminderService::checkIsNoReminder($this->uid, $bid) ? 1 : 0;
  813. return $is_no_reminder == 0;
  814. }
  815. /**
  816. * 用户是否关注
  817. * @param $uid
  818. * @return bool
  819. */
  820. protected function getSubscribe()
  821. {
  822. $res = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $this->uid]);
  823. if ($res) {
  824. $this->force_subscribe_time = isset($res->created_at)?$res->created_at:null;
  825. return true;
  826. }
  827. return false;
  828. }
  829. /**
  830. * 获取公众号信息
  831. * @param $distribution_channel_id
  832. */
  833. protected function getOfficialAccount()
  834. {
  835. $distribution_channel_id = $this->distribution_channel_id;
  836. $res = OfficialAccountService::canUseOfficialAccountByChannelId(compact('distribution_channel_id'));
  837. if (isset($res->nickname) && !empty($res->nickname)) {
  838. Cookie::queue('force_subscribe_name', $res->nickname, env('U_COOKIE_EXPIRE'));
  839. }
  840. return $res;
  841. }
  842. /**
  843. * 获取二维码
  844. * @param $param
  845. * @return bool
  846. */
  847. private function getRcodeInfo($param, $bid)
  848. {
  849. //return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQGI8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyZEZ0T0ZoSHljM2kxOTUwQU5zY08AAgRF8-xbAwQAjScA";
  850. $param_need = [
  851. 'gzh_app_id' => $param['appid'],
  852. 'scene_id' => $this->uid,
  853. 'timestamp' => time(),
  854. ];
  855. $param_need['sign'] = $this->getSign($param_need);
  856. $client = new Client(['timeout' => 3.0,]);
  857. try {
  858. Log::info('get qrcode url is:' . 'https://zsyauth.aizhuishu.com/api/get_qrcode_url?' . http_build_query($param_need));
  859. $qrcode_url_res = $client->request('get', 'https://zsyauth.aizhuishu.com/api/get_qrcode_url?' . http_build_query($param_need))->getBody()->getContents();
  860. if ($qrcode_url_res) {
  861. $qrcode_url = json_decode($qrcode_url_res, true);
  862. if ($qrcode_url['code'] == 1) {
  863. //保存强关时的bid
  864. if (isset($qrcode_url['data']) && !empty($qrcode_url['data'])) {
  865. Redis::hset('force_subscribe_from_bid', $param['appid'] . '_' . $this->uid, $bid);
  866. $send_order_id = Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0;
  867. Redis::hset('force_subscribe_from_send_order_id', $param['appid'] . '_' . $this->uid, $send_order_id);
  868. Log::info('uid is' . $this->uid . '--qrcode is:' . $qrcode_url['data']);
  869. return $qrcode_url['data'];
  870. }
  871. }
  872. }
  873. } catch (\Exception $e) {
  874. //\Log::info($e);
  875. //Redis::sadd('force_subscribe_qrcode:error', $param['appid'] . '_' . time());
  876. }
  877. return false;
  878. }
  879. /**
  880. * 获取订购记录
  881. * @param $book_info
  882. * @param $chapter_id
  883. * @return bool
  884. */
  885. private function getOrderRecord($bid, $chapter_id)
  886. {
  887. //包年记录
  888. $uid = $this->uid;
  889. $res = YearOrderService::getRecord($uid);
  890. if ($res) return true;
  891. $res = null;
  892. //单本订购记录
  893. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  894. if ($res) return true;
  895. $res = null;
  896. //章节订购记录
  897. $chapterOrder = new ChapterOrderService();
  898. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  899. return false;
  900. }
  901. /**
  902. * 计算价格
  903. * @param $book_info
  904. * @param $chapter_size
  905. * @return float
  906. */
  907. private function getPrice($book_info, $chapter)
  908. {
  909. if ($book_info->charge_type == 'BOOK') {
  910. $fee = $book_info->price * 100;
  911. } else {
  912. //$fee = ceil($chapter->size/100);
  913. //$channel_fee = BookService::getChapterPrice()
  914. $channel_fee = BookService::getChapterPrice($this->distribution_channel_id);
  915. if ($channel_fee) {
  916. $price_rate = $channel_fee / 100;
  917. } else {
  918. //$price_rate = 0.012;
  919. $price_rate = env('DEFAULT_CHAPTER_PRICE', 0.015);
  920. }
  921. //Log::info($price_rate);
  922. $fee = ceil($chapter->size * $price_rate);
  923. if($this->isRaisePrice()){
  924. $fee = round($chapter->size * 0.0165);
  925. }
  926. }
  927. //首个计费章节书币价格以35书币为基准,低于35提升至35,原价高于35书币,维持原价
  928. if (isset($chapter->sequence) && isset($book_info->vip_seq)) {
  929. if ($chapter->sequence == $book_info->vip_seq) {
  930. if ($fee < 35) {
  931. $fee = 35;
  932. }
  933. }
  934. }
  935. return $fee;
  936. }
  937. //降低付费用户书币价值
  938. private function isRaisePrice(){
  939. if(!$this->uid)
  940. return false;
  941. if(!$this->user)
  942. return false;
  943. if($this->user->balance < 100){
  944. return false;
  945. }
  946. if(!in_array($this->distribution_channel_id,[211,123])){
  947. return false;
  948. }
  949. if(DataAnalysisSelectUserService::getByUidAndType($this->uid,'RAISE')){
  950. return true;
  951. }
  952. return false;
  953. }
  954. /**
  955. * 获取强关章节数
  956. * @param $book
  957. * @return int
  958. */
  959. private function getSubscribeChapterNum($book)
  960. {
  961. //派单强关
  962. if ($this->send_order_id) {
  963. $send_order_info = SendOrderService::getById($this->send_order_id);
  964. if ($send_order_info && $send_order_info->book_id == $book->bid && $send_order_info->subscribe_chapter_seq) {
  965. return $send_order_info->subscribe_chapter_seq;
  966. }
  967. }
  968. $subscribe = BookSubscribleChapterService::getSubcribleChapter($book->bid, $this->distribution_channel_id);
  969. if ($subscribe) {
  970. return (isset($subscribe->subscribe_chapter_id) && $subscribe->subscribe_chapter_id > 0) ? $subscribe->subscribe_chapter_id : $book->force_subscribe_chapter_seq;
  971. }
  972. return $book->force_subscribe_chapter_seq;
  973. }
  974. /**
  975. * 判断访问的域名和数据库指定的域名是否一致,一致返回false ,不一致 true
  976. * @param $domain 当前访问的doain
  977. * @return bool
  978. */
  979. private function isVisitDomainEqualBookDomain($domain)
  980. {
  981. if ($domain == 'zhuishuyun' && stripos($this->book_info->promotion_domain, 'leyuee') !== false) {
  982. return false;
  983. }
  984. return stripos($this->book_info->promotion_domain, $domain) === false;
  985. }
  986. //获取对当前图书对应域名的链接
  987. private function getCorrespondBookUrl()
  988. {
  989. $url = sprintf(
  990. 'https://site%s.%s/reader?bid=%s&cid=%s&send_order_id=%s&fromtype=%s&stats=2',
  991. $this->en_distribution_channel_id,
  992. $this->book_info->promotion_domain,
  993. $this->en_bid,
  994. $this->cid,
  995. $this->send_order_id,
  996. $this->from_type
  997. );
  998. return $url;
  999. }
  1000. /**
  1001. * 用户添加标签
  1002. * @param $book_info
  1003. */
  1004. private function addTag($book_info)
  1005. {
  1006. if (!UserDeepReadTagService::isAddTag($this->uid, $book_info->bid)) {
  1007. try {
  1008. UserDeepReadTagService::addTag([
  1009. 'uid' => $this->uid,
  1010. 'bid' => $book_info->bid,
  1011. 'book_name' => $book_info->book_name,
  1012. 'category_id' => $book_info->category_id,
  1013. 'category_name' => $book_info->category_name,
  1014. 'sex_preference' => $book_info->channel_name ? $book_info->channel_name : '',
  1015. 'distribution_channel_id' => $this->distribution_channel_id ? $this->distribution_channel_id : '0',
  1016. 'send_order_id' => Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0,
  1017. ]);
  1018. } catch (\Exception $e) {
  1019. }
  1020. }
  1021. }
  1022. private function newUserRewardRead()
  1023. {
  1024. $count = ChapterOrderService::getUserRecordCount($this->uid);
  1025. return $count > 2;
  1026. }
  1027. private function isBookOrdered($bid)
  1028. {
  1029. $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $bid);
  1030. if ($chapter_order) return true;
  1031. $res = BookOrderService::getRecordByuidBid($this->uid, $bid);
  1032. if ($res) return true;
  1033. return false;
  1034. }
  1035. private function beforeForceSubAndBeforeVipUvAndPv($book, $book_force_seq, $chapter)
  1036. {
  1037. $send_order_id = Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 14;
  1038. $prev_sub_seq = 0;
  1039. if ($book_force_seq) {
  1040. $prev_sub_seq = $book_force_seq - 1;
  1041. }
  1042. $prev_vip_seq = 0;
  1043. $date = date('Y-m-d');
  1044. if (isset($book->vip_seq) && !empty($book->vip_seq)) {
  1045. $prev_vip_seq = $book->vip_seq - 1;
  1046. }
  1047. if ($chapter->sequence == $prev_vip_seq) {
  1048. $uv_key = 'book:' . $book->bid . ':beforevip:uv:date:' . $date . ':send_order_id:' . $send_order_id;
  1049. $pv_key = 'book:' . $book->bid . ':beforevip:pv:send_order_id:' . $send_order_id;
  1050. Redis::hincrby($pv_key, $date, 1);
  1051. Redis::hincrby($pv_key, 'total', 1);
  1052. Redis::sadd($uv_key, $this->uid);
  1053. Redis::sadd('beforevip' . $date, $book->bid . '-' . $send_order_id);
  1054. }
  1055. if ($chapter->sequence == $prev_sub_seq) {
  1056. $uv_key = 'book:' . $book->bid . ':beforeforcesub:uv:date:' . $date . ':send_order_id:' . $send_order_id;
  1057. $pv_key = 'book:' . $book->bid . ':beforeforcesub::pvsend_order_id:' . $send_order_id;
  1058. Redis::hincrby($pv_key, $date, 1);
  1059. Redis::hincrby($pv_key, 'total', 1);
  1060. Redis::sadd($uv_key, $this->uid);
  1061. Redis::sadd('beforeforcesub' . $date, $book->bid . '-' . $send_order_id);
  1062. }
  1063. }
  1064. private function readRecordOther()
  1065. {
  1066. $chapter = $this->chapter;
  1067. try {
  1068. $data = [];
  1069. $data['uid'] = $this->uid;
  1070. $data['bid'] = $chapter->bid;
  1071. $data['cid'] = $chapter->id;
  1072. $data['is_vip'] = $chapter->is_vip;
  1073. $data['seq'] = $chapter->sequence;
  1074. $data['created_at'] = date('Y-m-d H:i:s');
  1075. $data['updated_at'] = date('Y-m-d H:i:s');
  1076. DB::table('read_records')->insert($data);
  1077. } catch (\Exception $e) {
  1078. }
  1079. }
  1080. /**
  1081. * 下架图书 true:下架不能阅读了, false:可以正常阅读
  1082. * @return bool
  1083. */
  1084. private function isOffShelf()
  1085. {
  1086. $book_info = $this->book_info;
  1087. if ($book_info->is_on_shelf == 0 || $book_info->is_on_shelf == 3) {
  1088. return true;
  1089. }
  1090. //屏蔽一本书1042 2626
  1091. if (in_array($book_info->bid, [1042, 2626]) && $this->distribution_channel_id == 123) {
  1092. return true;
  1093. }
  1094. if ($book_info->is_on_shelf == 4) {
  1095. //版权到期,有阅读记录还可以继续阅读,订购
  1096. /*if (!ReadRecordService::getRecordByUidBid($this->uid, $book_info->bid)) {
  1097. return true;
  1098. }*/
  1099. //版权到期,有包年可以继续阅读
  1100. $year_order = YearOrderService::getRecord($this->uid);
  1101. if($year_order){
  1102. return false;
  1103. }
  1104. //版权到期,有订购记录还可以继续阅读,订购
  1105. $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $book_info->bid);
  1106. if (!$chapter_order) return true;
  1107. }
  1108. return false;
  1109. }
  1110. //章节被删除的情况
  1111. private function chapterNotExists()
  1112. {
  1113. $chapter = $this->chapter;
  1114. $cid = $this->cid;
  1115. if ($chapter) return false;
  1116. //章节不存在的情况
  1117. $no_check = ChapterService::getChapterNameByIdNoCheck($cid);
  1118. $h5_scheme = 'https';
  1119. if (!$no_check) {
  1120. //章节真正不存在的情况
  1121. $url = $h5_scheme . '://' . _domain() . '/catalog?id=' . $this->en_bid;
  1122. return $url;
  1123. }
  1124. //章节被删除的情况
  1125. if ($no_check->next_cid) {
  1126. $url = $h5_scheme . '://' . _domain() . '/reader?bid=' . $this->en_bid . '&cid=' . $no_check->next_cid;
  1127. return $url;
  1128. }
  1129. //章节被删除的是最后一章的情况
  1130. $url = $h5_scheme . '://' . _domain() . '/catalog?id=' . $this->en_bid . '&sequence=' . $no_check->sequence;
  1131. return $url;
  1132. }
  1133. /**
  1134. * 深度阅读
  1135. * @param $chapter
  1136. */
  1137. private function deepReadRecord()
  1138. {
  1139. if (!env('ON_BOOK_UV_PV', false)) {
  1140. return;
  1141. }
  1142. $chapter = $this->chapter;
  1143. $key = 'book:' . $chapter->bid . ':deepread:uv';
  1144. $chapter_key = 'book:' . $chapter->bid . ':chapterseq:' . $chapter->sequence;
  1145. if (Redis::scard($key) > 50000) {
  1146. if (Redis::Sismember($key, $this->uid)) {
  1147. Redis::sadd($chapter_key, $this->uid);
  1148. }
  1149. } else {
  1150. Redis::sadd($key, $this->uid);
  1151. Redis::sadd($chapter_key, $this->uid);
  1152. }
  1153. }
  1154. /**
  1155. * 强关类型
  1156. * @param $appid
  1157. * @return array
  1158. */
  1159. private function getSubscribeType($appid, $bid)
  1160. {
  1161. $send_order_id = Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0;
  1162. $sub_setting = ChannelSubscribeSettingService::getSubscribeTypeByAppId($appid);
  1163. if (!$sub_setting) return ['type' => [1], 'url' => ''];
  1164. $types = explode(',', $sub_setting->force_subscribe_type);
  1165. $url = '';
  1166. if (in_array(3, $types) && !empty($sub_setting->resource_url)) {
  1167. $h5_scheme = env('H5_SCHEME', 'https');
  1168. $url = $h5_scheme . '://' . _domain() . '/subscribeauth/?uid=' . $this->uid . '&distribution_channel_id=' . $this->distribution_channel_id . '&appid=' . $appid . '&bid=' . $bid . '&send_order_id=' . $send_order_id;
  1169. }
  1170. return ['type' => $types, 'url' => $url];
  1171. }
  1172. private function showChapterPrice($bid, $charge_type)
  1173. {
  1174. if ($charge_type == 'BOOK') {
  1175. return false;
  1176. }
  1177. $show_chapter_price_bid = env('SHOW_CHAPTER_PRICE_BID', 'all');
  1178. if ($show_chapter_price_bid != 'all') {
  1179. $show_chapter_price_bid_array = explode(',', $show_chapter_price_bid);
  1180. if (!in_array($bid, $show_chapter_price_bid_array)) {
  1181. return false;
  1182. }
  1183. }
  1184. $show_chapter_price_channel = env('SHOW_CHAPTER_PRICE_CHANNEL', '');
  1185. if (!$show_chapter_price_channel) {
  1186. return false;
  1187. }
  1188. if ($show_chapter_price_channel == 'all') {
  1189. return true;
  1190. }
  1191. $show_chapter_price_channel_array = explode(',', $show_chapter_price_channel);
  1192. if (in_array($this->distribution_channel_id, $show_chapter_price_channel_array)) {
  1193. return true;
  1194. }
  1195. return false;
  1196. }
  1197. /**
  1198. * 阅读统计
  1199. */
  1200. private function chapterStats()
  1201. {
  1202. $chapter = $this->chapter;
  1203. $bid = $this->book_info->bid;
  1204. //派单精确统计
  1205. if ($chapter->sequence <= 30) {
  1206. //$this->sendOrderChapterUv($bid, $chapter->sequence);
  1207. }
  1208. //智能推送 测书统计章节uv
  1209. if ($chapter->sequence <= 60) {
  1210. //测书数据 由30改到60 2018-11-26 18:00
  1211. $this->smartPushTestBookChapterUv($bid, $chapter->sequence);
  1212. }
  1213. //智能推送 测书统计第二章 uv
  1214. if ($chapter->sequence == 2) {
  1215. $this->smartPushTestBookSecondChapterUv($bid);
  1216. }
  1217. // 统计前5张uv 会放在book_five_chapter_uv表中,脚本在 \App\Console\Commands\BookTest::data1里面
  1218. // 作用早忘记了
  1219. $field = date('Y-m-d');
  1220. /*
  1221. if ($chapter->sequence <= 5 && $this->send_order_id) {
  1222. try {
  1223. Redis::sadd(sprintf('book_before_five_uv_%s_%s_%s', $field, $bid, $chapter->sequence), $this->uid);
  1224. Redis::sadd('book_before_five_uvs', sprintf('%s_%s_%s', $field, $bid, $chapter->sequence));
  1225. } catch (\Exception $e) {
  1226. }
  1227. }*/
  1228. //统计点击率
  1229. $key = 'book_click_num_bid_' . $bid;
  1230. try {
  1231. Redis::HINCRBY($key, $field, 1);
  1232. } catch (\Exception $e) {
  1233. }
  1234. //阅读页面pvuv
  1235. $this->recordReaderUvAndPv();
  1236. }
  1237. private function smartPushTestBook($bid, $balance)
  1238. {
  1239. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1240. if ($smart_bid && in_array($bid, $smart_bid)) {
  1241. $field = sprintf('%s_amount', $bid);
  1242. try {
  1243. Redis::HINCRBY('smart_push_test_book', $field, $balance);
  1244. Redis::sadd('smart_push_test_book_user_count' . $bid, $this->uid);
  1245. } catch (\Exception $e) {
  1246. }
  1247. }
  1248. }
  1249. private function smartPushTestBookSecondChapterUv($bid)
  1250. {
  1251. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1252. if ($smart_bid && in_array($bid, $smart_bid)) {
  1253. try {
  1254. Redis::sadd('smart_push_test_book_second_uv' . $bid, $this->uid);
  1255. } catch (\Exception $e) {
  1256. }
  1257. }
  1258. }
  1259. private function smartPushTestBookChapterUv($bid, $sequence)
  1260. {
  1261. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1262. if ($smart_bid && in_array($bid, $smart_bid)) {
  1263. try {
  1264. $key = 'smartPushTestBookChapterUv:bid:%s:seq:%s';
  1265. Redis::sadd(sprintf($key, $bid, $sequence), $this->uid);
  1266. } catch (\Exception $e) {
  1267. }
  1268. }
  1269. }
  1270. /**
  1271. * 测书 智能推送统计
  1272. * @param $bid
  1273. */
  1274. private function smartPushTestBookPayPageUv($bid)
  1275. {
  1276. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1277. if ($smart_bid && in_array($bid, $smart_bid)) {
  1278. try {
  1279. $key = 'smartPushTestBookPayPageUv:bid:%s';
  1280. Redis::sadd(sprintf($key, $bid), $this->uid);
  1281. } catch (\Exception $e) {
  1282. }
  1283. }
  1284. }
  1285. /**
  1286. * 派单精确统计--章节
  1287. * @param $bid
  1288. * @param $sequence
  1289. */
  1290. private function sendOrderChapterUv($bid, $sequence)
  1291. {
  1292. if ($this->send_order_id) {
  1293. $send_order_bid = Redis::hget('sendOrderIdToBid', $this->send_order_id);
  1294. if (!$send_order_bid) {
  1295. $send_order_info = SendOrderService::getById($this->send_order_id);
  1296. if ($send_order_info) {
  1297. $send_order_bid = $send_order_info->book_id;
  1298. Redis::hset('sendOrderIdToBid', $this->send_order_id, $send_order_bid . '-' . time());
  1299. }
  1300. } else {
  1301. $send_order_bid_arr = explode('-', $send_order_bid);
  1302. $send_order_bid = $send_order_bid_arr[0];
  1303. }
  1304. if ($send_order_bid && $send_order_bid == $bid) {
  1305. try {
  1306. $key = 'accurateSendOrderChapterUv:bid:%s:seq:%s:sendorderid:' . $this->send_order_id;
  1307. Redis::sadd(sprintf($key, $bid, $sequence), $this->uid);
  1308. } catch (\Exception $e) {
  1309. }
  1310. }
  1311. }
  1312. }
  1313. /**
  1314. * 派单精确统计--强关页面uv
  1315. * @param $bid
  1316. */
  1317. private function sendOrderSubscribePageUv($bid)
  1318. {
  1319. if ($this->send_order_id) {
  1320. $send_order_bid = Redis::hget('sendOrderIdToBid', $this->send_order_id);
  1321. if (!$send_order_bid) {
  1322. $send_order_info = SendOrderService::getById($this->send_order_id);
  1323. if ($send_order_info) {
  1324. $send_order_bid = $send_order_info->book_id;
  1325. Redis::hset('sendOrderIdToBid', $this->send_order_id, $send_order_bid . '-' . time());
  1326. }
  1327. } else {
  1328. $send_order_bid_arr = explode('-', $send_order_bid);
  1329. $send_order_bid = $send_order_bid_arr[0];
  1330. }
  1331. if ($send_order_bid && $send_order_bid == $bid) {
  1332. try {
  1333. $key = 'accurateSendOrderSubscribePageUv:bid:%s:sendorderid:' . $this->send_order_id;
  1334. Redis::sadd(sprintf($key, $bid), $this->uid);
  1335. } catch (\Exception $e) {
  1336. }
  1337. }
  1338. }
  1339. }
  1340. /**
  1341. * 派单精确统计--支付页面uv
  1342. * @param $bid
  1343. */
  1344. private function sendOrderPayPageUv($bid)
  1345. {
  1346. if ($this->send_order_id) {
  1347. $send_order_bid = Redis::hget('sendOrderIdToBid', $this->send_order_id);
  1348. if (!$send_order_bid) {
  1349. $send_order_info = SendOrderService::getById($this->send_order_id);
  1350. if ($send_order_info) {
  1351. $send_order_bid = $send_order_info->book_id;
  1352. Redis::hset('sendOrderIdToBid', $this->send_order_id, $send_order_bid . '-' . time());
  1353. }
  1354. } else {
  1355. $send_order_bid_arr = explode('-', $send_order_bid);
  1356. $send_order_bid = $send_order_bid_arr[0];
  1357. }
  1358. if ($send_order_bid && $send_order_bid == $bid) {
  1359. try {
  1360. $key = 'accurateSendOrderPayPageUv:bid:%s:send_order_id:' . $this->send_order_id;
  1361. Redis::sadd(sprintf($key, $bid), $this->uid);
  1362. } catch (\Exception $e) {
  1363. }
  1364. }
  1365. }
  1366. }
  1367. private function recordReaderUvAndPv()
  1368. {
  1369. WapVisitStatService::recordReaderUvAndPv($this->uid, $this->distribution_channel_id);
  1370. }
  1371. //用户是否在广告样本中
  1372. private function isUserInAdSample(){
  1373. $res = DataAnalysisSelectUserService::getByUidAndType($this->uid,'AD');
  1374. if($res){
  1375. return true;
  1376. }else{
  1377. return false;
  1378. }
  1379. }
  1380. //阅读页面底部是否显示广告
  1381. private function isShowAd(){
  1382. //按章付费才显示
  1383. if($this->book_info->charge_type != 'CHAPTER'){
  1384. return ;
  1385. }
  1386. //用户是否在广告样本中
  1387. if(!$this->isUserInAdSample()) return ;
  1388. $cid = 0;
  1389. if($this->chapter && $this->chapter->next_cid){
  1390. $cid = $this->chapter->next_cid;
  1391. }
  1392. if(!$cid){
  1393. return ;
  1394. }
  1395. //如果下一张已经付费,广告不显示
  1396. if($this->getOrderRecord($this->book_info->bid, $cid)){
  1397. return ;
  1398. }
  1399. $next_chapter = ChapterService::getChapterNameById($cid, $this->book_info->bid);
  1400. //下一章事vip的话 才显示广告
  1401. if(!$next_chapter || !$next_chapter->is_vip){
  1402. return ;
  1403. }
  1404. $price = $this->getPrice($this->book_info,$next_chapter);
  1405. if($price > $this->user->balance){
  1406. $this->chapter->is_show_ad = 1;
  1407. AdVisitStatService::create($this->uid,$this->book_info->bid,$this->cid,'EXPOSURE');
  1408. }
  1409. }
  1410. public function getWechatJsConfig(Request $request)
  1411. {
  1412. //try {
  1413. // $chapter->is_show_share_button = 1;
  1414. $bid = $request->input('bid', '');
  1415. $cid = $request->input('cid', '');
  1416. $url = $request->input('url', '');
  1417. $url = urldecode($url);
  1418. \Log::info('url:' . ($this->uid) . $url);
  1419. if (empty($bid) || empty($cid) || empty($url)) {
  1420. return response()->error('PARAM_EMPTY');
  1421. }
  1422. $share_url = 'https://'._domain().'/detail?fromtype=readershare&id='.$bid.'&fromflag='.($this->uid).'&fromcid='.$cid;
  1423. //$js_config = ChapterShareWechatConfigService::getConfig($url);
  1424. $app_hash = array(
  1425. 4025=>['appid'=>'wx80b618c9dcc940f7','appsecret'=>'255a2de8db51e4084b5e9087d26d4ecb'],
  1426. 4147=>['appid'=>'wx2a99699cd9572812','appsecret'=>'0e0d57f94eeba50abb3af6883df05a5c'],
  1427. 691=>['appid'=>'wx5cefcd251dc40693','appsecret'=>'755f64c85173dad21f20870e38f59d8a'],
  1428. 123=>['appid'=>'wx983808610aa9dcc5','appsecret'=>'91002999590b79f3c7be35fd73c04049'],
  1429. 211=>['appid'=>'wx39338e374cc2055b','appsecret'=>'1a4c2039be271b3811182be5bb53eb34'],
  1430. );
  1431. $appId = $app_hash[$this->distribution_channel_id]['appid'];
  1432. $appSecret = $app_hash[$this->distribution_channel_id]['appsecret'];
  1433. $js_config = (new ChapterShareWechatConfigService($appId,$appSecret))->getSignPackage($url);
  1434. /*$appid = DB::table('official_accounts')->where('distribution_channel_id',$this->distribution_channel_id)->first();//(['distribution_channel_id'=>$this->distribution_channel_id]);
  1435. $appid = $appid->appid;
  1436. //\Log::info('get_appid:'.($appid));
  1437. $js_config = (new ChapterShareWechatConfigService($appid))->getSignPackage($url);
  1438. //$chapter->js_config = $js_config;
  1439. //$chapter->share_url = $share_url;
  1440. $book = BookService::getBookById(Hashids::decode($bid)[0]);
  1441. $first_cid = $book->first_cid;
  1442. $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;
  1443. /*if (in_array($this->uid, explode(',', env('TEST_SHARE_UIDS')))) {
  1444. $share_url = 'https://site5017zqeyzwgyx9n8.zhuishuyun.com/reader?fromtype=readershare&bid=' . $bid . '&fromflag=' . ($this->uid) . '&fromcid=' . $cid;
  1445. }*/
  1446. $book = BookService::getBookById(Hashids::decode($bid)[0]);
  1447. $first_cid = $book->first_cid;
  1448. $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;
  1449. if (in_array($this->distribution_channel_id, [2, 14])) {
  1450. $js_config = ChapterShareWechatConfigService::getConfig($url);
  1451. $share_url = 'https://site' . ($this->distribution_channel_id) . '.' . env('SHARE_DOMAIN') . '/reader?fromtype=readershare&bid=' . $bid . '&cid=' . ($first_cid) . '&fromflag=' . ($this->uid) . '&fromcid=' . $cid;
  1452. }
  1453. $js_config['share_url'] = $share_url;
  1454. /*} catch (\Exception $e) {
  1455. \Log::error($e->getMessage());
  1456. }*/
  1457. //\Log::info($js_config);
  1458. return response()->success($js_config);
  1459. }
  1460. }