ejs的简单应用

获取数据

var init = function(){
    api.my_bean_list({
    },function(datas){
        console.log(datas);
        if (datas.errno){
            alert(datas.errdesc);
            return;
        } else {
            var h = new EJS({element:bean_list_ejs}).render(datas);
            $('#for_bean_list_ejs').html(h);
        }
    },function(){
        console.log("网络错误");
        return;
    });
};


$(function(){
    setTimeout(function(){
        init();
    },500);
});

展示数据

<section>
    <div id="for_bean_list_ejs">
    </div>
</section>

<script type="text/template" id="bean_list_ejs">
    <div class="middle">
        [% for(var i=0;i< my_bean.length;i++){ %]
        <div class="every_div">
            <div class="left_div">
                <div class="top_div">
                    [%= my_bean[i]['info'] %]
                </div>
                <div class="bottom_div">
                    [%= timestampToTime(my_bean[i]['ctime']) %]
                </div>
            </div>
            <div class="right_div">
                [% if(my_bean[i]['type'] == '1'){ %]
                +
                [% } %]
                [% if(my_bean[i]['type'] == '0'){ %]
                -
                [% } %]
                [%= my_bean[i]['bean_nums'] %]
            </div>
        </div>
        [% } %]
    </div>
</script>

间接接口获取数据

public function my_bean_list(){
        $json = new Json();

        $token = $_POST['token'];
        if(!$token) {
            $token = $_COOKIE['token'];
        }

        $uid = $_POST['uid'];
        if(!$uid){
            $flag = $this->l_intercept();
            if(!$flag){
                $json->setErr(10015, '用户未登录');
                $json->Send();
            }
            $uid = $_COOKIE['uid'];
        }

        if(!$uid){
            $json->setErr(10015, '用户未登录');
            $json->Send();
        }

        $url = C('MOUSE_HOST') . 'Bean/my_bean_list';
        $param = array(
            'token'     =>      $token,
            'uid'       =>      $uid,
        );

        $result = Http::doPost($url, $param);
        $result = json_decode($result, true);
        $this->set_log($param, $result, 'my_bean_list');
        if($result['errno'] == 0){
            $json->setErr(0,'获取我的5号豆信息成功');
            $json->setAttr('my_bean', $result['data']);
            $json->Send();
        } else {
            $json->setErr($result['errno'], $result['errdesc']);
            $json->Send();
        }

    }

直接接口获取数据

/***
     * 我的5号豆信息
     */
    public function my_bean_list(){
        $this->checkLogin();
        $bean_list = M('BeanList');
        $uid = (int)$_POST['uid'];
        if (!$uid){
            $this->json->setErr('10022','缺少用户信息');
            $this->json->Send();
        }

        $list_flag = $bean_list->where(['uid' => $uid])->order('id desc')->select();
        $list_flag = $list_flag ?: [];
        $this->json->setAttr('data',$list_flag);
        $this->json->Send();
    }
posted @ 2018-04-23 17:45  TBHacker  阅读(356)  评论(0编辑  收藏  举报