Files
orico-official-website-old/app/vn/controller/Product.php
2024-10-29 14:04:59 +08:00

223 lines
10 KiB
PHP
Executable File

<?php
namespace app\vn\controller;
use think\Loader;
use think\Cookie;
use think\Config;
class Product extends BaseController {
public function get_filter() {
if ($this->cacheHas('product_filter_vn'))
{
$product_filter = $this->cacheGet('product_filter_vn');
}
else
{
$product_filter = model('product')->where(['stat' => 0, 'country_code' => $this->country_code])->field('name, brand_id, keyword')->select();
$this->cacheSet('product_filter_vn', $product_filter, 86400);
}
$data = [
'product_name' => $product_filter,
];
return $this->json(200, 'ok', $data);
}
public function lists($id = 0) {
$category = Loader::model('product_category')->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_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('product_category')->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_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('product_category')->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_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, 'p.siteid' => $this->siteid];
$id = intval($id);
if ($id > 0) {
if (0) {
$ids = Loader::model('product_category')->getChildIDArray(4);
$arg_where['cid'] = ['in', $ids];
} else {
$arg_where['cid'] = $id;
}
$category = Loader::model('product_category')->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 (empty($detail)) {
return exception('数据有误,请检查后再操作');
}
$category = Loader::model('product_category')->getRow($detail['cid']);
$cid = Loader::model('product_category')->getRow($category['pid']);
$pid = Loader::model('product_category')->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_images'] = Loader::model('product_image')->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('product_dl')->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']
];
$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);
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]);
}
}
}