index.php 42 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
<?php
/**
 * @author Tiny <tinylofty@gmail.com>
 * @copyright Copyright(c) 2010-2014 http://www.tinyrise.com All rights reserved
 * @version 1.0
 */
/**
 * description...
 *
 * @author Tiny
 * @package IndexController
 */
class IndexController extends Controller{

    public $layout='index';
    public $safebox = null;
    private $model = null;
    private $category = array();

    protected $needRightActions=array('review'=>true,'review_act'=>true);

    public function init(){
        header("Content-type: text/html; charset=".$this->encoding);
        $this->model = new Model();
        $this->safebox =  Safebox::getInstance();
        $this->user = $this->safebox->get('user');
        $category = Category::getInstance();
        if($this->user==null){
            $this->user = Common::autoLoginUserInfo();
            $this->safebox->set('user',$this->user);
        }

        $this->category = $category->getCategory();
        $cart = Cart::getCart();
        $this->assign("cart",$cart->all());
        $this->assign("category",$this->category);
        $keyword = urldecode(Req::args('keyword'));
        if($keyword!=null)$this->assign("keyword",Filter::text($keyword));
        $url_index = '/'.Req::args('con').'/'.Req::args('act');
        $url_index = Url::urlFormat($url_index);
        $this->assign("url_index",Url::requestUri());
        //配制中的站点信息
        $config = Config::getInstance();
        $site_config = $config->get("globals");
        $this->assign('seo_title',$site_config['site_name']);
        $this->assign('site_title',$site_config['site_name']);
        $this->assign('seo_keywords',$site_config['site_keywords']);
        $this->assign('seo_description',$site_config['site_description']);

    }
    public function attention()
    {
        $goods_id = Filter::int(Req::args('goods_id'));
        $info = array('status' => 0);
        if(isset($this->user['name'])){
            $obj = $this->model->table("attention")->where("goods_id=$goods_id and user_id=".$this->user['id'])->find();
            if($obj) $info = array('status' => 2);
            else {
                $this->model->table("attention")->data(array('goods_id'=>$goods_id,'user_id'=>$this->user['id'],'time'=>date('Y-m-d H:i:s')))->insert();
                $info = array('status' => 1);
            }
        }
        echo JSON::encode($info);
    }
    public function msg()
    {

        $type = $this->type==null?'fail':$this->type;
        $msg = $this->msg==null?'失败':$this->msg;
        $content = $this->content==null?'':$this->content;
        $redirect = $this->redirect==null?'':$this->redirect;

        $this->assign("type",$type);
        $this->assign("msg",$msg);
        $this->assign("content",$content);
        $this->assign("redirect",$redirect);
        $this->redirect();
    }
    public function img_upload()
    {
        $path = APP_ROOT;
        $upf = new  UploadFile('imgFile',$path,'2m','jpg,jpeg,gif');
        $upf->save();
        $info = $upf->getInfo();
        if($info[0]['status']==1)
        {
            echo JSON::encode(array('error' => 0, 'url' => $info[0]['path']));
            exit;
        }
    }

    public function cart_get()
    {
        $cart = $this->getCart();
        $products = $cart->all();
        echo JSON::encode($products);
    }
    private function getCart(){
        $type = Req::args('cart_type');
        if($type=='goods'){
            return Cart::getCart('goods');
        }else{
            return Cart::getCart();
        }
    }

    public function cart_add()
    {
        $id = Filter::int(Req::args("id"));
        $num = intval(Req::args("num"));
        $num = $num>0?$num:1;
        $cart = $this->getCart();
        $cart->addItem($id,$num);
        $products = $cart->all();
        echo JSON::encode($products);
    }

    public function goods_add()
    {
        $cart = Cart::getCart('goods');
        $cart->clear();
        $id = Filter::int(Req::args("id"));
        $num = intval(Req::args("num"));
        $num = $num>0?$num:1;
        $cart->addItem($id,$num);
        $this->redirect('/simple/cart/cart_type/goods');
    }
    public function cart_del()
    {
        $id = Filter::int(Req::args("id"));
        $cart = $this->getCart();
        $cart->delItem($id);
        $info = array('status' =>"fail");
        if(!$cart->hasItem($id))$info = array('status' =>"success");
        echo JSON::encode($info);
    }

