Пример #1
0
 private function _getScratch($responseId, $openId, $type)
 {
     $disable = 1;
     $logTable = 'scratch_log';
     Yii::import('application.modules.scratch.models.ScratchModel');
     Yii::import('application.modules.scratch.models.ScratchLogModel');
     $scratch = ScratchModel::model()->findByPk($responseId);
     if (!$type) {
         $keywords = KeywordsModel::model()->find('type=:type and responseId=:responseId', array(':type' => Globals::TYPE_SCRATCH, ':responseId' => $responseId));
         $content = '参与' . $scratch->title . '请回复:正版(混版)' . $keywords->name;
         $responseObj = new WeChatTextResponse($content);
         return $responseObj;
     }
     //查看刮卡次数
     $totalCount = $scratch->times;
     if ($totalCount == -1) {
         //本活动只能参与一次
         $count = ScratchLogModel::model($logTable)->count('openId=:openId and scratchId=:scratchId', array(':openId' => $openId, ':scratchId' => $scratch->id));
         if ($count > 0) {
             $disable = 0;
         }
     }
     if ($totalCount > 0) {
         $start = strtotime(date('Y-m-d')) - 1;
         $end = strtotime(date('Y-m-d', strtotime('1 days'))) - 1;
         $count = ScratchLogModel::model($logTable)->count('openId=:openId and scratchId=:scratchId and datetime>:start and datetime<:end', array(':openId' => $openId, ':scratchId' => $scratch->id, ':start' => $start, ':end' => $end));
         if ($count >= $totalCount) {
             $disable = 0;
         }
     }
     if ($disable == 0) {
         $content = $totalCount == -1 ? '你本次活动的参与次数已完' : '今天的刮奖次数已完,明天再来吧';
         $responseObj = new WeChatTextResponse($content);
         return $responseObj;
     }
     if ($scratch->startTime > date('Y-m-d H:i:s')) {
         $content = $scratch->unstartMsg ? $scratch->unstartMsg : "抱歉,还未开始呢";
     } elseif ($scratch->endTime < date('Y-m-d H:i:s')) {
         $content = $scratch->endMsg ? $scratch->endMsg : "抱歉,你来晚了";
     } elseif ($scratch->status == 0) {
         $content = $scratch->pauseMsg ? $scratch->pauseMsg : "抱歉,活动暂时停止";
     } else {
         $string = $openId . '|' . $responseId . '|' . $type;
         $code = Globals::authcode($string, 'ENCODE');
         $url = Yii::app()->params['siteUrl'] . Yii::app()->createUrl('scratch/handle', array('code' => $code));
         $responseObj = new WeChatArticleResponse();
         $responseObj->add_article($scratch->title, '', Yii::app()->params['siteUrl'] . '/wechat/' . Yii::app()->params['scratchPath'] . '/' . $scratch->wechatId . '/' . $scratch->backgroundPic, $url);
     }
     $responseObj = isset($responseObj) ? $responseObj : new WeChatTextResponse($content);
     return $responseObj;
 }
