Files
2024-10-29 17:57:44 +08:00

503 lines
23 KiB
PHP
Executable File

<?php
namespace app\index\controller;
use think\Collection;
use think\Db;
use think\Loader;
use think\Cookie;
use think\Config;
class Product extends BaseController {
public function get_filter() {
if ($this->cacheHas('product_filter_index'))
{
$product_filter_index = $this->cacheGet('product_filter_index');
}
else
{
$product_filter_index = model('product')->field('name, brand_id, keyword')->where(['stat' => 0, 'is_show' => 0, 'country_code' => $this->country_code])->select();
$this->cacheSet('product_filter_index', $product_filter_index, 86400);
}
$data = [
'product_name' => $product_filter_index,
];
return $this->json(200, 'ok', $data);
}
public function lists($id = 0) {
$category = Loader::model('ProductCategory')->getRow(['stat' => 0, 'pid' => 0, 'country_code' => $this->country_code], null, ['id' => 'asc']);
if (empty($category)) {
return exception('数据有误,请检查后再操作');
}
$value = [
'category' => $category,
];
$value['seo_title'] = config('product_seo_title')? : config('website_seo_title');
$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, 'country_code' => $this->country_code];
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'];
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');
$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, 'country_code' => $this->country_code];
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'];
$subproductCategory = $this->list_to_tree($this->categoryList, 'id', 'pid', 'child', $id);
$value = ['subproductCategory' => $subproductCategory];
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');
$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, 'p.siteid' => $this->siteid, 'p.country_code' => $this->country_code];
$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 = '') {
if ($id > 0) {
$detail = Loader::model('Product')->where(['stat' => 0, 'is_show' => 0, 'country_code' => $this->country_code, 'id' => $id])->find();
/*if ($this->customer_id > 0)
{
// 如果登录成功判断用户是否收藏该产品
$customer_info = json_decode(Cookie::get('c'), true);
if (empty($customer_info))
{
$detail['is_collection'] = 0;
}
else
{
$where = [
'customer_id' => $customer_info['id'],
'type' => 1,
'coll_id' => $detail['id'],
'stat' => 0
];
$result = model('collection')->where($where)->find();
if ($result)
{
$detail['is_collection'] = 1;
}
else
{
$detail['is_collection'] = 0;
}
}
}
else
{
$detail['is_collection'] = 0;
}*/
if(empty($detail)){
//return exception('该产品已下架,请检查后再操作');
return $this->error('该产品已下架,请检查后再操作');
}
$detail['is_collection'] = 0;
$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']);
$tmp_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']);
$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'], []);
$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.createtime', 'p.brand_id','pr.id' => 'related_id', 'pr.related_product_id', 'pr.related_sort', 'pr.related_desc']);
$product_images = [];
$attributes = [];
foreach ($tmp_product_images as $k => $v) {
//兼容新旧两种版附加图、相册属性及图片
if (is_array($v['image_url']) && sizeof($v['image_url'])) {
$v['image_url'] = json_decode($v['image_url'], true); //多图Json格式
$v['image_color'] = json_decode($v['image_color'], true); //多属性规则: 颜色,尺码,排序等
foreach($detail['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 "<pre>++"; print_r($attributes);
$value['attributes'] = $attributes;
$product_images = [];
$attributes = [];
foreach ($tmp_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){
if(isset($v['image_color']) && sizeof($v['image_color'])){
$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 "<pre>++"; print_r($attributes);
$value['attributes'] = $attributes;
$value['product_images'] = $product_images;
$value['seo_title'] = $detail['seo_title']? : $detail['name'] . '-' . config('website_seo_title');
$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']
];
$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);
//dump($value['product_relateds']);die;
return $this->fetch($template);
} else {
return exception('数据有误,请检查后再操作');
}
}
public function detailed($id = 0, $color = '') {
if ($id > 0) {
$detail = Loader::model('Product')->where(['stat' => 0, 'is_show' => 0, 'country_code' => $this->country_code, 'id' => $id])->find();
/*if ($this->customer_id > 0)
{
// 如果登录成功判断用户是否收藏该产品
$customer_info = json_decode(Cookie::get('c'), true);
if (empty($customer_info))
{
$detail['is_collection'] = 0;
}
else
{
$where = [
'customer_id' => $customer_info['id'],
'type' => 1,
'coll_id' => $detail['id'],
'stat' => 0
];
$result = model('collection')->where($where)->find();
if ($result)
{
$detail['is_collection'] = 1;
}
else
{
$detail['is_collection'] = 0;
}
}
}
else
{
$detail['is_collection'] = 0;
}*/
$detail['is_collection'] = 0;
$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']);
$tmp_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']);
$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'], []);
$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.createtime', 'p.brand_id','pr.id' => 'related_id', 'pr.related_product_id', 'pr.related_sort', 'pr.related_desc']);
$product_images = [];
$attributes = [];
foreach ($tmp_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 "<pre>++"; print_r($attributes);
$value['attributes'] = $attributes;
$value['product_images'] = $product_images;
$value['seo_title'] = $detail['seo_title']? : $detail['name'] . '-' . config('website_seo_title');
$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']
];
$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);
//dump($value['product_relateds']);die;
return $this->fetch('detail_202312');
} 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');
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;
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 newarrival() {
$value = Loader::model('Product')->getNewItemLists(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 "<pre>---"; print_r($productCategory);die; //echo "<pre>=="; 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('newarrival');
}
}