ChapterController.php 62 KB

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