ChapterController.php 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  1. <?php
  2. namespace App\Http\Controllers\Wap\Book;
  3. use App\Modules\Book\Models\DataAnalysisBookConfig;
  4. use App\Modules\Book\Services\BookService;
  5. use App\Modules\Book\Services\ChapterCommentsService;
  6. use App\Modules\Book\Services\ChapterShareWechatConfigService;
  7. use App\Modules\Statistic\Models\DataAnalysisSelectUser;
  8. use App\Modules\Statistic\Services\AdVisitStatService;
  9. use App\Modules\Statistic\Services\DataAnalysisChapterService;
  10. use App\Modules\Statistic\Services\DataAnalysisSelectUserService;
  11. use App\Modules\Statistic\Services\WapVisitStatService;
  12. use App\Modules\Subscribe\Services\OrderService;
  13. use App\Modules\Subscribe\Models\Order;
  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. $this->chapter->is_visiable_ad = 0;//可不可显示
  774. }
  775. private function sign()
  776. {
  777. //渠道123 未关注的签到放到强关前一章
  778. if ($this->distribution_channel_id == 123 && !$this->is_had_subscribe) {
  779. if ($this->book_info->force_subscribe_chapter_seq - 1 == $this->chapter->sequence) {
  780. $sign_status = $this->userSign($this->uid);
  781. //$sign_status = 0;
  782. $this->chapter->sign_status = $sign_status ? 1 : 0;
  783. $this->chapter->sign_reard = $sign_status;
  784. $this->chapter->sign_days = ReadRecordService::getSignCount($this->uid);
  785. }
  786. } else {
  787. $sign_status = $this->userSign($this->uid);
  788. //$sign_status =0;
  789. $this->chapter->sign_status = $sign_status ? 1 : 0;
  790. $this->chapter->sign_reard = $sign_status;
  791. $this->chapter->sign_days = ReadRecordService::getSignCount($this->uid);
  792. }
  793. }
  794. private function forceSubscribe()
  795. {
  796. if($this->need_subscribe_appid){
  797. $src = $this->getRcodeInfo(['appid' => $this->need_subscribe_appid], $this->book_info->bid);
  798. if (empty($src)) return false;
  799. $url = '/subscribe/qrcode?img=' . urlencode($src) . '&title=' . $this->need_subscribe_name . '&uid=' . $this->uid;
  800. return ['error_type' => 'WAP_NOT_SUBSCRIBE', 'data' => ['src' => $url, 'head_img' => '']];
  801. }
  802. //获取公众号信息
  803. $officialAccount = $this->getOfficialAccount();
  804. if (!$officialAccount || empty($officialAccount->appid)) {
  805. return false;
  806. }
  807. //如果是链接强关,则跳转
  808. $sub_type = $this->getSubscribeType($officialAccount->appid, $this->book_info->bid);
  809. if (in_array(3, $sub_type['type']) && $sub_type['url']) {
  810. return ['error_type' => 'WAP_LINK_SUBSCRIBE', 'data' => ['url' => $sub_type['url']]];
  811. }
  812. //普通强关
  813. if (in_array(1, $sub_type['type'])) {
  814. $src = $this->getRcodeInfo(['appid' => $officialAccount->appid], $this->book_info->bid);
  815. if (empty($src)) return false;
  816. if ($officialAccount->head_img) {
  817. $url = '/subscribe/qrcode?img=' . urlencode($src) . '&title=' . $officialAccount->nickname . '&head_img=' . urlencode($officialAccount->head_img) . '&uid=' . $this->uid;
  818. } else {
  819. $url = '/subscribe/qrcode?img=' . urlencode($src) . '&title=' . $officialAccount->nickname . '&uid=' . $this->uid;
  820. }
  821. return ['error_type' => 'WAP_NOT_SUBSCRIBE', 'data' => ['src' => $url, 'head_img' => $officialAccount->head_img]];
  822. }
  823. return false;
  824. }
  825. //强关页面的统计
  826. private function subscribeStats()
  827. {
  828. if ($this->send_order_id) {
  829. //$this->sendOrderSubscribePageUv($this->book_info->bid);
  830. //强关页面的uv
  831. /*try {
  832. Redis::sadd('subscribe_page_uv' . $this->send_order_id, $this->uid);
  833. Redis::sadd('subscribe_page_uv_send_order_ids', $this->send_order_id);
  834. } catch (\Exception $e) {
  835. }*/
  836. }
  837. }
  838. /**
  839. * 短推长
  840. */
  841. private function shortPushLong()
  842. {
  843. if ($this->book_info->charge_type = 'CHAPTER') return;
  844. $is_has_book_order = BookOrderService::isHasBookOrder($this->uid);
  845. if ($this->chapter->is_vip && $is_has_book_order) {
  846. $this->chapter->show_push_title = 1;
  847. $this->chapter->push_title = DataAnalysisBookConfig::getUserReaderPagePushBook($this->uid, $this->distribution_channel_id);
  848. }
  849. if (!$this->chapter->next_cid && $is_has_book_order) {
  850. $link = DataAnalysisBookConfig::getUserReaderPagePushBook($this->uid, $this->distribution_channel_id, 1);
  851. if ($link && isset($link['link']) && !empty($link['link'])) {
  852. $this->chapter->next_jump = 1;
  853. $this->chapter->next_jump_url = $link['link'];
  854. }
  855. }
  856. }
  857. /**
  858. * 短推长、短推短对比需求
  859. * 目标用户:全站短篇进入新注册且订阅短篇的付费用户
  860. * 方式:根据uid奇偶取用,奇:测试用户1 偶:测试用户2
  861. * 测试用户1:底部链接+完本推荐 均使用短篇小说
  862. * 测试用户2:底部链接+完本推荐 均使用长篇小说
  863. * 目标用户:全站短篇进入新注册且订阅短篇的付费用户
  864. */
  865. private function shortPushshort()
  866. {
  867. //新用户 注册时间大于某个值
  868. if(!$this->user){
  869. return ;
  870. }
  871. if(!env('SHORT_PUSH_SHORT_START_TIME') || strtotime($this->user->created_at) < env('SHORT_PUSH_SHORT_START_TIME')){
  872. return ;
  873. }
  874. //没有派单
  875. if(!$this->user->send_order_id) return ;
  876. //只在vip章节底部显示
  877. if(!$this->chapter->is_vip){
  878. return ;
  879. }
  880. //强关
  881. if(!$this->is_had_subscribe){
  882. return ;
  883. }
  884. //短篇进入
  885. $send_order_info = SendOrderService::getById($this->user->send_order_id);
  886. if(!$send_order_info || $send_order_info->charge_type != 'BOOK'){
  887. return ;
  888. }
  889. //订阅短篇的付费用户
  890. $is_has_book_order = BookOrderService::isHasBookOrder($this->uid);
  891. if(!$is_has_book_order) return ;
  892. //是否付费用户
  893. if(!OrderService::isPaidUser($this->uid)) return ;
  894. $type = $this->uid %2 == 1? 'BOOK':'CHAPTER';
  895. $result = DataAnalysisSelectUserService::create($this->uid, $this->distribution_channel_id, $type, 500, $attach = 'short2s2l');
  896. if(!$result) return ;
  897. //底部链接
  898. $this->chapter->show_push_title = 1;
  899. $this->chapter->push_title = DataAnalysisBookConfig::getUserReaderPagePushBookV2($this->uid, $this->distribution_channel_id,$type);
  900. //完本推荐
  901. if (!$this->chapter->next_cid ) {
  902. $link = DataAnalysisBookConfig::getUserReaderPagePushBookV2($this->uid, $this->distribution_channel_id,$type, 1);
  903. if($link && $link['link']){
  904. $this->chapter->next_jump = 1;
  905. $this->chapter->next_jump_url = $link['link'];
  906. }
  907. }
  908. }
  909. private function share()
  910. {
  911. if ($this->is_had_subscribe && in_array($this->distribution_channel_id, [2, 14, 211,4025,4147,691,123]) && $this->user) {//,4147,691,123,4025
  912. $this->chapter->is_show_share_button = 1;
  913. }
  914. }
  915. /**
  916. * 123 渠道阅读底部强关图片
  917. */
  918. private function specialSubscribeFlag()
  919. {
  920. if ($this->distribution_channel_id == 123 && !$this->is_had_subscribe) {
  921. $this->chapter->is_had_subscribe = 1;
  922. }
  923. }
  924. //订阅统计
  925. private function chapterOrderStats($fee)
  926. {
  927. $day = date('Y--m-d');
  928. try {
  929. Redis::hincrby('wap:chapterandbookorder:bid:' . $this->book_info->bid, $day, $fee);
  930. } catch (\Exception $e) {
  931. }
  932. $this->smartPushTestBook($this->book_info->bid, $fee);
  933. $specialChannelIdStats = env('SPECIAL_CHANNEL_STATS', 211);
  934. if (in_array($this->distribution_channel_id, explode(',', $specialChannelIdStats))) {
  935. WapVisitStatService::specialChannelIdStatsSave($this->uid, $this->book_info->bid, $fee);
  936. }
  937. }
  938. /**
  939. * 签到
  940. * @param $uid
  941. * @return int
  942. */
  943. protected function userSign($uid)
  944. {
  945. if (UserSignService::isSign($uid)) {
  946. //签过到了
  947. return 0;
  948. }
  949. if ($res = UserSignService::signToday($uid)) {
  950. //$sign_key = 'leyuee:wap:usersigni';
  951. //Redis::hset($sign_key, $uid, $res);
  952. return $res;
  953. }
  954. return 0;
  955. }
  956. /**
  957. * 添加订购记录
  958. * @param $book_info
  959. * @param $chapter_id
  960. * @param $fee
  961. * @return bool
  962. */
  963. protected function bookOrderOrChapterOrder($book_info, $chapter_id, $fee, $chapter_name, $is_remind)
  964. {
  965. if ($book_info['charge_type'] == 'BOOK') {
  966. $data = [
  967. 'uid' => $this->uid,
  968. 'fee' => $fee,
  969. 'u' => Cookie::get('send_order_id'),
  970. 'distribution_channel_id' => $this->distribution_channel_id,
  971. 'bid' => $book_info->bid,
  972. 'book_name' => $book_info->book_name,
  973. 'send_order_id' => Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0,
  974. ];
  975. return BookOrderService::addOrderRecodeAndDecrUserBalance($data, $this->uid);
  976. } else {
  977. $data = [
  978. 'uid' => $this->uid,
  979. 'fee' => $fee,
  980. 'cid' => $chapter_id,
  981. 'bid' => $book_info->bid,
  982. 'distribution_channel_id' => $this->distribution_channel_id,
  983. 'book_name' => $book_info->book_name,
  984. 'chapter_name' => $chapter_name,
  985. 'send_order_id' => Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0,
  986. 'is_remind' => $is_remind
  987. ];
  988. //print_r($data);
  989. if ($is_remind) {
  990. $this->addOrderRemind($book_info->bid);
  991. }
  992. return ChapterOrderService::addOrderAndDecrUserBalance($data, $this->uid);
  993. }
  994. }
  995. protected function addOrderRemind($bid)
  996. {
  997. if (ChapterReminderService::checkIsNoReminder($this->uid, $bid)) {
  998. return true;
  999. } else {
  1000. ChapterReminderService::add($this->uid, $bid);
  1001. return true;
  1002. }
  1003. }
  1004. /**
  1005. * 是否订购提醒
  1006. * @param $chapter_id
  1007. * @return bool
  1008. */
  1009. protected function isOrderRemind($bid)
  1010. {
  1011. $is_no_reminder = ChapterReminderService::checkIsNoReminder($this->uid, $bid) ? 1 : 0;
  1012. return $is_no_reminder == 0;
  1013. }
  1014. /**
  1015. * 用户是否关注
  1016. * @param $uid
  1017. * @return bool
  1018. */
  1019. protected function getSubscribe()
  1020. {
  1021. $send_order_info = null;
  1022. if(!$this->send_order_id && Redis::hget('channel:setting:'.$this->distribution_channel_id,'no_send_order_subscribe')){
  1023. return true;
  1024. }
  1025. if($this->send_order_id){
  1026. $send_order_info = SendOrderService::getById($this->send_order_id);
  1027. }
  1028. $send_order_subscribe_status = Redis::hget('channel:setting:'.$this->distribution_channel_id,'send_order_subscribe');
  1029. /*if(is_null($send_order_subsribe_status)){
  1030. $send_order_subsribe_status = 0;
  1031. }*/
  1032. //$send_order_subscribe_status = 0;
  1033. if($send_order_subscribe_status &&
  1034. $send_order_info &&
  1035. $send_order_info->promotion_type == 'EXTERNAL' &&
  1036. $send_order_info->send_time &&
  1037. strtotime($send_order_info->send_time) >= 1557712800
  1038. ){
  1039. $subscribe_info = ForceSubscribeUserIService::subscribes($this->uid,$this->distribution_channel_id,$this->send_order_id);
  1040. $this->need_subscribe_appid = $subscribe_info['appid'];
  1041. $this->need_subscribe_name = $subscribe_info['name'];
  1042. return $subscribe_info['is_subscribed'];
  1043. }
  1044. if($send_order_info &&
  1045. $send_order_info->promotion_type == 'INTERNAL' &&
  1046. strtotime($send_order_info->created_at) > 1555488000){
  1047. return true;
  1048. }
  1049. $res = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $this->uid]);
  1050. if ($res) {
  1051. $this->force_subscribe_time = isset($res->created_at)?$res->created_at:null;
  1052. return true;
  1053. }
  1054. return false;
  1055. }
  1056. /**
  1057. * 获取公众号信息
  1058. * @param $distribution_channel_id
  1059. */
  1060. protected function getOfficialAccount()
  1061. {
  1062. $distribution_channel_id = $this->distribution_channel_id;
  1063. $res = OfficialAccountService::canUseOfficialAccountByChannelId(compact('distribution_channel_id'));
  1064. if (isset($res->nickname) && !empty($res->nickname)) {
  1065. Cookie::queue('force_subscribe_name', $res->nickname, env('U_COOKIE_EXPIRE'));
  1066. }
  1067. return $res;
  1068. }
  1069. /**
  1070. * 获取二维码
  1071. * @param $param
  1072. * @return bool
  1073. */
  1074. private function getRcodeInfo($param, $bid)
  1075. {
  1076. //return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQGI8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyZEZ0T0ZoSHljM2kxOTUwQU5zY08AAgRF8-xbAwQAjScA";
  1077. $param_need = [
  1078. 'gzh_app_id' => $param['appid'],
  1079. 'scene_id' => $this->uid,
  1080. 'timestamp' => time(),
  1081. ];
  1082. $param_need['sign'] = $this->getSign($param_need);
  1083. $client = new Client(['timeout' => 3.0,]);
  1084. try {
  1085. Log::info('get qrcode url is:' . 'https://zsyauth.aizhuishu.com/api/get_qrcode_url?' . http_build_query($param_need));
  1086. $qrcode_url_res = $client->request('get', 'https://zsyauth.aizhuishu.com/api/get_qrcode_url?' . http_build_query($param_need))->getBody()->getContents();
  1087. if ($qrcode_url_res) {
  1088. $qrcode_url = json_decode($qrcode_url_res, true);
  1089. if ($qrcode_url['code'] == 1) {
  1090. //保存强关时的bid
  1091. if (isset($qrcode_url['data']) && !empty($qrcode_url['data'])) {
  1092. Redis::hset('force_subscribe_from_bid', $param['appid'] . '_' . $this->uid, $bid);
  1093. $send_order_id = Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0;
  1094. Redis::hset('force_subscribe_from_send_order_id', $param['appid'] . '_' . $this->uid, $send_order_id);
  1095. Log::info('uid is' . $this->uid . '--qrcode is:' . $qrcode_url['data']);
  1096. return $qrcode_url['data'];
  1097. }
  1098. }
  1099. }
  1100. } catch (\Exception $e) {
  1101. //\Log::info($e);
  1102. //Redis::sadd('force_subscribe_qrcode:error', $param['appid'] . '_' . time());
  1103. }
  1104. return false;
  1105. }
  1106. /**
  1107. * 获取订购记录
  1108. * @param $book_info
  1109. * @param $chapter_id
  1110. * @return bool
  1111. */
  1112. private function getOrderRecord($bid, $chapter_id)
  1113. {
  1114. //包年记录
  1115. $uid = $this->uid;
  1116. $res = YearOrderService::getRecord($uid);
  1117. if ($res) return true;
  1118. $res = null;
  1119. //单本订购记录
  1120. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  1121. if ($res) return true;
  1122. $res = null;
  1123. //章节订购记录
  1124. $chapterOrder = new ChapterOrderService();
  1125. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  1126. return false;
  1127. }
  1128. /**
  1129. * 计算价格
  1130. * @param $book_info
  1131. * @param $chapter_size
  1132. * @return float
  1133. */
  1134. private function getPrice($book_info, $chapter)
  1135. {
  1136. if ($book_info->charge_type == 'BOOK') {
  1137. $fee = $book_info->price * 100;
  1138. } else {
  1139. //$fee = ceil($chapter->size/100);
  1140. //$channel_fee = BookService::getChapterPrice()
  1141. $channel_fee = BookService::getChapterPrice($this->distribution_channel_id);
  1142. if ($channel_fee) {
  1143. $price_rate = $channel_fee / 100;
  1144. } else {
  1145. //$price_rate = 0.012;
  1146. $price_rate = env('DEFAULT_CHAPTER_PRICE', 0.015);
  1147. }
  1148. //Log::info($price_rate);
  1149. $fee = ceil($chapter->size * $price_rate);
  1150. /*if($this->isRaisePrice()){
  1151. $fee = round($chapter->size * 0.018);
  1152. }*/
  1153. }
  1154. //首个计费章节书币价格以35书币为基准,低于35提升至35,原价高于35书币,维持原价
  1155. if (isset($chapter->sequence) && isset($book_info->vip_seq)) {
  1156. if ($chapter->sequence == $book_info->vip_seq) {
  1157. if ($fee < 35) {
  1158. $fee = 35;
  1159. }
  1160. }
  1161. }
  1162. return $fee;
  1163. }
  1164. //降低付费用户书币价值
  1165. private function isRaisePrice(){
  1166. if(!$this->uid)
  1167. return false;
  1168. if(!$this->user)
  1169. return false;
  1170. if($this->user->balance < 100){
  1171. return false;
  1172. }
  1173. if(!in_array($this->distribution_channel_id,[211,123])){
  1174. return false;
  1175. }
  1176. if(DataAnalysisSelectUserService::getByUidAndType($this->uid,'RAISE')){
  1177. return true;
  1178. }
  1179. return false;
  1180. }
  1181. /**
  1182. * 获取强关章节数
  1183. * @param $book
  1184. * @return int
  1185. */
  1186. private function getSubscribeChapterNum($book)
  1187. {
  1188. $hour = date('G');
  1189. $send_order_info = null;
  1190. if(in_array($this->distribution_channel_id,[123,211]) && $this->send_order_id){
  1191. //开启落地页强关的需求有变,需要在链接实际推广1小时后就开启功能
  1192. $send_order_info = SendOrderService::getById($this->send_order_id);
  1193. if($send_order_info && $send_order_info->send_time && time()-strtotime($send_order_info->send_time) > 3600){
  1194. return 1;
  1195. }
  1196. }
  1197. if($this->send_order_id && $hour <=5 && in_array($this->send_order_id,[392216,392218,415200,415212,415163])){
  1198. return 1;
  1199. }
  1200. 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])){
  1201. return 1;
  1202. }
  1203. //派单强关
  1204. if ($this->send_order_id) {
  1205. $send_order_info || $send_order_info = SendOrderService::getById($this->send_order_id);
  1206. if ($send_order_info && $send_order_info->book_id == $book->bid && $send_order_info->subscribe_chapter_seq) {
  1207. return $send_order_info->subscribe_chapter_seq;
  1208. }
  1209. }
  1210. $subscribe = BookSubscribleChapterService::getSubcribleChapter($book->bid, $this->distribution_channel_id);
  1211. if ($subscribe) {
  1212. return (isset($subscribe->subscribe_chapter_id) && $subscribe->subscribe_chapter_id > 0) ? $subscribe->subscribe_chapter_id : $book->force_subscribe_chapter_seq;
  1213. }
  1214. return $book->force_subscribe_chapter_seq;
  1215. }
  1216. /**
  1217. * 判断访问的域名和数据库指定的域名是否一致,一致返回false ,不一致 true
  1218. * @param $domain 当前访问的doain
  1219. * @return bool
  1220. */
  1221. private function isVisitDomainEqualBookDomain($domain)
  1222. {
  1223. $book_domain = $this->book_info->promotion_domain;
  1224. if ($domain == 'zhuishuyun' && stripos($book_domain, 'leyuee') !== false) {
  1225. return false;
  1226. }
  1227. return stripos($book_domain, $domain) === false;
  1228. }
  1229. //获取对当前图书对应域名的链接
  1230. private function getCorrespondBookUrl()
  1231. {
  1232. $url = sprintf(
  1233. 'https://site%s.%s/reader?bid=%s&cid=%s&send_order_id=%s&fromtype=%s&stats=2&crm=%s',
  1234. $this->en_distribution_channel_id,
  1235. $this->book_info->promotion_domain,
  1236. $this->en_bid,
  1237. $this->cid,
  1238. $this->send_order_id,
  1239. $this->from_type,
  1240. $this->crm
  1241. );
  1242. return $url;
  1243. }
  1244. /**
  1245. * 用户添加标签
  1246. * @param $book_info
  1247. */
  1248. private function addTag($book_info)
  1249. {
  1250. if (!UserDeepReadTagService::isAddTag($this->uid, $book_info->bid)) {
  1251. try {
  1252. UserDeepReadTagService::addTag([
  1253. 'uid' => $this->uid,
  1254. 'bid' => $book_info->bid,
  1255. 'book_name' => $book_info->book_name,
  1256. 'category_id' => $book_info->category_id,
  1257. 'category_name' => $book_info->category_name,
  1258. 'sex_preference' => $book_info->channel_name ? $book_info->channel_name : '',
  1259. 'distribution_channel_id' => $this->distribution_channel_id ? $this->distribution_channel_id : '0',
  1260. 'send_order_id' => Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0,
  1261. ]);
  1262. } catch (\Exception $e) {
  1263. }
  1264. }
  1265. }
  1266. private function newUserRewardRead()
  1267. {
  1268. $count = ChapterOrderService::getUserRecordCount($this->uid);
  1269. return $count > 2;
  1270. }
  1271. private function isBookOrdered($bid)
  1272. {
  1273. $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $bid);
  1274. if ($chapter_order) return true;
  1275. $res = BookOrderService::getRecordByuidBid($this->uid, $bid);
  1276. if ($res) return true;
  1277. return false;
  1278. }
  1279. private function beforeForceSubAndBeforeVipUvAndPv($book, $book_force_seq, $chapter)
  1280. {
  1281. $send_order_id = Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 14;
  1282. $prev_sub_seq = 0;
  1283. if ($book_force_seq) {
  1284. $prev_sub_seq = $book_force_seq - 1;
  1285. }
  1286. $prev_vip_seq = 0;
  1287. $date = date('Y-m-d');
  1288. if (isset($book->vip_seq) && !empty($book->vip_seq)) {
  1289. $prev_vip_seq = $book->vip_seq - 1;
  1290. }
  1291. if ($chapter->sequence == $prev_vip_seq) {
  1292. $uv_key = 'book:' . $book->bid . ':beforevip:uv:date:' . $date . ':send_order_id:' . $send_order_id;
  1293. $pv_key = 'book:' . $book->bid . ':beforevip:pv:send_order_id:' . $send_order_id;
  1294. Redis::hincrby($pv_key, $date, 1);
  1295. Redis::hincrby($pv_key, 'total', 1);
  1296. Redis::sadd($uv_key, $this->uid);
  1297. Redis::sadd('beforevip' . $date, $book->bid . '-' . $send_order_id);
  1298. }
  1299. if ($chapter->sequence == $prev_sub_seq) {
  1300. $uv_key = 'book:' . $book->bid . ':beforeforcesub:uv:date:' . $date . ':send_order_id:' . $send_order_id;
  1301. $pv_key = 'book:' . $book->bid . ':beforeforcesub::pvsend_order_id:' . $send_order_id;
  1302. Redis::hincrby($pv_key, $date, 1);
  1303. Redis::hincrby($pv_key, 'total', 1);
  1304. Redis::sadd($uv_key, $this->uid);
  1305. Redis::sadd('beforeforcesub' . $date, $book->bid . '-' . $send_order_id);
  1306. }
  1307. }
  1308. private function readRecordOther()
  1309. {
  1310. $bid = $this->book_info->bid;
  1311. $cid = $this->cid;
  1312. $distribution_channel_id = $this->distribution_channel_id;
  1313. $send_order_id = $this->send_order_id;
  1314. $from = $this->from_type;
  1315. $sequence = $this->chapter->sequence;
  1316. ReadRecordStatsService::record($this->uid,$bid,$cid,$distribution_channel_id,$send_order_id,$from,$sequence);
  1317. /*$chapter = $this->chapter;
  1318. try {
  1319. $data = [];
  1320. $data['uid'] = $this->uid;
  1321. $data['bid'] = $chapter->bid;
  1322. $data['cid'] = $chapter->id;
  1323. $data['is_vip'] = $chapter->is_vip;
  1324. $data['seq'] = $chapter->sequence;
  1325. $data['created_at'] = date('Y-m-d H:i:s');
  1326. $data['updated_at'] = date('Y-m-d H:i:s');
  1327. DB::table('read_records')->insert($data);
  1328. } catch (\Exception $e) {
  1329. }*/
  1330. }
  1331. /**
  1332. * 下架图书 true:下架不能阅读了, false:可以正常阅读
  1333. * @return bool
  1334. */
  1335. private function isOffShelf()
  1336. {
  1337. $book_info = $this->book_info;
  1338. //针对米玩站点部分图书放开
  1339. $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];
  1340. if(in_array($this->distribution_channel_id,$miwan_sites) && in_array($book_info->bid,[1154,1232,1276,1295,1535,1542,1629,2430,4437,4730]))
  1341. {
  1342. return false;
  1343. }
  1344. if ($book_info->is_on_shelf == 0 || $book_info->is_on_shelf == 3) {
  1345. return true;
  1346. }
  1347. //屏蔽一本书1042 2626
  1348. if (in_array($book_info->bid, [1042, 2626]) && $this->distribution_channel_id == 123) {
  1349. return true;
  1350. }
  1351. if ($book_info->is_on_shelf == 4) {
  1352. //版权到期,有阅读记录还可以继续阅读,订购
  1353. /*if (!ReadRecordService::getRecordByUidBid($this->uid, $book_info->bid)) {
  1354. return true;
  1355. }*/
  1356. //版权到期,有包年可以继续阅读
  1357. $year_order = YearOrderService::getRecord($this->uid);
  1358. if($year_order){
  1359. return false;
  1360. }
  1361. //1155
  1362. //版权到期,有订购记录还可以继续阅读,订购
  1363. $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $book_info->bid);
  1364. if (!$chapter_order) return true;
  1365. if($book_info->bid == 1155){
  1366. $charge_info = Order::where('uid',$this->uid)
  1367. ->where('price','>=',redisEnv('OFF_SHELF_BOOK_LIMIT_CHARGE',0))
  1368. ->where('from_bid',$book_info->bid)
  1369. ->where('status','PAID')
  1370. ->first();
  1371. if(!$charge_info){
  1372. return true;
  1373. }
  1374. }
  1375. }
  1376. if($book_info->is_on_shelf == 10){
  1377. //Log::info('ddddddddddddddddddddddddddd');
  1378. $yq_book_user = BookUserService::selectUser($this->uid);
  1379. //Log::info($yq_book_user);
  1380. if($yq_book_user && $yq_book_user->type == 'ENABLE'){
  1381. return false;
  1382. }
  1383. return true;
  1384. }
  1385. return false;
  1386. }
  1387. //章节被删除的情况
  1388. private function chapterNotExists()
  1389. {
  1390. $chapter = $this->chapter;
  1391. $cid = $this->cid;
  1392. if ($chapter) return false;
  1393. //章节不存在的情况
  1394. $no_check = ChapterService::getChapterNameByIdNoCheck($cid);
  1395. $h5_scheme = 'https';
  1396. if (!$no_check) {
  1397. //章节真正不存在的情况
  1398. $url = $h5_scheme . '://' . _domain() . '/catalog?id=' . $this->en_bid;
  1399. return $url;
  1400. }
  1401. //章节被删除的情况
  1402. if ($no_check->next_cid) {
  1403. $url = $h5_scheme . '://' . _domain() . '/reader?bid=' . $this->en_bid . '&cid=' . $no_check->next_cid;
  1404. return $url;
  1405. }
  1406. //章节被删除的是最后一章的情况
  1407. $url = $h5_scheme . '://' . _domain() . '/catalog?id=' . $this->en_bid . '&sequence=' . $no_check->sequence;
  1408. return $url;
  1409. }
  1410. /**
  1411. * 深度阅读
  1412. * @param $chapter
  1413. */
  1414. private function deepReadRecord()
  1415. {
  1416. if (!env('ON_BOOK_UV_PV', false)) {
  1417. return;
  1418. }
  1419. $chapter = $this->chapter;
  1420. $key = 'book:' . $chapter->bid . ':deepread:uv';
  1421. $chapter_key = 'book:' . $chapter->bid . ':chapterseq:' . $chapter->sequence;
  1422. if (Redis::scard($key) > 50000) {
  1423. if (Redis::Sismember($key, $this->uid)) {
  1424. Redis::sadd($chapter_key, $this->uid);
  1425. }
  1426. } else {
  1427. Redis::sadd($key, $this->uid);
  1428. Redis::sadd($chapter_key, $this->uid);
  1429. }
  1430. }
  1431. /**
  1432. * 强关类型
  1433. * @param $appid
  1434. * @return array
  1435. */
  1436. private function getSubscribeType($appid, $bid)
  1437. {
  1438. $send_order_id = Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0;
  1439. $sub_setting = ChannelSubscribeSettingService::getSubscribeTypeByAppId($appid);
  1440. if (!$sub_setting) return ['type' => [1], 'url' => ''];
  1441. $types = explode(',', $sub_setting->force_subscribe_type);
  1442. $url = '';
  1443. if (in_array(3, $types) && !empty($sub_setting->resource_url)) {
  1444. $h5_scheme = env('H5_SCHEME', 'https');
  1445. $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;
  1446. }
  1447. return ['type' => $types, 'url' => $url];
  1448. }
  1449. private function showChapterPrice($bid, $charge_type)
  1450. {
  1451. if ($charge_type == 'BOOK') {
  1452. return false;
  1453. }
  1454. $show_chapter_price_bid = env('SHOW_CHAPTER_PRICE_BID', 'all');
  1455. if ($show_chapter_price_bid != 'all') {
  1456. $show_chapter_price_bid_array = explode(',', $show_chapter_price_bid);
  1457. if (!in_array($bid, $show_chapter_price_bid_array)) {
  1458. return false;
  1459. }
  1460. }
  1461. $show_chapter_price_channel = env('SHOW_CHAPTER_PRICE_CHANNEL', '');
  1462. if (!$show_chapter_price_channel) {
  1463. return false;
  1464. }
  1465. if ($show_chapter_price_channel == 'all') {
  1466. return true;
  1467. }
  1468. $show_chapter_price_channel_array = explode(',', $show_chapter_price_channel);
  1469. if (in_array($this->distribution_channel_id, $show_chapter_price_channel_array)) {
  1470. return true;
  1471. }
  1472. return false;
  1473. }
  1474. /**
  1475. * 阅读统计
  1476. */
  1477. private function chapterStats()
  1478. {
  1479. $chapter = $this->chapter;
  1480. $bid = $this->book_info->bid;
  1481. //派单精确统计
  1482. if ($chapter->sequence <= 30) {
  1483. //$this->sendOrderChapterUv($bid, $chapter->sequence);
  1484. }
  1485. //智能推送 测书统计章节uv
  1486. if ($chapter->sequence <= 60) {
  1487. //测书数据 由30改到60 2018-11-26 18:00
  1488. $this->smartPushTestBookChapterUv($bid, $chapter->sequence);
  1489. }
  1490. //智能推送 测书统计第二章 uv
  1491. if ($chapter->sequence == 2) {
  1492. $this->smartPushTestBookSecondChapterUv($bid);
  1493. }
  1494. // 统计前5张uv 会放在book_five_chapter_uv表中,脚本在 \App\Console\Commands\BookTest::data1里面
  1495. // 作用早忘记了
  1496. $field = date('Y-m-d');
  1497. /*
  1498. if ($chapter->sequence <= 5 && $this->send_order_id) {
  1499. try {
  1500. Redis::sadd(sprintf('book_before_five_uv_%s_%s_%s', $field, $bid, $chapter->sequence), $this->uid);
  1501. Redis::sadd('book_before_five_uvs', sprintf('%s_%s_%s', $field, $bid, $chapter->sequence));
  1502. } catch (\Exception $e) {
  1503. }
  1504. }*/
  1505. //统计点击率
  1506. $key = 'book_click_num_bid_' . $bid;
  1507. try {
  1508. Redis::HINCRBY($key, $field, 1);
  1509. } catch (\Exception $e) {
  1510. }
  1511. //阅读页面pvuv
  1512. $this->recordReaderUvAndPv();
  1513. }
  1514. private function smartPushTestBook($bid, $balance)
  1515. {
  1516. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1517. if ($smart_bid && in_array($bid, $smart_bid)) {
  1518. $field = sprintf('%s_amount', $bid);
  1519. try {
  1520. Redis::HINCRBY('smart_push_test_book', $field, $balance);
  1521. Redis::sadd('smart_push_test_book_user_count' . $bid, $this->uid);
  1522. } catch (\Exception $e) {
  1523. }
  1524. }
  1525. }
  1526. private function smartPushTestBookSecondChapterUv($bid)
  1527. {
  1528. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1529. if ($smart_bid && in_array($bid, $smart_bid)) {
  1530. try {
  1531. Redis::sadd('smart_push_test_book_second_uv' . $bid, $this->uid);
  1532. } catch (\Exception $e) {
  1533. }
  1534. }
  1535. }
  1536. private function smartPushTestBookChapterUv($bid, $sequence)
  1537. {
  1538. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1539. if ($smart_bid && in_array($bid, $smart_bid)) {
  1540. try {
  1541. $key = 'smartPushTestBookChapterUv:bid:%s:seq:%s';
  1542. Redis::sadd(sprintf($key, $bid, $sequence), $this->uid);
  1543. } catch (\Exception $e) {
  1544. }
  1545. }
  1546. }
  1547. /**
  1548. * 测书 智能推送统计
  1549. * @param $bid
  1550. */
  1551. private function smartPushTestBookPayPageUv($bid)
  1552. {
  1553. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1554. if ($smart_bid && in_array($bid, $smart_bid)) {
  1555. try {
  1556. $key = 'smartPushTestBookPayPageUv:bid:%s';
  1557. Redis::sadd(sprintf($key, $bid), $this->uid);
  1558. } catch (\Exception $e) {
  1559. }
  1560. }
  1561. }
  1562. /**
  1563. * 派单精确统计--章节
  1564. * @param $bid
  1565. * @param $sequence
  1566. */
  1567. private function sendOrderChapterUv($bid, $sequence)
  1568. {
  1569. if ($this->send_order_id) {
  1570. $send_order_bid = Redis::hget('sendOrderIdToBid', $this->send_order_id);
  1571. if (!$send_order_bid) {
  1572. $send_order_info = SendOrderService::getById($this->send_order_id);
  1573. if ($send_order_info) {
  1574. $send_order_bid = $send_order_info->book_id;
  1575. Redis::hset('sendOrderIdToBid', $this->send_order_id, $send_order_bid . '-' . time());
  1576. }
  1577. } else {
  1578. $send_order_bid_arr = explode('-', $send_order_bid);
  1579. $send_order_bid = $send_order_bid_arr[0];
  1580. }
  1581. if ($send_order_bid && $send_order_bid == $bid) {
  1582. try {
  1583. $key = 'accurateSendOrderChapterUv:bid:%s:seq:%s:sendorderid:' . $this->send_order_id;
  1584. Redis::sadd(sprintf($key, $bid, $sequence), $this->uid);
  1585. } catch (\Exception $e) {
  1586. }
  1587. }
  1588. }
  1589. }
  1590. /**
  1591. * 派单精确统计--强关页面uv
  1592. * @param $bid
  1593. */
  1594. private function sendOrderSubscribePageUv($bid)
  1595. {
  1596. if ($this->send_order_id) {
  1597. $send_order_bid = Redis::hget('sendOrderIdToBid', $this->send_order_id);
  1598. if (!$send_order_bid) {
  1599. $send_order_info = SendOrderService::getById($this->send_order_id);
  1600. if ($send_order_info) {
  1601. $send_order_bid = $send_order_info->book_id;
  1602. Redis::hset('sendOrderIdToBid', $this->send_order_id, $send_order_bid . '-' . time());
  1603. }
  1604. } else {
  1605. $send_order_bid_arr = explode('-', $send_order_bid);
  1606. $send_order_bid = $send_order_bid_arr[0];
  1607. }
  1608. if ($send_order_bid && $send_order_bid == $bid) {
  1609. try {
  1610. $key = 'accurateSendOrderSubscribePageUv:bid:%s:sendorderid:' . $this->send_order_id;
  1611. Redis::sadd(sprintf($key, $bid), $this->uid);
  1612. } catch (\Exception $e) {
  1613. }
  1614. }
  1615. }
  1616. }
  1617. /**
  1618. * 派单精确统计--支付页面uv
  1619. * @param $bid
  1620. */
  1621. private function sendOrderPayPageUv($bid)
  1622. {
  1623. if ($this->send_order_id) {
  1624. $send_order_bid = Redis::hget('sendOrderIdToBid', $this->send_order_id);
  1625. if (!$send_order_bid) {
  1626. $send_order_info = SendOrderService::getById($this->send_order_id);
  1627. if ($send_order_info) {
  1628. $send_order_bid = $send_order_info->book_id;
  1629. Redis::hset('sendOrderIdToBid', $this->send_order_id, $send_order_bid . '-' . time());
  1630. }
  1631. } else {
  1632. $send_order_bid_arr = explode('-', $send_order_bid);
  1633. $send_order_bid = $send_order_bid_arr[0];
  1634. }
  1635. if ($send_order_bid && $send_order_bid == $bid) {
  1636. try {
  1637. $key = 'accurateSendOrderPayPageUv:bid:%s:send_order_id:' . $this->send_order_id;
  1638. Redis::sadd(sprintf($key, $bid), $this->uid);
  1639. } catch (\Exception $e) {
  1640. }
  1641. }
  1642. }
  1643. }
  1644. private function recordReaderUvAndPv()
  1645. {
  1646. WapVisitStatService::recordReaderUvAndPv($this->uid, $this->distribution_channel_id);
  1647. }
  1648. //用户是否在广告样本中
  1649. private function isUserInAdSample(){
  1650. /*$res = DataAnalysisSelectUserService::getByUidAndType($this->uid,'AD_V2');
  1651. if($res){
  1652. return true;
  1653. }else{
  1654. return false;
  1655. }*/
  1656. $is_ad_user = Cookie::get('cpc_ad_status');
  1657. if($is_ad_user && $is_ad_user == 'show') return true;
  1658. return false;
  1659. }
  1660. //阅读页面底部是否显示广告
  1661. private function isShowAd(){
  1662. $ad_status = env('AD_STATUS',1);
  1663. if(!$ad_status){
  1664. return ;
  1665. }
  1666. if(!$this->is_had_subscribe){
  1667. return ;
  1668. }
  1669. //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])){
  1670. $cpc_status = Redis::hget('channel:setting:'.$this->distribution_channel_id,'cpc_status');
  1671. if($cpc_status === '0'){
  1672. return ;
  1673. }
  1674. //按章付费才显示
  1675. if($this->book_info->charge_type != 'CHAPTER'){
  1676. return ;
  1677. }
  1678. //用户是否在广告样本中
  1679. //if(!$this->isUserInAdSample()) return ;
  1680. $is_ad_user = Cookie::get('cpc_ad_status');
  1681. if(!$is_ad_user || $is_ad_user != 'show') return ;
  1682. $cid = 0;
  1683. if($this->chapter && $this->chapter->next_cid){
  1684. $cid = $this->chapter->next_cid;
  1685. }
  1686. if(!$cid){
  1687. return ;
  1688. }
  1689. //付费用户没有广告
  1690. if(OrderService::isPaidUser($this->uid)) return ;
  1691. //如果下一张已经付费,广告不显示
  1692. /*$exists = AdVisitStatService::getByUid($this->uid);
  1693. if(!$exists){
  1694. if($this->uid %2 == 0){
  1695. DataAnalysisSelectUserService::create($this->uid,$this->distribution_channel_id,'AD_TEST_3',-1,'unshow');
  1696. return ;
  1697. }else{
  1698. DataAnalysisSelectUserService::create($this->uid,$this->distribution_channel_id,'AD_TEST_3',-1,'show');
  1699. }
  1700. }*/
  1701. //广告都显示
  1702. $this->chapter->is_visiable_ad = 1;
  1703. AdVisitStatService::create($this->uid,$this->book_info->bid,$this->cid,'EXPOSURE');
  1704. if($this->getOrderRecord($this->book_info->bid, $cid)){
  1705. return ;
  1706. }
  1707. $next_chapter = ChapterService::getChapterNameById($cid, $this->book_info->bid);
  1708. //下一章事vip的话 才显示广告
  1709. if(!$next_chapter || !$next_chapter->is_vip){
  1710. return ;
  1711. }
  1712. $price = $this->getPrice($this->book_info,$next_chapter);
  1713. $flag = $price > $this->user->balance;
  1714. if( $flag ){
  1715. $this->chapter->is_show_ad = 1;
  1716. AdVisitStatService::create($this->uid,$this->book_info->bid,$this->cid,'CAN_CLICK');
  1717. }
  1718. }
  1719. public function getWechatJsConfig(Request $request)
  1720. {
  1721. //try {
  1722. // $chapter->is_show_share_button = 1;
  1723. $bid = $request->input('bid', '');
  1724. $cid = $request->input('cid', '');
  1725. $url = $request->input('url', '');
  1726. $url = urldecode($url);
  1727. \Log::info('url:' . ($this->uid) . $url);
  1728. if (empty($bid) || empty($cid) || empty($url)) {
  1729. return response()->error('PARAM_EMPTY');
  1730. }
  1731. $share_url = 'https://'._domain().'/detail?fromtype=readershare&id='.$bid.'&fromflag='.($this->uid).'&fromcid='.$cid;
  1732. //$js_config = ChapterShareWechatConfigService::getConfig($url);
  1733. $app_hash = array(
  1734. 4025=>['appid'=>'wx80b618c9dcc940f7','appsecret'=>'255a2de8db51e4084b5e9087d26d4ecb'],
  1735. 4147=>['appid'=>'wx2a99699cd9572812','appsecret'=>'0e0d57f94eeba50abb3af6883df05a5c'],
  1736. 691=>['appid'=>'wx5cefcd251dc40693','appsecret'=>'755f64c85173dad21f20870e38f59d8a'],
  1737. 123=>['appid'=>'wx983808610aa9dcc5','appsecret'=>'91002999590b79f3c7be35fd73c04049'],
  1738. 211=>['appid'=>'wx39338e374cc2055b','appsecret'=>'1a4c2039be271b3811182be5bb53eb34'],
  1739. );
  1740. $appId = $app_hash[$this->distribution_channel_id]['appid'];
  1741. $appSecret = $app_hash[$this->distribution_channel_id]['appsecret'];
  1742. $js_config = (new ChapterShareWechatConfigService($appId,$appSecret))->getSignPackage($url);
  1743. /*$appid = DB::table('official_accounts')->where('distribution_channel_id',$this->distribution_channel_id)->first();//(['distribution_channel_id'=>$this->distribution_channel_id]);
  1744. $appid = $appid->appid;
  1745. //\Log::info('get_appid:'.($appid));
  1746. $js_config = (new ChapterShareWechatConfigService($appid))->getSignPackage($url);
  1747. //$chapter->js_config = $js_config;
  1748. //$chapter->share_url = $share_url;
  1749. $book = BookService::getBookById(Hashids::decode($bid)[0]);
  1750. $first_cid = $book->first_cid;
  1751. $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;
  1752. /*if (in_array($this->uid, explode(',', env('TEST_SHARE_UIDS')))) {
  1753. $share_url = 'https://site5017zqeyzwgyx9n8.zhuishuyun.com/reader?fromtype=readershare&bid=' . $bid . '&fromflag=' . ($this->uid) . '&fromcid=' . $cid;
  1754. }*/
  1755. $book = BookService::getBookById(Hashids::decode($bid)[0]);
  1756. $first_cid = $book->first_cid;
  1757. $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;
  1758. if (in_array($this->distribution_channel_id, [2, 14])) {
  1759. $js_config = ChapterShareWechatConfigService::getConfig($url);
  1760. $share_url = 'https://site' . ($this->distribution_channel_id) . '.' . env('SHARE_DOMAIN') . '/reader?fromtype=readershare&bid=' . $bid . '&cid=' . ($first_cid) . '&fromflag=' . ($this->uid) . '&fromcid=' . $cid;
  1761. }
  1762. $js_config['share_url'] = $share_url;
  1763. /*} catch (\Exception $e) {
  1764. \Log::error($e->getMessage());
  1765. }*/
  1766. //\Log::info($js_config);
  1767. return response()->success($js_config);
  1768. }
  1769. }