/**
  * 登录
  *
  * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface|void
  * @author Hunter.<*****@*****.**>
  * @throws \Exception
  */
 public function loginAction()
 {
     $form = BaseForm::getForm('UserLoginForm');
     $this->assign('form', $form);
     if (IS_POST) {
         //验证数据失败
         if (!$form->isValid($_POST)) {
             foreach ($form->getMessages() as $message) {
                 $this->flash->error($message);
                 return;
                 //每次只输出一个错误
             }
         }
         //根据请求的信息判断登录信息并判断身份
         $username = $this->request->getPost('username', 'trim');
         $password = $this->request->getPost('password', 'trim');
         //$user = User::getUserByName($username);
         $user = self::$service->getCommon(["username = :username: ", 'bind' => ['username' => $username]], 'User', true);
         if ($user) {
             if ($user->getPassword() == st_md5($password, $user->getSalt())) {
                 //保存登录日志
                 self::$service->insertUserLoginLog(['uid' => $user->getId(), 'ip' => $this->request->getClientAddress(), 'user_agent' => $this->request->getUserAgent()]);
                 //保存登录者信息并跳转
                 $this->setLoginInfo($user, $this->request->getPost('remember', 'int', 0));
                 return $this->redirect('/home');
             } else {
                 $this->flash->error('密码错误');
             }
         } else {
             $this->flash->error('用户名或密码错误');
         }
     }
     $this->pick();
 }
 public function uploadImportStudentAction()
 {
     $this->view->disable();
     if ($this->request->hasFiles()) {
         foreach ($this->request->getUploadedFiles() as $file) {
             if (!in_array($file->getExtension(), Config('CONFIG_UPLOAD_EXT_EXCEL'))) {
                 return $this->displayAjax(false, '格式不正确,目前上传的格式是' . $file->getExtension() . ',请上传 ' . implode(',', Config('CONFIG_UPLOAD_EXT_EXCEL')) . ' 格式的文件');
             }
             if ($file->getSize() <= 0) {
                 return $this->displayAjax(false, '上传的文件为空');
             }
             $grades = st_school_type($this->user->getSchoolId());
             $grades_flip = array_flip($grades);
             $this->db->begin();
             //获取所有班级
             $classes = self::$service->getClassesForSchool($this->user->getSchoolId());
             $excel = st_excel($file->getTempName());
             $highestRow = $excel['sheet']->getHighestRow();
             // 取得总行数
             $highestColumn = $excel['sheet']->getHighestColumn();
             // 取得总列数
             for ($j = 1; $j <= $highestRow; $j++) {
                 $grade_class = $excel['obj']->getActiveSheet()->getCell("A" . $j)->getValue();
                 //获取A列的值
                 $name = $excel['obj']->getActiveSheet()->getCell("B" . $j)->getValue();
                 //获取B列的值
                 $std_no = $excel['obj']->getActiveSheet()->getCell("C" . $j)->getValue();
                 //获取C列的值
                 $sex = $excel['obj']->getActiveSheet()->getCell("D" . $j)->getValue();
                 //获取D列的值
                 //对年级班级进行拆分
                 $grade_tmp = mb_substr($grade_class, 0, 3, 'utf-8');
                 if (!isset($grades_flip[$grade_tmp])) {
                     $this->db->rollback();
                     return $this->displayAjax(false, '年级(' . $grade_tmp . ')不存在,无法添加');
                 }
                 $class_tmp_key = false;
                 $class_tmp = mb_substr($grade_class, 3, mb_strlen($grade_class) - 3, 'utf-8');
                 foreach ($classes as $cls) {
                     if ($cls->getName() == $class_tmp and $grades_flip[$grade_tmp] == $cls->getGradeId()) {
                         $class_tmp_key = $cls->getId();
                         break;
                     }
                 }
                 if ($class_tmp_key === false) {
                     $this->db->rollback();
                     return $this->displayAjax(false, '班级(' . $class_tmp . ')不存在,请先添加');
                 }
                 $data = [];
                 $data['classes_id'] = $class_tmp_key;
                 $data['type'] = 1;
                 $data['username'] = $std_no . st_rand();
                 $data['salt'] = st_rand();
                 $data['password'] = st_md5('123456', $data['salt']);
                 $data['std_number'] = $std_no;
                 $data['real_name'] = $name;
                 $data['sex'] = $sex == '男' ? 1 : 2;
                 $data['credit'] = $data['mobile'] = $data['email'] = $data['parent_id'] = $data['parent_id'] = 0;
                 $data['school_id'] = $this->user->getSchoolId();
                 //重名问题
                 $if = self::$service->getCommon(['classes_id=:classes_id: and school_id=:school_id: and real_name=:real_name:', 'bind' => ['classes_id' => $class_tmp_key, 'school_id' => $this->user->getSchoolId(), 'real_name' => $name]], 'User', true);
                 if ($if) {
                     $this->db->rollback();
                     return $this->displayAjax(false, '学生(' . $name . ')已经存在,请不要重复导入');
                 }
                 $res = self::$service->insertCommon($data, 'User');
                 if ($res !== true) {
                     $this->db->rollback();
                     return $this->ajax($res);
                 }
             }
             $this->db->commit();
             break;
         }
         $this->displayAjax(true);
     } else {
         $this->displayAjax(false, '没有检测到上传的文件');
     }
 }
 public function editPasswordAction()
 {
     $this->thisController->tag->prependTitle('修改密码');
     $form = new UserPasswordEditForm();
     $this->thisController->assign('form', $form);
     if (IS_POST) {
         //验证数据
         if (!$form->isValid($this->thisController->request->getPost())) {
             foreach ($form->getMessages() as $message) {
                 return $this->thisController->flash->error($message);
             }
         }
         //验证原始密码是否正确
         $old_password = st_md5($this->thisController->request->getPost('old_password'), $this->thisController->user->getSalt());
         if ($old_password != $this->thisController->user->getPassword()) {
             return $this->thisController->flash->error('原始密码不正确');
         }
         //新密码
         $rand = st_rand();
         $new_password = st_md5($this->thisController->request->getPost('new_password'), $rand);
         $data = ['password' => $new_password, 'salt' => $rand];
         if (!self::$service->updateCommon($data, $this->thisController->user)) {
             $this->thisController->flash->error('更新数据失败');
         }
         $this->thisController->redirect('/home/user/editPassword');
     }
 }