ChapterController.php 71 KB

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