ChapterController.php 69 KB

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