/**
  * 登录
  *
  * @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 integralShopBuyAction()
 {
     $this->thisController->view->disable();
     if (!$this->thisController->id) {
         return $this->thisController->displayAjax(false, '参数有误');
     }
     $result = self::$service->getCommon($this->thisController->id, 'Good', true);
     if (!$result) {
         return $this->thisController->displayAjax(false, '参数有误');
     }
     if (IS_POST) {
         $form = BaseForm::getForm('BookOrderForm');
         if (!$form->isValid($this->thisController->request->getPost())) {
             foreach ($form->getMessages() as $message) {
                 return $this->thisController->displayAjax(false, $message->getMessage());
             }
         }
         //写入数据
         $data = $this->thisController->request->getPost();
         $data['number'] = st_order_number();
         $data['book_id'] = 0;
         if ($this->thisController->user->getCredit() >= $result->getCredit()) {
             $data['pay_status'] = 2;
             $data['status'] = 3;
             //已支付
         } else {
             return $this->thisController->displayAjax(false, '积分不足');
         }
         $data['price'] = 0;
         $data['pay_method'] = 'credit';
         $data['good_id'] = $this->thisController->id;
         $data['credit'] = $result->getCredit();
         $data['uid'] = $this->thisController->user->id;
         $insert_result = self::$service->insertCommon($data, 'Order');
         if ($insert_result === true) {
             //减去相应的积分
             $update_data = ['credit' => (int) ($this->thisController->user->getCredit() - $result->getCredit())];
             self::$service->updateCommon($update_data, $this->thisController->user);
             //写入积分日志
             $insert_data = ['uid' => $this->thisController->user->id, 'val' => -$result->getCredit(), 'type' => 1, 'content' => '兑换积分商品【' . $result->getName() . '】', 'send_uid' => 0, 'classes_id' => $this->thisController->user->type == 1 ? $this->thisController->user->getClassesId() : 0];
             self::$service->insertCommon($insert_data, 'UserCreditLog');
         }
         return $this->thisController->ajax($insert_result);
     }
 }