public function actionRegister()
 {
     $type = Yii::$app->request->get('type');
     $trainId = Yii::$app->request->get('train_id');
     $session = Yii::$app->session;
     if (Yii::$app->request->isPost) {
         $trainId = Yii::$app->request->post('train_id');
         if (empty($trainId)) {
             throw new ServerErrorHttpException('请重新选择课程!');
         }
         $registerParams = Yii::$app->request->post();
         if (empty($registerParams['email']) && empty($registerParams['phone'])) {
             throw new ServerErrorHttpException('请输入手机号或者邮箱!');
         }
         if (empty($registerParams['password'])) {
             throw new ServerErrorHttpException('请填写密码!');
         }
         if ($registerParams['password'] != $registerParams['password_repeat']) {
             throw new ServerErrorHttpException('两次输入的密码不相同,请重新输入!');
         }
         $usersModel = new Users();
         if (!empty($registerParams['email'])) {
             //验证
             $isExist = Users::findOne(['email' => $registerParams['email']]);
             if (!empty($isExist)) {
                 throw new ServerErrorHttpException('已经存在的邮箱地址!');
             }
             $registerInfo = ['_csrf' => $registerParams['_csrf'], 'Users' => ['email' => $registerParams['email'], 'password' => $registerParams['password'], 'score' => 0, 'username' => '注册学员' . time(), 'level_id' => 1, 'level_order' => 1]];
         } else {
             $isExist = Users::findOne(['mobile_phone' => $registerParams['phone']]);
             if (!empty($isExist)) {
                 throw new ServerErrorHttpException('已经存在的手机号码!');
             }
             if ($registerParams['check_num'] != $session['checkNum']) {
                 throw new ServerErrorHttpException('短信验证码输入错误,请重新输入!');
             }
             //验证手机
             $registerInfo = ['_csrf' => $registerParams['_csrf'], 'Users' => ['mobile_phone' => $registerParams['phone'], 'password' => $registerParams['password'], 'username' => '注册学员' . time(), 'score' => 0, 'level_id' => 1, 'level_order' => 1]];
         }
         $transaction = Yii::$app->db->beginTransaction();
         if ($usersModel->load($registerInfo) && $usersModel->save()) {
             //新增一条用users_level
             $userLevelModel = new UsersLevel();
             $userLevelModel->user_id = $usersModel->id;
             $userLevelModel->level_id = 1;
             $userLevelModel->train_id = $trainId;
             if (!$userLevelModel->save()) {
                 $transaction->rollBack();
                 throw new ServerErrorHttpException('更新状态错误,原因:' . json_encode($userLevelModel->errors, JSON_UNESCAPED_UNICODE) . '!');
             } else {
                 $trainUsersOrder = TrainUsers::getTrainUsersOrder($trainId);
                 if (empty($trainUsersOrder)) {
                     $trainUsersOrder = 1;
                 } else {
                     $trainUsersOrder = $trainUsersOrder + 1;
                 }
                 //新增一条用train_users
                 $trainUsers = new TrainUsers();
                 $trainUsers->train_id = $trainId;
                 $trainUsers->level_id = 2;
                 $trainUsers->user_id = $usersModel->id;
                 $trainUsers->status = TrainUsers::SIGN;
                 $trainUsers->practice_score = 0;
                 $trainUsers->theory_score = 0;
                 $trainUsers->rule_score = 0;
                 $trainUsers->orders = $trainUsersOrder;
                 if (!$trainUsers->save()) {
                     $transaction->rollBack();
                     throw new ServerErrorHttpException(json_encode($trainUsers->errors, JSON_UNESCAPED_UNICODE));
                 } else {
                     $transaction->commit();
                 }
             }
             $model = new LoginForm();
             $loginInfo['_csrf'] = $registerInfo['_csrf'];
             $loginInfo['LoginForm'] = $registerInfo['Users'];
             unset($loginInfo['LoginForm']['level_id']);
             unset($loginInfo['LoginForm']['level_order']);
             unset($loginInfo['LoginForm']['score']);
             $loginInfo['LoginForm']['rememberMe'] = 0;
             if ($model->load($loginInfo) && $model->login()) {
                 return $this->redirect(['/user/register-info', 'train_id' => $trainId]);
             } else {
                 throw new ServerErrorHttpException('自动登录失败,原因:' . json_encode($model->errors, JSON_UNESCAPED_UNICODE));
             }
         } else {
             throw new ServerErrorHttpException('系统错误,原因:' . json_encode($usersModel->errors, JSON_UNESCAPED_UNICODE));
         }
     } else {
         $maxCount = TrainUsers::getMaxSignUpOrder($trainId);
         if ($maxCount < 1) {
             $maxCount = 1;
         } else {
             $maxCount = $maxCount + 1;
         }
         $trainName = Train::getOneTrainNameById($trainId);
         $data = ['maxCount' => $maxCount, 'trainName' => $trainName, 'train_id' => $trainId];
         return $this->render('register', ['type' => $type, 'data' => $data]);
     }
 }
 public function actionApplySuccess()
 {
     $trainUserId = Yii::$app->request->get('id');
     $trainUser = TrainUsers::findOne(['id' => $trainUserId]);
     $maxCount = TrainUsers::getMaxSignUpOrder($trainUser['train_id']);
     $trainName = Train::getOneTrainNameById($trainUser['train_id']);
     $data = ['maxCount' => $maxCount, 'trainName' => $trainName];
     return $this->redirect(['/train/apply-success', 'data' => $data]);
 }