Пример #2
0
 public function actionIndex()
 {
     $logTable = 'scratch_log';
     $table = 'scratch_awards';
     $disable = 1;
     $probability = $remainCount = 0;
     $return = $encryption = '';
     $rand = rand(1, 100000);
     $code = Yii::app()->request->getParam('code');
     list($openId, $scratchId, $type) = explode('|', Globals::authcode($code, 'DECODE'));
     $scratch = ScratchModel::model()->findByPk($scratchId);
     //活动是否开始
     if ($scratch->startTime > date('Y-m-d H:i:s')) {
         $disable = false;
     } elseif ($scratch->endTime < date('Y-m-d H:i:s')) {
         $disable = false;
     } elseif ($scratch->status == 0) {
         $disable = false;
     }
     $totalCount = $scratch->times;
     $button = Yii::app()->params['siteUrl'] . '/wechat/' . Yii::app()->params['scratchPath'] . '/' . $scratch->wechatId . '/' . $scratch->button;
     $awards = unserialize($scratch->awards);
     $return['grade'] = -1;
     $return['name'] = '谢谢参与';
     //次数限制
     if ($totalCount == -1) {
         //本活动只能参与一次
         $count = ScratchLogModel::model($logTable)->count('openId=:openId and scratchId=:scratchId', array(':openId' => $openId, ':scratchId' => $scratchId));
         if ($count > 0) {
             $disable = 0;
         }
     }
     if ($totalCount > 0) {
         $start = strtotime(date('Y-m-d')) - 1;
         $end = strtotime(date('Y-m-d', strtotime('1 days'))) - 1;
         $count = ScratchLogModel::model($logTable)->count('openId=:openId and scratchId=:scratchId and datetime>:start and datetime<:end', array(':openId' => $openId, ':scratchId' => $scratchId, ':start' => $start, ':end' => $end));
         if ($count >= $totalCount) {
             $disable = 0;
         } else {
             $remainCount = $totalCount - $count;
         }
     }
     //查看当前用户是否已中奖
     $awardInfo = ScratchAwardsModel::model($table)->find('scratchId=:scratchId and openId=:openId and status<>0', array(':scratchId' => $scratchId, ':openId' => $openId));
     if (!$awardInfo && $disable) {
         foreach ($awards as $k => $v) {
             $probability += $v['probability'] * 1000;
             $award[$k] = array('name' => $v['name'], 'num' => $probability);
         }
         //奖品不足时,发送激活码,
         if ($probability < 100000) {
             $award[] = array('name' => '礼包码', 'num' => 100000);
         }
         //返回给客户端的奖品名称
         foreach ($award as $key => $val) {
             if (isset($award[$key - 1])) {
                 if ($rand > $award[$key - 1]['num'] && $rand <= $val['num']) {
                     //if ($rand > $award[$key - 1]['num']) {
                     $return = $val;
                     $return['grade'] = $key;
                     break;
                 }
             } else {
                 if ($rand > 0 && $rand <= $val['num']) {
                     $return = $val;
                     $return['grade'] = $key;
                     break;
                 }
             }
         }
         if ($return['grade'] > 0) {
             $awardCount = ScratchAwardsModel::model($table)->count('grade=:grade and scratchId=:scratchId and status<>0', array(':grade' => $return['grade'], ':scratchId' => $scratchId));
             //已中奖个数
             if ($awardCount <= 0 || $awardCount < $awards[$return['grade']]['count']) {
                 //未超过系统设置中奖个数
                 $awardModel = new ScratchAwardsModel($table);
                 $awardModel->openId = $openId;
                 $awardModel->scratchId = $scratchId;
                 $awardModel->grade = $return['grade'];
                 $awardModel->code = $return['name'];
                 $awardModel->isentity = isset($awards[$return['grade']]['isentity']) ? $awards[$return['grade']]['isentity'] : 0;
                 $awardModel->status = 0;
                 $awardModel->type = $type;
                 $awardModel->datetime = time();
                 $awardModel->save();
             } else {
                 //奖品达到系统设置个数
                 //查看是否已经赠送了礼包
                 $code = ScratchAwardsModel::model($table)->find('grade=:grade and scratchId=:scratchId and openId=:openId and status<>0', array(':grade' => 0, ':scratchId' => $scratchId, ':openId' => $openId));
                 if (!$code) {
                     //取一条礼包码
                     $code = ScratchAwardsModel::model($table)->find('grade=:grade and scratchId=:scratchId and type=:type and status=:status', array(':grade' => 0, ':scratchId' => $scratchId, ':type' => $type, ':status' => 0));
                     if ($code) {
                         $code->openId = $openId;
                         $code->time = time();
                         $code->save();
                         $return['grade'] = 0;
                         $return['name'] = $code->code;
                         //礼包码
                     }
                 }
             }
         } else {
             //礼包奖品,查询是否已经获得过礼包
             $code = ScratchAwardsModel::model($table)->find('grade=:grade and scratchId=:scratchId and openId=:openId and status<>:status', array(':grade' => 0, ':scratchId' => $scratchId, ':openId' => $openId, ':status' => 0));
             if (!$code) {
                 //取一条礼包码
                 $code = ScratchAwardsModel::model($table)->find('grade=:grade and scratchId=:scratchId and type=:type and status=:status', array(':grade' => 0, ':scratchId' => $scratchId, ':type' => $type, ':status' => 0));
                 if ($code) {
                     $code->openId = $openId;
                     $code->datetime = time();
                     $code->save();
                     $return['grade'] = 0;
                     $return['name'] = $code->code;
                     //礼包码
                 }
             } else {
                 $return['grade'] = -1;
                 $return['name'] = '谢谢参与';
             }
         }
     }
     $encryption = Globals::authcode($openId . '|' . $return['grade'] . '|' . $scratchId, 'ENCODE');
     $this->renderPartial('active', array('scratch' => $scratch, 'prize' => $return, 'encryption' => $encryption, 'button' => $button, 'disable' => $disable, 'remainCount' => $remainCount, 'totalCount' => $totalCount));
 }