ChapterController.php 76 KB

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