From 047a358a7782ffb3a6821796e174b4196b9cda8f Mon Sep 17 00:00:00 2001 From: wangxiaowei Date: Mon, 2 Sep 2024 11:30:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96shop=E5=95=86=E5=93=81?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/logic/goods/GoodsLogic.php | 4 +-- app/shop/controller/goods/Goods.php | 6 ++-- app/shop/logic/goods/GoodsLogic.php | 48 +++++++++++++++++++--------- app/shop/view/goods/goods/lists.html | 13 ++++++-- 4 files changed, 47 insertions(+), 24 deletions(-) diff --git a/app/admin/logic/goods/GoodsLogic.php b/app/admin/logic/goods/GoodsLogic.php index 832ebcef..84bee007 100755 --- a/app/admin/logic/goods/GoodsLogic.php +++ b/app/admin/logic/goods/GoodsLogic.php @@ -427,8 +427,6 @@ class GoodsLogic extends Logic ->toArray(); } - - $title = [ 'name' => '商品名称', 'spec_value_str' => '规格', @@ -441,7 +439,7 @@ class GoodsLogic extends Logic ]; $data = array(); - foreach ($lists as $key => $item) { + foreach ($lists as $item) { if ($item['goods_item']) { $count_goods_item = count($item['goods_item']); foreach ($item['goods_item'] as $key2 => $items) { diff --git a/app/shop/controller/goods/Goods.php b/app/shop/controller/goods/Goods.php index 3d503c06..77b1f725 100755 --- a/app/shop/controller/goods/Goods.php +++ b/app/shop/controller/goods/Goods.php @@ -386,12 +386,12 @@ class Goods extends ShopBase public function export() { - $result = GoodsLogic::exportData($this->shop_id); + $ids = $this->request->post('ids'); + $result = GoodsLogic::exportData($this->shop_id, $ids); if(false === $result) { return JsonServer::error(GoodsLogic::getError() ?: '导出失败'); } - $src = input('server.REQUEST_SCHEME') . '://' . input('server.SERVER_NAME'); - $result['url'] = $src . '/uploads/excel/' . $result['url']; + return JsonServer::success('', ['url' => $result['url']]); } } diff --git a/app/shop/logic/goods/GoodsLogic.php b/app/shop/logic/goods/GoodsLogic.php index 1efa1b8e..8b9fb225 100755 --- a/app/shop/logic/goods/GoodsLogic.php +++ b/app/shop/logic/goods/GoodsLogic.php @@ -20,7 +20,6 @@ namespace app\shop\logic\goods; - use app\common\basics\Logic; use app\common\enum\GoodsEnum; use app\common\model\distribution\DistributionGoods; @@ -32,12 +31,10 @@ use app\common\model\goods\GoodsSpecValue; use app\common\model\goods\Goodswholesale; use app\common\model\goods\Goodspurchase; use app\common\model\goods\Goodscontact; +use app\common\server\ExportExcelServer; use app\common\server\UrlServer; use think\facade\Db; use app\common\model\shop\Shop; -use app\common\server\ExportGoodsExcelServer; - - /** * 商品管理-逻辑 @@ -1237,7 +1234,7 @@ class GoodsLogic extends Logic return $data; } - public static function exportData($shop_id) { + public static function exportData($shop_id, $ids) { try { $where = [ ['shop_id', '=', $shop_id], @@ -1252,19 +1249,38 @@ class GoodsLogic extends Logic 'id' => 'desc' ]; - $lists = Goods::where($where) - ->with('goods_item') - ->order($order) - ->select() - ->toArray(); + if ($ids) { + $lists = Goods::where($where) + ->whereIn('id', $ids) + ->with('goods_item') + ->order($order) + ->select() + ->toArray(); + } else { + $lists = Goods::where($where) + ->with('goods_item') + ->order($order) + ->select() + ->toArray(); + } + + $title = [ + 'name' => '商品名称', + 'spec_value_str' => '规格', + 'market_price' => '市场价', + 'price' => '价格', + 'chengben_price' => '成本价', + 'pifa_price' => '批发价', + 'purchase_price' => '集采价', + 'issuing_price' => '代发价', + ]; - $title = ['商品名称', '颜色', '市场价', '价格', '成本价', '批发价', '集采价', '代发价']; $data = array(); - foreach ($lists as $key => $item) { + foreach ($lists as $item) { if ($item['goods_item']) { $count_goods_item = count($item['goods_item']); foreach ($item['goods_item'] as $key2 => $items) { - $data[$key][] = array( + $data[] = array( 'name' => ($count_goods_item > 1 && $key2 > 0) ? '-' : $item['name'], 'spec_value_str' => $items['spec_value_str'], 'market_price' => $items['market_price'], @@ -1278,8 +1294,10 @@ class GoodsLogic extends Logic } } - $export = ExportGoodsExcelServer::export($title, $data, ROOT_PATH .'/uploads/excel/', '商品-' . time() . '.xlsx'); - return $export; + $export = new ExportExcelServer(); + $export->setFileName('商品列表'); + $result = $export->createExcel($title, $data); + return ['url' => $result]; } catch (\Exception $e) { self::$error = $e->getMessage(); return false; diff --git a/app/shop/view/goods/goods/lists.html b/app/shop/view/goods/goods/lists.html index 893fbce3..274433b4 100755 --- a/app/shop/view/goods/goods/lists.html +++ b/app/shop/view/goods/goods/lists.html @@ -165,11 +165,18 @@ // 导出数据 form.on('submit(data-export)', function (data) { - var field = data.field; + var checkStatus = table.checkStatus('goods-lists'); + var checkData = checkStatus.data; + var ids = []; + // 取出选中的行ID + checkData.forEach(function (item) { + ids.push(parseInt(item['id'])); + }); + like.ajax({ url: '{:url("goods.goods/export")}' - , data: field - , type: 'get' + ,data: {ids} + , type: 'post' , success: function (res) { if (res.code == 1) { window.location.href = res.data.url;