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. $this->user->balance -= $fee;
  523. return true;
  524. }
  525. }
  526. return false;
  527. }
  528. /**
  529. * 获取章节内容
  530. * @param $bid
  531. * @param $cid
  532. * @return bool|mixed
  533. */
  534. private function getChapter()
  535. {
  536. //初始化 默认字段
  537. $this->initChapterDefaultField();
  538. //章节内容
  539. $chapter_content = ChapterService::getChapter($this->book_info->bid, $this->cid);
  540. $this->chapter->content = str_replace($chapter_content->name, '', $chapter_content->content);
  541. //统计
  542. $this->chapterStats();
  543. //123特殊强关
  544. $this->specialSubscribeFlag();
  545. //签到奖励
  546. $this->sign();
  547. //#短篇推送长篇需求#
  548. //$this->shortPushLong();
  549. //#短推长、短推短对比
  550. $this->shortPushshort();
  551. //阅读器分享
  552. $this->share();
  553. //广告
  554. $this->isShowAd();
  555. }
  556. /**
  557. * 章节阅读接口需要配置返回的字段
  558. */
  559. private function initChapterDefaultField()
  560. {
  561. if (!$this->chapter) {
  562. return;
  563. }
  564. //内容
  565. $this->chapter->content = '';
  566. //签到默认字段
  567. $this->chapter->sign_days = 1;
  568. $this->chapter->sign_status = 0;
  569. $this->chapter->sign_reard = 0;
  570. //#短篇推送长篇需求#
  571. $this->chapter->next_jump = 0;
  572. $this->chapter->next_jump_url = '';
  573. $this->chapter->show_push_title = 0;
  574. $this->chapter->push_title_url = '';
  575. $this->chapter->push_title = [];
  576. //分享
  577. $this->chapter->is_show_share_button = 0;
  578. //阅读底部强关图片 只有123有
  579. $this->chapter->is_had_subscribe = 0;
  580. //广告
  581. $this->chapter->is_show_ad = 0;
  582. }
  583. private function sign()
  584. {
  585. //渠道123 未关注的签到放到强关前一章
  586. if ($this->distribution_channel_id == 123 && !$this->is_had_subscribe) {
  587. if ($this->book_info->force_subscribe_chapter_seq - 1 == $this->chapter->sequence) {
  588. $sign_status = $this->userSign($this->uid);
  589. //$sign_status = 0;
  590. $this->chapter->sign_status = $sign_status ? 1 : 0;
  591. $this->chapter->sign_reard = $sign_status;
  592. $this->chapter->sign_days = ReadRecordService::getSignCount($this->uid);
  593. }
  594. } else {
  595. $sign_status = $this->userSign($this->uid);
  596. //$sign_status =0;
  597. $this->chapter->sign_status = $sign_status ? 1 : 0;
  598. $this->chapter->sign_reard = $sign_status;
  599. $this->chapter->sign_days = ReadRecordService::getSignCount($this->uid);
  600. }
  601. }
  602. private function forceSubscribe()
  603. {
  604. //获取公众号信息
  605. $officialAccount = $this->getOfficialAccount();
  606. if (!$officialAccount || empty($officialAccount->appid)) {
  607. return false;
  608. }
  609. //如果是链接强关,则跳转
  610. $sub_type = $this->getSubscribeType($officialAccount->appid, $this->book_info->bid);
  611. if (in_array(3, $sub_type['type']) && $sub_type['url']) {
  612. return ['error_type' => 'WAP_LINK_SUBSCRIBE', 'data' => ['url' => $sub_type['url']]];
  613. }
  614. //普通强关
  615. if (in_array(1, $sub_type['type'])) {
  616. $src = $this->getRcodeInfo(['appid' => $officialAccount->appid], $this->book_info->bid);
  617. if (empty($src)) return false;
  618. if ($officialAccount->head_img) {
  619. $url = '/subscribe/qrcode?img=' . urlencode($src) . '&title=' . $officialAccount->nickname . '&head_img=' . urlencode($officialAccount->head_img) . '&uid=' . $this->uid;
  620. } else {
  621. $url = '/subscribe/qrcode?img=' . urlencode($src) . '&title=' . $officialAccount->nickname . '&uid=' . $this->uid;
  622. }
  623. return ['error_type' => 'WAP_NOT_SUBSCRIBE', 'data' => ['src' => $url, 'head_img' => $officialAccount->head_img]];
  624. }
  625. return false;
  626. }
  627. //强关页面的统计
  628. private function subscribeStats()
  629. {
  630. if ($this->send_order_id) {
  631. //$this->sendOrderSubscribePageUv($this->book_info->bid);
  632. //强关页面的uv
  633. /*try {
  634. Redis::sadd('subscribe_page_uv' . $this->send_order_id, $this->uid);
  635. Redis::sadd('subscribe_page_uv_send_order_ids', $this->send_order_id);
  636. } catch (\Exception $e) {
  637. }*/
  638. }
  639. }
  640. /**
  641. * 短推长
  642. */
  643. private function shortPushLong()
  644. {
  645. if ($this->book_info->charge_type = 'CHAPTER') return;
  646. $is_has_book_order = BookOrderService::isHasBookOrder($this->uid);
  647. if ($this->chapter->is_vip && $is_has_book_order) {
  648. $this->chapter->show_push_title = 1;
  649. $this->chapter->push_title = DataAnalysisBookConfig::getUserReaderPagePushBook($this->uid, $this->distribution_channel_id);
  650. }
  651. if (!$this->chapter->next_cid && $is_has_book_order) {
  652. $link = DataAnalysisBookConfig::getUserReaderPagePushBook($this->uid, $this->distribution_channel_id, 1);
  653. if ($link && isset($link['link']) && !empty($link['link'])) {
  654. $this->chapter->next_jump = 1;
  655. $this->chapter->next_jump_url = $link['link'];
  656. }
  657. }
  658. }
  659. /**
  660. * 短推长、短推短对比需求
  661. * 目标用户:全站短篇进入新注册且订阅短篇的付费用户
  662. * 方式:根据uid奇偶取用,奇:测试用户1 偶:测试用户2
  663. * 测试用户1:底部链接+完本推荐 均使用短篇小说
  664. * 测试用户2:底部链接+完本推荐 均使用长篇小说
  665. * 目标用户:全站短篇进入新注册且订阅短篇的付费用户
  666. */
  667. private function shortPushshort()
  668. {
  669. //新用户 注册时间大于某个值
  670. if(!$this->user){
  671. return ;
  672. }
  673. if(!env('SHORT_PUSH_SHORT_START_TIME') || strtotime($this->user->created_at) < env('SHORT_PUSH_SHORT_START_TIME')){
  674. return ;
  675. }
  676. //没有派单
  677. if(!$this->user->send_order_id) return ;
  678. //只在vip章节底部显示
  679. if(!$this->chapter->is_vip){
  680. return ;
  681. }
  682. //强关
  683. if(!$this->is_had_subscribe){
  684. return ;
  685. }
  686. //短篇进入
  687. $send_order_info = SendOrderService::getById($this->user->send_order_id);
  688. if(!$send_order_info || $send_order_info->charge_type != 'BOOK'){
  689. return ;
  690. }
  691. //订阅短篇的付费用户
  692. $is_has_book_order = BookOrderService::isHasBookOrder($this->uid);
  693. if(!$is_has_book_order) return ;
  694. //是否付费用户
  695. if(!OrderService::isPaidUser($this->uid)) return ;
  696. $type = $this->uid %2 == 1? 'BOOK':'CHAPTER';
  697. $result = DataAnalysisSelectUserService::create($this->uid, $this->distribution_channel_id, $type, 500, $attach = 'short2s2l');
  698. if(!$result) return ;
  699. //底部链接
  700. $this->chapter->show_push_title = 1;
  701. $this->chapter->push_title = DataAnalysisBookConfig::getUserReaderPagePushBookV2($this->uid, $this->distribution_channel_id,$type);
  702. //完本推荐
  703. if (!$this->chapter->next_cid ) {
  704. $link = DataAnalysisBookConfig::getUserReaderPagePushBookV2($this->uid, $this->distribution_channel_id,$type, 1);
  705. if($link && $link['link']){
  706. $this->chapter->next_jump = 1;
  707. $this->chapter->next_jump_url = $link['link'];
  708. }
  709. }
  710. }
  711. private function share()
  712. {
  713. if ($this->is_had_subscribe && in_array($this->distribution_channel_id, [2, 14, 211,4025,4147,691,123]) && $this->user) {//,4147,691,123,4025
  714. $this->chapter->is_show_share_button = 1;
  715. }
  716. }
  717. /**
  718. * 123 渠道阅读底部强关图片
  719. */
  720. private function specialSubscribeFlag()
  721. {
  722. if ($this->distribution_channel_id == 123 && !$this->is_had_subscribe) {
  723. $this->chapter->is_had_subscribe = 1;
  724. }
  725. }
  726. //订阅统计
  727. private function chapterOrderStats($fee)
  728. {
  729. $day = date('Y--m-d');
  730. try {
  731. Redis::hincrby('wap:chapterandbookorder:bid:' . $this->book_info->bid, $day, $fee);
  732. } catch (\Exception $e) {
  733. }
  734. $this->smartPushTestBook($this->book_info->bid, $fee);
  735. $specialChannelIdStats = env('SPECIAL_CHANNEL_STATS', 211);
  736. if (in_array($this->distribution_channel_id, explode(',', $specialChannelIdStats))) {
  737. WapVisitStatService::specialChannelIdStatsSave($this->uid, $this->book_info->bid, $fee);
  738. }
  739. }
  740. /**
  741. * 签到
  742. * @param $uid
  743. * @return int
  744. */
  745. protected function userSign($uid)
  746. {
  747. if (UserSignService::isSign($uid)) {
  748. //签过到了
  749. return 0;
  750. }
  751. if ($res = UserSignService::signToday($uid)) {
  752. //$sign_key = 'leyuee:wap:usersigni';
  753. //Redis::hset($sign_key, $uid, $res);
  754. return $res;
  755. }
  756. return 0;
  757. }
  758. /**
  759. * 添加订购记录
  760. * @param $book_info
  761. * @param $chapter_id
  762. * @param $fee
  763. * @return bool
  764. */
  765. protected function bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)
  766. {
  767. if ($book_info['charge_type'] == 'BOOK') {
  768. $data = [
  769. 'uid' => $this->uid,
  770. 'fee' => $fee,
  771. 'u' => Cookie::get('send_order_id'),
  772. 'distribution_channel_id' => $this->distribution_channel_id,
  773. 'bid' => $book_info->bid,
  774. 'book_name' => $book_info->book_name,
  775. 'send_order_id' => Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0,
  776. ];
  777. return BookOrderService::addOrderRecodeAndDecrUserBalance($data, $this->uid);
  778. } else {
  779. $data = [
  780. 'uid' => $this->uid,
  781. 'fee' => $fee,
  782. 'cid' => $chapter_id,
  783. 'bid' => $book_info->bid,
  784. 'distribution_channel_id' => $this->distribution_channel_id,
  785. 'book_name' => $book_info->book_name,
  786. 'chapter_name' => $chapter_name,
  787. 'send_order_id' => Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0,
  788. 'is_remind' => $is_remind
  789. ];
  790. //print_r($data);
  791. if ($is_remind) {
  792. $this->addOrderRemind($book_info->bid);
  793. }
  794. return ChapterOrderService::addOrderAndDecrUserBalance($data, $this->uid);
  795. }
  796. }
  797. protected function addOrderRemind($bid)
  798. {
  799. if (ChapterReminderService::checkIsNoReminder($this->uid, $bid)) {
  800. return true;
  801. } else {
  802. ChapterReminderService::add($this->uid, $bid);
  803. return true;
  804. }
  805. }
  806. /**
  807. * 是否订购提醒
  808. * @param $chapter_id
  809. * @return bool
  810. */
  811. protected function isOrderRemind($bid)
  812. {
  813. $is_no_reminder = ChapterReminderService::checkIsNoReminder($this->uid, $bid) ? 1 : 0;
  814. return $is_no_reminder == 0;
  815. }
  816. /**
  817. * 用户是否关注
  818. * @param $uid
  819. * @return bool
  820. */
  821. protected function getSubscribe()
  822. {
  823. $res = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $this->uid]);
  824. if ($res) {
  825. $this->force_subscribe_time = isset($res->created_at)?$res->created_at:null;
  826. return true;
  827. }
  828. return false;
  829. }
  830. /**
  831. * 获取公众号信息
  832. * @param $distribution_channel_id
  833. */
  834. protected function getOfficialAccount()
  835. {
  836. $distribution_channel_id = $this->distribution_channel_id;
  837. $res = OfficialAccountService::canUseOfficialAccountByChannelId(compact('distribution_channel_id'));
  838. if (isset($res->nickname) && !empty($res->nickname)) {
  839. Cookie::queue('force_subscribe_name', $res->nickname, env('U_COOKIE_EXPIRE'));
  840. }
  841. return $res;
  842. }
  843. /**
  844. * 获取二维码
  845. * @param $param
  846. * @return bool
  847. */
  848. private function getRcodeInfo($param, $bid)
  849. {
  850. //return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQGI8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyZEZ0T0ZoSHljM2kxOTUwQU5zY08AAgRF8-xbAwQAjScA";
  851. $param_need = [
  852. 'gzh_app_id' => $param['appid'],
  853. 'scene_id' => $this->uid,
  854. 'timestamp' => time(),
  855. ];
  856. $param_need['sign'] = $this->getSign($param_need);
  857. $client = new Client(['timeout' => 3.0,]);
  858. try {
  859. Log::info('get qrcode url is:' . 'https://zsyauth.aizhuishu.com/api/get_qrcode_url?' . http_build_query($param_need));
  860. $qrcode_url_res = $client->request('get', 'https://zsyauth.aizhuishu.com/api/get_qrcode_url?' . http_build_query($param_need))->getBody()->getContents();
  861. if ($qrcode_url_res) {
  862. $qrcode_url = json_decode($qrcode_url_res, true);
  863. if ($qrcode_url['code'] == 1) {
  864. //保存强关时的bid
  865. if (isset($qrcode_url['data']) && !empty($qrcode_url['data'])) {
  866. Redis::hset('force_subscribe_from_bid', $param['appid'] . '_' . $this->uid, $bid);
  867. $send_order_id = Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0;
  868. Redis::hset('force_subscribe_from_send_order_id', $param['appid'] . '_' . $this->uid, $send_order_id);
  869. Log::info('uid is' . $this->uid . '--qrcode is:' . $qrcode_url['data']);
  870. return $qrcode_url['data'];
  871. }
  872. }
  873. }
  874. } catch (\Exception $e) {
  875. //\Log::info($e);
  876. //Redis::sadd('force_subscribe_qrcode:error', $param['appid'] . '_' . time());
  877. }
  878. return false;
  879. }
  880. /**
  881. * 获取订购记录
  882. * @param $book_info
  883. * @param $chapter_id
  884. * @return bool
  885. */
  886. private function getOrderRecord($bid, $chapter_id)
  887. {
  888. //包年记录
  889. $uid = $this->uid;
  890. $res = YearOrderService::getRecord($uid);
  891. if ($res) return true;
  892. $res = null;
  893. //单本订购记录
  894. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  895. if ($res) return true;
  896. $res = null;
  897. //章节订购记录
  898. $chapterOrder = new ChapterOrderService();
  899. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  900. return false;
  901. }
  902. /**
  903. * 计算价格
  904. * @param $book_info
  905. * @param $chapter_size
  906. * @return float
  907. */
  908. private function getPrice($book_info, $chapter)
  909. {
  910. if ($book_info->charge_type == 'BOOK') {
  911. $fee = $book_info->price * 100;
  912. } else {
  913. //$fee = ceil($chapter->size/100);
  914. //$channel_fee = BookService::getChapterPrice()
  915. $channel_fee = BookService::getChapterPrice($this->distribution_channel_id);
  916. if ($channel_fee) {
  917. $price_rate = $channel_fee / 100;
  918. } else {
  919. //$price_rate = 0.012;
  920. $price_rate = env('DEFAULT_CHAPTER_PRICE', 0.015);
  921. }
  922. //Log::info($price_rate);
  923. $fee = ceil($chapter->size * $price_rate);
  924. if($this->isRaisePrice()){
  925. $fee = round($chapter->size * 0.0165);
  926. }
  927. }
  928. //首个计费章节书币价格以35书币为基准,低于35提升至35,原价高于35书币,维持原价
  929. if (isset($chapter->sequence) && isset($book_info->vip_seq)) {
  930. if ($chapter->sequence == $book_info->vip_seq) {
  931. if ($fee < 35) {
  932. $fee = 35;
  933. }
  934. }
  935. }
  936. return $fee;
  937. }
  938. //降低付费用户书币价值
  939. private function isRaisePrice(){
  940. if(!$this->uid)
  941. return false;
  942. if(!$this->user)
  943. return false;
  944. if($this->user->balance < 100){
  945. return false;
  946. }
  947. if(!in_array($this->distribution_channel_id,[211,123])){
  948. return false;
  949. }
  950. if(DataAnalysisSelectUserService::getByUidAndType($this->uid,'RAISE')){
  951. return true;
  952. }
  953. return false;
  954. }
  955. /**
  956. * 获取强关章节数
  957. * @param $book
  958. * @return int
  959. */
  960. private function getSubscribeChapterNum($book)
  961. {
  962. //派单强关
  963. if ($this->send_order_id) {
  964. $send_order_info = SendOrderService::getById($this->send_order_id);
  965. if ($send_order_info && $send_order_info->book_id == $book->bid && $send_order_info->subscribe_chapter_seq) {
  966. return $send_order_info->subscribe_chapter_seq;
  967. }
  968. }
  969. $subscribe = BookSubscribleChapterService::getSubcribleChapter($book->bid, $this->distribution_channel_id);
  970. if ($subscribe) {
  971. return (isset($subscribe->subscribe_chapter_id) && $subscribe->subscribe_chapter_id > 0) ? $subscribe->subscribe_chapter_id : $book->force_subscribe_chapter_seq;
  972. }
  973. return $book->force_subscribe_chapter_seq;
  974. }
  975. /**
  976. * 判断访问的域名和数据库指定的域名是否一致,一致返回false ,不一致 true
  977. * @param $domain 当前访问的doain
  978. * @return bool
  979. */
  980. private function isVisitDomainEqualBookDomain($domain)
  981. {
  982. if ($domain == 'zhuishuyun' && stripos($this->book_info->promotion_domain, 'leyuee') !== false) {
  983. return false;
  984. }
  985. return stripos($this->book_info->promotion_domain, $domain) === false;
  986. }
  987. //获取对当前图书对应域名的链接
  988. private function getCorrespondBookUrl()
  989. {
  990. $url = sprintf(
  991. 'https://site%s.%s/reader?bid=%s&cid=%s&send_order_id=%s&fromtype=%s&stats=2',
  992. $this->en_distribution_channel_id,
  993. $this->book_info->promotion_domain,
  994. $this->en_bid,
  995. $this->cid,
  996. $this->send_order_id,
  997. $this->from_type
  998. );
  999. return $url;
  1000. }
  1001. /**
  1002. * 用户添加标签
  1003. * @param $book_info
  1004. */
  1005. private function addTag($book_info)
  1006. {
  1007. if (!UserDeepReadTagService::isAddTag($this->uid, $book_info->bid)) {
  1008. try {
  1009. UserDeepReadTagService::addTag([
  1010. 'uid' => $this->uid,
  1011. 'bid' => $book_info->bid,
  1012. 'book_name' => $book_info->book_name,
  1013. 'category_id' => $book_info->category_id,
  1014. 'category_name' => $book_info->category_name,
  1015. 'sex_preference' => $book_info->channel_name ? $book_info->channel_name : '',
  1016. 'distribution_channel_id' => $this->distribution_channel_id ? $this->distribution_channel_id : '0',
  1017. 'send_order_id' => Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0,
  1018. ]);
  1019. } catch (\Exception $e) {
  1020. }
  1021. }
  1022. }
  1023. private function newUserRewardRead()
  1024. {
  1025. $count = ChapterOrderService::getUserRecordCount($this->uid);
  1026. return $count > 2;
  1027. }
  1028. private function isBookOrdered($bid)
  1029. {
  1030. $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $bid);
  1031. if ($chapter_order) return true;
  1032. $res = BookOrderService::getRecordByuidBid($this->uid, $bid);
  1033. if ($res) return true;
  1034. return false;
  1035. }
  1036. private function beforeForceSubAndBeforeVipUvAndPv($book, $book_force_seq, $chapter)
  1037. {
  1038. $send_order_id = Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 14;
  1039. $prev_sub_seq = 0;
  1040. if ($book_force_seq) {
  1041. $prev_sub_seq = $book_force_seq - 1;
  1042. }
  1043. $prev_vip_seq = 0;
  1044. $date = date('Y-m-d');
  1045. if (isset($book->vip_seq) && !empty($book->vip_seq)) {
  1046. $prev_vip_seq = $book->vip_seq - 1;
  1047. }
  1048. if ($chapter->sequence == $prev_vip_seq) {
  1049. $uv_key = 'book:' . $book->bid . ':beforevip:uv:date:' . $date . ':send_order_id:' . $send_order_id;
  1050. $pv_key = 'book:' . $book->bid . ':beforevip:pv:send_order_id:' . $send_order_id;
  1051. Redis::hincrby($pv_key, $date, 1);
  1052. Redis::hincrby($pv_key, 'total', 1);
  1053. Redis::sadd($uv_key, $this->uid);
  1054. Redis::sadd('beforevip' . $date, $book->bid . '-' . $send_order_id);
  1055. }
  1056. if ($chapter->sequence == $prev_sub_seq) {
  1057. $uv_key = 'book:' . $book->bid . ':beforeforcesub:uv:date:' . $date . ':send_order_id:' . $send_order_id;
  1058. $pv_key = 'book:' . $book->bid . ':beforeforcesub::pvsend_order_id:' . $send_order_id;
  1059. Redis::hincrby($pv_key, $date, 1);
  1060. Redis::hincrby($pv_key, 'total', 1);
  1061. Redis::sadd($uv_key, $this->uid);
  1062. Redis::sadd('beforeforcesub' . $date, $book->bid . '-' . $send_order_id);
  1063. }
  1064. }
  1065. private function readRecordOther()
  1066. {
  1067. $chapter = $this->chapter;
  1068. try {
  1069. $data = [];
  1070. $data['uid'] = $this->uid;
  1071. $data['bid'] = $chapter->bid;
  1072. $data['cid'] = $chapter->id;
  1073. $data['is_vip'] = $chapter->is_vip;
  1074. $data['seq'] = $chapter->sequence;
  1075. $data['created_at'] = date('Y-m-d H:i:s');
  1076. $data['updated_at'] = date('Y-m-d H:i:s');
  1077. DB::table('read_records')->insert($data);
  1078. } catch (\Exception $e) {
  1079. }
  1080. }
  1081. /**
  1082. * 下架图书 true:下架不能阅读了, false:可以正常阅读
  1083. * @return bool
  1084. */
  1085. private function isOffShelf()
  1086. {
  1087. $book_info = $this->book_info;
  1088. if ($book_info->is_on_shelf == 0 || $book_info->is_on_shelf == 3) {
  1089. return true;
  1090. }
  1091. //屏蔽一本书1042 2626
  1092. if (in_array($book_info->bid, [1042, 2626]) && $this->distribution_channel_id == 123) {
  1093. return true;
  1094. }
  1095. if ($book_info->is_on_shelf == 4) {
  1096. //版权到期,有阅读记录还可以继续阅读,订购
  1097. /*if (!ReadRecordService::getRecordByUidBid($this->uid, $book_info->bid)) {
  1098. return true;
  1099. }*/
  1100. //版权到期,有包年可以继续阅读
  1101. $year_order = YearOrderService::getRecord($this->uid);
  1102. if($year_order){
  1103. return false;
  1104. }
  1105. //版权到期,有订购记录还可以继续阅读,订购
  1106. $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $book_info->bid);
  1107. if (!$chapter_order) return true;
  1108. }
  1109. return false;
  1110. }
  1111. //章节被删除的情况
  1112. private function chapterNotExists()
  1113. {
  1114. $chapter = $this->chapter;
  1115. $cid = $this->cid;
  1116. if ($chapter) return false;
  1117. //章节不存在的情况
  1118. $no_check = ChapterService::getChapterNameByIdNoCheck($cid);
  1119. $h5_scheme = 'https';
  1120. if (!$no_check) {
  1121. //章节真正不存在的情况
  1122. $url = $h5_scheme . '://' . _domain() . '/catalog?id=' . $this->en_bid;
  1123. return $url;
  1124. }
  1125. //章节被删除的情况
  1126. if ($no_check->next_cid) {
  1127. $url = $h5_scheme . '://' . _domain() . '/reader?bid=' . $this->en_bid . '&cid=' . $no_check->next_cid;
  1128. return $url;
  1129. }
  1130. //章节被删除的是最后一章的情况
  1131. $url = $h5_scheme . '://' . _domain() . '/catalog?id=' . $this->en_bid . '&sequence=' . $no_check->sequence;
  1132. return $url;
  1133. }
  1134. /**
  1135. * 深度阅读
  1136. * @param $chapter
  1137. */
  1138. private function deepReadRecord()
  1139. {
  1140. if (!env('ON_BOOK_UV_PV', false)) {
  1141. return;
  1142. }
  1143. $chapter = $this->chapter;
  1144. $key = 'book:' . $chapter->bid . ':deepread:uv';
  1145. $chapter_key = 'book:' . $chapter->bid . ':chapterseq:' . $chapter->sequence;
  1146. if (Redis::scard($key) > 50000) {
  1147. if (Redis::Sismember($key, $this->uid)) {
  1148. Redis::sadd($chapter_key, $this->uid);
  1149. }
  1150. } else {
  1151. Redis::sadd($key, $this->uid);
  1152. Redis::sadd($chapter_key, $this->uid);
  1153. }
  1154. }
  1155. /**
  1156. * 强关类型
  1157. * @param $appid
  1158. * @return array
  1159. */
  1160. private function getSubscribeType($appid, $bid)
  1161. {
  1162. $send_order_id = Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0;
  1163. $sub_setting = ChannelSubscribeSettingService::getSubscribeTypeByAppId($appid);
  1164. if (!$sub_setting) return ['type' => [1], 'url' => ''];
  1165. $types = explode(',', $sub_setting->force_subscribe_type);
  1166. $url = '';
  1167. if (in_array(3, $types) && !empty($sub_setting->resource_url)) {
  1168. $h5_scheme = env('H5_SCHEME', 'https');
  1169. $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;
  1170. }
  1171. return ['type' => $types, 'url' => $url];
  1172. }
  1173. private function showChapterPrice($bid, $charge_type)
  1174. {
  1175. if ($charge_type == 'BOOK') {
  1176. return false;
  1177. }
  1178. $show_chapter_price_bid = env('SHOW_CHAPTER_PRICE_BID', 'all');
  1179. if ($show_chapter_price_bid != 'all') {
  1180. $show_chapter_price_bid_array = explode(',', $show_chapter_price_bid);
  1181. if (!in_array($bid, $show_chapter_price_bid_array)) {
  1182. return false;
  1183. }
  1184. }
  1185. $show_chapter_price_channel = env('SHOW_CHAPTER_PRICE_CHANNEL', '');
  1186. if (!$show_chapter_price_channel) {
  1187. return false;
  1188. }
  1189. if ($show_chapter_price_channel == 'all') {
  1190. return true;
  1191. }
  1192. $show_chapter_price_channel_array = explode(',', $show_chapter_price_channel);
  1193. if (in_array($this->distribution_channel_id, $show_chapter_price_channel_array)) {
  1194. return true;
  1195. }
  1196. return false;
  1197. }
  1198. /**
  1199. * 阅读统计
  1200. */
  1201. private function chapterStats()
  1202. {
  1203. $chapter = $this->chapter;
  1204. $bid = $this->book_info->bid;
  1205. //派单精确统计
  1206. if ($chapter->sequence <= 30) {
  1207. //$this->sendOrderChapterUv($bid, $chapter->sequence);
  1208. }
  1209. //智能推送 测书统计章节uv
  1210. if ($chapter->sequence <= 60) {
  1211. //测书数据 由30改到60 2018-11-26 18:00
  1212. $this->smartPushTestBookChapterUv($bid, $chapter->sequence);
  1213. }
  1214. //智能推送 测书统计第二章 uv
  1215. if ($chapter->sequence == 2) {
  1216. $this->smartPushTestBookSecondChapterUv($bid);
  1217. }
  1218. // 统计前5张uv 会放在book_five_chapter_uv表中,脚本在 \App\Console\Commands\BookTest::data1里面
  1219. // 作用早忘记了
  1220. $field = date('Y-m-d');
  1221. /*
  1222. if ($chapter->sequence <= 5 && $this->send_order_id) {
  1223. try {
  1224. Redis::sadd(sprintf('book_before_five_uv_%s_%s_%s', $field, $bid, $chapter->sequence), $this->uid);
  1225. Redis::sadd('book_before_five_uvs', sprintf('%s_%s_%s', $field, $bid, $chapter->sequence));
  1226. } catch (\Exception $e) {
  1227. }
  1228. }*/
  1229. //统计点击率
  1230. $key = 'book_click_num_bid_' . $bid;
  1231. try {
  1232. Redis::HINCRBY($key, $field, 1);
  1233. } catch (\Exception $e) {
  1234. }
  1235. //阅读页面pvuv
  1236. $this->recordReaderUvAndPv();
  1237. }
  1238. private function smartPushTestBook($bid, $balance)
  1239. {
  1240. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1241. if ($smart_bid && in_array($bid, $smart_bid)) {
  1242. $field = sprintf('%s_amount', $bid);
  1243. try {
  1244. Redis::HINCRBY('smart_push_test_book', $field, $balance);
  1245. Redis::sadd('smart_push_test_book_user_count' . $bid, $this->uid);
  1246. } catch (\Exception $e) {
  1247. }
  1248. }
  1249. }
  1250. private function smartPushTestBookSecondChapterUv($bid)
  1251. {
  1252. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1253. if ($smart_bid && in_array($bid, $smart_bid)) {
  1254. try {
  1255. Redis::sadd('smart_push_test_book_second_uv' . $bid, $this->uid);
  1256. } catch (\Exception $e) {
  1257. }
  1258. }
  1259. }
  1260. private function smartPushTestBookChapterUv($bid, $sequence)
  1261. {
  1262. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1263. if ($smart_bid && in_array($bid, $smart_bid)) {
  1264. try {
  1265. $key = 'smartPushTestBookChapterUv:bid:%s:seq:%s';
  1266. Redis::sadd(sprintf($key, $bid, $sequence), $this->uid);
  1267. } catch (\Exception $e) {
  1268. }
  1269. }
  1270. }
  1271. /**
  1272. * 测书 智能推送统计
  1273. * @param $bid
  1274. */
  1275. private function smartPushTestBookPayPageUv($bid)
  1276. {
  1277. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1278. if ($smart_bid && in_array($bid, $smart_bid)) {
  1279. try {
  1280. $key = 'smartPushTestBookPayPageUv:bid:%s';
  1281. Redis::sadd(sprintf($key, $bid), $this->uid);
  1282. } catch (\Exception $e) {
  1283. }
  1284. }
  1285. }
  1286. /**
  1287. * 派单精确统计--章节
  1288. * @param $bid
  1289. * @param $sequence
  1290. */
  1291. private function sendOrderChapterUv($bid, $sequence)
  1292. {
  1293. if ($this->send_order_id) {
  1294. $send_order_bid = Redis::hget('sendOrderIdToBid', $this->send_order_id);
  1295. if (!$send_order_bid) {
  1296. $send_order_info = SendOrderService::getById($this->send_order_id);
  1297. if ($send_order_info) {
  1298. $send_order_bid = $send_order_info->book_id;
  1299. Redis::hset('sendOrderIdToBid', $this->send_order_id, $send_order_bid . '-' . time());
  1300. }
  1301. } else {
  1302. $send_order_bid_arr = explode('-', $send_order_bid);
  1303. $send_order_bid = $send_order_bid_arr[0];
  1304. }
  1305. if ($send_order_bid && $send_order_bid == $bid) {
  1306. try {
  1307. $key = 'accurateSendOrderChapterUv:bid:%s:seq:%s:sendorderid:' . $this->send_order_id;
  1308. Redis::sadd(sprintf($key, $bid, $sequence), $this->uid);
  1309. } catch (\Exception $e) {
  1310. }
  1311. }
  1312. }
  1313. }
  1314. /**
  1315. * 派单精确统计--强关页面uv
  1316. * @param $bid
  1317. */
  1318. private function sendOrderSubscribePageUv($bid)
  1319. {
  1320. if ($this->send_order_id) {
  1321. $send_order_bid = Redis::hget('sendOrderIdToBid', $this->send_order_id);
  1322. if (!$send_order_bid) {
  1323. $send_order_info = SendOrderService::getById($this->send_order_id);
  1324. if ($send_order_info) {
  1325. $send_order_bid = $send_order_info->book_id;
  1326. Redis::hset('sendOrderIdToBid', $this->send_order_id, $send_order_bid . '-' . time());
  1327. }
  1328. } else {
  1329. $send_order_bid_arr = explode('-', $send_order_bid);
  1330. $send_order_bid = $send_order_bid_arr[0];
  1331. }
  1332. if ($send_order_bid && $send_order_bid == $bid) {
  1333. try {
  1334. $key = 'accurateSendOrderSubscribePageUv:bid:%s:sendorderid:' . $this->send_order_id;
  1335. Redis::sadd(sprintf($key, $bid), $this->uid);
  1336. } catch (\Exception $e) {
  1337. }
  1338. }
  1339. }
  1340. }
  1341. /**
  1342. * 派单精确统计--支付页面uv
  1343. * @param $bid
  1344. */
  1345. private function sendOrderPayPageUv($bid)
  1346. {
  1347. if ($this->send_order_id) {
  1348. $send_order_bid = Redis::hget('sendOrderIdToBid', $this->send_order_id);
  1349. if (!$send_order_bid) {
  1350. $send_order_info = SendOrderService::getById($this->send_order_id);
  1351. if ($send_order_info) {
  1352. $send_order_bid = $send_order_info->book_id;
  1353. Redis::hset('sendOrderIdToBid', $this->send_order_id, $send_order_bid . '-' . time());
  1354. }
  1355. } else {
  1356. $send_order_bid_arr = explode('-', $send_order_bid);
  1357. $send_order_bid = $send_order_bid_arr[0];
  1358. }
  1359. if ($send_order_bid && $send_order_bid == $bid) {
  1360. try {
  1361. $key = 'accurateSendOrderPayPageUv:bid:%s:send_order_id:' . $this->send_order_id;
  1362. Redis::sadd(sprintf($key, $bid), $this->uid);
  1363. } catch (\Exception $e) {
  1364. }
  1365. }
  1366. }
  1367. }
  1368. private function recordReaderUvAndPv()
  1369. {
  1370. WapVisitStatService::recordReaderUvAndPv($this->uid, $this->distribution_channel_id);
  1371. }
  1372. //用户是否在广告样本中
  1373. private function isUserInAdSample(){
  1374. $res = DataAnalysisSelectUserService::getByUidAndType($this->uid,'AD');
  1375. if($res){
  1376. return true;
  1377. }else{
  1378. return false;
  1379. }
  1380. }
  1381. //阅读页面底部是否显示广告
  1382. private function isShowAd(){
  1383. //按章付费才显示
  1384. if($this->book_info->charge_type != 'CHAPTER'){
  1385. return ;
  1386. }
  1387. //用户是否在广告样本中
  1388. if(!$this->isUserInAdSample()) return ;
  1389. $cid = 0;
  1390. if($this->chapter && $this->chapter->next_cid){
  1391. $cid = $this->chapter->next_cid;
  1392. }
  1393. if(!$cid){
  1394. return ;
  1395. }
  1396. //如果下一张已经付费,广告不显示
  1397. if($this->getOrderRecord($this->book_info->bid, $cid)){
  1398. return ;
  1399. }
  1400. $next_chapter = ChapterService::getChapterNameById($cid, $this->book_info->bid);
  1401. //下一章事vip的话 才显示广告
  1402. if(!$next_chapter || !$next_chapter->is_vip){
  1403. return ;
  1404. }
  1405. $price = $this->getPrice($this->book_info,$next_chapter);
  1406. $flag = $price > $this->user->balance;
  1407. if( $flag ){
  1408. $this->chapter->is_show_ad = 1;
  1409. AdVisitStatService::create($this->uid,$this->book_info->bid,$this->cid,'EXPOSURE');
  1410. }
  1411. }
  1412. public function getWechatJsConfig(Request $request)
  1413. {
  1414. //try {
  1415. // $chapter->is_show_share_button = 1;
  1416. $bid = $request->input('bid', '');
  1417. $cid = $request->input('cid', '');
  1418. $url = $request->input('url', '');
  1419. $url = urldecode($url);
  1420. \Log::info('url:' . ($this->uid) . $url);
  1421. if (empty($bid) || empty($cid) || empty($url)) {
  1422. return response()->error('PARAM_EMPTY');
  1423. }
  1424. $share_url = 'https://'._domain().'/detail?fromtype=readershare&id='.$bid.'&fromflag='.($this->uid).'&fromcid='.$cid;
  1425. //$js_config = ChapterShareWechatConfigService::getConfig($url);
  1426. $app_hash = array(
  1427. 4025=>['appid'=>'wx80b618c9dcc940f7','appsecret'=>'255a2de8db51e4084b5e9087d26d4ecb'],
  1428. 4147=>['appid'=>'wx2a99699cd9572812','appsecret'=>'0e0d57f94eeba50abb3af6883df05a5c'],
  1429. 691=>['appid'=>'wx5cefcd251dc40693','appsecret'=>'755f64c85173dad21f20870e38f59d8a'],
  1430. 123=>['appid'=>'wx983808610aa9dcc5','appsecret'=>'91002999590b79f3c7be35fd73c04049'],
  1431. 211=>['appid'=>'wx39338e374cc2055b','appsecret'=>'1a4c2039be271b3811182be5bb53eb34'],
  1432. );
  1433. $appId = $app_hash[$this->distribution_channel_id]['appid'];
  1434. $appSecret = $app_hash[$this->distribution_channel_id]['appsecret'];
  1435. $js_config = (new ChapterShareWechatConfigService($appId,$appSecret))->getSignPackage($url);
  1436. /*$appid = DB::table('official_accounts')->where('distribution_channel_id',$this->distribution_channel_id)->first();//(['distribution_channel_id'=>$this->distribution_channel_id]);
  1437. $appid = $appid->appid;
  1438. //\Log::info('get_appid:'.($appid));
  1439. $js_config = (new ChapterShareWechatConfigService($appid))->getSignPackage($url);
  1440. //$chapter->js_config = $js_config;
  1441. //$chapter->share_url = $share_url;
  1442. $book = BookService::getBookById(Hashids::decode($bid)[0]);
  1443. $first_cid = $book->first_cid;
  1444. $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;
  1445. /*if (in_array($this->uid, explode(',', env('TEST_SHARE_UIDS')))) {
  1446. $share_url = 'https://site5017zqeyzwgyx9n8.zhuishuyun.com/reader?fromtype=readershare&bid=' . $bid . '&fromflag=' . ($this->uid) . '&fromcid=' . $cid;
  1447. }*/
  1448. $book = BookService::getBookById(Hashids::decode($bid)[0]);
  1449. $first_cid = $book->first_cid;
  1450. $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;
  1451. if (in_array($this->distribution_channel_id, [2, 14])) {
  1452. $js_config = ChapterShareWechatConfigService::getConfig($url);
  1453. $share_url = 'https://site' . ($this->distribution_channel_id) . '.' . env('SHARE_DOMAIN') . '/reader?fromtype=readershare&bid=' . $bid . '&cid=' . ($first_cid) . '&fromflag=' . ($this->uid) . '&fromcid=' . $cid;
  1454. }
  1455. $js_config['share_url'] = $share_url;
  1456. /*} catch (\Exception $e) {
  1457. \Log::error($e->getMessage());
  1458. }*/
  1459. //\Log::info($js_config);
  1460. return response()->success($js_config);
  1461. }
  1462. }