public function run($args)
 {
     $userModel = new UserModel();
     $pushMsgModel = new PushMsgModel();
     $url = Yii::app()->params['baiduPushUrl'];
     $apikey = Yii::app()->params['baiduPushApiKey'];
     $secret_key = Yii::app()->params['baiduPushSecretKey'];
     //此处替换为应用的secret key
     $data = array();
     $timestamp = time();
     //var_dump($users);exit;
     $leaguePushMsgModel = new LeaguePushMsgModel();
     //找到需要进行消息推送的球队
     $pushData = $leaguePushMsgModel->findPush(0, 1);
     //		var_dump($pushData);exit;
     $pushTeams = array();
     if (isset($pushData[0]->teams)) {
         $pushTeams = $pushData[0]->teams;
     }
     $content = '更新了赛程';
     $method = 'POST';
     $aps = array("alert" => "{$content}", "sound" => "1", "badge" => 1);
     $mess = array("title" => "更新了赛程", "description" => $content, "aps" => $aps);
     if (!empty($pushTeams)) {
         foreach ($pushTeams as $team) {
             $arrContent = array('method' => 'push_msg', 'apikey' => $apikey, 'push_type' => 2, 'tag' => $team, 'device_type' => 4, 'message_type' => 1, 'deploy_status' => 1, 'messages' => json_encode($mess), 'msg_keys' => '1', 'timestamp' => $timestamp, 'expires' => intval($timestamp) + 600);
             $sign = $this->genSign($secret_key, $method, $url, $arrContent);
             $arrContent['sign'] = $sign;
             //var_dump($arrContent);exit;
             $query = http_build_query($arrContent);
             //找到联赛中的球队
             $users = $userModel->findRowsByRelation("footballer", "Team", $team);
             if (!empty($users)) {
                 $saveBatchArr = array();
                 foreach ($users as $user) {
                     if (isset($user->username) && !empty($user->username)) {
                         $extra = array("belongId" => $user->username, "title" => "联赛赛程", "type" => 2, "time" => time(), "targetId" => $user->objectId, "subtype" => 19);
                         $saveArr = array('belongUsername' => $user->username, 'isRead' => 0, 'status' => 0, 'msgType' => 0, 'extra' => json_encode($extra), 'content' => $content);
                         array_push($saveBatchArr, $saveArr);
                         //记录球员接收的推送消息
                         //$result = $pushMsgModel->addPushMsg($saveArr);
                     }
                 }
                 $result = $pushMsgModel->saveBatch($saveBatchArr);
             }
             //var_dump($result);exit;
             //	echo $url.'?'.$query;exit;
             $res = CommonFunction::sendMobileMsg($url . '?' . $query, $data);
             //				$res = Yii::app()->CURL->run($url.'?'.$query,FALSE,$data);
         }
         var_dump($res);
         exit;
     }
 }
