php接口

入口

<?php

class Api_IphoneController extends actions_api {

    var $identity = null;

    function init() {
        parent::init();
        $this->setView('api');
    }

    function indexAction() {
        $json = $_REQUEST;
        //print_r(json_decode($json['json'],true));exit;
        if (array_key_exists('json', $json)) {
            $json_info = json_decode($json['json'], true);
            //$this->debuglog($json['json']); // debug
        } else {
            echo "无请求参数,请输入请求参数!";
            die();
        }
        //通过reqCode转入控制层
        switch ($json_info["reqCode"]) {
            case 'edu00001':
                // 获取新闻,分页获取
                // 请求{"reqCode":"edu00001","data":{"nowpage":"1","pageNum":"10","user_id":"10","bigclassid":"221"}}
                // 响应
                $json_info = json_decode($json['json'], true);
                $this->_forward('newslist', 'news', 'api', $json_info);
                break;

            case 'edu00002':
                // 获取所有老师
                // 请求{"reqCode":"edu00002"}
                // 响应
                $json_info = json_decode($json['json'], true);
                $this->_forward('allteacher', 'teacher', 'api', $json_info);
                break;

            case 'edu00003':
                // 新闻top5
                // 请求{"reqCode":"edu00003","data":{"user_id":"105"}}
                // 响应 
                $json_info = json_decode($json['json'], true);
                $this->_forward('newstop5', 'news', 'api', $json_info);
                break;


            case 'edu00004':
                // 获取收件箱消息,分页获取
                // 请求{"reqCode":"edu00004","data":{"user_id":"105","nowpage":"1","pageNum":"10"}}
                // 响应
                $json_info = json_decode($json['json'], true);
                $this->_forward('msginlist', 'message', 'api', $json_info);
                break;

            case 'edu00005':
                // 所有班级
                // 请求{"reqCode":"edu00005"}
                // 响应
                $json_info = json_decode($json['json'], true);
                $this->_forward('allclassinfo', 'student', 'api', $json_info);
                break;

            case 'edu00006':
                // 登录
                // 请求{"reqCode":"edu00006","data":{"username":"test3","password":"123456"}}
                // 响应
                $json_info = json_decode($json['json'], true);
                $this->_forward('login', 'auth', 'api', $json_info);
                break;

            case 'edu00007':
                // 班级信息
                // 请求{"reqCode":"edu00007","data":{"class_id":"19"}}
                // 响应
                $json_info = json_decode($json['json'], true);
                $this->_forward('classinfo', 'student', 'api', $json_info);
                break;

            case 'edu00008':
                // 新闻分类
                // 请求{"reqCode":"edu00008"}
                // 响应
                $json_info = json_decode($json['json'], true);

                $this->_forward('newstype', 'news', 'api', $json_info);
                break;

            case 'edu00009':
                // 学生信息
                // 请求{"reqCode":"edu00009","data":{"user_id":"166"}}
                // 响应
                $json_info = json_decode($json['json'], true);
                $this->_forward('studentinfo', 'student', 'api', $json_info);
                break;

            case 'edu00010':
                // 消息发送
                // 请求{"reqCode":"edu00010","data":{"from_id":"21","to_id":"105,22,23","topic":"消息主题","content":"消息内容"}}
                // 响应
                $json_info = json_decode($json['json'], true);
                $this->_forward('msgsend', 'message', 'api', $json_info);
                break;
            default:
                echo '请求代码错误!!';
        }
    }

}

处理

<?php
class Api_NewsController extends actions_api {
    public $doMain = 'http://testserver.njlrxx.com/';


    //接口中可以直接操作sql语句进行一些处理
    function init() {
        parent::init();
        $this->setView('api');
        $this->dao_newsrecord = new dao_newsrecord();
        $this->dao_module = new dao_module();
        $this->dao_message = new dao_message();
        $this->dao_user = new dao_user();
        $this->dao_news = new dao_news();
        $this->inData = $this->_getParam('data',false);//请求参数
        $this->reqCode = $this->_getParam('reqCode',false);
        $this->outData = array('status'=>0,'msg'=>'','data'=>array(),'reqCode'=>$this->reqCode);//输出参数
    }
    

    //新闻分类
    function newstypeAction()
    {
        $where['BigClass.typeid = ?'] = array("type"=>1,"val"=>60);
        $aBigClass = $this->dao_news->getBigClass($where, 'BigClassID ASC', 3);
        if(COUNT($aBigClass) == 0)
        {
            $this->outData['msg'] = '暂无数据';
            $this->printOut();
        }
        $this->outData['status'] = 1;
        $this->outData['msg'] = '获取成功';
        $this->outData['data'] = $aBigClass;
        $this->printOut();
    }


