123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- <?php
- namespace app\index\controller;
- use app\common\controller\Frontend;
- use app\common\library\Redis;
- use app\common\service\CardFlipService;
- use app\main\constants\ActivityConstants;
- use app\main\constants\KafkaDotConstants;
- use app\main\constants\UrlConstants;
- use app\main\constants\UserConstants;
- use app\main\model\object\AnalysisObject;
- use app\main\service\AdminService;
- use app\main\service\KafkaDotService;
- use app\main\service\SubscripService;
- use app\main\service\UrlService;
- use app\main\service\UserService;
- use think\Config;
- use think\Cookie;
- use think\Request;
- class Activity extends Frontend
- {
- protected $layout = '';
- /**
- * @return mixed
- * @throws \think\Exception
- */
- public function pay()
- {
- $id = intval($_GET['id']);
- $aid = intval($_GET['aid'] ?? 0);
- UserService::instance()->setUserCache(Request::instance()->get('user_id'));
- $is_range = 0;
- $user_id = UserService::instance()->getUserInfo()->id;
- $admin_id = UserService::instance()->getAdminId()->data;
- $channel_id = UserService::instance()->getUserInfo()->channel_id;
- if ($this->request->has('isrange') || $this->request->has('isRange')) {
- $is_range = 1;
- }
- if ($this->request->has('visitor')) {
- Cookie::set('visitor', $this->request->get('visitor'));
- }
- /**
- * 获取渠道商/代理商资料
- */
- $adminExtend = AdminService::instance()->getAdminExtendModel()->getInfo($admin_id);
- if (!$adminExtend) {
- $this->error('管理员信息获取失败');
- }
- $activityInfo = model('Activity')->getInfo($aid);
- $isGiveAct = $activityInfo['type'] == ActivityConstants::ACTIVITY_TYPE_GIVE;
- if ($activityInfo && in_array($activityInfo['type'],
- [ActivityConstants::ACTIVITY_TYPE_CUSTOM, ActivityConstants::ACTIVITY_TYPE_GIVE])) {
- $resource_info = (model('Resource')->getInfo($id));
- $res['starttime'] = $activityInfo['starttime'];
- $res['endtime'] = $activityInfo['endtime'];
- $res['astatus'] = $activityInfo['status'];
- $res['aname'] = $activityInfo['name'];
- $res = array_merge($res, $resource_info);
- $res['activity_id'] = $aid;
- if (intval($activityInfo['limited'])) {
- $redis = Redis::instance();
- //用户在渠道自定义的活动充值次数统计
- if ($activityInfo['type'] == ActivityConstants::ACTIVITY_TYPE_CUSTOM) {
- $pay_count = intval($redis->hGet("ATPU:{$aid}", $user_id));
- if (intval($activityInfo['limited']) <= $pay_count) {
- $this->error("该活动限购 {$activityInfo['limited']} 次");
- }
- }
- //用户在赠币的活动充值次数统计
- if ($isGiveAct) {
- $this->assign('the_max_time', $activityInfo['limited']);
- $pay_count = intval($redis->hGet("ATGP:{$aid}", $user_id));
- if (intval($activityInfo['limited']) <= $pay_count) {
- $this->assign('is_max_end',1);
- }
- }
- }
- } else {
- $res = model('resource')->join('activity a', 'a.id=resource.activity_id')->where('resource.id', $id)
- ->field('a.starttime,a.endtime,a.status as astatus,a.name as aname,resource.*')->find();
- }
- if(!$res){
- $this->error('活动不存在');
- }
- $recent_url = getCurrentDomain($channel_id, '/index/user/recent');
- $this->assign('recent_url', $recent_url);
- if ($res['activity_id'] == 100) {
- if (UserService::instance()->getUserInfo()->is_pay == UserConstants::USER_IS_PAY_YES) {
- $is_range = 1;
- }
- }
- if($res['endtime'] < $this->time||empty($res['astatus'])){
- $this->assign('isend',1);
- }
- if($res['starttime'] > $this->time && !empty($res['astatus'])){
- $this->assign('needWait',1);
- $lastTime = $this->lastTime($res['starttime']);
- $this->assign('lastTime',$lastTime);
- }
- $arr = [];
- array_push($arr,"\r\n");
- array_push($arr,"\r");
- array_push($arr,"\n");
- $pay_url = '';
- $res['warning'] = str_replace($arr,"<br/>",$res['warning']);
- $to_url = getCurrentDomain($channel_id,'/index/user/recent');
- if ($admin_id) {
- $admin_info = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($admin_id);
- if (!empty($admin_info) && $admin_info['appid']) {
- $to_url = getCurrentDomain($admin_id, '/index/user/recent');
- }
- if ($admin_info) {
- $menuophost = $info['menuophost'] ?? null;
- $menuwxpay_host = $info['menuwxpay_host'] ?? null;
- if ($menuophost && $menuwxpay_host && $menuophost == getCurrentOphost()) {
- $pay_url = Config::get('site.scheme') . '://' . $admin_info['menuwxpay_host'] . '/index/recharge/pay?from=wechat&admin_id=' . $admin_id . '&channel_id=' . $channel_id . '&user_id=' . $user_id;
- } else {
- $pay_url = Config::get('site.scheme') . '://' . $admin_info['wxpay_pay_host'] . '/index/recharge/pay?from=wechat&admin_id=' . $admin_id . '&channel_id=' . $channel_id . '&user_id=' . $user_id;
- }
- }
- }
- $payment_method = empty($admin_info['payment_method']) ? '0' : '1';
- $this->assign('res', $res);
- $this->assign('isGiveAct', $isGiveAct);
- $this->assign('payment_method', $payment_method);
- $this->assign('aid', $res['activity_id']);
- $this->assign('payUrl', $pay_url);
- $this->assign('toUrl', $to_url);
- $this->assign('isRange', $is_range);
- return $this->fetch();
- }
- /**
- * 获取赠送书币
- */
- public function getActGiveMoney()
- {
- $redis = Redis::instance();
- $act_id = $this->request->get('act_id');
- $res_id = $this->request->get('res_id');
- $user_id = UserService::instance()->getUserInfo()->id;
- if (empty($user_id)) {
- $this->error("获取用户信息失败");
- }
- $activity_give_config = Config::get('site.activity_give_config');
- if (!$act_id || !$res_id || !is_numeric($act_id) || !is_numeric($res_id)) {
- $this->error("参数错误", 'Index/User/recent');
- }
- //如果是赠币活动在处理
- if (model('Activity')->checkIsGiveActivity($act_id)) {
- $activity_info = model('Activity')->getInfo($act_id);
- $resource_info = model('Resource')->getInfo($res_id);
- $res['starttime'] = $activity_info['starttime'];
- $res['endtime'] = $activity_info['endtime'];
- $res['astatus'] = $activity_info['status'];
- if (!$activity_info || !$resource_info) {
- $this->error('活动不存在');
- } else {
- //检查活动是否结束
- if ($activity_info['endtime'] < $this->time|| empty($activity_info['status'])) {
- $this->error("活动已结束", 'Index/User/recent');
- }
- //检查活动开始
- if ($activity_info['starttime'] > $this->time && !empty($activity_info['status'])) {
- $this->error("活动还未开始");
- }
- //检查限购
- if (intval($activity_info['limited'])) {
- $pay_count = intval($redis->hGet("ATGP:{$act_id}", $user_id));
- if (intval($activity_info['limited']) <= $pay_count) {
- $this->error("该活动限购 {$activity_info['limited']} 次");
- }
- }
- //写入Recharge表
- $recharge_data = [
- 'user_id' => $user_id,
- 'type' => 6,
- 'free_kandian' => $resource_info['free_kandian'],
- 'remain_free_kandian' => $resource_info['free_kandian'],
- 'free_endtime' => intval($resource_info['free_day']) * 86400 + time(),
- 'createtime' => time(),
- 'updatetime' => time()
- ];
- if (!$recharge_id = model('Recharge')->setConnect($user_id)->insertGetId($recharge_data)) {
- $this->error("写入数据失败");
- }
- $oAna = new AnalysisObject();
- $oAna->type = KafkaDotConstants::TYPE_ACTIVITY;
- $oAna->event_time = Request::instance()->time();
- $oAna->user_from = [
- 'activity_id' => $act_id,
- ];
- $oAna->data = [
- 'stage' => 'start'
- ];
- KafkaDotService::instance()->sendMsg($user_id, $oAna);
- $oAna->data = [
- 'stage' => 'end'
- ];
- KafkaDotService::instance()->sendMsg($user_id, $oAna);
- //赠送的免费书币存到redis
- $zkey = 'ZR:' . $user_id;
- $redis->del($zkey);
- //写入次数限制
- $pay_count_key = "ATGP:{$act_id}";
- $redis->hIncrBy($pay_count_key, $user_id, 1);
- $redis->expire($pay_count_key, (intval($activity_give_config['time_interval']) * 86400) * 2);
- //成功
- $this->success("操作成功", 'Index/User/recent');
- }
- } else {
- $this->error("错误的请求", 'Index/User/recent');
- }
- }
- /**
- * 剩余时间
- * @param int $unixEndTime
- * @return string
- */
- function lastTime($unixEndTime=0)
- {
- if ($unixEndTime <= $this->time) { // 如果过了活动终止日期
- return '0天0时0分';
- }
- // 使用当前日期时间到活动截至日期时间的毫秒数来计算剩余天时分
- $time = $unixEndTime - $this->time;
- $days = 0;
- if ($time >= 86400) { // 如果大于1天
- $days = (int)($time / 86400);
- $time = $time % 86400; // 计算天后剩余的毫秒数
- }
- $xiaoshi = 0;
- if ($time >= 3600) { // 如果大于1小时
- $xiaoshi = (int)($time / 3600);
- $time = $time % 3600; // 计算小时后剩余的毫秒数
- }
- $fen = (int)($time / 60); // 剩下的毫秒数都算作分
- if(empty($fen)){
- $fen = 1;
- }
- return $days.'天'.$xiaoshi.'时'.$fen.'分';
- }
- /**
- * 订阅活动阅读记录页面
- * @return mixed
- */
- public function subscribe()
- {
- $userInfo = UserService::instance()->getUserInfo();
- $actList = SubscripService::instance()->getUserActList($userInfo->id);
- $this->assign('actList',$actList);
- $this->view->assign('adminconfig',$this->adminconfig);
- return $this->fetch('record');
- }
- /**
- * 订阅活动打卡记录页面
- */
- public function UserActLog()
- {
- $actId = $this->request->request('actId',0);
- $userInfo = UserService::instance()->getUserInfo();
- $activity = SubscripService::instance()->getActivityById($actId);
- $state = SubscripService::instance()->checkUserState($userInfo->id,$actId);
- $this->view->assign('adminconfig',$this->adminconfig);
- if ( $state == 3 ){//如果活动时间已经结束,用户还没有被标记为成功,直接标记为失败,跳转到失败页面
- return $this->fetch('fail',$activity);
- }
- if ($state == 2){
- return $this->fetch('success',$activity);
- }
- if ( $activity['end_date'] < date('Ymd',time()) && $state == 1 ){
- SubscripService::instance()->updateUserState($userInfo->id,$actId,3);
- return $this->fetch('fail',$activity);
- }
- $actLog = SubscripService::instance()->getUserActLog($userInfo,$actId,$activity);
- return $this->fetch('record_info',$actLog);
- }
- /**
- * 订阅活动成功页面
- */
- public function actSuccess()
- {
- $actId = $this->request->request('actId',0);
- $activity = SubscripService::instance()->getActivityById($actId);
- $this->view->assign('adminconfig',$this->adminconfig);
- return $this->fetch('success',$activity);
- }
- /**
- * 订阅活动失败页面
- */
- public function actFail()
- {
- $actId = $this->request->request('actId',0);
- $activity = SubscripService::instance()->getActivityById($actId);
- $this->view->assign('adminconfig',$this->adminconfig);
- return $this->fetch('fail',$activity);
- }
- /**
- * 消耗活动-翻牌活动
- */
- public function cardFlip()
- {
- //跳转到业务域名
- if(UrlService::instance()->checkDomainIsShare()) { //是活动域名
- $userInfo = UserService::instance()->getUserInfo();
- $actId = $this->request->request('actId',0);
- $params['from'] = 'wechat';
- $params['user_id'] = $userInfo->id;
- $params['actId'] = $actId;
- $host = getCurrentDomain($userInfo->channel_id,'/index/activity/cardFlip').'?';
- $host .= http_build_query($params);
- $this -> redirect($host);
- }
- $actId = $this->request->request('actId',0);
- $cardInfo = CardFlipService::instance()->getCardInfo($actId);
- if (empty($cardInfo)){
- return $this->fetch('index@activity/noactive');
- }
- $userInfo = UserService::instance()->getUserInfo();
- $isHave = CardFlipService::instance()->checkUserAuth($cardInfo['group_id'],$userInfo);
- if (!$isHave){
- return $this->fetch('index@activity/noactive');
- }
- //kafka打点
- CardFlipService::instance()->sendShow($actId,$userInfo);
- //活动是否有抽奖次数
- $drawsNum = CardFlipService::instance()->getDrawNum($cardInfo['id'],$userInfo->id);
- $cardInfo['draw_text'] = $drawsNum ? '本次免费' : '每次消耗'.$cardInfo['kandian'].'书币';
- $this->assign('log_host', Config::get('site.loghost')); //设置打点域名
- return $this->fetch('card',$cardInfo);
- }
- }
|