ChapterController.php 60 KB

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