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.

180 lines
5.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace app\integral\logic;
use app\integral\model\IntegralCategory;
use app\integral\model\IntegralConfig;
use app\integral\model\IntegralProduct;
use app\integral\model\IntegralProductSpec;
use think\facade\Request;
class Product extends Base
{
/**
* 获取一级分类
* @date 2022-11-08
*/
public function listCategory()
{
$category_model = new IntegralCategory();
$config_model = new IntegralConfig();
// 获取分类
$where = [
['uid', '=', $this->mid]
];
$category_list = $category_model->getAllData($where, 'id,name,cover_img', 'sort desc');
$category_list = empty($category_list) ? [] : $category_list->toArray();
// 领取优惠券图标
$coupon_category_cover_img = $config_model->getOneData([
['uid', '=', $this->mid]
], 'coupon_category_img');
// 拼接领取优惠券页面
$coupon_category = [
'id' => '',
'name' => '兑换优惠券',
'cover_img' => $coupon_category_cover_img
];
// 在数组前插入一个元素
array_unshift($category_list, $coupon_category);
return sendSuccessArray([
// 分类列表
'category_list' => $category_list
]);
}
/**
* 获取商品列表
* @param int $category_id 分类ID
* @param string $keyword 关键字
* @param int $is_recommend 是否推荐 0--不限制 1--已推荐
* @param int $order 排序 1--综合排序(发布时间倒序) 2--价格升序 3--价格降序 4--积分升序 5--积分降序 6--销量降序不排序不传该字段默认1排序
* @param int $page 第X页
* @date 2022-11-09
*/
public function listProduct($category_id, $keyword, $is_recommend, $order, $page)
{
$product_model = new IntegralProduct();
//获取产品
$where = [
['uid', '=', $this->mid],
// 已发布
['is_publish', '=', 1],
];
// 是否推荐 0--全部 1--推荐
if (!empty($is_recommend)) {
$where[] = ['is_recommend', '=', $is_recommend];
}
// 关键字
if (!empty($keyword)) {
$where[] = ['name', 'like', '%' . $keyword . '%'];
}
$where_product_category = [];
// 分类ID
if (!empty($category_id)) {
$where_product_category[] = ['category_id', '=', $category_id];
}
// 顺序
if (empty($order)) {
$order = 'publish_time desc';
} else if ($order == 1) {
$order = 'publish_time desc';
} else if ($order == 2) {
$order = 'price asc';
} else if ($order == 3) {
$order = 'price desc';
} else if ($order == 4) {
$order = 'integral asc';
} else if ($order == 5) {
$order = 'integral desc';
} else if ($order == 6) {
$order = 'sales_number desc';
}
$field = 'id,name,integral,price,cover_img,sales_origial_number + sales_actual_number as sales_number';
$list = $product_model->listProductWithPage($where, $where_product_category, $page, $field, $order);
return sendSuccessArray([
// 产品列表
'list' => $list
]);
}
/**
* 获取商品详情
* @param int $product_id 产品ID
* @date 2022-11-09
*/
public function getProduct($product_id)
{
$product_model = new IntegralProduct();
$spec_model = new IntegralProductSpec();
// 获取详情
$field = 'id,name,description,img_path,cover_img,video_url,video_cover_img,stock,integral,price,sales_origial_number + sales_actual_number as sales_number,is_spec_open';
$product = $product_model->getProduct([
['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['integralProductSku'];
unset($product['integralProductSku']);
// 获取商品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['integralProductSpecItem'];
unset($spec[$k]['integralProductSpecItem']);
}
$product['spec'] = $spec;
}
// 分享连接
$product['h5_href'] = Request::domain() . '/h5/#/pages/common/download';
return sendSuccessArray([
// 产品
'product' => $product
]);
}
}