ChapterController.php 76 KB

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