    //图片新闻前五条
    function newstop5Action(){
        $where['News.picnews = ?'] = array("type"=>1,"val"=>1);
        $aNewsData = $this->dao_news->getNews2($where, 'NewsID DESC', 5, FALSE , false, array('NewsID','picname','Title'));
        foreach($aNewsData as &$val)
        {
            $val['webview_url'] = $this->doMain.'api/news/newsview/NewsID/'.$val['NewsID'].'/user_id/'.$this->inData['user_id'];
        }
        if(COUNT($aNewsData) == 0)
        {
            $this->outData['msg'] = '暂无数据';
            $this->printOut();
        }
        $this->outData['status'] = 1;
        $this->outData['msg'] = '获取成功';
        $this->outData['data'] = $aNewsData;
        $this->printOut();
    }

    function newslistAction(){
        $nowPage = !empty($this->inData['nowpage'])?$this->inData['nowpage']:1;    //$this->_getParam('nowpage',1);
        $pageNum = !empty($this->inData['pageNum'])?$this->inData['pageNum']:10;    //$this->_getParam('pageNum',10);
        $bigclassid = !empty($this->inData['bigclassid'])?$this->inData['bigclassid']:false;    
         if(!$bigclassid)
        {
            $this->outData['msg'] = '缺少分类参数';
            $this->printOut();
        }
        $offset = ($nowPage - 1)*$pageNum;
        $where['News.bigclassid = ?'] = array("type"=>1,"val"=>$bigclassid);
        $total = $this->dao_news->getNews2($where, '', false, false , true);
        $aNewsData = $this->dao_news->getNews2($where, 'NewsID DESC', $pageNum, $offset , false, array('NewsID','Title','UpdateTime','convert(text,Content) as Content'));

        foreach($aNewsData as &$val)
        {
            $val['webview_url'] = $this->doMain.'api/news/newsview/NewsID/'.$val['NewsID'].'/user_id/'.$this->inData['user_id'];
            $val['Content'] = $this->substring($this->clearhtml($val["Content"]),16);
        }
        if($total == 0)
        {
            $this->outData['msg'] = '暂无数据';
            $this->printOut();
        }
        $this->outData['next'] = 0;
        if($total > ($nowPage * $pageNum))
        {
            $this->outData['next'] = 1;
        }
        $this->outData['status'] = 1;
        $this->outData['msg'] = '获取成功';
        $this->outData['data'] = $aNewsData;

        $this->printOut();
    }

    function newsviewAction(){
        $newsID = $this->_getParam("NewsID",0);//新闻id
        $userID = $this->_getParam("user_id");//uid

        //$whereUser['lx_user.id = ?'] = array("type"=>1,"val"=>$user_id);
        //$aUser = $this->dao_user->getUser($whereUser);
    
        $whereNews['News.NewsID = ?'] = array("type"=>1,"val"=>$newsID);
        $aNews = $this->dao_news->getNews($whereNews);

        $aNewsData = $aNews ? $aNews[0] : false;
        if(!$aNewsData && $aNewsData['checkked'] == 1 && $userID)//是否审核
        {
            $this->dao_news->updateNews(array('click'=>'`click`+1','NewsID'=>$newsID));//更新新闻点击次数
             //阅读人记录到数据库中
            $aNewsrecord = array();
            $aNewsrecord['news_id'] = $NewsID;
            $aNewsrecord['user_id'] = $userID;
            $aNewsrecord['user_name'] = !is_null($aUser[0]["name"]) ? $aUser[0]["name"] : $aUser[0]["surname"].$aUser[0]["givenname"];
            $aNewsrecord['user_avatar'] = $aUser[0]["avatar"];
            $aNewsrecord['time'] = time();
            $whereNewsrecord = array();
            $whereNewsrecord['lx_newsrecord.news_id = ?'] = array("type"=>1,"val"=>$NewsID);
            $whereNewsrecord['lx_newsrecord.user_id = ?'] = array("type"=>1,"val"=>$userID);
            $result = $this->dao_newsrecord->getNewsrecord($whereNewsrecord);
            if($result){
                $aNewsrecord['id'] = $result[0]['id'];
                $this->dao_newsrecord->updateNewsrecord($aNewsrecord);
            }else{
                $this->dao_newsrecord->addNewsrecord($aNewsrecord);
            }
        }
        $aNewsData['Content'] =  str_replace('src="/webedit/uploadfile/','src="http://www.njlrxx.com/webedit/uploadfile/',$aNewsData['Content']);

        $this->view_->assign('aNewsData', $aNewsData);
        $this->view_->assign('userID', $userID);
        $this->view_->display('news_view_iphone.tpl');
    }
}

 

posted @ 2014-02-20 16:14  TBHacker  阅读(826)  评论(0编辑  收藏  举报