ChapterController.php 60 KB

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