| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- <?php
- namespace mobile\controllers;
- use Yii;
- use common\components\util;
- use common\models\xhLuckyDrawAwardCustomer;
- use common\models\xhLuckyDrawAwardParticipant;
- use common\services\xhLuckyDrawAwardService;
- use common\services\xhLuckyDrawActivityService;
- use common\services\xhUserService;
- use common\services\xhLuckyDrawAwardParticipantService;
- use common\services\xhLuckyDrawAwardCustomerService;
- class LotteryController extends MobileendController
- {
- /**
- * 抽奖活动首页
- * @return string
- */
- public function actionIndex()
- {
- $luckyDrawActivityId = Yii::$app->request->get('id');
- $inviteEncriptKey = urldecode(Yii::$app->request->get('encryptKey'));//加密的用户ID值
- $inviteEncriptKey = empty($inviteEncriptKey) ? '' : $inviteEncriptKey;
- $account = Yii::$app->request->get('account');
- $needAttention = 0;
- $needVip = 0;
- $activity = xhLuckyDrawActivityService::getById($luckyDrawActivityId);
- if(empty($activity)){
- $this->error($_GET['account']);
- }
- //取奖品
- $awardIds = $activity['awardIds'];
- $awardIdsArr = explode(',', $awardIds);
- foreach ($awardIdsArr as $awardId){
- $awards[] = xhLuckyDrawAwardService::getById($awardId);
- }
- //无奖品情况的特殊处理
- if(empty($awards[0])){
- unset($awards[0]);
- }
- /* if(!empty($awards)){
- $awards = \common\components\util::arraySequence($awards, 'order', 'SORT_ASC');
- }*/
- if(!empty($awards)){
- $newAwards = [];
- foreach($awards as $key => $award){
- $newAwards[$award['order']] = $award;
- }
- $awards = $newAwards;
- }
- //dump($awards);die;
- //$this->merchantId = 1;
- $this->merchantCheck($activity, $account);//检测权限
- $customer = xhUserService::getById($this->userId);
- //$isMember = $customer['isMember'];//会员
- $isMember = isset($customer['mobile']) ? 1 : 0;//会员暂时用有无手机号识别
- $subscribe = $customer['subscribe'];//关注
- if($activity['needAttention'] == 1 && $subscribe == 0){//活动需关注
- $needAttention = 1;
- }
- if($activity['needVip'] == 1 && $isMember == 0){//活动需是会员
- $needVip = 1;
- }
- $participant = xhLuckyDrawAwardParticipant::getByCondition(['uid'=>$this->userId, 'luckyDrawActivityId'=>$luckyDrawActivityId]);
- if(empty($participant)){
- $drawCount = $activity['drawCount'];
- }else{
- $drawCount = xhLuckyDrawAwardParticipantService::getDrawCount($participant['id'], $activity['drawCount'], $activity['rewardCount'], $activity['inviteCount']);
- }
- return $this->renderPartial('index', [
- 'account'=>$account,
- 'activity'=>$activity,
- 'luckyDrawActivityId'=>$luckyDrawActivityId,
- 'drawCount'=>$drawCount,//客户拥有抽奖次数
- 'awardsCount'=>count($awards),//奖品数量
- 'awards' => $awards,
- 'uid' => $this->userId,
- 'needAttention' => $needAttention,
- 'needVip' => $needVip,
- 'encryptKey' => Yii::$app->getSecurity()->encryptByPassword($this->userId, Yii::$app->params['luckDrawSecretKey']),
- 'inviteEncriptKey' => $inviteEncriptKey,//邀请人的uid(加密过的)
- ]);
- }
- /**
- * 抽奖活动介绍
- */
- public function actionActivityIntro(){
- $luckyDrawActivityId = Yii::$app->request->get('id');
- $account = Yii::$app->request->get('account');
- $activity = xhLuckyDrawActivityService::getById($luckyDrawActivityId);
- $this->merchantCheck($activity, $account);//检测权限
- dump($activity);
- }
- /**
- * 中奖列表
- * @return string
- */
- public function actionWinList()
- {
- $luckyDrawActivityId = Yii::$app->request->post('id');
- $condition = ['luckyDrawActivityId' => $luckyDrawActivityId];
- $winnerList = xhLuckyDrawAwardCustomer::getAllByCondition($condition);
- if(!empty($winnerList)){
- foreach ($winnerList as $key => $winner){
- $uid = $winner['uid'];
- $customer = xhUserService::getById($uid);
- $winnerList[$key]['avatar'] = $customer['avatar'];
- }
- }
- //按照奖品分类,展出获奖者
- util::successInfo('获取成功', 'success', ['winnerList'=>$winnerList]);
- }
- /**
- * 我参与的活动
- * @return string
- */
- public function actionMyParticipation()
- {
- }
- /**
- * 我获得的奖品
- */
- public function actionMyAwards(){
- $uid = $this->userId;
- $luckyDrawActivityId = Yii::$app->request->post('id');
- $condition = ['uid' => $uid, 'luckyDrawActivityId' => $luckyDrawActivityId];
- $winnerList = xhLuckyDrawAwardCustomer::getAllByCondition($condition);
- util::successInfo('获取成功', 'success', ['winnerList'=>$winnerList]);
- }
- /**
- * 按概率进行抽奖
- */
- public function actionDraw(){
- $encryptKey = Yii::$app->request->get('key');//加密的用户ID值
- $post = Yii::$app->request->post();
- $luckyDrawActivityId = intval($post['id']);
- $totalProbability = 1000;//总概率大小
- $inviteUid = 0;
- $awardSortKeys = [];//所有奖品的排列序号
- $activity = xhLuckyDrawActivityService::getById($luckyDrawActivityId);
- //更新客户抽奖数
- $participant = xhLuckyDrawAwardParticipant::getByCondition(['uid'=>$this->userId, 'luckyDrawActivityId'=>$luckyDrawActivityId]);
- if(empty($participant)){//新增参与者
- if(!empty($encryptKey)){//判断是否受邀请而来,是:更新邀请者的数据
- $encryptKey = urldecode($encryptKey);
- $inviteUid = Yii::$app->getSecurity()->decryptByPassword($encryptKey, Yii::$app->params['luckDrawSecretKey']);
- Yii::warning('lottory:InviteUid '.$inviteUid, __METHOD__);
- if(!is_numeric($inviteUid)){
- $this->error($_GET['account']);
- }
- if($inviteUid != $this->userId){//排除自己邀请自己的情形
- //更新此$inviteUid邀请人数
- $inviteParticipant = xhLuckyDrawAwardParticipant::getByCondition(['uid'=>$inviteUid, 'luckyDrawActivityId'=>$luckyDrawActivityId]);
- if(empty($inviteParticipant)) {//若推荐人未参加抽奖,也给其记录推荐好友数,但不记录参与时间
- $data = [
- 'luckyDrawActivityId' => $activity['id'],
- 'uid' => $inviteUid,
- 'useDrawCount' => 0,
- 'inviteCount' => 1,
- //'participateTime' => time(),
- 'invite' => 0,
- ];
- xhLuckyDrawAwardParticipantService::add($data);
- }else{
- $data = ['inviteCount' => $inviteParticipant['inviteCount'] + 1];//更新邀请好友数
- xhLuckyDrawAwardParticipantService::updateById($inviteParticipant['id'], $data);
- }
- }
- }
- Yii::warning('123 como int');
- $drawCount = $activity['drawCount'];
- $data = [
- 'luckyDrawActivityId' => $activity['id'],
- 'uid' => $this->userId,
- 'useDrawCount' => 1,
- 'participateTime' => time(),
- 'invite' => $inviteUid,
- ];
- xhLuckyDrawAwardParticipantService::add($data);
- $data = ['participants' => $activity['participants']+1];
- xhLuckyDrawActivityService::updateById($luckyDrawActivityId, $data);
- }else{
- $drawCount = xhLuckyDrawAwardParticipantService::getDrawCount($participant['id'], $activity['drawCount'], $activity['rewardCount'], $activity['inviteCount']);
- if($drawCount <= 0){
- util::failInfo('您已无抽奖机会,去邀请好友获取吧!', 'A0004', ['key'=>-1, 'drawCount'=>$drawCount]);
- Yii::$app->end();
- }
- $data = ['useDrawCount' => $participant['useDrawCount'] + 1];
- if($participant['participateTime'] == 0){//邀请他人新增的记录,无参与时间及增加抽奖活动的人数
- $data['participateTime'] = time();
- $activityData = ['participants' => $activity['participants']+1];
- xhLuckyDrawActivityService::updateById($luckyDrawActivityId, $activityData);
- }
- xhLuckyDrawAwardParticipantService::updateById($participant['id'], $data);
- }
- //获取该抽奖活动的奖品数据
- $awardIds = $activity['awardIds'];
- $awardIdsArr = explode(',', $awardIds);
- foreach ($awardIdsArr as $awardId){
- $awards[] = xhLuckyDrawAwardService::getById($awardId);
- }
- //无奖品情况的特殊处理
- if(empty($awards[0])){
- unset($awards[0]);
- }
- /*$awards = \common\components\util::arraySequence($awards, 'order', 'SORT_ASC');*/
- if(!empty($awards)){
- $newAwards = [];
- foreach($awards as $key => $award){
- $newAwards[$award['order']] = $award;
- $awardSortKeys[] = $award['order'];
- }
- $awards = $newAwards;
- }
- //dump($awards);die;
- foreach ($awards as $key => $val) {
- $arr[$key] = $val['probability'];
- }
- $key = $this->getRand($arr, $totalProbability); //根据概率获取奖品id
- if($key == ''){
- $count = count($awards);
- while(true){
- $randomKey = mt_rand($count, 8);
- if(!in_array($randomKey, $awardSortKeys)){
- $key = $randomKey;
- break;
- }
- }
- $code = 'notHit';
- $msg = '未中奖';
- }else{
- //验证有无奖品,有往下走程序,否则报未中奖(终止)
- $award = xhLuckyDrawAwardService::getById($awards[$key]['id']);
- if(($award['count'] - $award['drawCount']) == 0 ){
- $code = 'notHit';
- $count = count($awards);
- while(true){
- $randomKey = mt_rand($count, 8);
- if(!in_array($randomKey, $awardSortKeys)){
- $key = $randomKey;
- break;
- }
- }
- $msg = '未中奖';
- }else{
- $code = 'hit';
- $key = $awards[$key]['order'];
- $msg = '获得'.$awards[$key]['awardName'];
- //记录获奖客户
- $data = [
- 'uid' => $this->userId,
- 'userName' => $this->userName,
- 'awardId' => $awards[$key]['id'],
- 'awardName' => $awards[$key]['awardName'],
- 'count' => 1,
- 'luckyDrawActivityId' => $luckyDrawActivityId,
- 'hitTime' => time(),
- ];
- xhLuckyDrawAwardCustomerService::add($data);
- //更新奖品数量
- $data = ['drawCount' => $awards[$key]['drawCount']+1];
- xhLuckyDrawAwardService::updateById($awards[$key]['id'], $data);
- }
- }
- $drawCount -= 1;
- util::successInfo($msg, $code, ['key'=>$key, 'drawCount'=>$drawCount]);
- }
- /**
- * 中奖运算
- * @param $proArr
- * @param $totalProbability
- * @return int|string
- */
- private function getRand($proArr, $totalProbability) { //计算中奖概率
- $rs = ''; //中奖结果
- $proSum = $totalProbability; //概率数组的总概率精度
- //概率数组循环
- foreach ($proArr as $key => $proCur) {
- $randNum = mt_rand(1, $proSum);
- if ($randNum <= $proCur) {
- $rs = $key;
- break;
- } else {
- $proSum -= $proCur;
- }
- }
- return $rs;
- }
- }
|