ChapterController.php 60 KB

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