Ejemplo n.º 2
0
 /**
  * @desc 分组管理
  */
 public function actionGrouping()
 {
     $masterId = CommonFunction::getUserId();
     $teamGroup = Yii::app()->request->getPost('TeamGroup');
     if (isset($teamGroup['GroupId'])) {
         $hasTeamGroupId = $teamGroup['GroupId'];
         unset($teamGroup['GroupId']);
     }
     //var_dump($hasTeamGroupId);exit;
     $leagueModel = new LeagueModel();
     //判断用户是否有管理联赛的权限
     $res = $leagueModel->allowEditLeague($masterId);
     $leagueId = $res[0]->objectId;
     $doubleLoop = $res[0]->double_loop;
     if (isset($res[0]->city) && isset($res[0]->playground)) {
         $leagueCity = $res[0]->city;
     } else {
         $this->redirect(array('/manage/leagueManage', 'errMsg' => '请先完善联赛信息'));
     }
     if (isset($res[0]->grouped) && $res[0]->grouped == true) {
         Helper::flash('error', "分组完成后,无法再进行分组!", '/site/');
     }
     $groupModel = new GroupModel();
     $teamModel = new TeamModel();
     $TournamentModel = new TournamentModel();
     if (!empty($teamGroup)) {
         $arr = array();
         $leagueTeamIds = array();
         foreach ($teamGroup as $groupName => $teams) {
             $arr = array('name' => $groupName, 'league' => array("reltoleagueid" => $leagueId));
             $existGroup = $groupModel->findLeagueGroupByName($leagueId, $groupName);
             // 				如果已存在此分组,则更新分组数据,否则新增数据
             if (!empty($existGroup)) {
                 throw new CHttpException(500, '分组确认后无法修改!');
             } else {
                 $reponse = $groupModel->save($arr);
                 $groupId = $reponse->objectId;
             }
             //关联分组对应的球队
             $saveGroupTeams = array();
             $relArr = array();
             foreach ($teams as $teamId) {
                 $relArr = array('table' => 'Team', 'reltoid' => $teamId);
                 array_push($saveGroupTeams, $relArr);
             }
             $groupModel->createRelationBatch('teams', $saveGroupTeams, $groupId);
         }
     }
     //找到联赛中的球队
     $leagueTeams = $teamModel->findRowsByRelation("teams", "League", $leagueId);
     $leagueTeamIds = array();
     //取得联赛的球队id
     foreach ($leagueTeams as $lTeam) {
         array_push($leagueTeamIds, $lTeam->objectId);
     }
     if (isset($res[0]->name) && !empty($res[0]->name)) {
         $leagueName = $res[0]->name;
     } else {
         $leagueName = '未知联赛';
     }
     $arr = array('msg' => $leagueName . '更新了联赛赛程', 'status' => 0, 'pushType' => 1, 'leagueName' => $leagueName, 'teams' => $leagueTeamIds);
     $leaguePushMsgModel = new LeaguePushMsgModel();
     $leaguePushMsgModel->save($arr);
     //先找到联赛中的组
     $groupRows = $groupModel->findByLeagueId($leagueId);
     $groupTeams = array();
     $groupTeamsArr = array();
     foreach ($groupRows as $group) {
         $groupId = $group->objectId;
         //找到某个小组的球队
         $groupRelTeams = $teamModel->findRowsByRelation("teams", "Group", $groupId);
         foreach ($groupRelTeams as $team) {
             $team->groupName = $group->name;
             $team->groupObjectId = $group->objectId;
             foreach ($leagueTeams as $lTeam) {
                 if ($lTeam->objectId == $team->objectId) {
                     $lTeam->selected = 1;
                 }
             }
         }
         if (!empty($teamGroup)) {
             $teamCount = count($groupRelTeams);
             //循环创建联赛赛事
             if ($teamCount > 1) {
                 $dataBatch = array();
                 for ($i = 0; $i < $teamCount; $i++) {
                     for ($j = $i + 1; $j < $teamCount; $j++) {
                         $homeCourtId = $groupRelTeams[$i]->objectId;
                         $opponentId = $groupRelTeams[$j]->objectId;
                         $data = array('name' => $groupRelTeams[$i]->name . " - " . $groupRelTeams[$j]->name, 'city' => $leagueCity, 'nature' => Yii::app()->params['NatureGroupMatch'], 'home_court' => array('homecourtid' => $homeCourtId), 'opponent' => array('opponentid' => $opponentId), 'league' => array('leagueid' => $leagueId), 'group' => array('groupid' => $groupId));
                         array_push($dataBatch, $data);
                         //							$TournamentRow = $TournamentModel->saveRow($data);
                         //							$TournamentId = $TournamentRow->objectId;
                         //							//关联到赛事的球队
                         //							$TournamentModel->createPointer('home_court','Team',$TournamentId,$homeCourtId);
                         //							$TournamentModel->createPointer('opponent','Team',$TournamentId,$opponentId);
                         //							//关联到赛事所在联赛
                         //							$TournamentModel->createPointer('league','League',$TournamentId,$leagueId);
                         //							//关联到赛事所在小组
                         //							$TournamentModel->createPointer('group','Group',$TournamentId,$groupId);
                     }
                 }
                 //双循环(此处增加一个循环)
                 if ($res[0]->double_loop) {
                     for ($i = 0; $i < $teamCount; $i++) {
                         for ($j = $i + 1; $j < $teamCount; $j++) {
                             $homeCourtId = $groupRelTeams[$j]->objectId;
                             $opponentId = $groupRelTeams[$i]->objectId;
                             $data = array('name' => $groupRelTeams[$j]->name . " - " . $groupRelTeams[$i]->name, 'city' => $leagueCity, 'nature' => Yii::app()->params['NatureGroupMatch'], 'home_court' => array('homecourtid' => $homeCourtId), 'opponent' => array('opponentid' => $opponentId), 'league' => array('leagueid' => $leagueId), 'group' => array('groupid' => $groupId));
                             array_push($dataBatch, $data);
                         }
                     }
                 }
                 $TournamentRow = $TournamentModel->saveBatch($dataBatch);
             }
         }
         $groupTeamsArr = $groupRelTeams;
         array_push($groupTeams, $groupTeamsArr);
     }
     if (!empty($teamGroup)) {
         //更新赛程发送推送
         $baiduPushModel = new BaiduPushModel();
         $baiduPushModel->sendMessageForAllLeague(4, 20, $leagueId, "更新了赛程");
         //将联赛标定为已分组
         $upadteGrouped = $leagueModel->update($leagueId, array('grouped' => true));
         $this->redirect('/manage/schedule');
     }
     $this->render('grouping', array('leagueTeams' => $leagueTeams, 'leagueMsg' => $res[0], 'groupTeams' => $groupTeams));
 }