You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
260 lines
8.5 KiB
260 lines
8.5 KiB
<?php
|
|
|
|
namespace app\mall\logic;
|
|
|
|
|
|
use app\mall\model\MallCategory;
|
|
use app\mall\model\MallConfig;
|
|
use app\mall\model\MallProduct;
|
|
use app\mall\model\MallProductSpec;
|
|
use think\facade\Request;
|
|
|
|
class Product extends Base
|
|
{
|
|
/**
|
|
* 获取商城一二级分类
|
|
* @date 2022-10-27
|
|
*/
|
|
public function listCategory()
|
|
{
|
|
$category_model = new MallCategory();
|
|
$config_model = new MallConfig();
|
|
|
|
// 获取二级分类图标
|
|
$two_category_img = $config_model->getOneData([
|
|
['uid', '=', $this->mid]
|
|
], 'two_category_img');
|
|
|
|
// 获取商城分类
|
|
$category_data = $category_model->getAllData([
|
|
['uid', '=', $this->mid]
|
|
], 'id,pid,name,cover_img,advert_img,advert_img_link', 'sort desc');
|
|
|
|
$category_data = empty($category_data) ? [] : $category_data->toArray();
|
|
|
|
// 树状结构
|
|
$category = listToTree($category_data);
|
|
|
|
// 给每个二级分类最前面组合全部
|
|
foreach ($category as $k => $v) {
|
|
$all_second_category_item = [
|
|
'id' => '',
|
|
'pid' => $v['id'],
|
|
'name' => '全部',
|
|
'cover_img' => $two_category_img,
|
|
'advert_img' => null,
|
|
'advert_img_link' => ''
|
|
];
|
|
|
|
// 二级分类数组
|
|
$second_category = empty($v['_child']) ? [] : $v['_child'];
|
|
|
|
// 数组开头插入一个或多个元素
|
|
array_unshift($second_category, $all_second_category_item);
|
|
|
|
$category[$k]['_child'] = $second_category;
|
|
}
|
|
|
|
return sendSuccessArray([
|
|
// 一二级分类树状结构列表
|
|
'category' => $category
|
|
]);
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取商品列表
|
|
* @param int $first_category_id 一级分类ID
|
|
* @param int $second_category_id 二级分类ID
|
|
* @param string $keyword 关键词
|
|
* @param int $is_recommend 是否推荐 0--不限制 1--已推荐
|
|
* @param int $price_min 最低价钱
|
|
* @param int $price_max 最高价钱
|
|
* @param int $order 排序 1--新品(默认) 2--销量 3--最低价格升序 4--最低价格降序 5--推荐时间倒序
|
|
* @param int $page 第X页
|
|
* @date 2022-10-27
|
|
*/
|
|
public function listProduct($first_category_id, $second_category_id, $keyword, $is_recommend, $price_min, $price_max, $order, $page)
|
|
{
|
|
$product_model = new MallProduct();
|
|
|
|
// 获取商城产品
|
|
$where = [
|
|
['uid', '=', $this->mid],
|
|
// 已发布
|
|
['is_publish', '=', 1],
|
|
// type == 1 商城产品
|
|
['MallProduct.type', 'like', '%1%'],
|
|
];
|
|
// 已推荐
|
|
if ($is_recommend == 1) {
|
|
$where[] = ['is_recommend', '=', $is_recommend];
|
|
}
|
|
// 名称模糊搜索
|
|
if (!empty($keyword)) {
|
|
$where[] = ['name', 'like', '%' . $keyword . '%'];
|
|
}
|
|
// 价钱(只有最小金额限制)
|
|
if (!empty($price_min) && empty($price_max)) {
|
|
$where[] = ['price_min', '>=', $price_min];
|
|
}
|
|
// 价钱(只有最大金额限制)
|
|
if (empty($price_min) && !empty($price_max)) {
|
|
$where[] = ['price_min', '<=', $price_max];
|
|
}
|
|
// 价钱(有最小和最大金额限制)
|
|
if (!empty($price_min) && !empty($price_max)) {
|
|
$where[] = ['price_min', 'between', [$price_min, $price_max]];
|
|
}
|
|
|
|
// 分类查询条件
|
|
$where_product_category = [];
|
|
if (!empty($first_category_id)) {
|
|
$where_product_category[] = ['type', '=', 1];
|
|
$where_product_category[] = ['category_id', '=', $first_category_id];
|
|
}
|
|
if (!empty($second_category_id)) {
|
|
unset($where_product_category);
|
|
$where_product_category[] = ['type', '=', 2];
|
|
$where_product_category[] = ['category_id', '=', $second_category_id];
|
|
}
|
|
|
|
// 排序
|
|
if ($order == 1) {
|
|
$order_type = 'publish_time desc,id desc';
|
|
} else if ($order == 2) {
|
|
$order_type = 'sales_number desc,id desc';
|
|
} else if ($order == 3) {
|
|
$order_type = 'price_min asc,id desc';
|
|
} else if ($order == 4) {
|
|
$order_type = 'price_min desc,id desc';
|
|
} else if ($order == 5) {
|
|
$order_type = 'recommend_time desc,id desc';
|
|
}
|
|
|
|
// 获取商品列表
|
|
$list = $product_model->listProductWithPage($where, $where_product_category, $page,
|
|
'id,name,price_min,cover_img,sales_origial_number + sales_actual_number as sales_number', $order_type);
|
|
|
|
return sendSuccessArray([
|
|
// 商品列表
|
|
'list' => $list
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取商品详情
|
|
* @param int $product_id 产品ID
|
|
* @date 2022-12-13
|
|
*/
|
|
public function getProduct($product_id)
|
|
{
|
|
$product_model = new MallProduct();
|
|
$spec_model = new MallProductSpec();
|
|
|
|
// 获取详情
|
|
$field = 'id,name,description,img_path,cover_img,video_url,video_cover_img,stock,price_min,price_max,price_original,review_number,sales_origial_number + sales_actual_number as sales_number,is_spec_open';
|
|
$product = $product_model->getOneProduct([
|
|
['id', '=', $product_id],
|
|
['is_publish', '=', 1]
|
|
], $field);
|
|
$product = empty($product) ? [] : $product->toArray();
|
|
|
|
if (!empty($product)) {
|
|
// 商品轮播图
|
|
$carousel = [];
|
|
if ($product['video_url']) {
|
|
$carousel[] = [
|
|
'type' => 2,
|
|
'image_url' => $product['video_cover_img'],
|
|
'video_url' => $product['video_url']
|
|
];
|
|
}
|
|
foreach ($product['img_path'] as $v) {
|
|
$carousel[] = [
|
|
'type' => 1,
|
|
'image_url' => $v,
|
|
'video_url' => ''
|
|
];
|
|
}
|
|
$product['carousel'] = $carousel;
|
|
unset($product['video_cover_img']);
|
|
unset($product['video_url']);
|
|
unset($product['img_path']);
|
|
|
|
// 规格浮层统一变量
|
|
$product['productSku'] = $product['mallProductSku'];
|
|
unset($product['mallProductSku']);
|
|
|
|
// 获取商品SPEC
|
|
$spec = $spec_model->listSpec([
|
|
['product_id', '=', $product_id]
|
|
], 'id,spec_id,spec_title');
|
|
$spec = empty($spec) ? [] : $spec->toArray();
|
|
|
|
// 规格浮层统一变量
|
|
foreach ($spec as $k => $v) {
|
|
$spec[$k]['productSpecItem'] = $v['mallProductSpecItem'];
|
|
unset($spec[$k]['mallProductSpecItem']);
|
|
}
|
|
|
|
$product['spec'] = $spec;
|
|
}
|
|
|
|
// 分享到H5的跳转链接
|
|
if (USER_AGENT == 'weixin' || USER_AGENT == 'mp_weixin') {
|
|
$product['h5_href'] = Request::domain() . '/h5/#/pages/mall/detail?id=' . $product_id;
|
|
} else {
|
|
$product['h5_href'] = Request::domain() . '/h5/#/pages/common/download';
|
|
}
|
|
|
|
|
|
return sendSuccessArray([
|
|
// 产品
|
|
'product' => $product
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 判断库存是否充足
|
|
* @date 2021-03-01
|
|
*/
|
|
public function isStockEnough($product_id, $sku = '', $number = 1)
|
|
{
|
|
$product_model = new MallProduct();
|
|
|
|
//获取商品详情
|
|
$product = $product_model->getOneData([
|
|
['id', '=', $product_id]
|
|
]);
|
|
if (empty($product)) {
|
|
return sendErrorArray(3001, '商品不存在');
|
|
}
|
|
if ($product['is_publish'] == 0) {
|
|
return sendErrorArray(3002, '商品已下架');
|
|
}
|
|
if ($product['is_spec_open'] == 1) {
|
|
if (empty($sku)) {
|
|
return sendErrorArray(3003, 'sku参数错误');
|
|
}
|
|
}
|
|
|
|
if ($product['is_spec_open'] == 0) { //没有规格
|
|
if ($number > $product['stock']) {
|
|
return sendErrorArray(3004, '商品加购件数(含已加购件数)超过库存');
|
|
}
|
|
} else {
|
|
foreach ($product['mallProductSku'] as $k => $v) {
|
|
if ($v['sku'] == $sku) {
|
|
if ($number > $v['stock']) {
|
|
return sendErrorArray(3005, '商品加购件数(含已加购件数)超过库存');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return sendSuccessArray();
|
|
}
|
|
|
|
|
|
}
|