123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Elton
- * Date: 2020/6/24
- * Time: 16:07
- *
- * 福利广告用户参与完互动游戏后进行回调操作
- */
- namespace app\api\controller;
- use app\common\library\Redis;
- use app\common\service\AdPlanService;
- use app\main\constants\AdConstants;
- use app\main\service\LogService;
- use app\main\service\UserService;
- use think\Request;
- use think\Response;
- use think\exception\HttpResponseException;
- class Adwelfare
- {
- public function sucOperate()
- {
- $uid = intval(Request::instance()->param('uid')) ?? 0;
- $ad_plan = intval(Request::instance()->param('ad_plan')) ?? 0; // 广告计划ID
- LogService::info("AD:sucOperate:Params:" . json_encode(Request::instance()->param()));
- if (empty($uid) || empty($ad_plan)) {
- LogService::info("AD:sucOperate:参数有误,Params:" . json_encode(Request::instance()->param()));
- $this->success('互动成功');
- }
- LogService::info("AD:".json_encode(Request::instance()->param()));
- //1.通过广告计划获取赠送的书币个数
- $data_plan = AdPlanService::instance()->getWelfarePlan($ad_plan);
- $day_receive_num = !empty($data_plan['day_receive_num']) ? $data_plan['day_receive_num'] : 0; //广告计划中设置的每天互动次数
- $give_kandian = !empty($data_plan['give_kandian']) ? $data_plan['give_kandian'] : 0;
- $redis_key = AdConstants::AD_WELFARE_SUC_NUM . date('Ymd') . ':' . $ad_plan . ':' . $uid;
- //2.给用户增加书币, 免费看点(带有效期)
- if($give_kandian){
- $join_num = (int)Redis::instance()->get($redis_key); // 用户参与的次数
- // 判断用户今日完成了几次互动
- if ($join_num < $day_receive_num) {
- Redis::instance()->incr($redis_key);
- Redis::instance()->expire($redis_key, 86400);
- UserService::instance()->addFreeKanDian($uid, $give_kandian, time(), '活动福利');
- LogService::info("AD:互动福利广告给用户" . $uid . "增加了" . $give_kandian . "看点");
- }else{
- LogService::info("AD:用户" . $uid . "已参与互动广告".$join_num."次,超过广告计划设置的次数".$day_receive_num.",不在加书币了。");
- }
- }else{
- LogService::error("AD:互动福利广告没有设置互动赠送的书币个数,广告ID:".$ad_plan);
- }
- $this->success('互动成功');
- }
- /**
- * 操作成功返回的数据
- * @param string $msg 提示信息
- * @param mixed $data 要返回的数据
- * @param string $type 输出类型
- * @param array $header 发送的 Header 信息
- */
- protected function success($msg = '', $data = '', $type = 'json', array $header = [])
- {
- $this->result($data, 1, $msg, $type, $header);
- }
- /**
- * 操作失败返回的数据
- * @param string $msg 提示信息
- * @param mixed $data 要返回的数据
- * @param string $type 输出类型
- * @param array $header 发送的 Header 信息
- */
- protected function error($msg = '', $data = '', $type = 'json', array $header = [])
- {
- $this->result($data, 0, $msg, $type, $header);
- }
- /**
- * 返回封装后的 API 数据到客户端
- * @access protected
- * @param mixed $data 要返回的数据
- * @param int $code 返回的 code
- * @param mixed $msg 提示信息
- * @param string $type 返回数据格式
- * @param array $header 发送的 Header 信息
- * @return void
- * @throws HttpResponseException
- */
- protected function result($data, $code = 0, $msg = '', $type = '', array $header = [])
- {
- $result = [
- 'code' => $code,
- 'msg' => $msg,
- 'time' => Request::instance()->server('REQUEST_TIME'),
- 'data' => $data,
- ];
- $type = $type ?: $this->getResponseType();
- $response = Response::create($result, $type)->header($header);
- throw new HttpResponseException($response);
- }
- }
|