ChapterController.php 72 KB

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