    public function cart_num()
    {
        $id = Filter::int(Req::args("id"));
        $num = intval(Req::args("num"));
        $num = $num>0?$num:1;
        $cart = $this->getCart();
        $cart->modNum($id,$num);
        $products = $cart->all();
        echo JSON::encode($products);
    }
    public function goods_consult()
    {
        $id = Filter::int(Req::args('id'));
        $content = Filter::txt(Req::args("content"));
        $content = TString::nl2br($content);
        $verifyCode = Req::args("verifyCode");
        $this->safebox = Safebox::getInstance();
        $code = $this->safebox->get($this->captchaKey);
        $info = array("status"=>"fail","msg"=>"验证码错误!");
        if(isset($this->user['id']) && $this->user['name']){
            if($code == $verifyCode){
                $this->model->table("ask")->data(array('question'=>$content,'user_id'=>$this->user['id'],'goods_id'=>$id,'ask_time'=>date('Y-m-d H:i:s')))->insert();
                $info = array("status"=>"success","msg"=>"咨询成功!");
                //发送用户咨询通知
                $NoticeService = new NoticeService();
                $template_data = array('user'=>$this->user['name'],
                    'content'=>$content
                    );

                $NoticeService->send('user_ask',$template_data);
            }
        }else{
            $info = array("status"=>"fail","msg"=>"登录后才能咨询。");
        }

        echo JSON::encode($info);
    }
    //帮助
    public function help()
    {
        $id = Filter::int(Req::args("id"));
        $help = $this->model->table('help')->where("id=$id")->find();
        if($help){
            $this->assign("id",$id);
            $this->assign('seo_title',$help['title']);
            $this->assign('help',$help);
            $this->redirect();
        }else{
            Tiny::Msg($this,'帮助文档不存在!');
        }

    }
    //文章
    public function article()
    {
        $id = Filter::int(Req::args("id"));
        $model = new Model('article');
        $article = $model->where("id = $id")->find();
        if($article){
            $this->assign('seo_title',$article['title']);
            $this->assign('article',$article);
            $this->assign("id",$id);
            $this->redirect();
        }else{
            Tiny::Msg($this,'文章不存在!');
        }

    }
    //文章列表
    public function article_list()
    {
        $id = Filter::int(Req::args("id"));
        $this->assign("id",$id);
        $where = "1=1";
        if($id){
            $where = 'category_id = '.$id;
        }
        $this->assign("where",$where);
        $this->redirect();
    }
    public function group()
    {
        $this->assign('seo_title','团购,优惠精选');
        $this->assign('seo_keywords','团购,优惠促销精选');
        $this->redirect();
    }
    public function flash()
    {
        $this->assign('seo_title','抢购,优惠精选');
        $this->assign('seo_keywords','抢购,优惠促销精选,限时抢购,更多优惠.');
        $this->redirect();
    }
    //团购
    public function groupbuy()
    {
        $id = Filter::int(Req::args("id"));
        $goods = $this->model->table("groupbuy as gb")->join("left join goods as go on gb.goods_id = go.id")->where("gb.id=$id")->find();
        if(isset($goods['id'])){
            //检测团购是否结束
            if($goods['store_nums']<=0 || $goods['goods_num']>=$goods['max_num'] || time()>= strtotime($goods['end_time'])){
                $this->model->table('groupbuy')->data(array('is_end'=>1))->where("id=$id")->update();
                $goods['is_end'] = 1;
            }
            $skumap = array();
            $products = $this->model->table("products")->fields("sell_price,market_price,store_nums,specs_key,pro_no,id")->where("goods_id = $goods[id]")->findAll();
            if($products){
                foreach ($products as $product) {
                    $skumap[$product['specs_key']] = $product;
                }
            }
            $attr_array = unserialize($goods['attrs']);
            $goods_attrs = array();
            if($attr_array){
                $rows = $this->model->fields("ga.*,av.name as vname,av.id as vid")->table("goods_attr as ga")->join("left join attr_value as av on ga.id=av.attr_id")->where("ga.type_id = $goods[type_id]")->findAll();
                $attrs = $_attrs = array();
                foreach ($rows as $row) {
                    $attrs[$row['id'].'-'.$row['vid']] = $row;
                    $_attrs[$row['id']] = $row;
                }
                foreach ($attr_array as $key => $value) {
                    if(isset($attrs[$key.'-'.$value])) $goods_attrs[] = $attrs[$key.'-'.$value];
                    else{
                        $_attrs[$key]['vname'] = $value;
                        $goods_attrs[] = $_attrs[$key];
                    }
                }
                unset($attrs,$_attrs);
            }
            $this->assign('seo_title',$goods['title']);
            $this->assign('id',$id);
            $this->assign("skumap",$skumap);
            $this->assign("attr_array",$attr_array);
            $this->assign("goods_attrs",$goods_attrs);
            $this->assign("goods",$goods);
            $this->redirect();
        }else{
            Tiny::Msg($this,"404");
        }
    }
    //抢购
    public function flashbuy(){
        $id = Filter::int(Req::args("id"));
        $goods = $this->model->table("flash_sale as gb")->join("left join goods as go on gb.goods_id = go.id")->where("gb.id=$id")->find();
        if($goods){
            //检测抢购是否结束
            if($goods['store_nums']<=0 || $goods['goods_num']>=$goods['max_num'] || time()>= strtotime($goods['end_time'])){
                $this->model->table('flash_sale')->data(array('is_end'=>1))->where("id=$id")->update();
                $goods['is_end'] = 1;
            }
            $skumap = array();
            $products = $this->model->table("products")->fields("sell_price,market_price,store_nums,specs_key,pro_no,id")->where("goods_id = $goods[id]")->findAll();
            if($products){
                foreach ($products as $product) {
                    $skumap[$product['specs_key']] = $product;
                }
            }
            $attr_array = unserialize($goods['attrs']);
            $goods_attrs = array();
            if($attr_array){
                $rows = $this->model->fields("ga.*,av.name as vname,av.id as vid")->table("goods_attr as ga")->join("left join attr_value as av on ga.id=av.attr_id")->where("ga.type_id = $goods[type_id]")->findAll();
                $attrs = $_attrs = array();
                foreach ($rows as $row) {
                    $attrs[$row['id'].'-'.$row['vid']] = $row;
                    $_attrs[$row['id']] = $row;
                }
                foreach ($attr_array as $key => $value) {
                    if(isset($attrs[$key.'-'.$value])) $goods_attrs[] = $attrs[$key.'-'.$value];
                    else{
                        $_attrs[$key]['vname'] = $value;
                        $goods_attrs[] = $_attrs[$key];
                    }
                }
                unset($attrs,$_attrs);
            }

            $this->assign('id',$id);
            $this->assign("skumap",$skumap);
            $this->assign("attr_array",$attr_array);
            $this->assign("goods_attrs",$goods_attrs);
            $this->assign("goods",$goods);
            $this->redirect();
        }else{
            Tiny::Msg($this,"404");
        }
    }
    //捆绑
    public function bundbuy(){
        $id = Filter::int(Req::args("id"));
        $bund = $this->model->table("bundling")->where("id=$id and status = 1")->find();
        if($bund){
            $goods = $this->model->table("goods")->where("id in ($bund[goods_id])")->findAll();
            $gids = array();
            $goods_price = 0.00;
            foreach ($goods as $go) {
                $gids[] = $go['id'];
                $goods_price += $go['sell_price'];
            }
            $gids = implode(',', $gids);
            $skumap = array();
            $products = $this->model->table("products")->fields("sell_price,market_price,store_nums,specs_key,pro_no,id,goods_id")->where("goods_id in ($gids)")->findAll();
            if($products){
                foreach ($products as $product) {
                    if($product['specs_key'])$skumap[$product['specs_key'].$product['goods_id']] = $product;
                    else $skumap[$product['goods_id']] = $product;
                }
            }
            $this->assign("bund",$bund);
            $this->assign("goods_price",$goods_price);
            $this->assign("skumap",$skumap);
            $this->assign("goods",$goods);
            $this->assign("id",$id);
            $this->redirect();
        }else{
            Tiny::msg($this,'',404);
        }

    }
    //商品展示
    public function product()
    {
        $parse = array('直接打折','减价优惠','固定金额','买就赠优惠券','买M件送N件');
        $id = Filter::int(Req::args('id'));
        $this->assign('id',$id);
        $id = is_numeric($id)?$id:0;
        $goods = $this->model->table("goods as go")->join("left join tiny_goods_category as gc on go.category_id = gc.id")->fields("go.*,gc.path")->where("go.id=$id and go.is_online=0")->find();
        if($goods){

            $skumap = array();
            $products = $this->model->table("products")->fields("sell_price,market_price,store_nums,specs_key,pro_no,id")->where("goods_id = $id")->findAll();
            if($products){
                foreach ($products as $product) {
                    $skumap[$product['specs_key']] = $product;
                }
            }

            $path = trim($goods['path'],',');
            $category = Category::getInstance();
            $childCategory = $category->getCategoryChild($path);
            $category = $this->model->table("goods_category")->where("id in ($path)")->order("field(`id`,$path)")->findAll();
            $time = "'".date('Y-m-d H:i:s')."'";

            $prom = $this->model->table("prom_goods")->where("id=".$goods['prom_id']." and is_close=0 and $time >=start_time and $time <= end_time")->find();


            $attr_array = unserialize($goods['attrs']);
            $goods_attrs = array();
            if($attr_array){
                $rows = $this->model->fields("ga.*,av.name as vname,av.id as vid")->table("goods_attr as ga")->join("left join attr_value as av on ga.id=av.attr_id")->where("ga.type_id = $goods[type_id]")->findAll();
                $attrs = $_attrs = array();
                foreach ($rows as $row) {
                    $attrs[$row['id'].'-'.$row['vid']] = $row;
                    $_attrs[$row['id']] = $row;
                }
                foreach ($attr_array as $key => $value) {
                    if(isset($attrs[$key.'-'.$value])) $goods_attrs[] = $attrs[$key.'-'.$value];
                    else{
                        $_attrs[$key]['vname'] = $value;
                        $goods_attrs[] = $_attrs[$key];
                    }
                }
                unset($attrs,$_attrs);
            }
            //评论
            $comment = array();
            $review = array('1'=>0,'2'=>0,'3'=>0,'4'=>0,'5'=>0);
            $rows = $this->model->table("review")->fields("count(id) as num,point")->where("status=1 and goods_id = $id")->group("point")->findAll();
            foreach ($rows as $row) {
                $review[$row['point']] = intval($row['num']);
            }
            $a = ($review[4]+$review[5]);
            $b = ($review[3]);
            $c = ($review[1]+$review[2]);
            $total = $a+$b+$c;
            $comment['total'] = $total;
            if($total==0) $total = 1;
            $comment['a'] = array('num'=>$a,'percent'=>round((100*$a/$total)));
            $comment['b'] = array('num'=>$b,'percent'=>round((100*$b/$total)));
            $comment['c'] = array('num'=>$c,'percent'=>round((100*$c/$total)));

            if($goods['seo_title']!='') $seo_title = $goods['seo_title'];
            else if($goods['name']!='') $seo_title = $goods['name'];

            if($seo_title!='') $this->assign('seo_title',$seo_title);
            if($goods['seo_keywords']!='') $this->assign('seo_keywords',$goods['seo_keywords']);
            if($goods['seo_description']!='') $this->assign('seo_description',$goods['seo_description']);

            $proms = new Prom();
            $goods['goods_nums'] = PHP_INT_MAX;
            if(!empty($prom))$prom['parse'] = $proms->prom_goods($goods);
            //售后保障
            $sale_protection = $this->model->table('help')->where("title='售后保障'")->find();
            if($sale_protection){
                $this->assign("sale_protection",$sale_protection['content']);
            }
            $this->assign("child_category",$childCategory);
            $this->assign("prom",$prom);
            $this->assign("goods",$goods);
            $this->assign("goods_attrs",$goods_attrs);
            $this->assign("category_nav",$category);
            $this->assign("skumap",$skumap);
            $this->assign("comment",$comment);
            $this->redirect();
        }else{
            Tiny::Msg($this,"404");
        }

    }
    //搜索处理
    public function search()
    {
        $this->parseCondition();
    }
    public function category()
    {
        $this->parseCondition();
    }
    //搜索与分类的条件解析
    private function parseCondition()
    {

        $page = intval(Req::args("p"));
        $page_size = 36;
        $sort = Filter::int(Req::args("sort"));
        $sort = $sort==null?0:$sort;
        $cid = Filter::int(Req::args("cid"));
        $cid = $cid==null?0:$cid;
        $brand = Filter::int(Req::args("brand"));
        $price = Req::args("price");//下面已进行拆分过滤


        $keyword = Req::args('keyword');
        if($keyword!=null){
            $keyword = urldecode($keyword);
            $keyword = Filter::text($keyword);
            $keyword = Filter::commonChar($keyword);
        }

        //初始化数据
        $attrs = $specs = $spec_attr = $category_child = $spec_attr_selected = $selected = $has_category = $category = $current_category = array();
        $where = $spec_attr_where = $url = "";
        $condition_num = 0;

        $model = $this->model;
        //基本条件的建立
        //关于搜索的处理
        $action = strtolower(Req::args("act"));
        if($action=='search'){
            //关于类型的处理
            ////提取商品下的类型
            $seo_title = $seo_keywords = $keyword;
            $where = "name like '%$keyword%'";
            $rows = $model->table("goods")->fields("category_id,count(id) as num")->where($where)->group("category_id")->findAll();
            $category_ids = "";
            $category_count = array();
            foreach ($rows as $row) {
                $category_ids .= $row['category_id'].',';
                $category_count[$row['category_id']] = $row['num'];
            }
            $category_ids = trim($category_ids,",");
            $has_category = array();

            $seo_description = '';

            if($category_ids){

                    //搜索到内容且真正的点击搜索时进行统计
                if($this->getModule()->checkToken()){
                    $keyword = urldecode(Req::args('keyword'));
                    $keyword = Filter::sql($keyword);
                    $keyword = trim($keyword);
                    $len = TString::strlen($keyword);
                    if($len>=2 && $len<=8){
                        $model = new Model("tags");
                        $obj = $model->where("name='$keyword'")->find();
                        if($obj){
                            $model->data(array('num'=>"`num`+1"))->where("id=".$obj['id'])->update();
                        }else{
                            $model->data(array('name'=>$keyword))->insert();
                        }
                    }
                }

                $rows = $model->table("goods_category")->where("id in ($category_ids)")->findAll();
                foreach ($rows as $row) {
                    $path = trim($row['path'],',');
                    $paths = explode(',', $path);
                    $root = 0;
                    if(is_array($paths)) $root = $paths[0];
                    $row['num'] = $category_count[$row['id']];
                    $has_category[$root][] = $row;
                    $seo_description .= $row['name'].',';
                }
            }
            if($cid!=0){
                $where = "category_id=$cid and name like '%$keyword%'";
                $category = $model->table("goods_category as gc ")->join("left join goods_type as gt on gc.type_id = gt.id")->where("gc.id=$cid")->find();
                if($category){
                    $attrs = unserialize($category['attr']);
                    $specs = unserialize($category['spec']);

                    if($category['seo_title']!='') $seo_title = $category['seo_title'];
                    else $seo_title = $category['name'];
                    if($category['seo_keywords']!='') $seo_keywords = $category['seo_keywords'];
                    if($category['seo_description']!='') $seo_description = $category['seo_description'];
                }
            }

        //关于分类检索的处理
        }else if($action=='category'){
            $seo_title = "分类检索";
            $seo_keywords = "全部分类";
            $seo_description = "所有分类商品";
            //取得商品的子分类
            $category_ids = "";
            $categ = Category::getInstance();
            if($cid==0){
                $category_child = $categ->getCategoryChild(0,1);
            }else{
                $current_category = $this->model->table("goods_category as gc")->fields("gc.*,gt.name as gname,gt.attr,gt.spec,gc.seo_title,gc.seo_keywords,gc.seo_description")->join("left join goods_type as gt on gc.type_id = gt.id")->where("gc.id = $cid")->find();
                if($current_category){
                    $path  = trim($current_category['path'],',');
                    $rows = $this->model->table("goods_category")->where("path like '$current_category[path]%'")->order("field(`id`,$path)")->findAll();
                    $category = $this->model->table("goods_category")->where("id in ($path)")->order("field(`id`,$path)")->findAll();

                    foreach ($rows as $row) {
                        $category_ids .= $row['id'].',';
                    }
                    $category_ids = trim($category_ids,",");
                    $category_child = $categ->getCategoryChild($path,1);

                    $attrs = unserialize($current_category['attr']);
                    $specs = unserialize($current_category['spec']);
                    $attrs = is_array($attrs)?$attrs:array();
                    $specs = is_array($specs)?$specs:array();


                }

            }
            $seo_category = $model->table('goods_category')->where("id=$cid")->find();
            if($seo_category){
                if($seo_category['seo_title']!='') $seo_title = $seo_category['seo_title'];
                else $seo_title = $seo_category['name'];
                if($seo_category['seo_keywords']!='') $seo_keywords = $seo_category['name'].','.$seo_category['seo_keywords'];
                else $seo_keywords = $seo_category['name'];
                if($seo_category['seo_description']!='') $seo_description = $seo_category['seo_description'];
                else $seo_description = $seo_category['name'];
            }
            if($category_ids != "")$where = "go.category_id in ($category_ids)";
            else $where = "1=1";

        }
        //品牌筛选
        $rows = $model->table("goods as go")->fields("brand_id,count(id) as num")->where($where)->group("brand_id")->findAll();
        $brand_ids = '';
        $brand_num = $has_brand = array();
        foreach ($rows as $row) {
            $brand_ids .= $row['brand_id'].',';
            $brand_num[$row['brand_id']] = $row['num'];
        }
        $brand_ids = trim($brand_ids,',');

        //价格区间
        $prices = $model->table("goods as go")->fields("max(sell_price) as max,min(sell_price) as min,avg(sell_price) as avg")->where($where)->find();
        $price_range = Common::priceRange($prices);

        if($brand_ids){
            $has_brand = $model->table("brand")->where("id in ($brand_ids)")->findAll();
        }
        //var_dump($price_range);exit();
        if(!empty($price_range))$has_price = array_flip($price_range);
        else $has_price = array();
        if($price && isset($has_price[$price])){
            $prices = explode('-', $price);
            if(count($prices)==2) $where .= " and sell_price>=".Filter::int($prices[0])." and sell_price <=".Filter::int($prices[1]);
            else $where .= " and sell_price>=".Filter::int($prices[0]);
            $url .= "/price/$price";
        }


        if($brand && isset($brand_num[$brand])){
            $url .= "/brand/$brand";
            $where .= " and brand_id = $brand ";
        }

        //规格与属性的处理
        if(is_array($attrs)){
            foreach ($attrs as $attr) {
                if($attr['show_type']==1){
                    $spec_attr[$attr['id']] = $attr;
                }
            }
        }

        if(is_array($specs)){
            foreach ($specs as $spec) {
                $spec['values'] = unserialize($spec['value']);
                unset($spec['value'],$spec['spec']);
                $spec_attr[$spec['id']] = $spec;
            }
        }
        foreach ($selected as $key => $value) {
            if(isset($spec_attr[$key])){
                $spec_attr_selected[$key] = $spec_attr[$key];
                foreach ($spec_attr_selected[$key]['values'] as $k => $v) {
                    if($value == $v['id']){
                        $spec_attr_selected[$key]['values'] = $v;
                        break;
                    }
                }
            }
        }


        //规格处属性的筛选
        $args = Req::args();
        unset($args['con'],$args['act'],$args['p'],$args['sort'],$args['brand'],$args['price']);
        foreach ($args as $key => $value) {
            if(is_numeric($key) && is_numeric($value)){
                if(isset($spec_attr[$key])){
                    $spec_attr_where .= "or (`key`=$key and `value` = $value) ";
                    $condition_num++;
                    $url .= '/'.$key.'/'.$value;
                }
            }
            $selected[$key] = $value;
        }
        $selected['price'] = $price;
        $selected['brand'] = $brand;

        $spec_attr_where = trim($spec_attr_where,"or");
        $where .= ' and go.is_online =0';
        if($condition_num>0){
            $where .= " and go.id in (select goods_id from tiny_spec_attr where $spec_attr_where group by goods_id having count(goods_id) >= $condition_num)";
        }

        //排序的处理
        switch ($sort) {
            case '1':
            $goods_model = $model->table("goods as go")->join("left join tiny_order_goods as og on go.id = og.goods_id")->fields("go.*,sum(og.goods_nums) as sell_num")->order("sell_num desc")->group("go.id");
            break;
            case '2':
            $goods_model = $model->table("goods as go")->join("left join tiny_review as re on go.id = re.goods_id")->fields("go.*,count(re.goods_id) as renum")->group("go.id")->order("renum desc");
            break;
            case '3':
            $goods_model = $model->table("goods as go")->order("sell_price desc");
            break;
            case '4':
            $goods_model = $model->table("goods as go")->order("sell_price");
            break;
            case '5':
            $goods_model = $model->table("goods as go")->order("id desc");
            break;
            default:
            $goods_model = $model->table("goods as go")->order("sort desc");
            break;
        }
        //var_dump($where);exit;
        //提取商品
        $goods = $goods_model->where($where)->findPage($page,$page_size);

        //品牌处理
        preg_match_all('!(<(a|span)[^>]+>(上一页|下一页)</\2>)!', $goods['html'], $matches);
        $topPageBar = "";
        if(count($matches[0])>0) $topPageBar = implode("", $matches[0]);
        $this->assign("topPageBar",$topPageBar);

        //赋值处理
        $this->assign('seo_title',$seo_title);
        $this->assign('seo_keywords',$seo_keywords);
        $this->assign('seo_description','对应的商品共有'.$goods['page']['total'].'件商品,包括以下分类:'.$seo_description);

        $keyword = str_replace('|', '',$keyword);
        $this->assign("keyword",$keyword);
        $this->assign("sort",$sort);
        $this->assign("has_brand",$has_brand);
        $this->assign("brand_num",$brand_num);
        $this->assign("current_category",$current_category);
        $this->assign("goods",$goods);
        $this->assign("selected",$selected);
        $this->assign("spec_attr",$spec_attr);
        $this->assign("spec_attr_selected",$spec_attr_selected);
        $this->assign("category_child",$category_child);
        $this->assign("price_range",$price_range);
        $this->assign("category_nav",$category);
        $this->assign("has_category",$has_category);
        $this->assign("cid",$cid);



        if($action=='search') $this->assign("url","/index/search/keyword/".$keyword."/cid/$cid/sort/$sort".$url);
        else $this->assign("url","/index/category/cid/".$cid."/sort/$sort".$url);
        $this->redirect();


    }
    //取得咨询
    public function get_ask(){
        $page = Filter::int(Req::args("page"));
        $id = Filter::int(Req::args("id"));
        $asks = $this->model->table("ask as ak")->fields("ak.*,ak.id as id,us.name as uname,us.head_pic")->join("left join user as us on ak.user_id = us.id")->where("ak.goods_id = $id and ak.status!=2")->order('ak.id desc')->findPage($page,10,1,true);
        foreach ($asks['data'] as $key => $value) {
            $asks['data'][$key]['head_pic'] = $value['head_pic']!=''?Url::urlFormat("@").$value['head_pic']:Url::urlFormat("#images/no-img.png");
            $asks['data'][$key]['uname'] = TString::msubstr($value['uname'],0,3,'utf-8','***');
        }
        $asks['status'] = "success";
        echo JSON::encode($asks);

    }
    //取得评价
    public function get_review(){
        $page = Filter::int(Req::args("page"));
        $id = Filter::int(Req::args("id"));
        $pagetype = Filter::int(Req::args("pagetype"));
        $score = Req::args("score");
        $where = "re.status=1 and re.goods_id = $id";
        switch ($score) {
            case 'a':
            $where .= " and re.point > 3";
            break;
            case 'b':
            $where .= " and re.point = 3";
            break;
            case 'c':
            $where .= " and re.point < 3";
            break;
            default:
            break;
        }
        $review = $this->model->table("review as re")->join("left join user as us on re.user_id = us.id")->fields("re.*,re.id as id,us.name as uname,us.head_pic")->where($where)->order("re.id desc")->findPage($page,10,$pagetype,true);
        $data = $review['data'];
        foreach ($data as  $key => $value) {
            $data[$key]['point'] = round($data[$key]['point']/5,2)*100;
            $data[$key]['head_pic'] = $value['head_pic']!=''?Url::urlFormat("@").$value['head_pic']:Url::urlFormat("#images/no-img.png");
            $data[$key]['uname'] = TString::msubstr($value['uname'],0,3,'utf-8','***');

        }
        $review['data'] = $data;
        $review['status'] = "success";
        echo JSON::encode($review);
    }

