ChapterController.php 69 KB

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