ChapterController.php 61 KB

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