    function index(){
        $this->redirect();
    }
    public function login(){
        $this->layout = "simple";
        $this->redirect("login");
    }
    public function result(){
        $this->layout = "simple";
        $this->redirect();
    }
    public function reg()
    {
        $this->layout = "simple";
        $this->redirect("reg");
    }
    //订阅到货通知
    public function notify()
    {
        $rules = array('email:email:邮箱格式不正确!','mobile:mobi:手机格式不正确!');
        $validator_info = Validator::check($rules);
        if(is_array($validator_info)){
            array('status'=>'fail','msg'=>$validator_info['msg']);
            echo JSON::encode($info);
        }else{
            $goods_id = Filter::int(Req::args('goods_id'));
            $email = Filter::sql(Req::args('email'));
            $mobile = Filter::int(Req::args('mobile'));
            $model =  new Model('notify');

            $register_time = Date('Y-m-d H:i:s');
            $info = array('status'=>'fail','msg'=>'您还没有登录,无法订阅到货通知。');
            if(isset($this->user['id'])){
                $time = date('Y-m-d H:i:s' , strtotime('-3 day'));
                $obj = $model->where('user_id = '.$this->user['id'].' and goods_id='.$goods_id.' and register_time >'."'$time'")->find();
                if($obj){
                    $info = array('status'=>'warning','msg'=>'您已订阅过了该商品的到货通知。');
                }else{
                    $data = array('user_id'=>$this->user['id'],'goods_id'=>$goods_id,'register_time'=>$register_time,'email'=>$email,'mobile'=>$mobile);
                    $last_id = $model->data($data)->insert();
                    if($last_id>0)$info = array('status'=>'success','msg'=>'订阅成功。');
                    else $info = array('status'=>'fail','msg'=>'订阅失败。');
                }
            }
            echo JSON::encode($info);
        }

    }
    //用户商品评价
    public function review()
    {
        $id = Filter::int(Req::args('id'));
        $review = $this->model->table("review as re")->join("left join goods as go on re.goods_id = go.id")->fields("re.*,go.name,go.img,go.id as gid,go.sell_price")->where("re.id=$id and user_id = ".$this->user['id']." and status=0")->find();
        if($review){
            $this->assign("review",$review);
            $this->redirect();
        }
        else{
            $this->redirect("msg",false,array('type'=>'fail','msg'=>'商品已经评论。'));
        }
    }
    public function review_act(){
        $id = Filter::int(Req::args('id'));
        $point = intval(Req::args('point'));
        if($point>5)$point = 5;
        elseif ($point<1) $point = 1;
        $content = Filter::txt(Req::args('content'));
        $content = TString::nl2br($content);

        $flage = $this->model->table("review")->data(array('status'=>1,'point'=>$point,'content'=>$content,'comment_time'=>date('Y-m-d')))->where("id=$id and user_id=".$this->user['id'])->update();
        $this->redirect("msg",false,array('type'=>"success",'msg'=>"评价成功!","redirect"=>"/ucenter/review"));
    }
    public function reg_act(){
        $email = Filter::sql(Req::post('email'));
        $passWord = Req::post('password');
        $rePassWord = Req::post('repassword');
        $this->safebox = Safebox::getInstance();
        $code = $this->safebox->get($this->captchaKey);
        $verifyCode = Req::args("verifyCode");
        $info = array('field'=>'verifyCode','msg'=>'验证码错误!');
        if($verifyCode==$code){
            if($passWord == $rePassWord){
                $model = $this->model->table("user");
                $obj = $model->where("email='$email'")->find();
                if($obj==null){
                    $validcode = CHash::random(8);
                    $model->data(array('email'=>$email,'name'=>$email,'password'=>CHash::md5($passWord,$validcode),'validcode'=>$validcode))->insert();
                    $this->redirect("index");
                }
                else{
                    $info = array('field'=>'email','msg'=>'此用户已经被注册!');
                }
            }
            else{
                $info = array('field'=>'repassword','msg'=>'两次密码输入不一致!');
            }
        }
        $this->assign("invalid",$info);
        $this->redirect("reg",false,Req::args());
    }
    public function js()
    {

        $id = Filter::sql(Req::args("id"));
        $model = new Model("ad");
        $time = date('Y-m-d');

        $ad = $model->where("number = '$id' and start_time<='$time' and end_time >='$time'")->find();
        if($ad==null) return;
        if($ad['is_open']==0) return;
        if($ad['type']!=5) $ad['content'] = unserialize($ad['content']);
        $str = '';
        $width = "";
        if($ad['width']==0) $width = "width:100%;";
        else $width = "width:".$ad['width'].'px;';
        if($ad['type']==1){
            foreach ($ad['content'] as $key => $item) {
               if($item['url']) $str = '<a href="'.$item['url'].'" target="_blank"><img src="'.Url::fullUrlFormat('@'.$item['path']).'" title="'.$item['title'].'"></a>';
               else $str = '<img src="'.Url::fullUrlFormat('@'.$item['path']).'" title="'.$item['title'].'">';
           }
       }
       else if($ad['type']==2){
          $str = '';

          foreach ($ad['content'] as $key => $item) {
                //$str .= '<a href="'.$item['url'].'" target="_blank" style="display:block;height:'.$ad['height'].'px;position:relative;width:100%;">fff</a>';
           if($item['url']) $str .= '<li  style="background-image: url('.Url::fullUrlFormat('@'.$item['path']).'); background-position: 50% 0%; background-repeat: no-repeat no-repeat; height: '.$ad['height'].'px;'.$width.';float:left;"><a href="'.$item['url'].'" target="_blank"></a></li>';
           else $str .= '<li  style="background-image: url('.Url::fullUrlFormat('@'.$item['path']).'); background-position: 50% 0%; background-repeat: no-repeat no-repeat; height: '.$ad['height'].'px;'.$width.';float:left;"></li>';
       }

       $str = '<div id="slider-'.$ad['number'].'" class="slider" style="margin-top:10px; height: '.$ad['height'].'px;'.$width.'"><ul class="items" style="white-space:nowrap;">'.$str.'</ul></div>';



   }
   else if($ad['type']==3){
    $content = $ad['content'];
    $str = '<a href="'.$content['url'].'" style="color:'.$content['color'].'" target="_blank">'.$content['title'].'</a>';
}
else if($ad['type']==4){
    foreach ($ad['content'] as $key => $item) {
       if($item['url']) $str = '<a href="'.$item['url'].'" target="_blank"><img src="'.Url::fullUrlFormat('@'.$item['path']).'" title="'.$item['title'].'"></a>';
       else $str = '<img src="'.Url::fullUrlFormat('@'.$item['path']).'" title="'.$item['title'].'">';
   }
            //$str = '<div style="position: fixed;left:0;bottom:0">'.$str.'</div>';
}
else{
    $str = preg_replace('/(\n|\r)/i', ' ', $ad['content']);
    $str = preg_replace("/'/i", "\'", $str);
}
$css='';
if($ad['type']==4){
    $info = $ad['content'][0];

    switch($info['position']){
        case 0:
        $css = "{left:0,top:0,right:'',bottom:'','z-index': 10000}";
        break;
        case 1:
        $css = "{left:w_middle,top:0,right:'',bottom:'','z-index': 10000}";
        break;
        case 2:
        $css = "{right:0,top:0,left:'',bottom:'','z-index': 10000}";
        break;
        case 3:
        $css = "{right:'',top:h_middle,left:0,bottom:'','z-index': 10000}";
        break;
        case 4:
        $css = "{right:'',top:h_middle,left:w_middle,bottom:'','z-index': 10000}";
        break;
        case 5:
        $css = "{right:0,top:h_middle,left:'',bottom:'','z-index': 10000}";
        break;
        case 6:
        $css = "{right:'',top:'',left:0,bottom:0,'z-index': 10000}";
        break;
        case 7:
        $css = "{right:'',top:'',left:w_middle,bottom:0,'z-index': 10000}";
        break;
        case 8:
        $css = "{right:0,top:'',left:'',bottom:0,'z-index': 10000}";
        break;

    }

    if($info['is_close']==1)$str .= '<a style="dispaly:block;border:red 1px solid;text-decoration: none; background: #f1f1f1; width:12px;height:12px; position:absolute;top:2px;right:2px;font-size:12px;font-family: serif;color:red;" href="javascript:$(\\\'#ad-'.$ad['number'].'\\\').remove()">×</a>';

    $str = '<div id="ad-'.$ad['number'].'" style="position: fixed;left:0;bottom:0;'.$width.'height:'.$ad['height'].'px;overflow: hidden;">'.$str.'</div>';
}
else{
    $str = '<div id="ad-'.$ad['number'].'" style="'.$width.'height:'.$ad['height'].'px;overflow: hidden;">'.$str.'</div>';
}
if($ad['type']==2)$str.='<script type="text/javascript">$("#slider-'.$ad['number'].'").Slider();</script>';
else if($ad['type']==4){
    $css = preg_replace('/\'/', '"', $css);
    $str.='<script type="text/javascript"> var w_middle = ($(window).width()-$("#ad-'.$ad['number'].'").width())/2+"px";var h_middle = ($(window).height()-$("#ad-'.$ad['number'].'").height())/2+"px";var css='.$css.';$("#ad-'.$ad['number'].'").css(css);</script>';
}
header('Content-type: text/javascript');
echo "document.write('".$str."');";
        // exit;
}
public function checkRight($actionId){
    if(isset($this->needRightActions[$actionId]) && $this->needRightActions[$actionId]){
        if(isset($this->user['name']) && $this->user['name']!=null) return true;
        else return false;
    }
    else return true;
}
public function noRight(){
    $this->redirect("/simple/login");
}
}