123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2020/4/28
- * Time: 下午6:34
- */
- namespace app\main\service;
- use app\common\library\Ip;
- use app\common\library\Redis;
- use app\common\service\BookService;
- use app\common\service\CampaignService;
- use app\common\service\WaterBookService;
- use app\main\constants\CacheConstants;
- use app\main\constants\CampaignConstants;
- use app\main\constants\MarkConstants;
- use app\main\constants\OpenPlatformConstants;
- use app\main\constants\UrlConstants;
- use app\main\helper\ArrayHelper;
- use app\main\model\object\UserObject;
- use think\Cookie;
- use function GuzzleHttp\Psr7\parse_query;
- use app\main\service\SubscripService;
- class PopupService extends BaseService
- {
- /**
- * @var PopupService
- */
- protected static $self = null;
- /**
- * @return BaseService|PopupService
- */
- public static function instance()
- {
- if (self::$self == null) {
- self::$self = new self();
- }
- return self::$self;
- }
- public function getExportFansPop(UserObject $user, $path)
- {
- $data = $cookieKey = false;
- $export = ExportFansService::instance()->checkFansLead($user->channel_id, $user->id)->data;
- if ($export) {
- //书币赠送次数超限
- if ($valid = $this->checkGiveKandianValid($export)->data) {
- $cache = CacheConstants::getExportFansGiveKandianCount($user->id);
- $share_times = ArrayHelper::array_get($export, 'share_times');
- if (Redis::instance()->get($cache) >= $share_times) {
- return $this->setData($data)->getReturn();
- }
- }
- //拉取未充值弹窗
- $unrechargeKey = CacheConstants::getFansUnrecharge($user->id);
- if (Redis::instance()->get($unrechargeKey)) {
- //获取未充值弹窗
- $cookieKey = 'unrecharge_tips_show_times';
- $cookie = Cookie::get($cookieKey);
- if (is_null($cookie) || $cookie < 1) {
- $data = [
- 'id' => $export['id'],
- 'img' => ArrayHelper::array_get($export, 'recharge_back_img'),
- 'lead_img' => ArrayHelper::array_get($export, 'recharge_back_lead_img'),
- 'type' => 'recharge_back',
- ];
- }
- }
- if (!$data) {
- switch ($path) {
- case '/index/user/recent':
- case '/index/book/shelf':
- //最近阅读
- $cookieKey = 'recent_tips_show_times';
- $cookie = Cookie::get($cookieKey);
- if (is_null($cookie) || $cookie < 1) {
- $data = [
- 'id' => $export['id'],
- 'img' => ArrayHelper::array_get($export, 'recent_img'),
- 'lead_img' => ArrayHelper::array_get($export, 'recent_lead_img'),
- 'type' => 'recent',
- ];
- }
- break;
- case '/index/index/index':
- //最近阅读
- $cookieKey = 'home_tips_show_times';
- $cookie = Cookie::get($cookieKey);
- if (is_null($cookie) || $cookie < 1) {
- $data = [
- 'id' => $export['id'],
- 'img' => ArrayHelper::array_get($export, 'recent_img'),
- 'lead_img' => ArrayHelper::array_get($export, 'recent_lead_img'),
- 'type' => 'recent',
- ];
- }
- break;
- case '/index/book/list':
- //排行等
- $cookieKey = 'rank_tips_show_times';
- $cookie = Cookie::get($cookieKey);
- if (is_null($cookie) || $cookie < 1) {
- $data = [
- 'id' => $export['id'],
- 'img' => ArrayHelper::array_get($export, 'rank_img'),
- 'lead_img' => ArrayHelper::array_get($export, 'rank_lead_img'),
- 'type' => 'rank',
- ];
- }
- break;
- case '/index/user/index':
- //个人中心
- $cookieKey = 'user_tips_show_times';
- $cookie = Cookie::get($cookieKey);
- if (is_null($cookie) || $cookie < 1) {
- $data = [
- 'id' => $export['id'],
- 'img' => ArrayHelper::array_get($export, 'user_index_img'),
- 'lead_img' => ArrayHelper::array_get($export, 'user_index_lead_img'),
- 'type' => 'user_index',
- ];
- }
- break;
- case '/index/user/sign':
- $cookieKey = 'sign_tips_show_times';
- $cookie = Cookie::get($cookieKey);
- if (is_null($cookie) || $cookie < 1) {
- $data = [
- 'id' => $export['id'],
- 'img' => ArrayHelper::array_get($export, 'sign_success_img'),
- 'lead_img' => ArrayHelper::array_get($export, 'sign_success_lead_img'),
- 'type' => 'sign_success',
- ];
- }
- break;
- }
- }
- if ($data) {
- $data['cookiekey'] = $cookieKey;
- $data['expiretime'] = 1 - (time() - strtotime(date("Ymd"))) / 86400;
- $data['tpl'] = 'export_fans';
- //缺少弹窗图片
- if (!$data['img']) {
- $data = [];
- } else {
- //满足导量条件
- if ($valid && $data['lead_img']) {
- $data['lead_url'] = getCurrentDomain($user->channel_id, '/index/share/landpage?id=' . $export['id'] . '&type=' . $data['type']);
- } else {
- $data['lead_url'] = '';
- }
- }
- }
- }
- return $this->setData($data)->getReturn();
- }
- /**
- * 获取活动弹窗
- * @param UserObject $user
- * @param $path
- * @return \app\main\model\object\ReturnObject
- */
- public function getActivityPop(UserObject $user, $path)
- {
- $pathArr = explode('/', $path);
- $controllerName = $pathArr[1] ?? '';
- $actionName = $pathArr[2] ?? '';
- $cookieKey = 'ac_val';
- $cookie = Cookie::get('ac_val');
- if (!is_null($cookie) && $cookie >= 4) {
- $data = [];
- } else {
- $data = ActivityService::instance()->getActivityFront(
- $controllerName,
- $actionName,
- $user->channel_id,
- $user->id
- );
- if ($data) {
- $info = [
- 'img' => $data['popimage'],
- 'jump_url' => "/s/0?rid={$data['id']}&aid={$data['aid']}",
- 'cookie_key' => $cookieKey,
- 'expiretime' => 1 / 3,
- 'dot_key' => 'activity_tips',
- 'tpl' => 'activity',
- ];
- $data = array_merge($data, $info);
- }
- }
- return $this->setData($data)->getReturn();
- }
- /**
- * 新用户继续阅读弹窗
- * @param UserObject $user
- * @return \app\main\model\object\ReturnObject
- */
- public function getRegisterContinuePop(UserObject $user)
- {
- $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($user->channel_id);
- $isWater = WaterBookService::instance()->showBook($adminConfig['admin_id'],$user->id,Ip::ip());
- $data = [];
- if ($user->is_subscribe && time() - $user->createtime < 3600 && !$user->referral_id_permanent) {
- //一个小时新注册用户
- //先判断cookie是否设置了不弹
- if (!Cookie::has('close_continue_tips')) {
- //开启了自动回复
- $switch = AdminService::instance()->getSubscribeSwitchCache($user->channel_id);
- if (!empty($switch) && $switch['status'] == 1) {
- if ($switch['type'] == 2) {//自定义
- $replyModel = AdminService::instance()->getSubscribeConfigCache($user->channel_id);
- if (!empty($replyModel) && !empty($replyModel['event_keys'])) {
- $json = json_decode($replyModel['event_keys'],true);
- $responseModel = model("WechatResponse")->where(
- [
- 'admin_id' => $adminConfig['admin_id'],
- 'eventkey' => $json[$replyModel['type']] ?? '',
- 'status' => 'normal'
- ]
- )->find();
- if (!empty($responseModel)) {
- $data = $this->getContinueTipData($responseModel, $adminConfig, $isWater);
- //被关注回复可能为空
- if ($data) {
- $data['tpl'] = 'continue';
- $data['expiretime'] = 1 / 3;
- }
- }
- }
- }
- }
- }
- }
- return $this->setData($data)->getReturn();
- }
- /**
- * 获取消耗活动弹窗
- * @param UserObject $user
- * @param $path
- * @return \app\main\model\object\ReturnObject
- */
- public function getCampPop(UserObject $user, $path)
- {
- //消耗活动弹窗
- $actionIndex = UrlConstants::getLocation($path);
- $campData = [];
- if (in_array($actionIndex, [CampaignConstants::CAMPAIGN_POPUP_POSITION_ONE, CampaignConstants::CAMPAIGN_POPUP_POSITION_TWO, CampaignConstants::CAMPAIGN_POPUP_POSITION_THR])) {
- $campActData = CampaignService::instance()->getLatestCampaign($user);
- if ($campActData) {
- if ($campActData['popup_position']) {
- $position = explode(',', $campActData['popup_position']);
- if (in_array($actionIndex, $position)) {
- $campData = [
- 'id' => $campActData['id'],
- 'image' => $campActData['popup_img'],
- 'url' => '/index/recharge/campaignIndex?active_id=' . $campActData['id'],
- 'expiretime' => 1 / 3,
- 'tpl' => 'camp'
- ];
- $tipsnums = Cookie::get('camp_ac_val');
- if ($tipsnums && $tipsnums >= 4) {
- $campData = [];
- }
- }
- }
- }
- }
- return $this->setData($campData)->getReturn();
- }
- /**
- * 获取客服消息弹窗
- * @param UserObject $user
- * @param $path
- * @return \app\main\model\object\ReturnObject
- */
- public function getCustomPop(UserObject $user, $path)
- {
- $customData = [];
- if (in_array($path, OpenPlatformConstants::$customTipsUrlArr)) {
- $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($user->channel_id);
- $customData = CustomService::instance()->getLastSendSuccessCustom($adminConfig['admin_id'], $adminConfig['appid']);
- //判断cookie
- if ($customData) {
- $customData['tpl'] = 'custom';
- $customData['expiretime'] = 2.5;
- $customId = $customData['custom_id'];
- if (Cookie::has('close_custom_tips_' . $customId)) {
- //已弹过了
- $customData = [];
- } else {
- //判断redis
- $userOtherData = UserService::instance()->getOtherDataFromRedis(UserService::instance()->getUserInfo()->id);
- if (isset($userOtherData['last_custom_id']) &&
- $userOtherData['last_custom_id'] == $customId
- ) {
- $customData = [];
- }
- }
- }
- }
- return $this->setData($customData)->getReturn();
- }
- /**
- * 关注回复资源继续阅读
- * @param $responseModel
- * @param $adminConfig
- * @param $isWater
- * @return array
- */
- protected function getContinueTipData($responseModel, $adminConfig, $isWater=false)
- {
- $push_time = time();
- $replace = [];
- $appid = $adminConfig['appid'];
- $content = UrlService::instance()->replaceReferralHost(
- $adminConfig['admin_id'],
- $responseModel['content'],
- false
- )->data;
- switch ($responseModel['type']) {
- case "text":
- $mark = MarkConstants::MARK_RESOURCE;
- preg_match_all("<a href=['\"](.*?)['\"]>", $content, $match);
- if ($match) {
- foreach ($match[1] as $index => $item) {
- $push_idx = $index + 1;
- $parse = parse_url($item);
- $host = $parse['host'] ?? '';
- $path = $parse['path'] ?? '';
- if (preg_match('/^' . $appid . '/', $host)) {
- if (array_key_exists('query', $parse)) {
- $query = parse_query($parse['query']);
- if (!empty($query)) {
- if (array_key_exists('book_id', $query)) {
- $push_id = $query['book_id'];
- $replace = [
- 'book_id' => $query['book_id'],
- 'url' => $item .= '&' . OfficialAccountsEventService::instance()->processExt($mark, $push_id, $push_idx, $push_time),
- ];
- break;
- } elseif (array_key_exists('t', $query)) {
- $push_id = $query['t'];
- $replace = [
- 'book_id' => '',
- 'url' => $item .= '&' . OfficialAccountsEventService::instance()->processExt($mark, $push_id, $push_idx, $push_time),
- 'referral_id' => $query['t'],
- ];
- break;
- }
- }
- }
- if (!empty($path)) {
- $path = explode('/', $parse['path']);
- if ($keyIndex = array_search('t', $path)) {
- $push_id = $path[$keyIndex+1];
- $replace = [
- 'book_id' => '',
- 'url' => $item .= '?' . OfficialAccountsEventService::instance()->processExt($mark, $push_id, $push_idx, $push_time),
- 'referral_id' => $push_id,
- ];
- break;
- }
- }
- }
- }
- }
- break;
- case "news":
- $mark = MarkConstants::MARK_RESOURCE;
- $items = json_decode($content, true);
- foreach ($items as $index => $item) {
- $push_idx = $index + 1;
- $parse = parse_url($item['url']);
- $path = $parse['path'] ?? '';
- if (array_key_exists('query', $parse)) {
- $query = parse_query($parse['query']);
- if (array_key_exists('book_id', $query)) {
- $push_id = $query['book_id'];
- $replace = [
- 'book_id' => $query['book_id'],
- 'url' => $item['url'] .= '&' . OfficialAccountsEventService::instance()->processExt($mark, $push_id, $push_idx, $push_time),
- 'referral_id' => ''
- ];
- break;
- } elseif (array_key_exists('t', $query)) {
- $push_id = $query['t'];
- $replace = [
- 'book_id' => '',
- 'url' => $item['url'] .= '&' . OfficialAccountsEventService::instance()->processExt($mark, $push_id, $push_idx, $push_time),
- 'referral_id' => $query['t'],
- ];
- break;
- } else {
- continue;
- }
- }
- if (!empty($path)) {
- $path = explode('/', $parse['path']);
- if ($keyIndex = array_search('t', $path)) {
- $push_id = $path[$keyIndex+1];
- $replace = [
- 'book_id' => '',
- 'url' => $item['url'] .= '?' . OfficialAccountsEventService::instance()->processExt($mark, $push_id, $push_idx, $push_time),
- 'referral_id' => $push_id,
- ];
- break;
- }
- }
- }
- break;
- }
- if (!empty($replace)) {
- //查询小说标题
- if (isset($replace['referral_id']) && $replace['referral_id']) {
- $referralinfo = model('referral')->getone($replace['referral_id'], false);
- if (empty($referralinfo) || !$referralinfo['book_id']) {
- return [];
- }
- $replace['book_id'] = $referralinfo['book_id'];
- }
- $bookinfo = BookService::instance()->getBookModel()->BookInfo($replace['book_id']);
- $replace['book_name'] = $bookinfo['name'];
- $replace['image'] = $bookinfo['image'];
- $replace['description'] = $bookinfo['description'];
- if ($isWater && (!isset($bookinfo['classify_white_list']) || $bookinfo['classify_white_list'] != 1)){
- LogService::info('清水用户,非清水书籍,过滤掉'.$bookinfo['id']);
- $replace = [];
- }
- }
- return $replace;
- }
- /**
- * 检测是否符合导量条件
- * @param $export
- * @return \app\main\model\object\ReturnObject
- */
- public function checkGiveKandianValid($export)
- {
- $fields = [
- 'share_free_kandian',
- 'share_times',
- 'share_icon',
- 'share_title',
- 'share_desc',
- 'share_url',
- ];
- foreach ($fields as $field) {
- if (!ArrayHelper::array_get($export, $field)) {
- return $this->setData(false)->getReturn();
- }
- }
- return $this->setData(true)->getReturn();
- }
- public function getSubscribePop(UserObject $user, $path)
- {
- $data = SubscripService::instance()->getActPop($user,$path);
- $subData = [];
- if ($data && $data['act_pop_img']){
- $subData = [
- 'id' => $data['id']??1,
- 'image' => $data['act_pop_img'],
- 'url' => '/index/recharge/subscription?type='.$data['type'],
- 'expiretime' => 1 / 3,
- 'tpl' => 'subscribe'
- ];
- }
- return $this->setData($subData)->getReturn();
- }
- }
|