PopupService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2020/4/28
  6. * Time: 下午6:34
  7. */
  8. namespace app\main\service;
  9. use app\common\library\Ip;
  10. use app\common\library\Redis;
  11. use app\common\service\BookService;
  12. use app\common\service\CampaignService;
  13. use app\common\service\WaterBookService;
  14. use app\main\constants\CacheConstants;
  15. use app\main\constants\CampaignConstants;
  16. use app\main\constants\MarkConstants;
  17. use app\main\constants\OpenPlatformConstants;
  18. use app\main\constants\UrlConstants;
  19. use app\main\helper\ArrayHelper;
  20. use app\main\model\object\UserObject;
  21. use think\Cookie;
  22. use function GuzzleHttp\Psr7\parse_query;
  23. use app\main\service\SubscripService;
  24. class PopupService extends BaseService
  25. {
  26. /**
  27. * @var PopupService
  28. */
  29. protected static $self = null;
  30. /**
  31. * @return BaseService|PopupService
  32. */
  33. public static function instance()
  34. {
  35. if (self::$self == null) {
  36. self::$self = new self();
  37. }
  38. return self::$self;
  39. }
  40. public function getExportFansPop(UserObject $user, $path)
  41. {
  42. $data = $cookieKey = false;
  43. $export = ExportFansService::instance()->checkFansLead($user->channel_id, $user->id)->data;
  44. if ($export) {
  45. //书币赠送次数超限
  46. if ($valid = $this->checkGiveKandianValid($export)->data) {
  47. $cache = CacheConstants::getExportFansGiveKandianCount($user->id);
  48. $share_times = ArrayHelper::array_get($export, 'share_times');
  49. if (Redis::instance()->get($cache) >= $share_times) {
  50. return $this->setData($data)->getReturn();
  51. }
  52. }
  53. //拉取未充值弹窗
  54. $unrechargeKey = CacheConstants::getFansUnrecharge($user->id);
  55. if (Redis::instance()->get($unrechargeKey)) {
  56. //获取未充值弹窗
  57. $cookieKey = 'unrecharge_tips_show_times';
  58. $cookie = Cookie::get($cookieKey);
  59. if (is_null($cookie) || $cookie < 1) {
  60. $data = [
  61. 'id' => $export['id'],
  62. 'img' => ArrayHelper::array_get($export, 'recharge_back_img'),
  63. 'lead_img' => ArrayHelper::array_get($export, 'recharge_back_lead_img'),
  64. 'type' => 'recharge_back',
  65. ];
  66. }
  67. }
  68. if (!$data) {
  69. switch ($path) {
  70. case '/index/user/recent':
  71. case '/index/book/shelf':
  72. //最近阅读
  73. $cookieKey = 'recent_tips_show_times';
  74. $cookie = Cookie::get($cookieKey);
  75. if (is_null($cookie) || $cookie < 1) {
  76. $data = [
  77. 'id' => $export['id'],
  78. 'img' => ArrayHelper::array_get($export, 'recent_img'),
  79. 'lead_img' => ArrayHelper::array_get($export, 'recent_lead_img'),
  80. 'type' => 'recent',
  81. ];
  82. }
  83. break;
  84. case '/index/index/index':
  85. //最近阅读
  86. $cookieKey = 'home_tips_show_times';
  87. $cookie = Cookie::get($cookieKey);
  88. if (is_null($cookie) || $cookie < 1) {
  89. $data = [
  90. 'id' => $export['id'],
  91. 'img' => ArrayHelper::array_get($export, 'recent_img'),
  92. 'lead_img' => ArrayHelper::array_get($export, 'recent_lead_img'),
  93. 'type' => 'recent',
  94. ];
  95. }
  96. break;
  97. case '/index/book/list':
  98. //排行等
  99. $cookieKey = 'rank_tips_show_times';
  100. $cookie = Cookie::get($cookieKey);
  101. if (is_null($cookie) || $cookie < 1) {
  102. $data = [
  103. 'id' => $export['id'],
  104. 'img' => ArrayHelper::array_get($export, 'rank_img'),
  105. 'lead_img' => ArrayHelper::array_get($export, 'rank_lead_img'),
  106. 'type' => 'rank',
  107. ];
  108. }
  109. break;
  110. case '/index/user/index':
  111. //个人中心
  112. $cookieKey = 'user_tips_show_times';
  113. $cookie = Cookie::get($cookieKey);
  114. if (is_null($cookie) || $cookie < 1) {
  115. $data = [
  116. 'id' => $export['id'],
  117. 'img' => ArrayHelper::array_get($export, 'user_index_img'),
  118. 'lead_img' => ArrayHelper::array_get($export, 'user_index_lead_img'),
  119. 'type' => 'user_index',
  120. ];
  121. }
  122. break;
  123. case '/index/user/sign':
  124. $cookieKey = 'sign_tips_show_times';
  125. $cookie = Cookie::get($cookieKey);
  126. if (is_null($cookie) || $cookie < 1) {
  127. $data = [
  128. 'id' => $export['id'],
  129. 'img' => ArrayHelper::array_get($export, 'sign_success_img'),
  130. 'lead_img' => ArrayHelper::array_get($export, 'sign_success_lead_img'),
  131. 'type' => 'sign_success',
  132. ];
  133. }
  134. break;
  135. }
  136. }
  137. if ($data) {
  138. $data['cookiekey'] = $cookieKey;
  139. $data['expiretime'] = 1 - (time() - strtotime(date("Ymd"))) / 86400;
  140. $data['tpl'] = 'export_fans';
  141. //缺少弹窗图片
  142. if (!$data['img']) {
  143. $data = [];
  144. } else {
  145. //满足导量条件
  146. if ($valid && $data['lead_img']) {
  147. $data['lead_url'] = getCurrentDomain($user->channel_id, '/index/share/landpage?id=' . $export['id'] . '&type=' . $data['type']);
  148. } else {
  149. $data['lead_url'] = '';
  150. }
  151. }
  152. }
  153. }
  154. return $this->setData($data)->getReturn();
  155. }
  156. /**
  157. * 获取活动弹窗
  158. * @param UserObject $user
  159. * @param $path
  160. * @return \app\main\model\object\ReturnObject
  161. */
  162. public function getActivityPop(UserObject $user, $path)
  163. {
  164. $pathArr = explode('/', $path);
  165. $controllerName = $pathArr[1] ?? '';
  166. $actionName = $pathArr[2] ?? '';
  167. $cookieKey = 'ac_val';
  168. $cookie = Cookie::get('ac_val');
  169. if (!is_null($cookie) && $cookie >= 4) {
  170. $data = [];
  171. } else {
  172. $data = ActivityService::instance()->getActivityFront(
  173. $controllerName,
  174. $actionName,
  175. $user->channel_id,
  176. $user->id
  177. );
  178. if ($data) {
  179. $info = [
  180. 'img' => $data['popimage'],
  181. 'jump_url' => "/s/0?rid={$data['id']}&aid={$data['aid']}",
  182. 'cookie_key' => $cookieKey,
  183. 'expiretime' => 1 / 3,
  184. 'dot_key' => 'activity_tips',
  185. 'tpl' => 'activity',
  186. ];
  187. $data = array_merge($data, $info);
  188. }
  189. }
  190. return $this->setData($data)->getReturn();
  191. }
  192. /**
  193. * 新用户继续阅读弹窗
  194. * @param UserObject $user
  195. * @return \app\main\model\object\ReturnObject
  196. */
  197. public function getRegisterContinuePop(UserObject $user)
  198. {
  199. $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($user->channel_id);
  200. $isWater = WaterBookService::instance()->showBook($adminConfig['admin_id'],$user->id,Ip::ip());
  201. $data = [];
  202. if ($user->is_subscribe && time() - $user->createtime < 3600 && !$user->referral_id_permanent) {
  203. //一个小时新注册用户
  204. //先判断cookie是否设置了不弹
  205. if (!Cookie::has('close_continue_tips')) {
  206. //开启了自动回复
  207. $switch = AdminService::instance()->getSubscribeSwitchCache($user->channel_id);
  208. if (!empty($switch) && $switch['status'] == 1) {
  209. if ($switch['type'] == 2) {//自定义
  210. $replyModel = AdminService::instance()->getSubscribeConfigCache($user->channel_id);
  211. if (!empty($replyModel) && !empty($replyModel['event_keys'])) {
  212. $json = json_decode($replyModel['event_keys'],true);
  213. $responseModel = model("WechatResponse")->where(
  214. [
  215. 'admin_id' => $adminConfig['admin_id'],
  216. 'eventkey' => $json[$replyModel['type']] ?? '',
  217. 'status' => 'normal'
  218. ]
  219. )->find();
  220. if (!empty($responseModel)) {
  221. $data = $this->getContinueTipData($responseModel, $adminConfig, $isWater);
  222. //被关注回复可能为空
  223. if ($data) {
  224. $data['tpl'] = 'continue';
  225. $data['expiretime'] = 1 / 3;
  226. }
  227. }
  228. }
  229. }
  230. }
  231. }
  232. }
  233. return $this->setData($data)->getReturn();
  234. }
  235. /**
  236. * 获取消耗活动弹窗
  237. * @param UserObject $user
  238. * @param $path
  239. * @return \app\main\model\object\ReturnObject
  240. */
  241. public function getCampPop(UserObject $user, $path)
  242. {
  243. //消耗活动弹窗
  244. $actionIndex = UrlConstants::getLocation($path);
  245. $campData = [];
  246. if (in_array($actionIndex, [CampaignConstants::CAMPAIGN_POPUP_POSITION_ONE, CampaignConstants::CAMPAIGN_POPUP_POSITION_TWO, CampaignConstants::CAMPAIGN_POPUP_POSITION_THR])) {
  247. $campActData = CampaignService::instance()->getLatestCampaign($user);
  248. if ($campActData) {
  249. if ($campActData['popup_position']) {
  250. $position = explode(',', $campActData['popup_position']);
  251. if (in_array($actionIndex, $position)) {
  252. $campData = [
  253. 'id' => $campActData['id'],
  254. 'image' => $campActData['popup_img'],
  255. 'url' => '/index/recharge/campaignIndex?active_id=' . $campActData['id'],
  256. 'expiretime' => 1 / 3,
  257. 'tpl' => 'camp'
  258. ];
  259. $tipsnums = Cookie::get('camp_ac_val');
  260. if ($tipsnums && $tipsnums >= 4) {
  261. $campData = [];
  262. }
  263. }
  264. }
  265. }
  266. }
  267. return $this->setData($campData)->getReturn();
  268. }
  269. /**
  270. * 获取客服消息弹窗
  271. * @param UserObject $user
  272. * @param $path
  273. * @return \app\main\model\object\ReturnObject
  274. */
  275. public function getCustomPop(UserObject $user, $path)
  276. {
  277. $customData = [];
  278. if (in_array($path, OpenPlatformConstants::$customTipsUrlArr)) {
  279. $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($user->channel_id);
  280. $customData = CustomService::instance()->getLastSendSuccessCustom($adminConfig['admin_id'], $adminConfig['appid']);
  281. //判断cookie
  282. if ($customData) {
  283. $customData['tpl'] = 'custom';
  284. $customData['expiretime'] = 2.5;
  285. $customId = $customData['custom_id'];
  286. if (Cookie::has('close_custom_tips_' . $customId)) {
  287. //已弹过了
  288. $customData = [];
  289. } else {
  290. //判断redis
  291. $userOtherData = UserService::instance()->getOtherDataFromRedis(UserService::instance()->getUserInfo()->id);
  292. if (isset($userOtherData['last_custom_id']) &&
  293. $userOtherData['last_custom_id'] == $customId
  294. ) {
  295. $customData = [];
  296. }
  297. }
  298. }
  299. }
  300. return $this->setData($customData)->getReturn();
  301. }
  302. /**
  303. * 关注回复资源继续阅读
  304. * @param $responseModel
  305. * @param $adminConfig
  306. * @param $isWater
  307. * @return array
  308. */
  309. protected function getContinueTipData($responseModel, $adminConfig, $isWater=false)
  310. {
  311. $push_time = time();
  312. $replace = [];
  313. $appid = $adminConfig['appid'];
  314. $content = UrlService::instance()->replaceReferralHost(
  315. $adminConfig['admin_id'],
  316. $responseModel['content'],
  317. false
  318. )->data;
  319. switch ($responseModel['type']) {
  320. case "text":
  321. $mark = MarkConstants::MARK_RESOURCE;
  322. preg_match_all("<a href=['\"](.*?)['\"]>", $content, $match);
  323. if ($match) {
  324. foreach ($match[1] as $index => $item) {
  325. $push_idx = $index + 1;
  326. $parse = parse_url($item);
  327. $host = $parse['host'] ?? '';
  328. $path = $parse['path'] ?? '';
  329. if (preg_match('/^' . $appid . '/', $host)) {
  330. if (array_key_exists('query', $parse)) {
  331. $query = parse_query($parse['query']);
  332. if (!empty($query)) {
  333. if (array_key_exists('book_id', $query)) {
  334. $push_id = $query['book_id'];
  335. $replace = [
  336. 'book_id' => $query['book_id'],
  337. 'url' => $item .= '&' . OfficialAccountsEventService::instance()->processExt($mark, $push_id, $push_idx, $push_time),
  338. ];
  339. break;
  340. } elseif (array_key_exists('t', $query)) {
  341. $push_id = $query['t'];
  342. $replace = [
  343. 'book_id' => '',
  344. 'url' => $item .= '&' . OfficialAccountsEventService::instance()->processExt($mark, $push_id, $push_idx, $push_time),
  345. 'referral_id' => $query['t'],
  346. ];
  347. break;
  348. }
  349. }
  350. }
  351. if (!empty($path)) {
  352. $path = explode('/', $parse['path']);
  353. if ($keyIndex = array_search('t', $path)) {
  354. $push_id = $path[$keyIndex+1];
  355. $replace = [
  356. 'book_id' => '',
  357. 'url' => $item .= '?' . OfficialAccountsEventService::instance()->processExt($mark, $push_id, $push_idx, $push_time),
  358. 'referral_id' => $push_id,
  359. ];
  360. break;
  361. }
  362. }
  363. }
  364. }
  365. }
  366. break;
  367. case "news":
  368. $mark = MarkConstants::MARK_RESOURCE;
  369. $items = json_decode($content, true);
  370. foreach ($items as $index => $item) {
  371. $push_idx = $index + 1;
  372. $parse = parse_url($item['url']);
  373. $path = $parse['path'] ?? '';
  374. if (array_key_exists('query', $parse)) {
  375. $query = parse_query($parse['query']);
  376. if (array_key_exists('book_id', $query)) {
  377. $push_id = $query['book_id'];
  378. $replace = [
  379. 'book_id' => $query['book_id'],
  380. 'url' => $item['url'] .= '&' . OfficialAccountsEventService::instance()->processExt($mark, $push_id, $push_idx, $push_time),
  381. 'referral_id' => ''
  382. ];
  383. break;
  384. } elseif (array_key_exists('t', $query)) {
  385. $push_id = $query['t'];
  386. $replace = [
  387. 'book_id' => '',
  388. 'url' => $item['url'] .= '&' . OfficialAccountsEventService::instance()->processExt($mark, $push_id, $push_idx, $push_time),
  389. 'referral_id' => $query['t'],
  390. ];
  391. break;
  392. } else {
  393. continue;
  394. }
  395. }
  396. if (!empty($path)) {
  397. $path = explode('/', $parse['path']);
  398. if ($keyIndex = array_search('t', $path)) {
  399. $push_id = $path[$keyIndex+1];
  400. $replace = [
  401. 'book_id' => '',
  402. 'url' => $item['url'] .= '?' . OfficialAccountsEventService::instance()->processExt($mark, $push_id, $push_idx, $push_time),
  403. 'referral_id' => $push_id,
  404. ];
  405. break;
  406. }
  407. }
  408. }
  409. break;
  410. }
  411. if (!empty($replace)) {
  412. //查询小说标题
  413. if (isset($replace['referral_id']) && $replace['referral_id']) {
  414. $referralinfo = model('referral')->getone($replace['referral_id'], false);
  415. if (empty($referralinfo) || !$referralinfo['book_id']) {
  416. return [];
  417. }
  418. $replace['book_id'] = $referralinfo['book_id'];
  419. }
  420. $bookinfo = BookService::instance()->getBookModel()->BookInfo($replace['book_id']);
  421. $replace['book_name'] = $bookinfo['name'];
  422. $replace['image'] = $bookinfo['image'];
  423. $replace['description'] = $bookinfo['description'];
  424. if ($isWater && (!isset($bookinfo['classify_white_list']) || $bookinfo['classify_white_list'] != 1)){
  425. LogService::info('清水用户,非清水书籍,过滤掉'.$bookinfo['id']);
  426. $replace = [];
  427. }
  428. }
  429. return $replace;
  430. }
  431. /**
  432. * 检测是否符合导量条件
  433. * @param $export
  434. * @return \app\main\model\object\ReturnObject
  435. */
  436. public function checkGiveKandianValid($export)
  437. {
  438. $fields = [
  439. 'share_free_kandian',
  440. 'share_times',
  441. 'share_icon',
  442. 'share_title',
  443. 'share_desc',
  444. 'share_url',
  445. ];
  446. foreach ($fields as $field) {
  447. if (!ArrayHelper::array_get($export, $field)) {
  448. return $this->setData(false)->getReturn();
  449. }
  450. }
  451. return $this->setData(true)->getReturn();
  452. }
  453. public function getSubscribePop(UserObject $user, $path)
  454. {
  455. $data = SubscripService::instance()->getActPop($user,$path);
  456. $subData = [];
  457. if ($data && $data['act_pop_img']){
  458. $subData = [
  459. 'id' => $data['id']??1,
  460. 'image' => $data['act_pop_img'],
  461. 'url' => '/index/recharge/subscription?type='.$data['type'],
  462. 'expiretime' => 1 / 3,
  463. 'tpl' => 'subscribe'
  464. ];
  465. }
  466. return $this->setData($subData)->getReturn();
  467. }
  468. }