优化shop商品管理导出

This commit is contained in:
wangxiaowei 2024-09-02 11:30:29 +08:00
parent 749a3901af
commit 047a358a77
4 changed files with 47 additions and 24 deletions

@ -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) {

@ -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']]);
}
}

@ -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;

@ -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;