feat: 新品上市
This commit is contained in:
359
app/index/controller/Product.php
Normal file
359
app/index/controller/Product.php
Normal file
@@ -0,0 +1,359 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\index\controller;
|
||||
|
||||
use app\index\model\ProductAttrModel;
|
||||
use app\index\model\ProductCategoryModel;
|
||||
use app\index\model\ProductInquiryModel;
|
||||
use app\index\model\ProductModel;
|
||||
use app\index\model\ProductParamsModel;
|
||||
use app\index\model\ProductPurchaseLinkModel;
|
||||
use app\index\model\ProductSkuAttrModel;
|
||||
use app\index\model\ProductSkuModel;
|
||||
use app\index\model\SysBannerModel;
|
||||
use app\index\validate\ProductInquiryValidate;
|
||||
use think\facade\View;
|
||||
use think\helper\Arr;
|
||||
|
||||
/**
|
||||
* 产品控制器
|
||||
*/
|
||||
class Product extends Common
|
||||
{
|
||||
// 产品分类
|
||||
public function category()
|
||||
{
|
||||
// 参数
|
||||
$param = request()->param(['id']);
|
||||
|
||||
$focus_image = [];
|
||||
// 获取产品分类页焦点横幅
|
||||
$banner = SysBannerModel::with(['items' => function($query) {
|
||||
$query->withoutField([
|
||||
'status',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'deleted_at'
|
||||
])
|
||||
->where('status', '=', 1)
|
||||
->order(['sort' => 'asc', 'id' => 'desc']);
|
||||
}])
|
||||
->uniqueLabel(['BANNER_6808abd813d78'])
|
||||
->language($this->lang_id)
|
||||
->enabled(true)
|
||||
->select();
|
||||
if (!$banner->isEmpty()) {
|
||||
$banner_map = [];
|
||||
foreach ($banner as $v) {
|
||||
$banner_map[$v->unique_label] = $v;
|
||||
}
|
||||
$focus_image = data_get($banner_map, 'BANNER_6808abd813d78')?->items->toArray();
|
||||
}
|
||||
View::assign('focus_image', $focus_image);
|
||||
|
||||
// 获取分类及产品信息
|
||||
$categorys_data = ProductCategoryModel::field(['id', 'name', 'level'])
|
||||
->language($this->lang_id)
|
||||
->displayed(true)
|
||||
->child($param['id'], true)
|
||||
->order(['sort' => 'asc', 'id' => 'desc'])
|
||||
->select();
|
||||
if (!$categorys_data->isEmpty()) {
|
||||
if ($categorys_data->count() > 1) {
|
||||
// 当分类数不只一个时,当前分类下有子分类,移除当前分类,只输出子分类
|
||||
$categorys_data = $categorys_data->filter(function($item) use($param) {
|
||||
return $item->id != $param['id'];
|
||||
});
|
||||
}
|
||||
$categorys_data = $categorys_data->toArray();
|
||||
|
||||
$products = ProductModel::field([
|
||||
'id',
|
||||
'category_id',
|
||||
'spu',
|
||||
'name',
|
||||
'short_name',
|
||||
'cover_image',
|
||||
'desc'
|
||||
])
|
||||
->byCategory(array_column($categorys_data, 'id'))
|
||||
->language($this->lang_id)
|
||||
->enabled(true)
|
||||
->onSale(true)
|
||||
->onShelves(true)
|
||||
->order(['sort' => 'asc', 'id' => 'desc'])
|
||||
->select();
|
||||
if (!$products->isEmpty()) {
|
||||
// 获取sku信息
|
||||
$skus_map = [];
|
||||
$color_map = [];
|
||||
$skus = ProductSkuModel::withoutField(['sort', 'created_at', 'updated_at'])
|
||||
->byProductId(Arr::pluck($products, 'id'))
|
||||
->order(['sort' => 'asc', 'id' => 'asc'])
|
||||
->select();
|
||||
if (!$skus->isEmpty()) {
|
||||
// 获取产品sku属性信息
|
||||
$sku_attrs_map = [];
|
||||
$sku_attrs = ProductSkuAttrModel::bySkuId(array_unique(Arr::pluck($skus, 'id')))->select()->toArray();
|
||||
if (!empty($sku_attrs)) {
|
||||
// 获取属性名称
|
||||
$attrs = ProductAttrModel::bypks(array_unique(Arr::pluck($sku_attrs, 'attr_id')))->column(['attr_name'], 'id');
|
||||
// 获取属性名称
|
||||
foreach ($sku_attrs as $v) {
|
||||
$sku_attrs_map[$v['sku_id']][] = [
|
||||
'sku_id' => $v['sku_id'],
|
||||
'attr_id' => $v['attr_id'],
|
||||
'attr_name' => $attrs[$v['attr_id']]?? '',
|
||||
'attr_value' => $v['attr_value']
|
||||
];
|
||||
}
|
||||
}
|
||||
// sku匹配属性
|
||||
foreach ($skus as $v) {
|
||||
// 找到相应产品的sku图片信息
|
||||
$skus_map[$v['product_id']][] = [
|
||||
'id' => $v['id'],
|
||||
'sku' => $v['sku'],
|
||||
'main_image' => $v['main_image'],
|
||||
];
|
||||
// 找到相应产品的sku产色属性信息
|
||||
$attr = $sku_attrs_map[$v['id']]?? [];
|
||||
if (!empty($attr)) {
|
||||
foreach ($attr as $at) {
|
||||
if ($at['attr_name'] == '颜色') {
|
||||
$color_map[$v['product_id']][] = $at;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 按分类分组产品
|
||||
$products_map = [];
|
||||
foreach ($products as $v) {
|
||||
$products_map[$v['category_id']][] = [
|
||||
'id' => $v['id'],
|
||||
'spu' => $v['spu'],
|
||||
'name' => $v['name'],
|
||||
'short_name' => $v['short_name'],
|
||||
'cover_image' => $v['cover_image'],
|
||||
'desc' => $v['desc'],
|
||||
'sku' => $skus_map[$v['id']]?? [],
|
||||
'colors' => $color_map[$v['id']]?? []
|
||||
];
|
||||
}
|
||||
foreach ($categorys_data as $k => $v) {
|
||||
$categorys_data[$k]['products'] = $products_map[$v['id']] ?? [];
|
||||
}
|
||||
}
|
||||
}
|
||||
View::assign('categorys_data', $categorys_data);
|
||||
|
||||
return View::fetch('category');
|
||||
}
|
||||
|
||||
/**
|
||||
* 产品详情页
|
||||
*/
|
||||
public function detail()
|
||||
{
|
||||
$param = request()->param(['id']);
|
||||
|
||||
// 获取产品信息
|
||||
$product = ProductModel::withoutField([
|
||||
'language_id',
|
||||
'status',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'deleted_at'
|
||||
])
|
||||
->bypk($param['id'])
|
||||
->find();
|
||||
View::assign('product', $product);
|
||||
|
||||
$product_categorys = [];
|
||||
$product_params = [];
|
||||
$product_skus = [];
|
||||
$product_sku_attrs = [];
|
||||
$product_purchase_links = [];
|
||||
if (!empty($product)) {
|
||||
// 获取产品分类信息
|
||||
$product_categorys = ProductCategoryModel::field(['id', 'pid', 'name'])
|
||||
->bypk($product['category_id'])
|
||||
->union(function($query) use($product) {
|
||||
$query->name('product_category')
|
||||
->field(['id', 'pid', 'name'])
|
||||
->where('id', 'IN', function($sub_query) use($product) {
|
||||
$sub_query->name('product_category')
|
||||
->where('id', '=', $product['category_id'])
|
||||
->field('path');
|
||||
});
|
||||
})
|
||||
->order(['id' => 'asc'])
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
// 获取产品参数信息
|
||||
$product_params = ProductParamsModel::field(['id', 'name', 'value'])
|
||||
->byProductId($product['id'])
|
||||
->order(['id' => 'asc'])
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
// 获取产品sku信息
|
||||
$skus = ProductSkuModel::withoutField(['created_at', 'updated_at'])
|
||||
->byProductId($product['id'])
|
||||
->order(['sort' => 'asc', 'id' => 'asc'])
|
||||
->select();
|
||||
if (!empty($skus)) {
|
||||
$product_skus = $skus->toArray();
|
||||
|
||||
// 获取产品sku属性信息
|
||||
$sku_attrs = ProductSkuAttrModel::bySkuId(array_unique(Arr::pluck($skus, 'id')))->select()->toArray();
|
||||
if (!empty($sku_attrs)) {
|
||||
// 获取属性名称
|
||||
$attrs = ProductAttrModel::bypks(array_unique(Arr::pluck($sku_attrs, 'attr_id')))->column(['attr_name'], 'id');
|
||||
foreach ($sku_attrs as $v) {
|
||||
$v['attr_name'] = $attrs[$v['attr_id']]?? '';
|
||||
// 按属性分组
|
||||
$product_sku_attrs[$v['attr_id']]['attr_id'] = $v['attr_id'];
|
||||
$product_sku_attrs[$v['attr_id']]['attr_name'] = $v['attr_name'];
|
||||
$product_sku_attrs[$v['attr_id']]['attr_values'][] = [
|
||||
'sku_id' => $v['sku_id'],
|
||||
'attr_value' => $v['attr_value'],
|
||||
];
|
||||
}
|
||||
$product_sku_attrs = array_values($product_sku_attrs);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取产品购买链接信息
|
||||
$product_purchase_links = ProductPurchaseLinkModel::with(['platform'])
|
||||
->field(['platform_id', 'link'])
|
||||
->byProductId($product['id'])
|
||||
->order(['sort' => 'asc', 'id' => 'desc'])
|
||||
->select()
|
||||
->hidden(['platform'])
|
||||
->bindAttr('platform', ['platform_name' => 'platform'])
|
||||
->toArray();
|
||||
}
|
||||
View::assign('product_categorys', $product_categorys);
|
||||
View::assign('product_params', $product_params);
|
||||
View::assign('product_skus', $product_skus);
|
||||
View::assign('product_sku_attrs', $product_sku_attrs);
|
||||
View::assign('product_purchase_links', $product_purchase_links);
|
||||
|
||||
// 获取询盘可选国家
|
||||
$config = $this->basic_config['optional_country_for_product_inquiry'];
|
||||
View::assign('country_list', explode(',', preg_replace('/\r?\n/', ',', $config['value']?? '')));
|
||||
|
||||
return View::fetch('detail');
|
||||
}
|
||||
|
||||
/**
|
||||
* 产品询盘
|
||||
*/
|
||||
public function inquiry()
|
||||
{
|
||||
$post = request()->post([
|
||||
'first_name',
|
||||
'last_name',
|
||||
'email',
|
||||
'phone',
|
||||
'country_name',
|
||||
'corp_name',
|
||||
'industry',
|
||||
'message'
|
||||
]);
|
||||
|
||||
// 输出校验
|
||||
$validate = new ProductInquiryValidate;
|
||||
if (!$validate->check($post)) {
|
||||
return error($validate->getError());
|
||||
}
|
||||
|
||||
// 保存询盘信息
|
||||
$ret = ProductInquiryModel::create([
|
||||
'language_id' => $this->lang_id,
|
||||
'first_name' => $post['first_name'],
|
||||
'last_name' => $post['last_name'],
|
||||
'email' => $post['email'],
|
||||
'phone' => $post['phone'],
|
||||
'country_name' => $post['country_name'],
|
||||
'corp_name' => $post['corp_name'],
|
||||
'industry' => $post['industry'],
|
||||
'referer_url' => request()->header('referer'),
|
||||
'message' => $post['message'],
|
||||
]);
|
||||
if ($ret->isEmpty()) {
|
||||
return error(lang('product_detail.send_fail'));
|
||||
}
|
||||
return success(lang('product_detail.send_success'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 新品上市
|
||||
*/
|
||||
public function newpro()
|
||||
{
|
||||
$focus_image = [];
|
||||
// 获取焦点图
|
||||
$banner = SysBannerModel::with(['items' => function($query) {
|
||||
$query->withoutField([
|
||||
'status',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'deleted_at'
|
||||
])
|
||||
->where('status', '=', 1)
|
||||
->order(['sort' => 'asc', 'id' => 'desc']);
|
||||
}])
|
||||
->uniqueLabel(['BANNER_680dd7ceaa529'])
|
||||
->language($this->lang_id)
|
||||
->enabled(true)
|
||||
->select();
|
||||
if (!$banner->isEmpty()) {
|
||||
$focus_image = $banner->first()?->items ?? [];
|
||||
}
|
||||
View::assign('focus_image', $focus_image);
|
||||
|
||||
$newpros = [];
|
||||
// 获取新品上市产品
|
||||
$products = ProductModel::field(['id', 'category_id', 'name', 'spu', 'cover_image'])
|
||||
->language($this->lang_id)
|
||||
->enabled(true)
|
||||
->onSale(true)
|
||||
->onShelves(true)
|
||||
->isNew(true)
|
||||
->order(['sort' => 'asc', 'id' => 'desc'])
|
||||
->select();
|
||||
if ($products->isEmpty()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// 按分类分组产品
|
||||
$products_map = [];
|
||||
foreach ($products as $product) {
|
||||
$products_map[$product['category_id']][] = $product;
|
||||
}
|
||||
// 获取产品分类信息
|
||||
$categorys = ProductCategoryModel::field(['id', 'name'])
|
||||
->byPks(array_keys($products_map))
|
||||
->language($this->lang_id)
|
||||
->displayed(true)
|
||||
->order(['sort' => 'asc', 'id' => 'desc'])
|
||||
->select();
|
||||
if (!$categorys->isEmpty()) {
|
||||
foreach ($categorys as $category) {
|
||||
$newpros[] = [
|
||||
'category' => $category,
|
||||
'products' => $products_map[$category['id']] ?? [],
|
||||
];
|
||||
}
|
||||
}
|
||||
View::assign('newpros', $newpros);
|
||||
|
||||
return View::fetch('newpro');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user