ChapterController.php 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  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. 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. if($book_info->is_on_shelf == 10){
  1114. $yq_book_user = BookUserService::selectUser($this->uid);
  1115. if($yq_book_user && $yq_book_user->type = 'ENABLE'){
  1116. return false;
  1117. }
  1118. return true;
  1119. }
  1120. return false;
  1121. }
  1122. //章节被删除的情况
  1123. private function chapterNotExists()
  1124. {
  1125. $chapter = $this->chapter;
  1126. $cid = $this->cid;
  1127. if ($chapter) return false;
  1128. //章节不存在的情况
  1129. $no_check = ChapterService::getChapterNameByIdNoCheck($cid);
  1130. $h5_scheme = 'https';
  1131. if (!$no_check) {
  1132. //章节真正不存在的情况
  1133. $url = $h5_scheme . '://' . _domain() . '/catalog?id=' . $this->en_bid;
  1134. return $url;
  1135. }
  1136. //章节被删除的情况
  1137. if ($no_check->next_cid) {
  1138. $url = $h5_scheme . '://' . _domain() . '/reader?bid=' . $this->en_bid . '&cid=' . $no_check->next_cid;
  1139. return $url;
  1140. }
  1141. //章节被删除的是最后一章的情况
  1142. $url = $h5_scheme . '://' . _domain() . '/catalog?id=' . $this->en_bid . '&sequence=' . $no_check->sequence;
  1143. return $url;
  1144. }
  1145. /**
  1146. * 深度阅读
  1147. * @param $chapter
  1148. */
  1149. private function deepReadRecord()
  1150. {
  1151. if (!env('ON_BOOK_UV_PV', false)) {
  1152. return;
  1153. }
  1154. $chapter = $this->chapter;
  1155. $key = 'book:' . $chapter->bid . ':deepread:uv';
  1156. $chapter_key = 'book:' . $chapter->bid . ':chapterseq:' . $chapter->sequence;
  1157. if (Redis::scard($key) > 50000) {
  1158. if (Redis::Sismember($key, $this->uid)) {
  1159. Redis::sadd($chapter_key, $this->uid);
  1160. }
  1161. } else {
  1162. Redis::sadd($key, $this->uid);
  1163. Redis::sadd($chapter_key, $this->uid);
  1164. }
  1165. }
  1166. /**
  1167. * 强关类型
  1168. * @param $appid
  1169. * @return array
  1170. */
  1171. private function getSubscribeType($appid, $bid)
  1172. {
  1173. $send_order_id = Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0;
  1174. $sub_setting = ChannelSubscribeSettingService::getSubscribeTypeByAppId($appid);
  1175. if (!$sub_setting) return ['type' => [1], 'url' => ''];
  1176. $types = explode(',', $sub_setting->force_subscribe_type);
  1177. $url = '';
  1178. if (in_array(3, $types) && !empty($sub_setting->resource_url)) {
  1179. $h5_scheme = env('H5_SCHEME', 'https');
  1180. $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;
  1181. }
  1182. return ['type' => $types, 'url' => $url];
  1183. }
  1184. private function showChapterPrice($bid, $charge_type)
  1185. {
  1186. if ($charge_type == 'BOOK') {
  1187. return false;
  1188. }
  1189. $show_chapter_price_bid = env('SHOW_CHAPTER_PRICE_BID', 'all');
  1190. if ($show_chapter_price_bid != 'all') {
  1191. $show_chapter_price_bid_array = explode(',', $show_chapter_price_bid);
  1192. if (!in_array($bid, $show_chapter_price_bid_array)) {
  1193. return false;
  1194. }
  1195. }
  1196. $show_chapter_price_channel = env('SHOW_CHAPTER_PRICE_CHANNEL', '');
  1197. if (!$show_chapter_price_channel) {
  1198. return false;
  1199. }
  1200. if ($show_chapter_price_channel == 'all') {
  1201. return true;
  1202. }
  1203. $show_chapter_price_channel_array = explode(',', $show_chapter_price_channel);
  1204. if (in_array($this->distribution_channel_id, $show_chapter_price_channel_array)) {
  1205. return true;
  1206. }
  1207. return false;
  1208. }
  1209. /**
  1210. * 阅读统计
  1211. */
  1212. private function chapterStats()
  1213. {
  1214. $chapter = $this->chapter;
  1215. $bid = $this->book_info->bid;
  1216. //派单精确统计
  1217. if ($chapter->sequence <= 30) {
  1218. //$this->sendOrderChapterUv($bid, $chapter->sequence);
  1219. }
  1220. //智能推送 测书统计章节uv
  1221. if ($chapter->sequence <= 60) {
  1222. //测书数据 由30改到60 2018-11-26 18:00
  1223. $this->smartPushTestBookChapterUv($bid, $chapter->sequence);
  1224. }
  1225. //智能推送 测书统计第二章 uv
  1226. if ($chapter->sequence == 2) {
  1227. $this->smartPushTestBookSecondChapterUv($bid);
  1228. }
  1229. // 统计前5张uv 会放在book_five_chapter_uv表中,脚本在 \App\Console\Commands\BookTest::data1里面
  1230. // 作用早忘记了
  1231. $field = date('Y-m-d');
  1232. /*
  1233. if ($chapter->sequence <= 5 && $this->send_order_id) {
  1234. try {
  1235. Redis::sadd(sprintf('book_before_five_uv_%s_%s_%s', $field, $bid, $chapter->sequence), $this->uid);
  1236. Redis::sadd('book_before_five_uvs', sprintf('%s_%s_%s', $field, $bid, $chapter->sequence));
  1237. } catch (\Exception $e) {
  1238. }
  1239. }*/
  1240. //统计点击率
  1241. $key = 'book_click_num_bid_' . $bid;
  1242. try {
  1243. Redis::HINCRBY($key, $field, 1);
  1244. } catch (\Exception $e) {
  1245. }
  1246. //阅读页面pvuv
  1247. $this->recordReaderUvAndPv();
  1248. }
  1249. private function smartPushTestBook($bid, $balance)
  1250. {
  1251. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1252. if ($smart_bid && in_array($bid, $smart_bid)) {
  1253. $field = sprintf('%s_amount', $bid);
  1254. try {
  1255. Redis::HINCRBY('smart_push_test_book', $field, $balance);
  1256. Redis::sadd('smart_push_test_book_user_count' . $bid, $this->uid);
  1257. } catch (\Exception $e) {
  1258. }
  1259. }
  1260. }
  1261. private function smartPushTestBookSecondChapterUv($bid)
  1262. {
  1263. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1264. if ($smart_bid && in_array($bid, $smart_bid)) {
  1265. try {
  1266. Redis::sadd('smart_push_test_book_second_uv' . $bid, $this->uid);
  1267. } catch (\Exception $e) {
  1268. }
  1269. }
  1270. }
  1271. private function smartPushTestBookChapterUv($bid, $sequence)
  1272. {
  1273. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1274. if ($smart_bid && in_array($bid, $smart_bid)) {
  1275. try {
  1276. $key = 'smartPushTestBookChapterUv:bid:%s:seq:%s';
  1277. Redis::sadd(sprintf($key, $bid, $sequence), $this->uid);
  1278. } catch (\Exception $e) {
  1279. }
  1280. }
  1281. }
  1282. /**
  1283. * 测书 智能推送统计
  1284. * @param $bid
  1285. */
  1286. private function smartPushTestBookPayPageUv($bid)
  1287. {
  1288. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1289. if ($smart_bid && in_array($bid, $smart_bid)) {
  1290. try {
  1291. $key = 'smartPushTestBookPayPageUv:bid:%s';
  1292. Redis::sadd(sprintf($key, $bid), $this->uid);
  1293. } catch (\Exception $e) {
  1294. }
  1295. }
  1296. }
  1297. /**
  1298. * 派单精确统计--章节
  1299. * @param $bid
  1300. * @param $sequence
  1301. */
  1302. private function sendOrderChapterUv($bid, $sequence)
  1303. {
  1304. if ($this->send_order_id) {
  1305. $send_order_bid = Redis::hget('sendOrderIdToBid', $this->send_order_id);
  1306. if (!$send_order_bid) {
  1307. $send_order_info = SendOrderService::getById($this->send_order_id);
  1308. if ($send_order_info) {
  1309. $send_order_bid = $send_order_info->book_id;
  1310. Redis::hset('sendOrderIdToBid', $this->send_order_id, $send_order_bid . '-' . time());
  1311. }
  1312. } else {
  1313. $send_order_bid_arr = explode('-', $send_order_bid);
  1314. $send_order_bid = $send_order_bid_arr[0];
  1315. }
  1316. if ($send_order_bid && $send_order_bid == $bid) {
  1317. try {
  1318. $key = 'accurateSendOrderChapterUv:bid:%s:seq:%s:sendorderid:' . $this->send_order_id;
  1319. Redis::sadd(sprintf($key, $bid, $sequence), $this->uid);
  1320. } catch (\Exception $e) {
  1321. }
  1322. }
  1323. }
  1324. }
  1325. /**
  1326. * 派单精确统计--强关页面uv
  1327. * @param $bid
  1328. */
  1329. private function sendOrderSubscribePageUv($bid)
  1330. {
  1331. if ($this->send_order_id) {
  1332. $send_order_bid = Redis::hget('sendOrderIdToBid', $this->send_order_id);
  1333. if (!$send_order_bid) {
  1334. $send_order_info = SendOrderService::getById($this->send_order_id);
  1335. if ($send_order_info) {
  1336. $send_order_bid = $send_order_info->book_id;
  1337. Redis::hset('sendOrderIdToBid', $this->send_order_id, $send_order_bid . '-' . time());
  1338. }
  1339. } else {
  1340. $send_order_bid_arr = explode('-', $send_order_bid);
  1341. $send_order_bid = $send_order_bid_arr[0];
  1342. }
  1343. if ($send_order_bid && $send_order_bid == $bid) {
  1344. try {
  1345. $key = 'accurateSendOrderSubscribePageUv:bid:%s:sendorderid:' . $this->send_order_id;
  1346. Redis::sadd(sprintf($key, $bid), $this->uid);
  1347. } catch (\Exception $e) {
  1348. }
  1349. }
  1350. }
  1351. }
  1352. /**
  1353. * 派单精确统计--支付页面uv
  1354. * @param $bid
  1355. */
  1356. private function sendOrderPayPageUv($bid)
  1357. {
  1358. if ($this->send_order_id) {
  1359. $send_order_bid = Redis::hget('sendOrderIdToBid', $this->send_order_id);
  1360. if (!$send_order_bid) {
  1361. $send_order_info = SendOrderService::getById($this->send_order_id);
  1362. if ($send_order_info) {
  1363. $send_order_bid = $send_order_info->book_id;
  1364. Redis::hset('sendOrderIdToBid', $this->send_order_id, $send_order_bid . '-' . time());
  1365. }
  1366. } else {
  1367. $send_order_bid_arr = explode('-', $send_order_bid);
  1368. $send_order_bid = $send_order_bid_arr[0];
  1369. }
  1370. if ($send_order_bid && $send_order_bid == $bid) {
  1371. try {
  1372. $key = 'accurateSendOrderPayPageUv:bid:%s:send_order_id:' . $this->send_order_id;
  1373. Redis::sadd(sprintf($key, $bid), $this->uid);
  1374. } catch (\Exception $e) {
  1375. }
  1376. }
  1377. }
  1378. }
  1379. private function recordReaderUvAndPv()
  1380. {
  1381. WapVisitStatService::recordReaderUvAndPv($this->uid, $this->distribution_channel_id);
  1382. }
  1383. //用户是否在广告样本中
  1384. private function isUserInAdSample(){
  1385. $res = DataAnalysisSelectUserService::getByUidAndType($this->uid,'AD');
  1386. if($res){
  1387. return true;
  1388. }else{
  1389. return false;
  1390. }
  1391. }
  1392. //阅读页面底部是否显示广告
  1393. private function isShowAd(){
  1394. if(!in_array($this->distribution_channel_id,[2,14,123,211])){
  1395. return ;
  1396. }
  1397. //按章付费才显示
  1398. if($this->book_info->charge_type != 'CHAPTER'){
  1399. return ;
  1400. }
  1401. //用户是否在广告样本中
  1402. if(!$this->isUserInAdSample()) return ;
  1403. $cid = 0;
  1404. if($this->chapter && $this->chapter->next_cid){
  1405. $cid = $this->chapter->next_cid;
  1406. }
  1407. if(!$cid){
  1408. return ;
  1409. }
  1410. //付费用户没有广告
  1411. if(OrderService::isPaidUser($this->uid)) return ;
  1412. //如果下一张已经付费,广告不显示
  1413. if($this->getOrderRecord($this->book_info->bid, $cid)){
  1414. return ;
  1415. }
  1416. $next_chapter = ChapterService::getChapterNameById($cid, $this->book_info->bid);
  1417. //下一章事vip的话 才显示广告
  1418. if(!$next_chapter || !$next_chapter->is_vip){
  1419. return ;
  1420. }
  1421. $price = $this->getPrice($this->book_info,$next_chapter);
  1422. $flag = $price > $this->user->balance;
  1423. if( $flag ){
  1424. $this->chapter->is_show_ad = 1;
  1425. AdVisitStatService::create($this->uid,$this->book_info->bid,$this->cid,'EXPOSURE');
  1426. }
  1427. }
  1428. public function getWechatJsConfig(Request $request)
  1429. {
  1430. //try {
  1431. // $chapter->is_show_share_button = 1;
  1432. $bid = $request->input('bid', '');
  1433. $cid = $request->input('cid', '');
  1434. $url = $request->input('url', '');
  1435. $url = urldecode($url);
  1436. \Log::info('url:' . ($this->uid) . $url);
  1437. if (empty($bid) || empty($cid) || empty($url)) {
  1438. return response()->error('PARAM_EMPTY');
  1439. }
  1440. $share_url = 'https://'._domain().'/detail?fromtype=readershare&id='.$bid.'&fromflag='.($this->uid).'&fromcid='.$cid;
  1441. //$js_config = ChapterShareWechatConfigService::getConfig($url);
  1442. $app_hash = array(
  1443. 4025=>['appid'=>'wx80b618c9dcc940f7','appsecret'=>'255a2de8db51e4084b5e9087d26d4ecb'],
  1444. 4147=>['appid'=>'wx2a99699cd9572812','appsecret'=>'0e0d57f94eeba50abb3af6883df05a5c'],
  1445. 691=>['appid'=>'wx5cefcd251dc40693','appsecret'=>'755f64c85173dad21f20870e38f59d8a'],
  1446. 123=>['appid'=>'wx983808610aa9dcc5','appsecret'=>'91002999590b79f3c7be35fd73c04049'],
  1447. 211=>['appid'=>'wx39338e374cc2055b','appsecret'=>'1a4c2039be271b3811182be5bb53eb34'],
  1448. );
  1449. $appId = $app_hash[$this->distribution_channel_id]['appid'];
  1450. $appSecret = $app_hash[$this->distribution_channel_id]['appsecret'];
  1451. $js_config = (new ChapterShareWechatConfigService($appId,$appSecret))->getSignPackage($url);
  1452. /*$appid = DB::table('official_accounts')->where('distribution_channel_id',$this->distribution_channel_id)->first();//(['distribution_channel_id'=>$this->distribution_channel_id]);
  1453. $appid = $appid->appid;
  1454. //\Log::info('get_appid:'.($appid));
  1455. $js_config = (new ChapterShareWechatConfigService($appid))->getSignPackage($url);
  1456. //$chapter->js_config = $js_config;
  1457. //$chapter->share_url = $share_url;
  1458. $book = BookService::getBookById(Hashids::decode($bid)[0]);
  1459. $first_cid = $book->first_cid;
  1460. $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;
  1461. /*if (in_array($this->uid, explode(',', env('TEST_SHARE_UIDS')))) {
  1462. $share_url = 'https://site5017zqeyzwgyx9n8.zhuishuyun.com/reader?fromtype=readershare&bid=' . $bid . '&fromflag=' . ($this->uid) . '&fromcid=' . $cid;
  1463. }*/
  1464. $book = BookService::getBookById(Hashids::decode($bid)[0]);
  1465. $first_cid = $book->first_cid;
  1466. $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;
  1467. if (in_array($this->distribution_channel_id, [2, 14])) {
  1468. $js_config = ChapterShareWechatConfigService::getConfig($url);
  1469. $share_url = 'https://site' . ($this->distribution_channel_id) . '.' . env('SHARE_DOMAIN') . '/reader?fromtype=readershare&bid=' . $bid . '&cid=' . ($first_cid) . '&fromflag=' . ($this->uid) . '&fromcid=' . $cid;
  1470. }
  1471. $js_config['share_url'] = $share_url;
  1472. /*} catch (\Exception $e) {
  1473. \Log::error($e->getMessage());
  1474. }*/
  1475. //\Log::info($js_config);
  1476. return response()->success($js_config);
  1477. }
  1478. }