作者 SHW\戥岁。。

数据处理

@@ -232,5 +232,397 @@ class Classification extends Api @@ -232,5 +232,397 @@ class Classification extends Api
232 $goods_spec = GoodsSpec::all(['goods_id' => $goods_id]); 232 $goods_spec = GoodsSpec::all(['goods_id' => $goods_id]);
233 $this->success('商品规格', ['list' => $goods_spec, 'sku' => $array]); 233 $this->success('商品规格', ['list' => $goods_spec, 'sku' => $array]);
234 } 234 }
  235 +
  236 + /**
  237 + * @ApiTitle (分类商品——二开)
  238 + * @ApiParams (name=sort_id, type=integer, required=false, description="分类id")
  239 + * @ApiParams (name=page, type=integer, required=false, description="页数")
  240 + * @ApiMethod (POST)
  241 + * @ApiReturn ({
  242 + "code": 1,
  243 + "msg": "分类商品列表",
  244 + "time": "1650252313",
  245 + "data": {
  246 + "list": {
  247 + "total": 2,
  248 + "per_page": 10,
  249 + "current_page": 1,
  250 + "last_page": 1,
  251 + "data": [
  252 + {
  253 + "goods_id": 41, //商品id
  254 + "goods_name": "黑人超白竹炭牙膏", //商品名称
  255 + "image": "/uploads/20220401/ff35da0c6cd2789f1f671d8ce1600b5d.png", //商品图
  256 + "cart_number": 0, //购物车数量
  257 + "price": "9.00", //折扣价格
  258 + "line_price": "10.00", //划线价
  259 + "is_discount": "is", //是否打折 is=打折(显示折扣) no= 未打折(不显示折扣)
  260 + "discount": "", //折扣
  261 + "goods_sku_list": [ //商品sku
  262 + {
  263 + "goods_spec_id": 273, //sku id
  264 + "goods_id": 41, //商品id
  265 + "goods_no": "4891338025416", //商品编号
  266 + "goods_price": "9.00", //折扣价
  267 + "line_price": "10.00", //划线价
  268 + "stock_num": 100, //库存
  269 + "goods_sales": 0,
  270 + "goods_weight": 0.09, //重量
  271 + "spec_sku_id": "",
  272 + "spec_image": "",
  273 + "create_time": 1648795163,
  274 + "update_time": 1648795163,
  275 + "discount": "" //折扣
  276 + }
  277 + ],
  278 + "goods_sku": [], //sku值
  279 + "image_text": "http://temporaryfood.qiniu.broing.cn/uploads/20220401/ff35da0c6cd2789f1f671d8ce1600b5d.png",
  280 + "images_text": [],
  281 + "down_image_text": "",
  282 + "four_image_text": [],
  283 + "detail_image_text": [],
  284 + "stock_num": 100
  285 + },
  286 + {
  287 + "goods_id": 46,
  288 + "goods_name": "宇宙无敌",
  289 + "image": "/uploads/20220415/88ede89dc926c7556d4caeec2def0cb5.jpg",
  290 + "cart_number": 0,
  291 + "price": "150.00",
  292 + "line_price": "150.00",
  293 + "is_discount": "no",
  294 + "discount": "3.3折",
  295 + "goods_sku_list": [
  296 + {
  297 + "goods_spec_id": 309,
  298 + "goods_id": 46,
  299 + "goods_no": "wzry20220415",
  300 + "goods_price": "150.00",
  301 + "line_price": "150.00",
  302 + "stock_num": 2000,
  303 + "goods_sales": 0,
  304 + "goods_weight": 0,
  305 + "spec_sku_id": "2",
  306 + "spec_image": "http://temporaryfood.qiniu.broing.cn/uploads/20220415/12ccfc9ce3ab739b7468210e1cb03b34.jpg",
  307 + "create_time": 1650010750,
  308 + "update_time": 1650010750,
  309 + "discount": "3.3折"
  310 + },
  311 + {
  312 + "goods_spec_id": 310,
  313 + "goods_id": 46,
  314 + "goods_no": "wzry20220415",
  315 + "goods_price": "50.00",
  316 + "line_price": "150.00",
  317 + "stock_num": 2000,
  318 + "goods_sales": 0,
  319 + "goods_weight": 0,
  320 + "spec_sku_id": "3",
  321 + "spec_image": "http://temporaryfood.qiniu.broing.cn/uploads/20220415/4c51cd16c74d566e501b3dc32f4af563.jpg",
  322 + "create_time": 1650010750,
  323 + "update_time": 1650010750,
  324 + "discount": "3.3折"
  325 + },
  326 + {
  327 + "goods_spec_id": 311,
  328 + "goods_id": 46,
  329 + "goods_no": "wzry20220415",
  330 + "goods_price": "50.00",
  331 + "line_price": "150.00",
  332 + "stock_num": 2000,
  333 + "goods_sales": 0,
  334 + "goods_weight": 0,
  335 + "spec_sku_id": "4",
  336 + "spec_image": "http://temporaryfood.qiniu.broing.cn/uploads/20220415/88ede89dc926c7556d4caeec2def0cb5.jpg",
  337 + "create_time": 1650010750,
  338 + "update_time": 1650010750,
  339 + "discount": "3.3折"
  340 + },
  341 + {
  342 + "goods_spec_id": 312,
  343 + "goods_id": 46,
  344 + "goods_no": "wzry20220415",
  345 + "goods_price": "50.00",
  346 + "line_price": "150.00",
  347 + "stock_num": 2000,
  348 + "goods_sales": 0,
  349 + "goods_weight": 0,
  350 + "spec_sku_id": "5",
  351 + "spec_image": "http://temporaryfood.qiniu.broing.cn/uploads/20220415/6e3683dac095dd567fc527de8a2a7ba6.jpg",
  352 + "create_time": 1650010750,
  353 + "update_time": 1650010750,
  354 + "discount": "3.3折"
  355 + }
  356 + ],
  357 + "goods_sku": [
  358 + {
  359 + "name": "分路",
  360 + "second": [
  361 + {
  362 + "id": 2,
  363 + "name": "上路"
  364 + },
  365 + {
  366 + "id": 3,
  367 + "name": "中路"
  368 + },
  369 + {
  370 + "id": 4,
  371 + "name": "下路"
  372 + },
  373 + {
  374 + "id": 5,
  375 + "name": "打野"
  376 + }
  377 + ]
  378 + }
  379 + ],
  380 + "image_text": "http://temporaryfood.qiniu.broing.cn/uploads/20220415/88ede89dc926c7556d4caeec2def0cb5.jpg",
  381 + "images_text": [],
  382 + "down_image_text": "",
  383 + "four_image_text": [],
  384 + "detail_image_text": [],
  385 + "stock_num": 8000
  386 + }
  387 + ]
  388 + },
  389 + "image": "http://temporaryfood.qiniu.broing.cn/uploads/20220114/95d210316c68dcc4d4c13f5b605ca463.jpg"
  390 + }
  391 + })
  392 + */
  393 + public function goods_list()
  394 + {
  395 + $sort_id = $this->request->param('sort_id', 0);
  396 + $page = $this->request->post('page', 1);
  397 + $model = new Goods();
  398 + if ($sort_id > 0) {
  399 + $list = $model
  400 + ->where('is_delete', '0')
  401 + ->where('category_id', $sort_id)
  402 + ->where('goods_status', '10')
  403 + ->field('goods_id,goods_name,image')
  404 + ->paginate(10, false, ['page' => $page])
  405 + ->each(function (&$item) {
  406 + if ($this->auth->isLogin()) {
  407 + $item['cart_number'] = Db::name('cart')
  408 + ->where('user_id', $this->auth->id)
  409 + ->where('goods_id', $item['goods_id'])
  410 + ->sum('number');
  411 + } else {
  412 + $item['cart_number'] = 0;
  413 + }
  414 + $goods_spec = Db::name('litestore_goods_spec')
  415 + ->where('goods_id', $item['goods_id'])
  416 + ->find();
  417 + $item['price'] = $goods_spec['goods_price'];
  418 + $item['line_price'] = $goods_spec['line_price'];
  419 + //判断是否打折
  420 + $item['is_discount'] = 'is';
  421 + if ($goods_spec['goods_price'] == $goods_spec['line_price']) {
  422 + $item['is_discount'] = 'no';
  423 + }
  424 + $item['discount'] = $goods_spec['discount'];
  425 + // 总库存
  426 + $item->append(['stock_num']);
  427 +
  428 + //商品sku
  429 + $goodsspecrelmodel = new GoodsSpecRel();
  430 + $list = $goodsspecrelmodel
  431 + ->where('goods_id', $item['goods_id'])
  432 + ->select();
  433 + $array = [];
  434 + $goods = \app\api\model\Goods::get($item['goods_id']);
  435 + if ($goods['spec_type'] == 20) {
  436 + foreach ($list as $key => $value) {
  437 + if (!isset($array[$value['spec_id']])) {
  438 + $array[$value['spec_id']]['name'] = Db::name('litestore_spec')
  439 + ->where('id', $value['spec_id'])
  440 + ->value('spec_name');
  441 + }
  442 + $spec_value = Db::name('litestore_spec_value')
  443 + ->where('id', $value['spec_value_id'])
  444 + ->value('spec_value');
  445 + $array[$value['spec_id']]['second'][] = [
  446 + 'id' => $value['spec_value_id'],
  447 + 'name' => $spec_value,
  448 + ];
  449 + }
  450 + $array = array_values($array);
  451 + }
  452 + $goods_spec = GoodsSpec::all(['goods_id' => $item['goods_id']]);
  453 + foreach ($goods_spec as &$spec_value) {
  454 + //判断是否打折
  455 + $spec_value['is_discount'] = 'is';
  456 + if ($spec_value['goods_price'] == $spec_value['line_price']) {
  457 + $spec_value['is_discount'] = 'no';
  458 + }
  459 + }
  460 + $item['goods_sku_list'] = $goods_spec;
  461 + $item['goods_sku'] = $array;
  462 + });
  463 + } else {
  464 + $list = $model
  465 + ->where('is_delete', '0')
  466 + ->where('goods_status', '10')
  467 + ->field('goods_id,goods_name,image')
  468 + ->paginate(10, false, ['page' => $page])
  469 + ->each(function (&$item) {
  470 + if ($this->auth->isLogin()) {
  471 + $item['cart_number'] = Db::name('cart')
  472 + ->where('user_id', $this->auth->id)
  473 + ->where('goods_id', $item['goods_id'])
  474 + ->sum('number');
  475 + } else {
  476 + $item['cart_number'] = 0;
  477 + }
  478 + $goods_spec = Db::name('litestore_goods_spec')
  479 + ->where('goods_id', $item['goods_id'])
  480 + ->find();
  481 + $item['price'] = $goods_spec['goods_price'];
  482 + $item['line_price'] = $goods_spec['line_price'];
  483 + //判断是否打折
  484 + $item['is_discount'] = 'is';
  485 + if ($goods_spec['goods_price'] == $goods_spec['line_price']) {
  486 + $item['is_discount'] = 'no';
  487 + }
  488 + $item['discount'] = $goods_spec['discount'];
  489 + // 总库存
  490 + $item->append(['stock_num']);
  491 +
  492 + //商品sku
  493 + $goodsspecrelmodel = new GoodsSpecRel();
  494 + $list = $goodsspecrelmodel
  495 + ->where('goods_id', $item['goods_id'])
  496 + ->select();
  497 + $array = [];
  498 + $goods = \app\api\model\Goods::get($item['goods_id']);
  499 + if ($goods['spec_type'] == 20) {
  500 + foreach ($list as $key => $value) {
  501 + if (!isset($array[$value['spec_id']])) {
  502 + $array[$value['spec_id']]['name'] = Db::name('litestore_spec')
  503 + ->where('id', $value['spec_id'])
  504 + ->value('spec_name');
  505 + }
  506 + $spec_value = Db::name('litestore_spec_value')
  507 + ->where('id', $value['spec_value_id'])
  508 + ->value('spec_value');
  509 + $array[$value['spec_id']]['second'][] = [
  510 + 'id' => $value['spec_value_id'],
  511 + 'name' => $spec_value,
  512 + ];
  513 + }
  514 + $array = array_values($array);
  515 + }
  516 + $goods_spec = GoodsSpec::all(['goods_id' => $item['goods_id']]);
  517 + foreach ($goods_spec as &$spec_value) {
  518 + //判断是否打折
  519 + $spec_value['is_discount'] = 'is';
  520 + if ($spec_value['goods_price'] == $spec_value['line_price']) {
  521 + $spec_value['is_discount'] = 'no';
  522 + }
  523 + }
  524 + $item['goods_sku_list'] = $goods_spec;
  525 + $item['goods_sku'] = $array;
  526 + });
  527 + }
  528 + $this->success('分类商品列表', ['list' => $list, 'image' => cdnurl(Config::get('site.advert'), true)]);
  529 +
  530 + }
  531 +
  532 + /**
  533 + * @ApiTitle (商品分类——二开)
  534 + * @ApiMethod (POST)
  535 + * @ApiReturn ({
  536 + "code": 1,
  537 + "msg": "ok",
  538 + "time": "1650251148",
  539 + "data": [
  540 + {
  541 + "id": 22, //分类id
  542 + "name": "RIO 3度白桃白兰地味鸡尾酒", //分类名称
  543 + "category_cart_number": 0, //该分类下的用户加入购物车的数量,未登录时为0
  544 + "image_text": ""
  545 + },
  546 + {
  547 + "id": 21,
  548 + "name": "冷制品",
  549 + "category_cart_number": 0,
  550 + "image_text": ""
  551 + },
  552 + {
  553 + "id": 20,
  554 + "name": "饮料",
  555 + "category_cart_number": 0,
  556 + "image_text": ""
  557 + },
  558 + {
  559 + "id": 13,
  560 + "name": "悠闲零食",
  561 + "category_cart_number": 0,
  562 + "image_text": ""
  563 + },
  564 + {
  565 + "id": 14,
  566 + "name": "酒水",
  567 + "category_cart_number": 0,
  568 + "image_text": ""
  569 + },
  570 + {
  571 + "id": 15,
  572 + "name": "乳品烘培",
  573 + "category_cart_number": 0,
  574 + "image_text": ""
  575 + },
  576 + {
  577 + "id": 16,
  578 + "name": "粮油调味",
  579 + "category_cart_number": 0,
  580 + "image_text": ""
  581 + },
  582 + {
  583 + "id": 17,
  584 + "name": "生活百货",
  585 + "category_cart_number": 0,
  586 + "image_text": ""
  587 + },
  588 + {
  589 + "id": 19,
  590 + "name": "日化类",
  591 + "category_cart_number": 0,
  592 + "image_text": ""
  593 + }
  594 + ]
  595 + })
  596 + */
  597 + public function category_list()
  598 + {
  599 + $model = new Category();
  600 + $list = $model->field('id,name')->order('weigh desc')->select();
  601 + $cart_sum = 0;
  602 + foreach ($list as $key => &$value) {
  603 + $model = new Goods();
  604 + if ($this->auth->isLogin()) {
  605 + $goods_list = $model
  606 + ->where('is_delete', '0')
  607 + ->where('category_id', $value['id'])
  608 + ->where('goods_status', '10')
  609 + ->field('goods_id')
  610 + ->select()->toArray();
  611 + foreach ($goods_list as &$item) {
  612 + $sum = Db::name('cart')
  613 + ->where('user_id', $this->auth->id)
  614 + ->where('goods_id', $item['goods_id'])
  615 + ->sum('number');
  616 + $cart_sum += $sum;
  617 + }
  618 + $value['category_cart_number'] = $cart_sum;
  619 + } else {
  620 + $value['category_cart_number'] = 0;
  621 + }
  622 + }
  623 + $this->success('ok', $list);
  624 +
  625 +
  626 + }
235 } 627 }
236 628
@@ -11,6 +11,7 @@ class Goods extends Model @@ -11,6 +11,7 @@ class Goods extends Model
11 { 11 {
12 // 自动写入时间戳字段 12 // 自动写入时间戳字段
13 protected $autoWriteTimestamp = 'int'; 13 protected $autoWriteTimestamp = 'int';
  14 + protected $resultSetType = 'collection';
14 protected $name = 'litestore_goods'; 15 protected $name = 'litestore_goods';
15 protected $createTime = 'createtime'; 16 protected $createTime = 'createtime';
16 protected $updateTime = 'updatetime'; 17 protected $updateTime = 'updatetime';