ChapterController.php 72 KB

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