Adwelfare.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Elton
  5. * Date: 2020/6/24
  6. * Time: 16:07
  7. *
  8. * 福利广告用户参与完互动游戏后进行回调操作
  9. */
  10. namespace app\api\controller;
  11. use app\common\library\Redis;
  12. use app\common\service\AdPlanService;
  13. use app\main\constants\AdConstants;
  14. use app\main\service\LogService;
  15. use app\main\service\UserService;
  16. use think\Request;
  17. use think\Response;
  18. use think\exception\HttpResponseException;
  19. class Adwelfare
  20. {
  21. public function sucOperate()
  22. {
  23. $uid = intval(Request::instance()->param('uid')) ?? 0;
  24. $ad_plan = intval(Request::instance()->param('ad_plan')) ?? 0; // 广告计划ID
  25. LogService::info("AD:sucOperate:Params:" . json_encode(Request::instance()->param()));
  26. if (empty($uid) || empty($ad_plan)) {
  27. LogService::info("AD:sucOperate:参数有误,Params:" . json_encode(Request::instance()->param()));
  28. $this->success('互动成功');
  29. }
  30. LogService::info("AD:".json_encode(Request::instance()->param()));
  31. //1.通过广告计划获取赠送的书币个数
  32. $data_plan = AdPlanService::instance()->getWelfarePlan($ad_plan);
  33. $day_receive_num = !empty($data_plan['day_receive_num']) ? $data_plan['day_receive_num'] : 0; //广告计划中设置的每天互动次数
  34. $give_kandian = !empty($data_plan['give_kandian']) ? $data_plan['give_kandian'] : 0;
  35. $redis_key = AdConstants::AD_WELFARE_SUC_NUM . date('Ymd') . ':' . $ad_plan . ':' . $uid;
  36. //2.给用户增加书币, 免费看点(带有效期)
  37. if($give_kandian){
  38. $join_num = (int)Redis::instance()->get($redis_key); // 用户参与的次数
  39. // 判断用户今日完成了几次互动
  40. if ($join_num < $day_receive_num) {
  41. Redis::instance()->incr($redis_key);
  42. Redis::instance()->expire($redis_key, 86400);
  43. UserService::instance()->addFreeKanDian($uid, $give_kandian, time(), '活动福利');
  44. LogService::info("AD:互动福利广告给用户" . $uid . "增加了" . $give_kandian . "看点");
  45. }else{
  46. LogService::info("AD:用户" . $uid . "已参与互动广告".$join_num."次,超过广告计划设置的次数".$day_receive_num.",不在加书币了。");
  47. }
  48. }else{
  49. LogService::error("AD:互动福利广告没有设置互动赠送的书币个数,广告ID:".$ad_plan);
  50. }
  51. $this->success('互动成功');
  52. }
  53. /**
  54. * 操作成功返回的数据
  55. * @param string $msg 提示信息
  56. * @param mixed $data 要返回的数据
  57. * @param string $type 输出类型
  58. * @param array $header 发送的 Header 信息
  59. */
  60. protected function success($msg = '', $data = '', $type = 'json', array $header = [])
  61. {
  62. $this->result($data, 1, $msg, $type, $header);
  63. }
  64. /**
  65. * 操作失败返回的数据
  66. * @param string $msg 提示信息
  67. * @param mixed $data 要返回的数据
  68. * @param string $type 输出类型
  69. * @param array $header 发送的 Header 信息
  70. */
  71. protected function error($msg = '', $data = '', $type = 'json', array $header = [])
  72. {
  73. $this->result($data, 0, $msg, $type, $header);
  74. }
  75. /**
  76. * 返回封装后的 API 数据到客户端
  77. * @access protected
  78. * @param mixed $data 要返回的数据
  79. * @param int $code 返回的 code
  80. * @param mixed $msg 提示信息
  81. * @param string $type 返回数据格式
  82. * @param array $header 发送的 Header 信息
  83. * @return void
  84. * @throws HttpResponseException
  85. */
  86. protected function result($data, $code = 0, $msg = '', $type = '', array $header = [])
  87. {
  88. $result = [
  89. 'code' => $code,
  90. 'msg' => $msg,
  91. 'time' => Request::instance()->server('REQUEST_TIME'),
  92. 'data' => $data,
  93. ];
  94. $type = $type ?: $this->getResponseType();
  95. $response = Response::create($result, $type)->header($header);
  96. throw new HttpResponseException($response);
  97. }
  98. }