ChapterController.php 62 KB

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