ChapterController.php 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908
  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_subsribe_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_subsribe_status = 0;
  1033. if($send_order_subsribe_status &&
  1034. $send_order_info &&
  1035. $send_order_info->promotion_type == 'EXTERNAL'
  1036. ){
  1037. $subscribe_info = ForceSubscribeUserIService::subscribes($this->uid,$this->distribution_channel_id,$this->send_order_id);
  1038. $this->need_subscribe_appid = $subscribe_info['appid'];
  1039. $this->need_subscribe_name = $subscribe_info['name'];
  1040. return $subscribe_info['is_subscribed'];
  1041. }
  1042. if($send_order_info &&
  1043. $send_order_info->promotion_type == 'INTERNAL' &&
  1044. strtotime($send_order_info->created_at) > 1555488000){
  1045. return true;
  1046. }
  1047. $res = ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $this->uid]);
  1048. if ($res) {
  1049. $this->force_subscribe_time = isset($res->created_at)?$res->created_at:null;
  1050. return true;
  1051. }
  1052. return false;
  1053. }
  1054. /**
  1055. * 获取公众号信息
  1056. * @param $distribution_channel_id
  1057. */
  1058. protected function getOfficialAccount()
  1059. {
  1060. $distribution_channel_id = $this->distribution_channel_id;
  1061. $res = OfficialAccountService::canUseOfficialAccountByChannelId(compact('distribution_channel_id'));
  1062. if (isset($res->nickname) && !empty($res->nickname)) {
  1063. Cookie::queue('force_subscribe_name', $res->nickname, env('U_COOKIE_EXPIRE'));
  1064. }
  1065. return $res;
  1066. }
  1067. /**
  1068. * 获取二维码
  1069. * @param $param
  1070. * @return bool
  1071. */
  1072. private function getRcodeInfo($param, $bid)
  1073. {
  1074. //return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQGI8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyZEZ0T0ZoSHljM2kxOTUwQU5zY08AAgRF8-xbAwQAjScA";
  1075. $param_need = [
  1076. 'gzh_app_id' => $param['appid'],
  1077. 'scene_id' => $this->uid,
  1078. 'timestamp' => time(),
  1079. ];
  1080. $param_need['sign'] = $this->getSign($param_need);
  1081. $client = new Client(['timeout' => 3.0,]);
  1082. try {
  1083. Log::info('get qrcode url is:' . 'https://zsyauth.aizhuishu.com/api/get_qrcode_url?' . http_build_query($param_need));
  1084. $qrcode_url_res = $client->request('get', 'https://zsyauth.aizhuishu.com/api/get_qrcode_url?' . http_build_query($param_need))->getBody()->getContents();
  1085. if ($qrcode_url_res) {
  1086. $qrcode_url = json_decode($qrcode_url_res, true);
  1087. if ($qrcode_url['code'] == 1) {
  1088. //保存强关时的bid
  1089. if (isset($qrcode_url['data']) && !empty($qrcode_url['data'])) {
  1090. Redis::hset('force_subscribe_from_bid', $param['appid'] . '_' . $this->uid, $bid);
  1091. $send_order_id = Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0;
  1092. Redis::hset('force_subscribe_from_send_order_id', $param['appid'] . '_' . $this->uid, $send_order_id);
  1093. Log::info('uid is' . $this->uid . '--qrcode is:' . $qrcode_url['data']);
  1094. return $qrcode_url['data'];
  1095. }
  1096. }
  1097. }
  1098. } catch (\Exception $e) {
  1099. //\Log::info($e);
  1100. //Redis::sadd('force_subscribe_qrcode:error', $param['appid'] . '_' . time());
  1101. }
  1102. return false;
  1103. }
  1104. /**
  1105. * 获取订购记录
  1106. * @param $book_info
  1107. * @param $chapter_id
  1108. * @return bool
  1109. */
  1110. private function getOrderRecord($bid, $chapter_id)
  1111. {
  1112. //包年记录
  1113. $uid = $this->uid;
  1114. $res = YearOrderService::getRecord($uid);
  1115. if ($res) return true;
  1116. $res = null;
  1117. //单本订购记录
  1118. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  1119. if ($res) return true;
  1120. $res = null;
  1121. //章节订购记录
  1122. $chapterOrder = new ChapterOrderService();
  1123. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  1124. return false;
  1125. }
  1126. /**
  1127. * 计算价格
  1128. * @param $book_info
  1129. * @param $chapter_size
  1130. * @return float
  1131. */
  1132. private function getPrice($book_info, $chapter)
  1133. {
  1134. if ($book_info->charge_type == 'BOOK') {
  1135. $fee = $book_info->price * 100;
  1136. } else {
  1137. //$fee = ceil($chapter->size/100);
  1138. //$channel_fee = BookService::getChapterPrice()
  1139. $channel_fee = BookService::getChapterPrice($this->distribution_channel_id);
  1140. if ($channel_fee) {
  1141. $price_rate = $channel_fee / 100;
  1142. } else {
  1143. //$price_rate = 0.012;
  1144. $price_rate = env('DEFAULT_CHAPTER_PRICE', 0.015);
  1145. }
  1146. //Log::info($price_rate);
  1147. $fee = ceil($chapter->size * $price_rate);
  1148. /*if($this->isRaisePrice()){
  1149. $fee = round($chapter->size * 0.018);
  1150. }*/
  1151. }
  1152. //首个计费章节书币价格以35书币为基准,低于35提升至35,原价高于35书币,维持原价
  1153. if (isset($chapter->sequence) && isset($book_info->vip_seq)) {
  1154. if ($chapter->sequence == $book_info->vip_seq) {
  1155. if ($fee < 35) {
  1156. $fee = 35;
  1157. }
  1158. }
  1159. }
  1160. return $fee;
  1161. }
  1162. //降低付费用户书币价值
  1163. private function isRaisePrice(){
  1164. if(!$this->uid)
  1165. return false;
  1166. if(!$this->user)
  1167. return false;
  1168. if($this->user->balance < 100){
  1169. return false;
  1170. }
  1171. if(!in_array($this->distribution_channel_id,[211,123])){
  1172. return false;
  1173. }
  1174. if(DataAnalysisSelectUserService::getByUidAndType($this->uid,'RAISE')){
  1175. return true;
  1176. }
  1177. return false;
  1178. }
  1179. /**
  1180. * 获取强关章节数
  1181. * @param $book
  1182. * @return int
  1183. */
  1184. private function getSubscribeChapterNum($book)
  1185. {
  1186. $hour = date('G');
  1187. $send_order_info = null;
  1188. if(in_array($this->distribution_channel_id,[123,211]) && $this->send_order_id){
  1189. //开启落地页强关的需求有变,需要在链接实际推广1小时后就开启功能
  1190. $send_order_info = SendOrderService::getById($this->send_order_id);
  1191. if($send_order_info && $send_order_info->send_time && time()-strtotime($send_order_info->send_time) > 3600){
  1192. return 1;
  1193. }
  1194. }
  1195. if($this->send_order_id && $hour <=5 && in_array($this->send_order_id,[392216,392218,415200,415212,415163])){
  1196. return 1;
  1197. }
  1198. 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])){
  1199. return 1;
  1200. }
  1201. //派单强关
  1202. if ($this->send_order_id) {
  1203. $send_order_info || $send_order_info = SendOrderService::getById($this->send_order_id);
  1204. if ($send_order_info && $send_order_info->book_id == $book->bid && $send_order_info->subscribe_chapter_seq) {
  1205. return $send_order_info->subscribe_chapter_seq;
  1206. }
  1207. }
  1208. $subscribe = BookSubscribleChapterService::getSubcribleChapter($book->bid, $this->distribution_channel_id);
  1209. if ($subscribe) {
  1210. return (isset($subscribe->subscribe_chapter_id) && $subscribe->subscribe_chapter_id > 0) ? $subscribe->subscribe_chapter_id : $book->force_subscribe_chapter_seq;
  1211. }
  1212. return $book->force_subscribe_chapter_seq;
  1213. }
  1214. /**
  1215. * 判断访问的域名和数据库指定的域名是否一致,一致返回false ,不一致 true
  1216. * @param $domain 当前访问的doain
  1217. * @return bool
  1218. */
  1219. private function isVisitDomainEqualBookDomain($domain)
  1220. {
  1221. $book_domain = $this->book_info->promotion_domain;
  1222. if ($domain == 'zhuishuyun' && stripos($book_domain, 'leyuee') !== false) {
  1223. return false;
  1224. }
  1225. return stripos($book_domain, $domain) === false;
  1226. }
  1227. //获取对当前图书对应域名的链接
  1228. private function getCorrespondBookUrl()
  1229. {
  1230. $url = sprintf(
  1231. 'https://site%s.%s/reader?bid=%s&cid=%s&send_order_id=%s&fromtype=%s&stats=2&crm=%s',
  1232. $this->en_distribution_channel_id,
  1233. $this->book_info->promotion_domain,
  1234. $this->en_bid,
  1235. $this->cid,
  1236. $this->send_order_id,
  1237. $this->from_type,
  1238. $this->crm
  1239. );
  1240. return $url;
  1241. }
  1242. /**
  1243. * 用户添加标签
  1244. * @param $book_info
  1245. */
  1246. private function addTag($book_info)
  1247. {
  1248. if (!UserDeepReadTagService::isAddTag($this->uid, $book_info->bid)) {
  1249. try {
  1250. UserDeepReadTagService::addTag([
  1251. 'uid' => $this->uid,
  1252. 'bid' => $book_info->bid,
  1253. 'book_name' => $book_info->book_name,
  1254. 'category_id' => $book_info->category_id,
  1255. 'category_name' => $book_info->category_name,
  1256. 'sex_preference' => $book_info->channel_name ? $book_info->channel_name : '',
  1257. 'distribution_channel_id' => $this->distribution_channel_id ? $this->distribution_channel_id : '0',
  1258. 'send_order_id' => Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0,
  1259. ]);
  1260. } catch (\Exception $e) {
  1261. }
  1262. }
  1263. }
  1264. private function newUserRewardRead()
  1265. {
  1266. $count = ChapterOrderService::getUserRecordCount($this->uid);
  1267. return $count > 2;
  1268. }
  1269. private function isBookOrdered($bid)
  1270. {
  1271. $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $bid);
  1272. if ($chapter_order) return true;
  1273. $res = BookOrderService::getRecordByuidBid($this->uid, $bid);
  1274. if ($res) return true;
  1275. return false;
  1276. }
  1277. private function beforeForceSubAndBeforeVipUvAndPv($book, $book_force_seq, $chapter)
  1278. {
  1279. $send_order_id = Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 14;
  1280. $prev_sub_seq = 0;
  1281. if ($book_force_seq) {
  1282. $prev_sub_seq = $book_force_seq - 1;
  1283. }
  1284. $prev_vip_seq = 0;
  1285. $date = date('Y-m-d');
  1286. if (isset($book->vip_seq) && !empty($book->vip_seq)) {
  1287. $prev_vip_seq = $book->vip_seq - 1;
  1288. }
  1289. if ($chapter->sequence == $prev_vip_seq) {
  1290. $uv_key = 'book:' . $book->bid . ':beforevip:uv:date:' . $date . ':send_order_id:' . $send_order_id;
  1291. $pv_key = 'book:' . $book->bid . ':beforevip:pv:send_order_id:' . $send_order_id;
  1292. Redis::hincrby($pv_key, $date, 1);
  1293. Redis::hincrby($pv_key, 'total', 1);
  1294. Redis::sadd($uv_key, $this->uid);
  1295. Redis::sadd('beforevip' . $date, $book->bid . '-' . $send_order_id);
  1296. }
  1297. if ($chapter->sequence == $prev_sub_seq) {
  1298. $uv_key = 'book:' . $book->bid . ':beforeforcesub:uv:date:' . $date . ':send_order_id:' . $send_order_id;
  1299. $pv_key = 'book:' . $book->bid . ':beforeforcesub::pvsend_order_id:' . $send_order_id;
  1300. Redis::hincrby($pv_key, $date, 1);
  1301. Redis::hincrby($pv_key, 'total', 1);
  1302. Redis::sadd($uv_key, $this->uid);
  1303. Redis::sadd('beforeforcesub' . $date, $book->bid . '-' . $send_order_id);
  1304. }
  1305. }
  1306. private function readRecordOther()
  1307. {
  1308. $bid = $this->book_info->bid;
  1309. $cid = $this->cid;
  1310. $distribution_channel_id = $this->distribution_channel_id;
  1311. $send_order_id = $this->send_order_id;
  1312. $from = $this->from_type;
  1313. $sequence = $this->chapter->sequence;
  1314. ReadRecordStatsService::record($this->uid,$bid,$cid,$distribution_channel_id,$send_order_id,$from,$sequence);
  1315. /*$chapter = $this->chapter;
  1316. try {
  1317. $data = [];
  1318. $data['uid'] = $this->uid;
  1319. $data['bid'] = $chapter->bid;
  1320. $data['cid'] = $chapter->id;
  1321. $data['is_vip'] = $chapter->is_vip;
  1322. $data['seq'] = $chapter->sequence;
  1323. $data['created_at'] = date('Y-m-d H:i:s');
  1324. $data['updated_at'] = date('Y-m-d H:i:s');
  1325. DB::table('read_records')->insert($data);
  1326. } catch (\Exception $e) {
  1327. }*/
  1328. }
  1329. /**
  1330. * 下架图书 true:下架不能阅读了, false:可以正常阅读
  1331. * @return bool
  1332. */
  1333. private function isOffShelf()
  1334. {
  1335. $book_info = $this->book_info;
  1336. //针对米玩站点部分图书放开
  1337. $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];
  1338. if(in_array($this->distribution_channel_id,$miwan_sites) && in_array($book_info->bid,[1154,1232,1276,1295,1535,1542,1629,2430,4437,4730]))
  1339. {
  1340. return false;
  1341. }
  1342. if ($book_info->is_on_shelf == 0 || $book_info->is_on_shelf == 3) {
  1343. return true;
  1344. }
  1345. //屏蔽一本书1042 2626
  1346. if (in_array($book_info->bid, [1042, 2626]) && $this->distribution_channel_id == 123) {
  1347. return true;
  1348. }
  1349. if ($book_info->is_on_shelf == 4) {
  1350. //版权到期,有阅读记录还可以继续阅读,订购
  1351. /*if (!ReadRecordService::getRecordByUidBid($this->uid, $book_info->bid)) {
  1352. return true;
  1353. }*/
  1354. //版权到期,有包年可以继续阅读
  1355. $year_order = YearOrderService::getRecord($this->uid);
  1356. if($year_order){
  1357. return false;
  1358. }
  1359. //1155
  1360. //版权到期,有订购记录还可以继续阅读,订购
  1361. $chapter_order = ChapterOrderService::checkBookIsOrdered($this->uid, $book_info->bid);
  1362. if (!$chapter_order) return true;
  1363. if($book_info->bid == 1155){
  1364. $charge_info = Order::where('uid',$this->uid)
  1365. ->where('price','>=',redisEnv('OFF_SHELF_BOOK_LIMIT_CHARGE',0))
  1366. ->where('from_bid',$book_info->bid)
  1367. ->where('status','PAID')
  1368. ->first();
  1369. if(!$charge_info){
  1370. return true;
  1371. }
  1372. }
  1373. }
  1374. if($book_info->is_on_shelf == 10){
  1375. //Log::info('ddddddddddddddddddddddddddd');
  1376. $yq_book_user = BookUserService::selectUser($this->uid);
  1377. //Log::info($yq_book_user);
  1378. if($yq_book_user && $yq_book_user->type == 'ENABLE'){
  1379. return false;
  1380. }
  1381. return true;
  1382. }
  1383. return false;
  1384. }
  1385. //章节被删除的情况
  1386. private function chapterNotExists()
  1387. {
  1388. $chapter = $this->chapter;
  1389. $cid = $this->cid;
  1390. if ($chapter) return false;
  1391. //章节不存在的情况
  1392. $no_check = ChapterService::getChapterNameByIdNoCheck($cid);
  1393. $h5_scheme = 'https';
  1394. if (!$no_check) {
  1395. //章节真正不存在的情况
  1396. $url = $h5_scheme . '://' . _domain() . '/catalog?id=' . $this->en_bid;
  1397. return $url;
  1398. }
  1399. //章节被删除的情况
  1400. if ($no_check->next_cid) {
  1401. $url = $h5_scheme . '://' . _domain() . '/reader?bid=' . $this->en_bid . '&cid=' . $no_check->next_cid;
  1402. return $url;
  1403. }
  1404. //章节被删除的是最后一章的情况
  1405. $url = $h5_scheme . '://' . _domain() . '/catalog?id=' . $this->en_bid . '&sequence=' . $no_check->sequence;
  1406. return $url;
  1407. }
  1408. /**
  1409. * 深度阅读
  1410. * @param $chapter
  1411. */
  1412. private function deepReadRecord()
  1413. {
  1414. if (!env('ON_BOOK_UV_PV', false)) {
  1415. return;
  1416. }
  1417. $chapter = $this->chapter;
  1418. $key = 'book:' . $chapter->bid . ':deepread:uv';
  1419. $chapter_key = 'book:' . $chapter->bid . ':chapterseq:' . $chapter->sequence;
  1420. if (Redis::scard($key) > 50000) {
  1421. if (Redis::Sismember($key, $this->uid)) {
  1422. Redis::sadd($chapter_key, $this->uid);
  1423. }
  1424. } else {
  1425. Redis::sadd($key, $this->uid);
  1426. Redis::sadd($chapter_key, $this->uid);
  1427. }
  1428. }
  1429. /**
  1430. * 强关类型
  1431. * @param $appid
  1432. * @return array
  1433. */
  1434. private function getSubscribeType($appid, $bid)
  1435. {
  1436. $send_order_id = Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0;
  1437. $sub_setting = ChannelSubscribeSettingService::getSubscribeTypeByAppId($appid);
  1438. if (!$sub_setting) return ['type' => [1], 'url' => ''];
  1439. $types = explode(',', $sub_setting->force_subscribe_type);
  1440. $url = '';
  1441. if (in_array(3, $types) && !empty($sub_setting->resource_url)) {
  1442. $h5_scheme = env('H5_SCHEME', 'https');
  1443. $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;
  1444. }
  1445. return ['type' => $types, 'url' => $url];
  1446. }
  1447. private function showChapterPrice($bid, $charge_type)
  1448. {
  1449. if ($charge_type == 'BOOK') {
  1450. return false;
  1451. }
  1452. $show_chapter_price_bid = env('SHOW_CHAPTER_PRICE_BID', 'all');
  1453. if ($show_chapter_price_bid != 'all') {
  1454. $show_chapter_price_bid_array = explode(',', $show_chapter_price_bid);
  1455. if (!in_array($bid, $show_chapter_price_bid_array)) {
  1456. return false;
  1457. }
  1458. }
  1459. $show_chapter_price_channel = env('SHOW_CHAPTER_PRICE_CHANNEL', '');
  1460. if (!$show_chapter_price_channel) {
  1461. return false;
  1462. }
  1463. if ($show_chapter_price_channel == 'all') {
  1464. return true;
  1465. }
  1466. $show_chapter_price_channel_array = explode(',', $show_chapter_price_channel);
  1467. if (in_array($this->distribution_channel_id, $show_chapter_price_channel_array)) {
  1468. return true;
  1469. }
  1470. return false;
  1471. }
  1472. /**
  1473. * 阅读统计
  1474. */
  1475. private function chapterStats()
  1476. {
  1477. $chapter = $this->chapter;
  1478. $bid = $this->book_info->bid;
  1479. //派单精确统计
  1480. if ($chapter->sequence <= 30) {
  1481. //$this->sendOrderChapterUv($bid, $chapter->sequence);
  1482. }
  1483. //智能推送 测书统计章节uv
  1484. if ($chapter->sequence <= 60) {
  1485. //测书数据 由30改到60 2018-11-26 18:00
  1486. $this->smartPushTestBookChapterUv($bid, $chapter->sequence);
  1487. }
  1488. //智能推送 测书统计第二章 uv
  1489. if ($chapter->sequence == 2) {
  1490. $this->smartPushTestBookSecondChapterUv($bid);
  1491. }
  1492. // 统计前5张uv 会放在book_five_chapter_uv表中,脚本在 \App\Console\Commands\BookTest::data1里面
  1493. // 作用早忘记了
  1494. $field = date('Y-m-d');
  1495. /*
  1496. if ($chapter->sequence <= 5 && $this->send_order_id) {
  1497. try {
  1498. Redis::sadd(sprintf('book_before_five_uv_%s_%s_%s', $field, $bid, $chapter->sequence), $this->uid);
  1499. Redis::sadd('book_before_five_uvs', sprintf('%s_%s_%s', $field, $bid, $chapter->sequence));
  1500. } catch (\Exception $e) {
  1501. }
  1502. }*/
  1503. //统计点击率
  1504. $key = 'book_click_num_bid_' . $bid;
  1505. try {
  1506. Redis::HINCRBY($key, $field, 1);
  1507. } catch (\Exception $e) {
  1508. }
  1509. //阅读页面pvuv
  1510. $this->recordReaderUvAndPv();
  1511. }
  1512. private function smartPushTestBook($bid, $balance)
  1513. {
  1514. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1515. if ($smart_bid && in_array($bid, $smart_bid)) {
  1516. $field = sprintf('%s_amount', $bid);
  1517. try {
  1518. Redis::HINCRBY('smart_push_test_book', $field, $balance);
  1519. Redis::sadd('smart_push_test_book_user_count' . $bid, $this->uid);
  1520. } catch (\Exception $e) {
  1521. }
  1522. }
  1523. }
  1524. private function smartPushTestBookSecondChapterUv($bid)
  1525. {
  1526. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1527. if ($smart_bid && in_array($bid, $smart_bid)) {
  1528. try {
  1529. Redis::sadd('smart_push_test_book_second_uv' . $bid, $this->uid);
  1530. } catch (\Exception $e) {
  1531. }
  1532. }
  1533. }
  1534. private function smartPushTestBookChapterUv($bid, $sequence)
  1535. {
  1536. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1537. if ($smart_bid && in_array($bid, $smart_bid)) {
  1538. try {
  1539. $key = 'smartPushTestBookChapterUv:bid:%s:seq:%s';
  1540. Redis::sadd(sprintf($key, $bid, $sequence), $this->uid);
  1541. } catch (\Exception $e) {
  1542. }
  1543. }
  1544. }
  1545. /**
  1546. * 测书 智能推送统计
  1547. * @param $bid
  1548. */
  1549. private function smartPushTestBookPayPageUv($bid)
  1550. {
  1551. $smart_bid = ReadRecordService::getSmartPush($this->uid);
  1552. if ($smart_bid && in_array($bid, $smart_bid)) {
  1553. try {
  1554. $key = 'smartPushTestBookPayPageUv:bid:%s';
  1555. Redis::sadd(sprintf($key, $bid), $this->uid);
  1556. } catch (\Exception $e) {
  1557. }
  1558. }
  1559. }
  1560. /**
  1561. * 派单精确统计--章节
  1562. * @param $bid
  1563. * @param $sequence
  1564. */
  1565. private function sendOrderChapterUv($bid, $sequence)
  1566. {
  1567. if ($this->send_order_id) {
  1568. $send_order_bid = Redis::hget('sendOrderIdToBid', $this->send_order_id);
  1569. if (!$send_order_bid) {
  1570. $send_order_info = SendOrderService::getById($this->send_order_id);
  1571. if ($send_order_info) {
  1572. $send_order_bid = $send_order_info->book_id;
  1573. Redis::hset('sendOrderIdToBid', $this->send_order_id, $send_order_bid . '-' . time());
  1574. }
  1575. } else {
  1576. $send_order_bid_arr = explode('-', $send_order_bid);
  1577. $send_order_bid = $send_order_bid_arr[0];
  1578. }
  1579. if ($send_order_bid && $send_order_bid == $bid) {
  1580. try {
  1581. $key = 'accurateSendOrderChapterUv:bid:%s:seq:%s:sendorderid:' . $this->send_order_id;
  1582. Redis::sadd(sprintf($key, $bid, $sequence), $this->uid);
  1583. } catch (\Exception $e) {
  1584. }
  1585. }
  1586. }
  1587. }
  1588. /**
  1589. * 派单精确统计--强关页面uv
  1590. * @param $bid
  1591. */
  1592. private function sendOrderSubscribePageUv($bid)
  1593. {
  1594. if ($this->send_order_id) {
  1595. $send_order_bid = Redis::hget('sendOrderIdToBid', $this->send_order_id);
  1596. if (!$send_order_bid) {
  1597. $send_order_info = SendOrderService::getById($this->send_order_id);
  1598. if ($send_order_info) {
  1599. $send_order_bid = $send_order_info->book_id;
  1600. Redis::hset('sendOrderIdToBid', $this->send_order_id, $send_order_bid . '-' . time());
  1601. }
  1602. } else {
  1603. $send_order_bid_arr = explode('-', $send_order_bid);
  1604. $send_order_bid = $send_order_bid_arr[0];
  1605. }
  1606. if ($send_order_bid && $send_order_bid == $bid) {
  1607. try {
  1608. $key = 'accurateSendOrderSubscribePageUv:bid:%s:sendorderid:' . $this->send_order_id;
  1609. Redis::sadd(sprintf($key, $bid), $this->uid);
  1610. } catch (\Exception $e) {
  1611. }
  1612. }
  1613. }
  1614. }
  1615. /**
  1616. * 派单精确统计--支付页面uv
  1617. * @param $bid
  1618. */
  1619. private function sendOrderPayPageUv($bid)
  1620. {
  1621. if ($this->send_order_id) {
  1622. $send_order_bid = Redis::hget('sendOrderIdToBid', $this->send_order_id);
  1623. if (!$send_order_bid) {
  1624. $send_order_info = SendOrderService::getById($this->send_order_id);
  1625. if ($send_order_info) {
  1626. $send_order_bid = $send_order_info->book_id;
  1627. Redis::hset('sendOrderIdToBid', $this->send_order_id, $send_order_bid . '-' . time());
  1628. }
  1629. } else {
  1630. $send_order_bid_arr = explode('-', $send_order_bid);
  1631. $send_order_bid = $send_order_bid_arr[0];
  1632. }
  1633. if ($send_order_bid && $send_order_bid == $bid) {
  1634. try {
  1635. $key = 'accurateSendOrderPayPageUv:bid:%s:send_order_id:' . $this->send_order_id;
  1636. Redis::sadd(sprintf($key, $bid), $this->uid);
  1637. } catch (\Exception $e) {
  1638. }
  1639. }
  1640. }
  1641. }
  1642. private function recordReaderUvAndPv()
  1643. {
  1644. WapVisitStatService::recordReaderUvAndPv($this->uid, $this->distribution_channel_id);
  1645. }
  1646. //用户是否在广告样本中
  1647. private function isUserInAdSample(){
  1648. /*$res = DataAnalysisSelectUserService::getByUidAndType($this->uid,'AD_V2');
  1649. if($res){
  1650. return true;
  1651. }else{
  1652. return false;
  1653. }*/
  1654. $is_ad_user = Cookie::get('cpc_ad_status');
  1655. if($is_ad_user && $is_ad_user == 'show') return true;
  1656. return false;
  1657. }
  1658. //阅读页面底部是否显示广告
  1659. private function isShowAd(){
  1660. $ad_status = env('AD_STATUS',1);
  1661. if(!$ad_status){
  1662. return ;
  1663. }
  1664. if(!$this->is_had_subscribe){
  1665. return ;
  1666. }
  1667. 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])){
  1668. return ;
  1669. }
  1670. //按章付费才显示
  1671. if($this->book_info->charge_type != 'CHAPTER'){
  1672. return ;
  1673. }
  1674. //用户是否在广告样本中
  1675. //if(!$this->isUserInAdSample()) return ;
  1676. $is_ad_user = Cookie::get('cpc_ad_status');
  1677. if(!$is_ad_user || $is_ad_user != 'show') return ;
  1678. $cid = 0;
  1679. if($this->chapter && $this->chapter->next_cid){
  1680. $cid = $this->chapter->next_cid;
  1681. }
  1682. if(!$cid){
  1683. return ;
  1684. }
  1685. //付费用户没有广告
  1686. if(OrderService::isPaidUser($this->uid)) return ;
  1687. //如果下一张已经付费,广告不显示
  1688. /*$exists = AdVisitStatService::getByUid($this->uid);
  1689. if(!$exists){
  1690. if($this->uid %2 == 0){
  1691. DataAnalysisSelectUserService::create($this->uid,$this->distribution_channel_id,'AD_TEST_3',-1,'unshow');
  1692. return ;
  1693. }else{
  1694. DataAnalysisSelectUserService::create($this->uid,$this->distribution_channel_id,'AD_TEST_3',-1,'show');
  1695. }
  1696. }*/
  1697. //广告都显示
  1698. $this->chapter->is_visiable_ad = 1;
  1699. AdVisitStatService::create($this->uid,$this->book_info->bid,$this->cid,'EXPOSURE');
  1700. if($this->getOrderRecord($this->book_info->bid, $cid)){
  1701. return ;
  1702. }
  1703. $next_chapter = ChapterService::getChapterNameById($cid, $this->book_info->bid);
  1704. //下一章事vip的话 才显示广告
  1705. if(!$next_chapter || !$next_chapter->is_vip){
  1706. return ;
  1707. }
  1708. $price = $this->getPrice($this->book_info,$next_chapter);
  1709. $flag = $price > $this->user->balance;
  1710. if( $flag ){
  1711. $this->chapter->is_show_ad = 1;
  1712. AdVisitStatService::create($this->uid,$this->book_info->bid,$this->cid,'CAN_CLICK');
  1713. }
  1714. }
  1715. public function getWechatJsConfig(Request $request)
  1716. {
  1717. //try {
  1718. // $chapter->is_show_share_button = 1;
  1719. $bid = $request->input('bid', '');
  1720. $cid = $request->input('cid', '');
  1721. $url = $request->input('url', '');
  1722. $url = urldecode($url);
  1723. \Log::info('url:' . ($this->uid) . $url);
  1724. if (empty($bid) || empty($cid) || empty($url)) {
  1725. return response()->error('PARAM_EMPTY');
  1726. }
  1727. $share_url = 'https://'._domain().'/detail?fromtype=readershare&id='.$bid.'&fromflag='.($this->uid).'&fromcid='.$cid;
  1728. //$js_config = ChapterShareWechatConfigService::getConfig($url);
  1729. $app_hash = array(
  1730. 4025=>['appid'=>'wx80b618c9dcc940f7','appsecret'=>'255a2de8db51e4084b5e9087d26d4ecb'],
  1731. 4147=>['appid'=>'wx2a99699cd9572812','appsecret'=>'0e0d57f94eeba50abb3af6883df05a5c'],
  1732. 691=>['appid'=>'wx5cefcd251dc40693','appsecret'=>'755f64c85173dad21f20870e38f59d8a'],
  1733. 123=>['appid'=>'wx983808610aa9dcc5','appsecret'=>'91002999590b79f3c7be35fd73c04049'],
  1734. 211=>['appid'=>'wx39338e374cc2055b','appsecret'=>'1a4c2039be271b3811182be5bb53eb34'],
  1735. );
  1736. $appId = $app_hash[$this->distribution_channel_id]['appid'];
  1737. $appSecret = $app_hash[$this->distribution_channel_id]['appsecret'];
  1738. $js_config = (new ChapterShareWechatConfigService($appId,$appSecret))->getSignPackage($url);
  1739. /*$appid = DB::table('official_accounts')->where('distribution_channel_id',$this->distribution_channel_id)->first();//(['distribution_channel_id'=>$this->distribution_channel_id]);
  1740. $appid = $appid->appid;
  1741. //\Log::info('get_appid:'.($appid));
  1742. $js_config = (new ChapterShareWechatConfigService($appid))->getSignPackage($url);
  1743. //$chapter->js_config = $js_config;
  1744. //$chapter->share_url = $share_url;
  1745. $book = BookService::getBookById(Hashids::decode($bid)[0]);
  1746. $first_cid = $book->first_cid;
  1747. $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;
  1748. /*if (in_array($this->uid, explode(',', env('TEST_SHARE_UIDS')))) {
  1749. $share_url = 'https://site5017zqeyzwgyx9n8.zhuishuyun.com/reader?fromtype=readershare&bid=' . $bid . '&fromflag=' . ($this->uid) . '&fromcid=' . $cid;
  1750. }*/
  1751. $book = BookService::getBookById(Hashids::decode($bid)[0]);
  1752. $first_cid = $book->first_cid;
  1753. $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;
  1754. if (in_array($this->distribution_channel_id, [2, 14])) {
  1755. $js_config = ChapterShareWechatConfigService::getConfig($url);
  1756. $share_url = 'https://site' . ($this->distribution_channel_id) . '.' . env('SHARE_DOMAIN') . '/reader?fromtype=readershare&bid=' . $bid . '&cid=' . ($first_cid) . '&fromflag=' . ($this->uid) . '&fromcid=' . $cid;
  1757. }
  1758. $js_config['share_url'] = $share_url;
  1759. /*} catch (\Exception $e) {
  1760. \Log::error($e->getMessage());
  1761. }*/
  1762. //\Log::info($js_config);
  1763. return response()->success($js_config);
  1764. }
  1765. }