cacheHas('product_filter_us')) { $product_filter = $this->cacheGet('product_filter_us'); } else { $product_filter = model('product')->where(['stat' => 0, 'is_show' => 0, 'country_code' => $this->country_code])->field('name, brand_id, keyword')->select(); $this->cacheSet('product_filter_us', $product_filter, 86400); } $data = [ 'product_name' => $product_filter, ]; return $this->json(200, 'ok', $data); } public function lists($id = 0) { $category = Loader::model('ProductCategory')->getRow(['stat' => 0, 'pid' => 0], null, ['id' => 'asc']); if (empty($category)) { return exception('数据有误,请检查后再操作'); } $value = [ 'category' => $category, ]; $value['seo_title'] = config('product_seo_title')? : config('website_seo_title_us'); $value['seo_keyword'] = config('product_seo_keyword')? : config('website_seo_keyword'); $value['seo_description'] = config('product_seo_description')? : config('website_seo_description'); $this->assign($value); return $this->fetch('catelists'); } public function catelists($id = 0) { $arg_where = ['a.siteid' => $this->siteid]; if ($id > 0) { $arg_where['cid'] = $id; $category = Loader::model('ProductCategory')->getRow($id); } if (empty($category)) { return exception('数据有误,请检查后再操作'); } switch ($category['classtype']) { case 2: $template = $category['templist']; // $arg_order = ['a.id' => 'desc']; // $arg_field = ['a.*', 'c.id' => 'categoryid', 'c.name' => 'categoryname']; // $dataObject = Loader::model('Product')->getCateProductLists($arg_where, $arg_order, $arg_field, 12); // $value = [ // 'list' => $dataObject->isEmpty() ? null : $dataObject->items(), //$dataObject->getCollection()->toArray() // 'page' => $dataObject->render(), // ]; break; case 3: header('location:' . $category['url']); exit; break; default: $template = $category['tempindex']; break; } $value['category'] = $category; $value['seo_title'] = $category['seo_title']? : config('website_seo_title_us'); $value['seo_keyword'] = $category['seo_keyword']? : config('website_seo_keyword'); $value['seo_description'] = $category['seo_description']? : config('website_seo_description'); $this->assign($value); return $this->fetch($template); } public function subcatelists($id = 0) { $arg_where = ['a.siteid' => $this->siteid]; if ($id > 0) { $arg_where['cid'] = $id; $category = Loader::model('ProductCategory')->getRow($id);//dump($category);die; } if (empty($category)) { return exception('数据有误,请检查后再操作'); } switch ($category['classtype']) { case 2: $template = $category['templist']; $subproductCategory = $this->list_to_tree($this->categoryList, 'id', 'pid', 'child', $id); $value = ['subproductCategory' => $subproductCategory];//dump($subproductCategory);die; break; case 3: header('location:' . $category['url']); exit; break; default: $template = $category['tempindex']; break; } $result = model('ProductCategory')->where(['pid' => $category['id']])->find(); $last_cate = empty($result) ? 1 : 0; $value['last_cate'] = $last_cate; $value['pid'] = $id; $value['category'] = $category; $value['seo_title'] = $category['seo_title']? : config('website_seo_title_us'); $value['seo_keyword'] = $category['seo_keyword']? : config('website_seo_keyword'); $value['seo_description'] = $category['seo_description']? : config('website_seo_description'); $this->assign($value); return $this->fetch($template); } public function ajaxcatelists($id = 0) { $arg_where = ['p.stat' => 0, 'c.stat' => 0, 'is_show' => 0, 'p.siteid' => $this->siteid]; $id = intval($id); if ($id > 0) { if (0) { $ids = Loader::model('ProductCategory')->getChildIDArray(4); $arg_where['cid'] = ['in', $ids]; } else { $arg_where['cid'] = $id; } $category = Loader::model('ProductCategory')->getRow($id); } if (empty($category)) { return exception('数据有误,请检查后再操作'); } $arg_field = ['p.id', 'p.cid', 'p.name', 'p.shortname', 'p.sort', 'p.ishot', 'p.isnew', 'p.recommend', 'p.viewcount', 'p.tags', 'p.description', 'p.picture', 'p.picture_back', 'p.createtime', 'p.createtime', 'c.name' => 'categoryname', 'c.id' => 'categoryid']; $arg_order['p.sort'] = 'asc'; $arg_order['p.id'] = 'desc'; $dataObject = Loader::model('Product')->getCateProductLists($arg_where, $arg_order, $arg_field, 8); if ($dataObject->isEmpty()) { $value = ['list' => null]; } else { $this->assign(['list' => $dataObject->items(), 'category' => $category,]); $value = ['list' => $this->fetch()]; } return $this->result($value, true, '分类列表'); } public function detail($id = 0, $color = '') { // echo $color;die; if ($id > 0) { $detail = Loader::model('Product')->where(['stat' => 0, 'is_show' => 0, 'country_code' => $this->country_code, 'id' => $id])->find(); //echo "
=="; print_r($detail);die;
             
            if(empty($detail)){
                //return exception('该产品已下架,请检查后再操作');
                return $this->error('该产品已下架,请检查后再操作');
            }           
            
            $detail['is_collection'] = 0;

            if (empty($detail)) {
                return exception('数据有误,请检查后再操作');
            }
             $category = Loader::model('ProductCategory')->getRow($detail['cid']);
           
            
            $cid = Loader::model('ProductCategory')->getRow($category['pid']);
            $pid = Loader::model('ProductCategory')->getRow($cid['pid']);
            if (empty($category)) {
                return exception('数据有误,请检查后再操作');
            }
            $template = $category['tempdetail'];
            //$prev_detail = Loader::model('Product')->getRow(['id' => ['gt', $id], 'cid' => $category['id'], 'stat' => 0], ['id', 'name'], ['id' => 'asc']);
            //$next_detail = Loader::model('Product')->getRow(['id' => ['lt', $id], 'cid' => $category['id'], 'stat' => 0], ['id', 'name'], ['id' => 'desc']);
            $value = [
                'detail' => $detail,
                'category' => $category,
                'pid'=>$pid,
                'cid'=>$cid
                    //'prev_detail' => $prev_detail,
                    //'next_detail' => $next_detail,
            ];
            $value['product_attr'] = is_null(json_decode($detail['product_attr'])) ?  $detail['product_attr'] : json_decode($detail['product_attr']);
            
          // echo "
=="; print_r($value);die;
            $value['product_images'] = Loader::model('ProductImage')->getList(array('product_id' => $detail['id']), ['image_sort' => 'asc', 'id' => 'asc'], ['id', 'product_id', 'image_url', 'image_sort', 'image_desc', 'image_color']);
            // tiaoshi($value['product_images']);die;
            $value['product_dls'] = Loader::model('ProductDl')->getList(array('product_id' => $detail['id']), ['dl_sort' => 'asc', 'id' => 'desc'], ['id', 'product_id', 'dl_url', 'dl_sort', 'dl_name', 'dl_type']);
            // $value['product_questions'] = Loader::model('Question')->getList(array('product' => $detail['id']), ['sort' => 'asc', 'id' => 'asc'], ['id', 'name', 'product', 'sort', 'headline', 'recommend', 'description', 'createtime']);
            $value['product_relateds'] = Loader::model('Product')->getRelatedProductList(array('pr.product_id' => $detail['id']), ['pr.related_sort' => 'asc', 'pr.id' => 'asc',], ['p.id', 'p.name', 'p.shortname', 'p.picture', 'p.sort', 'p.ishot', 'p.list_bk_img','p.isnew', 'p.recommend', 'p.description','p.brand_id', 'p.createtime', 'pr.id' => 'related_id', 'pr.related_product_id', 'pr.related_sort', 'pr.related_desc']);
            $value['seo_title'] = $detail['seo_title']? : $detail['name'] . '-' . config('website_seo_title_us');
            $value['seo_keyword'] = $detail['seo_keyword']? : config('website_seo_keyword');
            $value['seo_description'] = $detail['seo_description']? : config('website_seo_description');
            $value['color'] = $color;
            //评论数据获取
            $where = [
                'product_id' => $detail['id']
            ];
            

            $product_images = [];
            $attributes = [];
            
            foreach ($value['product_images'] as $k => $v) {
                
                //兼容新旧两种版附加图、相册属性及图片
                if (is_string($v['image_url']) && json_decode($v['image_url'])) {
                    $v['image_url'] = json_decode($v['image_url'], true);           //多图Json格式
                    $v['image_color'] = json_decode($v['image_color'], true);       //多属性规则: 颜色,尺码,排序等
                    
                    if(isset($value['product_attr']) && is_array($value['product_attr'])){
                        foreach($value['product_attr'] as $ka => $attr){
                            $attributes[$attr][] =  $v['image_color'][$ka][$attr];
                        }
                    }
                    $product_images[] = $v;
                    
                }
                //原版, 仅支持颜色属性
                else{
                    $image_color    = $v['image_color'];
                    $pos            = strrpos($image_color, '/');
                    $key            = substr($image_color, $pos + 1, strpos($image_color, '.') - $pos - 1);
                    $product_images[$key][] = $v;
                    
                }                
                
            }
            foreach($attributes as $attrbute => $attrValue) {
                    $attributes[$attrbute] = array_unique($attrValue);
            }

            //echo "
++"; print_r($attributes);
            $value['attributes'] =  $attributes;
            $value['product_images'] = $product_images;

          
            
            $count = db('shopselle')->where($where)->count();
            $list = db('shopselle')->where($where)->paginate(10,$count);
            foreach ($list as $k => $v){
                $v['pics'] = json_decode($v['pics']);
                $list[$k] = $v;
            }
            $page = $list->render();
            $this->assign('page',$page);
            $this->assign('list',$list);
            $this->assign('count',$count);
            $this->assign($value);
            $this->viewcount($id);
            
            $purchase_links = Db::name('product_purchase_links')->alias('links')
                ->field(['links.id', 'platforms.platform', 'links.link'])
                ->join('product_purchase_link_platforms platforms', 'platforms.id=links.platform_id')
                ->where('links.product_id', '=', $id)
                ->where('links.country_code', '=', $this->country_code)
                ->select();
            $this->assign('purchase_links', $purchase_links);
            
            return $this->fetch($template);
        } else {
            return exception('数据有误,请检查后再操作');
        }
    }

    protected function viewcount($id) {
        $view = Cookie::get('productview', 'history');
        if (empty($view) || $view != $id) {
            Loader::model('Product')->where(['id' => $id])->setInc('viewcount');
            Cookie::set('productview', $id, ['prefix' => 'history', 'expire' => 3600]);
        }
    }

    protected function historyproduct($id) {
        $product = Cookie::get('product', 'history'); //print_r($history);exit;
        if (isset($product) && !empty($product)) {
            $product_ids = explode(',', $product);
            if ($product_ids[0] != $id) {
                //array_unshift($product_ids, $id);
                //$product_ids = array_unique($product_ids);
                $num = Config::get('history_number') > 0 ? Config::get('history_number') : 10;
                //$product_ids = array_slice($product_ids, 0, $num);
                while (count($product_ids) > $num) {
                    array_pop($product_ids);
                }
                Cookie::set('product', implode(',', $product_ids), ['prefix' => 'history', 'setcookie' => true, 'expire' => 3600 * 24 * 30]);
            }
        } else {
            Cookie::set('product', $id, ['prefix' => 'history', 'setcookie' => true, 'expire' => 3600 * 24 * 30]);
        }
    }

    /********新品展示 *********/
    public function new_arrival() {

        $value = Loader::model('Product')->getNewProductLists(array(),['t.sort' => 'asc', 't.id' => 'asc',], ['p.id', 'p.name', 'p.shortname', 'p.picture', 'p.sort', 'p.ishot', 'p.list_bk_img','p.isnew', 'p.recommend', 'p.description','p.brand_id', 'p.createtime', 't.id' => 'category_id', 't.name'=>'category_name', 't.sort']);
        
        //分类分组
        $newProduct = array();
        foreach($value as $key => $item) {
            $newProduct[$item['category_id']]['category_id'] = $item['category_id'];
            $newProduct[$item['category_id']]['category_name'] = $item['category_name'];
            $newProduct[$item['category_id']]['list'][] = $item;
        }
        //echo "
---"; print_r($productCategory);die;        //echo "
=="; print_r($productCategory);die;
        $this->assign('newProduct',$newProduct);

        $value['category'] = "New Product";
        $value['seo_title'] = $value['category']? : config('website_seo_title_us');
        $value['seo_keyword'] = $value['category']? : config('website_seo_keyword');
        $value['seo_description'] = $value['category']? : config('website_seo_description');
        $this->assign($value);

        return $this->fetch('new');
    }
}