ChapterController.php 61 KB

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