ChapterController.php 61 KB

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