Aduserapi.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Elton
  5. * Date: 2020/5/21
  6. * Time: 15:10
  7. */
  8. namespace app\api\controller;
  9. use app\common\controller\Api;
  10. use app\common\library\Redis;
  11. use app\common\service\AdPlanService;
  12. use app\main\constants\AdConstants;
  13. class Aduserapi extends Api
  14. {
  15. /**
  16. * API接口:返回页面匹配的广告计划和广告素材
  17. * @param channel_id 渠道ID
  18. * @param uid 用户ID
  19. */
  20. public function advert()
  21. {
  22. $channel_id = (integer)$this->request->param('channel_id') ?? 0;
  23. $uid = (integer)$this->request->param('uid') ?? 0;
  24. // region 参数校验
  25. if($channel_id == 0){
  26. $this->error('参数channel_id错误');
  27. }
  28. if($uid == 0){
  29. $this->error('参数uid错误');
  30. }
  31. // endregion
  32. // 广告库,所有广告计划,包括广告素材
  33. $adFull = json_decode(Redis::instance()->get(AdConstants::AD_FULL), true);
  34. $target_data = [];
  35. if($adFull){
  36. $target_data = $adFull;
  37. // (筛选所有非福利广告计划数据) 所有广告位 ()
  38. $postions_arr = AdPlanService::instance()->getAllAdPositions();
  39. if ($postions_arr) {
  40. foreach ($postions_arr as $k => $postion) {
  41. if (isset($target_data['positions'][$postion])) {
  42. $redisIdx = Redis::getRedisIndex(AdConstants::AD_JU_KEY);
  43. $redis = Redis::getRedisConnect($redisIdx);
  44. $pipe = $redis->multi(\Redis::PIPELINE);
  45. //$pipe = Redis::instance()->multi(\Redis::PIPELINE);
  46. foreach ($target_data['positions'][$postion] as $ad_id => $ad_plan) {
  47. $redis_key = AdConstants::AD_JU_KEY . $ad_id . ':' . $channel_id;
  48. $pipe->sIsMember($redis_key, $uid);
  49. }
  50. $rst = $pipe->exec();
  51. $keys_arr = array_keys($target_data['positions'][$postion]);
  52. foreach ($rst as $sub_key => $value) {
  53. $real_key = $keys_arr[$sub_key];
  54. if (isset($target_data['positions'][$postion][$real_key])) {
  55. $group_type = $target_data['positions'][$postion][$real_key]['group_type']; // 广告计划关联的用户组类型, 0 => 全部 1=>指定用户ID 2=>用户条件筛选
  56. // $goup_type == 0 如果广告计划关联的是全部用户,则不删除
  57. if ($group_type > 0) {
  58. // $value = true => 是集合中的成员
  59. if (!$value) {
  60. //如果用户不在集合中, 删除位置
  61. //array_splice($target_data['positions'][$postion], $sub_key, 1);
  62. unset($target_data['positions'][$postion][$real_key]);
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. // 筛选所有福利广告计划数据
  71. if (isset($target_data['welfare_plans'])) {
  72. $redisIdx = Redis::getRedisIndex(AdConstants::AD_JU_KEY);
  73. $redis = Redis::getRedisConnect($redisIdx);
  74. $pipe = $redis->multi(\Redis::PIPELINE);
  75. foreach ($target_data['welfare_plans'] as $ad_id => $ad_plan) {
  76. $redis_key = AdConstants::AD_JU_KEY . $ad_id . ':' . $channel_id;
  77. $pipe->sIsMember($redis_key, $uid);
  78. }
  79. $rst = $pipe->exec();
  80. $keys_arr = array_keys($target_data['welfare_plans']);
  81. foreach ($rst as $sub_key => $value) {
  82. $real_key = $keys_arr[$sub_key];
  83. if (isset($target_data['welfare_plans'][$real_key])) {
  84. $group_type = $target_data['welfare_plans'][$real_key]['group_type']; // 广告计划关联的用户组类型, 0 => 全部 1=>指定用户ID 2=>用户条件筛选
  85. // $goup_type == 0 如果广告计划关联的是全部用户,则不删除
  86. if ($group_type > 0) {
  87. // $value = true => 是集合中的成员
  88. if (!$value) {
  89. //如果用户不在集合中, 删除位置
  90. unset($target_data['welfare_plans'][$real_key]);
  91. }
  92. }
  93. }
  94. }
  95. }
  96. // END 筛选所有福利广告计划数据
  97. }
  98. $this->success('success', $target_data);
  99. }
  100. /**
  101. * API接口:用户点击福利广告后,记录用户信息
  102. * @param channel_id 渠道ID
  103. * @param uid 用户ID
  104. */
  105. public function welfareDot()
  106. {
  107. $uid = (integer)$this->request->param('uid') ?? 0;
  108. //$channel_id = (integer)$this->request->param('channel_id') ?? 0;
  109. if($uid == 0){
  110. $this->error('参数uid错误');
  111. }
  112. $redis_key = AdConstants::AD_WELFARE_USER . date('Ymd') . ':' . $uid;
  113. if (!Redis::instance()->get($redis_key)) {
  114. //Redis::instance()->set($redis_key, 1, 86400);
  115. Redis::instance()->set($redis_key, $uid, 86400);
  116. }
  117. // region 参数校验
  118. /*if($channel_id == 0){
  119. $this->error('参数channel_id错误');
  120. }*/
  121. // 记录用户点击
  122. /*$redis_key = AdConstants::AD_WELFARE . $channel_id;
  123. Redis::instance()->sAdd($redis_key, $uid);
  124. // 计算过期时间 24点过期
  125. $tomorrow_time = strtotime(date('Y-m-d 00:00:00', strtotime("+1 day")));
  126. $ttl = $tomorrow_time - time();
  127. Redis::instance()->expire($redis_key, $ttl);*/
  128. $this->success('success');
  129. }
  130. }