ChapterController.php 77 KB

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