123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Elton
- * Date: 2020/5/21
- * Time: 15:10
- */
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\library\Redis;
- use app\common\service\AdPlanService;
- use app\main\constants\AdConstants;
- class Aduserapi extends Api
- {
- /**
- * API接口:返回页面匹配的广告计划和广告素材
- * @param channel_id 渠道ID
- * @param uid 用户ID
- */
- public function advert()
- {
- $channel_id = (integer)$this->request->param('channel_id') ?? 0;
- $uid = (integer)$this->request->param('uid') ?? 0;
- // region 参数校验
- if($channel_id == 0){
- $this->error('参数channel_id错误');
- }
- if($uid == 0){
- $this->error('参数uid错误');
- }
- // endregion
- // 广告库,所有广告计划,包括广告素材
- $adFull = json_decode(Redis::instance()->get(AdConstants::AD_FULL), true);
- $target_data = [];
- if($adFull){
- $target_data = $adFull;
- // (筛选所有非福利广告计划数据) 所有广告位 ()
- $postions_arr = AdPlanService::instance()->getAllAdPositions();
- if ($postions_arr) {
- foreach ($postions_arr as $k => $postion) {
- if (isset($target_data['positions'][$postion])) {
- $redisIdx = Redis::getRedisIndex(AdConstants::AD_JU_KEY);
- $redis = Redis::getRedisConnect($redisIdx);
- $pipe = $redis->multi(\Redis::PIPELINE);
- //$pipe = Redis::instance()->multi(\Redis::PIPELINE);
- foreach ($target_data['positions'][$postion] as $ad_id => $ad_plan) {
- $redis_key = AdConstants::AD_JU_KEY . $ad_id . ':' . $channel_id;
- $pipe->sIsMember($redis_key, $uid);
- }
- $rst = $pipe->exec();
- $keys_arr = array_keys($target_data['positions'][$postion]);
- foreach ($rst as $sub_key => $value) {
- $real_key = $keys_arr[$sub_key];
- if (isset($target_data['positions'][$postion][$real_key])) {
- $group_type = $target_data['positions'][$postion][$real_key]['group_type']; // 广告计划关联的用户组类型, 0 => 全部 1=>指定用户ID 2=>用户条件筛选
- // $goup_type == 0 如果广告计划关联的是全部用户,则不删除
- if ($group_type > 0) {
- // $value = true => 是集合中的成员
- if (!$value) {
- //如果用户不在集合中, 删除位置
- //array_splice($target_data['positions'][$postion], $sub_key, 1);
- unset($target_data['positions'][$postion][$real_key]);
- }
- }
- }
- }
- }
- }
- }
- // 筛选所有福利广告计划数据
- if (isset($target_data['welfare_plans'])) {
- $redisIdx = Redis::getRedisIndex(AdConstants::AD_JU_KEY);
- $redis = Redis::getRedisConnect($redisIdx);
- $pipe = $redis->multi(\Redis::PIPELINE);
- foreach ($target_data['welfare_plans'] as $ad_id => $ad_plan) {
- $redis_key = AdConstants::AD_JU_KEY . $ad_id . ':' . $channel_id;
- $pipe->sIsMember($redis_key, $uid);
- }
- $rst = $pipe->exec();
- $keys_arr = array_keys($target_data['welfare_plans']);
- foreach ($rst as $sub_key => $value) {
- $real_key = $keys_arr[$sub_key];
- if (isset($target_data['welfare_plans'][$real_key])) {
- $group_type = $target_data['welfare_plans'][$real_key]['group_type']; // 广告计划关联的用户组类型, 0 => 全部 1=>指定用户ID 2=>用户条件筛选
- // $goup_type == 0 如果广告计划关联的是全部用户,则不删除
- if ($group_type > 0) {
- // $value = true => 是集合中的成员
- if (!$value) {
- //如果用户不在集合中, 删除位置
- unset($target_data['welfare_plans'][$real_key]);
- }
- }
- }
- }
- }
- // END 筛选所有福利广告计划数据
- }
- $this->success('success', $target_data);
- }
- /**
- * API接口:用户点击福利广告后,记录用户信息
- * @param channel_id 渠道ID
- * @param uid 用户ID
- */
- public function welfareDot()
- {
- $uid = (integer)$this->request->param('uid') ?? 0;
- //$channel_id = (integer)$this->request->param('channel_id') ?? 0;
- if($uid == 0){
- $this->error('参数uid错误');
- }
- $redis_key = AdConstants::AD_WELFARE_USER . date('Ymd') . ':' . $uid;
- if (!Redis::instance()->get($redis_key)) {
- //Redis::instance()->set($redis_key, 1, 86400);
- Redis::instance()->set($redis_key, $uid, 86400);
- }
- // region 参数校验
- /*if($channel_id == 0){
- $this->error('参数channel_id错误');
- }*/
- // 记录用户点击
- /*$redis_key = AdConstants::AD_WELFARE . $channel_id;
- Redis::instance()->sAdd($redis_key, $uid);
- // 计算过期时间 24点过期
- $tomorrow_time = strtotime(date('Y-m-d 00:00:00', strtotime("+1 day")));
- $ttl = $tomorrow_time - time();
- Redis::instance()->expire($redis_key, $ttl);*/
- $this->success('success');
- }
- }
|