<?php
class AjaxAction extends IniAction {
    public function ajGetGoods(){
        if(IS_AJAX){
            $page = I('get.p','','intval');
            $where['state'] = 1;
            $sort_id = I('get.id','','intval');
            if($sort_id>0){
                $where['sort_id'] = $sort_id;
            }
            $once = I('get.once','','intval');
            $once = ($once>0 && $once<=40)?$once:0;
            $count = M('goods')->where($where)->count();
            $goods = GoodsModel::I()->_getGoodsList($this->iniPage($count),$where);
            if($goods){
                $this->assign('goods',$goods);
                $this->assign('page',$page+1);
            }
            $this->d();
        }
    }
    
    public function ajGetGoodsDetial(){
        if(IS_AJAX){
            $url = I('get.url');
            $data = file_get_contents($url);
            preg_match('/dsc\.taobaocdn\.com[\w\.\%\/]*[\"]/i', $data,$descurl);
            $descurl = "http://".trim($descurl[0],'"');
            $content = file_get_contents($descurl);
            //$json['state'] = $json['content']?1:0;
            if($content){
                $content = mb_convert_encoding($content, 'utf-8', 'GBK,UTF-8,ASCII');
                $where['item_url'] = htmlspecialchars_decode($url);
                $save['item_body'] = trim($content,"var desc='");
                $save['item_body'] = substr($save['item_body'],0,-3);
                M('goods')->where($where)->save($save);
            }else{
                $this->ajaxReturn("商品详情获取失败，请刷新重试");
            }
            $this->assign('content',$content);
            $this->display();
        }
    }
    
    public function favor(){
        $this->isLogin();
        if(IS_AJAX){
            $data['goods_id'] = I('get.id','','intval');
            $data['uid'] = $this->my['uid'];
            if(M('favor')->where($data)->find()){
                $json['state'] = 1;
                $json['info'] = "已经收藏过了";
                $this->ajaxReturn($json);
            }
            if(M('favor')->add($data)){
                $json['state'] = 1;
                $json['info'] = "收藏成功";
            }else{
                $json['state'] = 0;
                $json['info'] = "收藏失败";
            }
            $this->ajaxReturn($json);
        }
    }
}
