作者 SHW\戥岁。。

数据处理

... ... @@ -232,5 +232,397 @@ class Classification extends Api
$goods_spec = GoodsSpec::all(['goods_id' => $goods_id]);
$this->success('商品规格', ['list' => $goods_spec, 'sku' => $array]);
}
/**
* @ApiTitle (分类商品——二开)
* @ApiParams (name=sort_id, type=integer, required=false, description="分类id")
* @ApiParams (name=page, type=integer, required=false, description="页数")
* @ApiMethod (POST)
* @ApiReturn ({
"code": 1,
"msg": "分类商品列表",
"time": "1650252313",
"data": {
"list": {
"total": 2,
"per_page": 10,
"current_page": 1,
"last_page": 1,
"data": [
{
"goods_id": 41, //商品id
"goods_name": "黑人超白竹炭牙膏", //商品名称
"image": "/uploads/20220401/ff35da0c6cd2789f1f671d8ce1600b5d.png", //商品图
"cart_number": 0, //购物车数量
"price": "9.00", //折扣价格
"line_price": "10.00", //划线价
"is_discount": "is", //是否打折 is=打折(显示折扣) no= 未打折(不显示折扣)
"discount": "", //折扣
"goods_sku_list": [ //商品sku
{
"goods_spec_id": 273, //sku id
"goods_id": 41, //商品id
"goods_no": "4891338025416", //商品编号
"goods_price": "9.00", //折扣价
"line_price": "10.00", //划线价
"stock_num": 100, //库存
"goods_sales": 0,
"goods_weight": 0.09, //重量
"spec_sku_id": "",
"spec_image": "",
"create_time": 1648795163,
"update_time": 1648795163,
"discount": "" //折扣
}
],
"goods_sku": [], //sku值
"image_text": "http://temporaryfood.qiniu.broing.cn/uploads/20220401/ff35da0c6cd2789f1f671d8ce1600b5d.png",
"images_text": [],
"down_image_text": "",
"four_image_text": [],
"detail_image_text": [],
"stock_num": 100
},
{
"goods_id": 46,
"goods_name": "宇宙无敌",
"image": "/uploads/20220415/88ede89dc926c7556d4caeec2def0cb5.jpg",
"cart_number": 0,
"price": "150.00",
"line_price": "150.00",
"is_discount": "no",
"discount": "3.3折",
"goods_sku_list": [
{
"goods_spec_id": 309,
"goods_id": 46,
"goods_no": "wzry20220415",
"goods_price": "150.00",
"line_price": "150.00",
"stock_num": 2000,
"goods_sales": 0,
"goods_weight": 0,
"spec_sku_id": "2",
"spec_image": "http://temporaryfood.qiniu.broing.cn/uploads/20220415/12ccfc9ce3ab739b7468210e1cb03b34.jpg",
"create_time": 1650010750,
"update_time": 1650010750,
"discount": "3.3折"
},
{
"goods_spec_id": 310,
"goods_id": 46,
"goods_no": "wzry20220415",
"goods_price": "50.00",
"line_price": "150.00",
"stock_num": 2000,
"goods_sales": 0,
"goods_weight": 0,
"spec_sku_id": "3",
"spec_image": "http://temporaryfood.qiniu.broing.cn/uploads/20220415/4c51cd16c74d566e501b3dc32f4af563.jpg",
"create_time": 1650010750,
"update_time": 1650010750,
"discount": "3.3折"
},
{
"goods_spec_id": 311,
"goods_id": 46,
"goods_no": "wzry20220415",
"goods_price": "50.00",
"line_price": "150.00",
"stock_num": 2000,
"goods_sales": 0,
"goods_weight": 0,
"spec_sku_id": "4",
"spec_image": "http://temporaryfood.qiniu.broing.cn/uploads/20220415/88ede89dc926c7556d4caeec2def0cb5.jpg",
"create_time": 1650010750,
"update_time": 1650010750,
"discount": "3.3折"
},
{
"goods_spec_id": 312,
"goods_id": 46,
"goods_no": "wzry20220415",
"goods_price": "50.00",
"line_price": "150.00",
"stock_num": 2000,
"goods_sales": 0,
"goods_weight": 0,
"spec_sku_id": "5",
"spec_image": "http://temporaryfood.qiniu.broing.cn/uploads/20220415/6e3683dac095dd567fc527de8a2a7ba6.jpg",
"create_time": 1650010750,
"update_time": 1650010750,
"discount": "3.3折"
}
],
"goods_sku": [
{
"name": "分路",
"second": [
{
"id": 2,
"name": "上路"
},
{
"id": 3,
"name": "中路"
},
{
"id": 4,
"name": "下路"
},
{
"id": 5,
"name": "打野"
}
]
}
],
"image_text": "http://temporaryfood.qiniu.broing.cn/uploads/20220415/88ede89dc926c7556d4caeec2def0cb5.jpg",
"images_text": [],
"down_image_text": "",
"four_image_text": [],
"detail_image_text": [],
"stock_num": 8000
}
]
},
"image": "http://temporaryfood.qiniu.broing.cn/uploads/20220114/95d210316c68dcc4d4c13f5b605ca463.jpg"
}
})
*/
public function goods_list()
{
$sort_id = $this->request->param('sort_id', 0);
$page = $this->request->post('page', 1);
$model = new Goods();
if ($sort_id > 0) {
$list = $model
->where('is_delete', '0')
->where('category_id', $sort_id)
->where('goods_status', '10')
->field('goods_id,goods_name,image')
->paginate(10, false, ['page' => $page])
->each(function (&$item) {
if ($this->auth->isLogin()) {
$item['cart_number'] = Db::name('cart')
->where('user_id', $this->auth->id)
->where('goods_id', $item['goods_id'])
->sum('number');
} else {
$item['cart_number'] = 0;
}
$goods_spec = Db::name('litestore_goods_spec')
->where('goods_id', $item['goods_id'])
->find();
$item['price'] = $goods_spec['goods_price'];
$item['line_price'] = $goods_spec['line_price'];
//判断是否打折
$item['is_discount'] = 'is';
if ($goods_spec['goods_price'] == $goods_spec['line_price']) {
$item['is_discount'] = 'no';
}
$item['discount'] = $goods_spec['discount'];
// 总库存
$item->append(['stock_num']);
//商品sku
$goodsspecrelmodel = new GoodsSpecRel();
$list = $goodsspecrelmodel
->where('goods_id', $item['goods_id'])
->select();
$array = [];
$goods = \app\api\model\Goods::get($item['goods_id']);
if ($goods['spec_type'] == 20) {
foreach ($list as $key => $value) {
if (!isset($array[$value['spec_id']])) {
$array[$value['spec_id']]['name'] = Db::name('litestore_spec')
->where('id', $value['spec_id'])
->value('spec_name');
}
$spec_value = Db::name('litestore_spec_value')
->where('id', $value['spec_value_id'])
->value('spec_value');
$array[$value['spec_id']]['second'][] = [
'id' => $value['spec_value_id'],
'name' => $spec_value,
];
}
$array = array_values($array);
}
$goods_spec = GoodsSpec::all(['goods_id' => $item['goods_id']]);
foreach ($goods_spec as &$spec_value) {
//判断是否打折
$spec_value['is_discount'] = 'is';
if ($spec_value['goods_price'] == $spec_value['line_price']) {
$spec_value['is_discount'] = 'no';
}
}
$item['goods_sku_list'] = $goods_spec;
$item['goods_sku'] = $array;
});
} else {
$list = $model
->where('is_delete', '0')
->where('goods_status', '10')
->field('goods_id,goods_name,image')
->paginate(10, false, ['page' => $page])
->each(function (&$item) {
if ($this->auth->isLogin()) {
$item['cart_number'] = Db::name('cart')
->where('user_id', $this->auth->id)
->where('goods_id', $item['goods_id'])
->sum('number');
} else {
$item['cart_number'] = 0;
}
$goods_spec = Db::name('litestore_goods_spec')
->where('goods_id', $item['goods_id'])
->find();
$item['price'] = $goods_spec['goods_price'];
$item['line_price'] = $goods_spec['line_price'];
//判断是否打折
$item['is_discount'] = 'is';
if ($goods_spec['goods_price'] == $goods_spec['line_price']) {
$item['is_discount'] = 'no';
}
$item['discount'] = $goods_spec['discount'];
// 总库存
$item->append(['stock_num']);
//商品sku
$goodsspecrelmodel = new GoodsSpecRel();
$list = $goodsspecrelmodel
->where('goods_id', $item['goods_id'])
->select();
$array = [];
$goods = \app\api\model\Goods::get($item['goods_id']);
if ($goods['spec_type'] == 20) {
foreach ($list as $key => $value) {
if (!isset($array[$value['spec_id']])) {
$array[$value['spec_id']]['name'] = Db::name('litestore_spec')
->where('id', $value['spec_id'])
->value('spec_name');
}
$spec_value = Db::name('litestore_spec_value')
->where('id', $value['spec_value_id'])
->value('spec_value');
$array[$value['spec_id']]['second'][] = [
'id' => $value['spec_value_id'],
'name' => $spec_value,
];
}
$array = array_values($array);
}
$goods_spec = GoodsSpec::all(['goods_id' => $item['goods_id']]);
foreach ($goods_spec as &$spec_value) {
//判断是否打折
$spec_value['is_discount'] = 'is';
if ($spec_value['goods_price'] == $spec_value['line_price']) {
$spec_value['is_discount'] = 'no';
}
}
$item['goods_sku_list'] = $goods_spec;
$item['goods_sku'] = $array;
});
}
$this->success('分类商品列表', ['list' => $list, 'image' => cdnurl(Config::get('site.advert'), true)]);
}
/**
* @ApiTitle (商品分类——二开)
* @ApiMethod (POST)
* @ApiReturn ({
"code": 1,
"msg": "ok",
"time": "1650251148",
"data": [
{
"id": 22, //分类id
"name": "RIO 3度白桃白兰地味鸡尾酒", //分类名称
"category_cart_number": 0, //该分类下的用户加入购物车的数量,未登录时为0
"image_text": ""
},
{
"id": 21,
"name": "冷制品",
"category_cart_number": 0,
"image_text": ""
},
{
"id": 20,
"name": "饮料",
"category_cart_number": 0,
"image_text": ""
},
{
"id": 13,
"name": "悠闲零食",
"category_cart_number": 0,
"image_text": ""
},
{
"id": 14,
"name": "酒水",
"category_cart_number": 0,
"image_text": ""
},
{
"id": 15,
"name": "乳品烘培",
"category_cart_number": 0,
"image_text": ""
},
{
"id": 16,
"name": "粮油调味",
"category_cart_number": 0,
"image_text": ""
},
{
"id": 17,
"name": "生活百货",
"category_cart_number": 0,
"image_text": ""
},
{
"id": 19,
"name": "日化类",
"category_cart_number": 0,
"image_text": ""
}
]
})
*/
public function category_list()
{
$model = new Category();
$list = $model->field('id,name')->order('weigh desc')->select();
$cart_sum = 0;
foreach ($list as $key => &$value) {
$model = new Goods();
if ($this->auth->isLogin()) {
$goods_list = $model
->where('is_delete', '0')
->where('category_id', $value['id'])
->where('goods_status', '10')
->field('goods_id')
->select()->toArray();
foreach ($goods_list as &$item) {
$sum = Db::name('cart')
->where('user_id', $this->auth->id)
->where('goods_id', $item['goods_id'])
->sum('number');
$cart_sum += $sum;
}
$value['category_cart_number'] = $cart_sum;
} else {
$value['category_cart_number'] = 0;
}
}
$this->success('ok', $list);
}
}
... ...
... ... @@ -11,6 +11,7 @@ class Goods extends Model
{
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
protected $resultSetType = 'collection';
protected $name = 'litestore_goods';
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
... ...