ChapterController.php 71 KB

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