第三周总结--王皓宇

阅读数:2234 发布时间:2016-09-25 07:51:21

作者:w4why 标签: w4why Phalcon OAuth API wx

秀野堂第三周记录

不知不觉在秀野堂已经过去了三周,每天都有新的东西需要汲取,消化,贵在坚持,了解自己的不足之处。本周基于Phalcon做了一系列项目,从开始的无从下手,到参考文档,阅读前人写过的代码,到能够总结出自己的思路,能自己动手做一些以前觉得遥不可及的API功能,困难和收获并存。
接下来是自己在项目里遇到的一些问题和一些自己书写的API的总结.

SQL查询部分:

query

query执行一条SQL语句,如果通过,则返回一个PDOStatement对象。query函数有个“非常好处”,就是可以直接遍历这个返回的记录集。query执行后,在下一次query执行之前,如果不取走所有返回的记录集,则query将会执行失败,除非我们调用 PDOStatement::closeCursor()来释放数据库资源与PDOStatement对象。

exec

exec执行一条SQL语句,并返回受影响的行数。此函数不会返回结果集合。exec支持SELECT/DELETE/UPDATE/INSERT等全部SQL语句执行,所以相比PDO query()函数功能要强大的多。由于只返回受影响的函数,所以,如果执行SELECT则无法得到PDOStatement对象,故也无法遍历结果集。

Volt模板部分:

首先注册 在视图里需要注册

    <?php

    //Registering Volt as template engine
    $di->set('view', function() {

        $view = new PhalconMvcView();

        $view->setViewsDir('../app/views/');

        $view->registerEngines(array(
            ".volt" => 'PhalconMvcViewEngineVolt'
        ));

        return $view;
    });

模板继承: extends "templates/xxx.volt"

OAuth 2.0

对于Oauth2.0的理解更多要结合实际项目来理解,比如说淘宝/爱用宝/用户模型,AppleStore/APP/用户模型来进行理解
OAuth2.0--阮一峰 更对请查阅相关资料

实际项目相关API代码选看:

      /**
       * 王皓宇定义的控制器注入
       * @var DocPubReplyController
       */
      $di->set('DocPubReplyController', function(){
        $DocPubReplyController = new DocPubReplyController();
        return $DocPubReplyController;
      });  

      //发表文章
      $app->post('/api/pub_doc', function() use ($app, $responseObj) {
        $data = $app->DocPubReplyController->pubDoc($app, $responseObj);
        $app->response->setJsonContent($data);
        $app->response->send();
      });  

       /**
       * 文章发表功能  @why 2016/9/23
       * @param  [type] $app         [description]
       * @param  [type] $responseObj [description]
       * @return [type]              [description]
       */
      public function pubDoc($app, $responseObj)
      {
        $username   = $app->request->getPost('username');
        $password   = $app->request->getPost('password');
        $uid        = $app->request->getPost('uid');
        $cid        = $app->request->getPost('cid');
        $title      = $app->request->getPost('title');
        $keywords   = $app->request->getPost('keywords');
        $content    = $app->request->getPost('content');
        $pubip      = 1;
        $pubhostid  = '1';
        $passwordOk = self::isPasswordOk($password);
        $userCheck  = self::isUserExisit($app, $username, $password);
        if ($passwordOk && $userCheck) {
          if ($uid == '' || $title == '' || $content == '' || $keywords == '' || $cid == '') {
            $responseObj['status'] = 0;
            $responseObj['msg']    = '填入信息不能为空';
            return $responseObj;
          }
          $replys            = new Doc();
          $replys->uid       = (int)$uid;
          $replys->cid       = (int)$cid;
          $replys->title     = $title;
          $replys->keywords  = $keywords;
          $replys->content   = $content;
          $replys->pubip     = $pubip;
          $replys->pubhostid = $pubhostid;
          $res               = $replys->save();
          if ($res) {
            $responseObj['status'] = 1;
            $responseObj['msg']    = '文章发表成功';
          }
          else {
            $responseObj['status'] = 0;
            $responseObj['msg']    = '文章发表失败';
          }
            return $responseObj;//返回状态码
        }
        else {
          $responseObj['status'] = 0;
          $responseObj['msg']    = '用户名或密码不正确';
          return $responseObj;
        }
        return $responseObj;
      }   

登录逻辑 API

      服务注册
      $di->set('NewUserController', function(){
        $NewUserController = new NewUserController();
        return $NewUserController;
      });  

      用户登陆
      $app->post('/api/login', function() use ($app, $responseObj) {
         $data = $app->NewUserController->login($app, $responseObj);
         $app->response->setJsonContent($data);
         $app->response->send();
       });  

      /**
     * 用户登陆逻辑
     * @param  对象 $app         [description]
     * @param  [type] $responseObj [description]
     * @param  数组 $params      用户登陆时提交的信息
     */
    public function login($app, $responseObj)
    {
         $redis              = self::initRedis(); //初始化redis对象
        $params             = array();                     //参数数组
        $params['username'] = $app->request->getPost('username');//获取用户名
        $params['password'] = $app->request->getPost('password'); //获取密码
        $params['password'] = md5($params['password']);

        if ($params['username'] == '' || $app->request->getPost('password') == '') {
            $responseObj['status'] = 0;//失败状态
            $responseObj['msg']    = '用户名和密码不能为空';
            return $responseObj;//返回状态码
        }
        $ifMobile = self::usernameIsMobile($params['username']);//判断是否是手机用户
        if ($ifMobile) {
            //如果是手机按照手机逻辑检测,返回查询结果
            $checkResult = self::userMobileCheckLogic($app, $params['username'], $params['password']);
        }
        else {
             //非手机逻辑检测
            $checkResult = self::usernameCheckLogic($app, $params['username'], $params['password']);
        }
        if ($checkResult) {            //返回值状态改变
            $responseObj['status'] = 1;
            $responseObj['msg']    = $checkResult;
            $responseObj['data']   = [
                'uid'     => strval($checkResult->user_uid),     //把变量转换成字符串类型
                'usrname' => strval($checkResult->user_name),
            ];
        }
        else {
            $responseObj['status'] = 0;
            $responseObj['msg']    = '没有该用户,或密码错误';
            $responseObj['data']   = '';
        }
    return $responseObj;
    }

柳茹美图

img1
img2 img3

相关文章推荐: