Exemplo n.º 1
0
 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]);
     }
 }
 /**
  * Updates an existing UsersLevel model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     if (Yii::$app->request->isPost) {
         $infoParams = Yii::$app->request->post();
         if ($infoParams['UsersLevel']['status'] == UsersLevel::SEND_CARD) {
             $infoParams['UsersLevel']['end_date'] = date('Y-m-d', time() + 3600 * 24 * 365);
         }
         $userLevelInfo = $infoParams;
         if ($model->load($userLevelInfo) && $model->save()) {
             //如果状态更新为已经注册
             if ($model->status == UsersLevel::SEND_CARD) {
                 if ($model->level_id == 1) {
                     //如果是注册学员升级市级教练
                     UsersLevel::updateAll(['status' => UsersLevel::LEVEL_UP], ['user_id' => $model->user_id, 'level_id' => $model->level_id]);
                     $userInfo = UsersInfo::findOne(['user_id' => $model->user_id]);
                     //新增一条用户和级别对应的信息
                     $userLevelModel = new UsersLevel();
                     $userLevelModel->user_id = $model->user_id;
                     $userLevelModel->level_id = $model->level_id + 1;
                     $userLevelModel->credentials_number = $userInfo['credentials_number'];
                     $userLevelModel->district = $userInfo['account_location'];
                     $userLevelModel->receive_address = $userInfo['contact_address'];
                     $userLevelModel->postcode = $userInfo['contact_postcode'];
                     $userLevelModel->status = UsersLevel::NO_TRAIN;
                     if (!$userLevelModel->save()) {
                         throw new ServerErrorHttpException(json_encode($userLevelModel->errors, JSON_UNESCAPED_UNICODE) . '!');
                     } else {
                         Users::updateAll(['level_id' => $model->level_id + 1, 'level_order' => $model->level_id + 1], ['id' => $model->user_id]);
                     }
                 }
                 $usersTrainInfo = UsersLevel::getUserTrainInfo($model->id);
                 $teacherInfo = Teachers::findOne($usersTrainInfo['teachers_id']);
                 $usersTrainModel = new UsersTrain();
                 $usersTrain['UsersTrain']['user_id'] = $usersTrainInfo['user_id'];
                 $usersTrain['UsersTrain']['credentials_number'] = $usersTrainInfo['certificate_number'];
                 $usersTrain['UsersTrain']['begin_time'] = $usersTrainInfo['begin_time'];
                 $usersTrain['UsersTrain']['end_time'] = $usersTrainInfo['end_time'];
                 $usersTrain['UsersTrain']['level'] = Level::getOneLevelNameById($usersTrainInfo['level_id']);
                 $usersTrain['UsersTrain']['address'] = $usersTrainInfo['address'];
                 $usersTrain['UsersTrain']['witness'] = $teacherInfo['name'];
                 $usersTrain['UsersTrain']['witness_phone'] = $teacherInfo['phone'];
                 $usersTrain['UsersTrain']['description'] = '通过培训课程' . $usersTrainInfo['content'];
                 if ($usersTrainModel->load($usersTrain) && $usersTrainModel->save()) {
                     $content = "很高兴的通知您,您已完成" . Level::getOneLevelNameById($model->level_id) . "级教练员的注册,注册时效为1年,您现在可以正式开展教练员工作。【教练系统】";
                     $userInfo = Users::findOne($model->user_id);
                     $result = $this->sendMessage($content, Messages::REGISTER_SUCCESS, $model->user_id, $userInfo['mobile_phone'], '1');
                     if ($result != '0') {
                         throw new ServerErrorHttpException($result);
                     } else {
                         return $this->redirect(['view', 'id' => $model->id]);
                     }
                 } else {
                     throw new ServerErrorHttpException(json_encode($usersTrainModel->errors, JSON_UNESCAPED_UNICODE));
                 }
             }
         }
     } else {
         $photo = UsersInfo::getPhotoByUserId($model->user_id);
         return $this->render('update', ['model' => $model, 'photo' => $photo]);
     }
 }
 public function actionLevelUp()
 {
     $levelInfo = Level::findOne(['id' => Yii::$app->user->identity->level_id + 1]);
     $usersLevel = UsersLevel::findOne(['user_id' => Yii::$app->user->id, 'level_id' => Yii::$app->user->identity->level_id]);
     $usersLevelStatusCurrent = $usersLevel['status'];
     if ($usersLevelStatusCurrent != UsersLevel::SEND_CARD) {
         throw new ServerErrorHttpException('当前注册状态不符合!');
     }
     $loginDuration = Users::getLoginDuration(Yii::$app->user->id);
     $loginDurationDiff = $levelInfo['login_duration'] - $loginDuration;
     if ($loginDurationDiff > 0) {
         throw new ServerErrorHttpException('当前注册时长不符合!');
     }
     $scoreDiff = $levelInfo['score'] - Yii::$app->user->identity->score;
     if ($scoreDiff > 0) {
         throw new ServerErrorHttpException('当前一般活动积分不符合!');
     }
     $creditDiff = $levelInfo['credit'] - Yii::$app->user->identity->credit;
     if ($creditDiff > 0) {
         throw new ServerErrorHttpException('当前公益活动积分不符合!');
     }
     $transaction = Yii::$app->db->beginTransaction();
     UsersLevel::updateAll(['status' => UsersLevel::LEVEL_UP], ['user_id' => Yii::$app->user->id, 'level_id' => Yii::$app->user->identity->level_id]);
     $userInfo = UsersInfo::findOne(['user_id' => Yii::$app->user->id]);
     //新增一条用户和级别对应的信息
     $userLevelModel = new UsersLevel();
     $userLevelModel->user_id = Yii::$app->user->id;
     $userLevelModel->level_id = Yii::$app->user->identity->level_id + 1;
     $userLevelModel->credentials_number = $userInfo['credentials_number'];
     $userLevelModel->district = $userInfo['account_location'];
     $userLevelModel->receive_address = $userInfo['contact_address'];
     $userLevelModel->postcode = $userInfo['contact_postcode'];
     $userLevelModel->status = UsersLevel::NO_TRAIN;
     if (!$userLevelModel->save()) {
         $transaction->rollBack();
         throw new ServerErrorHttpException('更新状态错误,原因:' . json_encode($userLevelModel->errors, JSON_UNESCAPED_UNICODE) . '!');
     } else {
         Users::updateAll(['level_id' => Yii::$app->user->identity->level_id + 1, 'level_order' => Yii::$app->user->identity->level_order + 1], ['id' => Yii::$app->user->id]);
         $transaction->commit();
     }
     return $this->redirect('/user-center/index');
 }