goods模块添加导出数据
This commit is contained in:
parent
1ecf0bdd98
commit
07d7f86c6e
57
app/common/server/ExportGoodsExcelServer.php
Normal file
57
app/common/server/ExportGoodsExcelServer.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
namespace app\common\server;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
/**
|
||||
* 导出Excel
|
||||
* Class ExportExcelServer
|
||||
* @package app\common\lists
|
||||
*/
|
||||
class ExportGoodsExcelServer
|
||||
{
|
||||
public static function export($title = [], $data = [],$path,$file_name="")
|
||||
{
|
||||
try {
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
// 表头单元格内容 第一行
|
||||
$titCol = 'A';
|
||||
foreach ($title as $value) {
|
||||
// 单元格内容写入
|
||||
$sheet->setCellValue($titCol . '1', $value);
|
||||
$titCol++;
|
||||
}
|
||||
//单元格内容居中
|
||||
$sheet->getDefaultColumnDimension()->setAutoSize(true);//列宽自适应
|
||||
$sheet->getStyle('A:' . $titCol)->getAlignment()->setWrapText(true);//内容容垂居中
|
||||
$sheet->getStyle('A:' . $titCol)->getAlignment()->setVertical('center');//内容容垂居中
|
||||
$sheet->getStyle('A:' . $titCol)->getAlignment()->setHorizontal('center');//内水平直居中
|
||||
// 从第二行开始写入数据
|
||||
$row = 2;
|
||||
foreach ($data as &$item) {
|
||||
foreach ($item as &$value) {
|
||||
$dataCol = 'A';
|
||||
foreach($value as &$v) {
|
||||
$sheet->setCellValue($dataCol . $row, $v);
|
||||
$len = strlen($v);
|
||||
if ($len > 1 ){ //空数据不做处理避免影响到有数据的列宽度
|
||||
$sheet->getColumnDimension($dataCol)->setAutoSize(true);
|
||||
}
|
||||
$dataCol++;
|
||||
}
|
||||
$row++;
|
||||
}
|
||||
}
|
||||
|
||||
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
|
||||
if (is_file($path . $file_name)) {
|
||||
file_put_contents($path . $file_name,'');;
|
||||
}
|
||||
$writer->save($path . $file_name);
|
||||
return array('url' => trim($file_name,'.'));
|
||||
}catch (\Exception $e){
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -382,5 +382,17 @@ class Goods extends ShopBase
|
||||
return JsonServer::error(GoodsLogic::getError());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 导出Excel
|
||||
*/
|
||||
public function export()
|
||||
{
|
||||
$result = GoodsLogic::exportData($this->shop_id);
|
||||
if(false === $result) {
|
||||
return JsonServer::error(OrderLogic::getError() ?: '导出失败');
|
||||
}
|
||||
$src = input('server.REQUEST_SCHEME') . '://' . input('server.SERVER_NAME');
|
||||
$result['url'] = $src . '/uploads/excel/' . $result['url'];
|
||||
return JsonServer::success('', ['url' => $result['url']]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -35,7 +35,7 @@ use app\common\model\goods\Goodscontact;
|
||||
use app\common\server\UrlServer;
|
||||
use think\facade\Db;
|
||||
use app\common\model\shop\Shop;
|
||||
|
||||
use app\common\server\ExportGoodsExcelServer;
|
||||
|
||||
/**
|
||||
* 商品管理-逻辑
|
||||
@ -1232,5 +1232,55 @@ class GoodsLogic extends Logic
|
||||
|
||||
return $data;
|
||||
}
|
||||
/**
|
||||
* @notes 导出Excel
|
||||
*/
|
||||
public static function exportData($shop_id) {
|
||||
try {
|
||||
$where = [
|
||||
['shop_id', '=', $shop_id],
|
||||
['status', '=', GoodsEnum::STATUS_SHELVES],
|
||||
['del', '=', GoodsEnum::DEL_NORMAL],
|
||||
['audit_status', '=', GoodsEnum::AUDIT_STATUS_OK],
|
||||
['stock','exp', Db::raw('>=stock_warn')]
|
||||
];
|
||||
|
||||
$order = [
|
||||
'sort' => 'asc',
|
||||
'id' => 'desc'
|
||||
];
|
||||
|
||||
$lists = Goods::where($where)
|
||||
->with('goods_item')
|
||||
->order($order)
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$title = ['商品名称', '颜色', '市场价', '价格', '成本价', '批发价', '集采价', '代发价'];
|
||||
$data = array();
|
||||
foreach ($lists as $key => $item) {
|
||||
if ($item['goods_item']) {
|
||||
$count_goods_item = count($item['goods_item']);
|
||||
foreach ($item['goods_item'] as $key2 => $items) {
|
||||
$data[$key][] = array(
|
||||
'name' => ($count_goods_item > 1 && $key2 > 0) ? '-' : $item['name'],
|
||||
'spec_value_str' => $items['spec_value_str'],
|
||||
'market_price' => $items['market_price'],
|
||||
'price' => $items['price'],
|
||||
'chengben_price' => $items['chengben_price'],
|
||||
'pifa_price' => $items['wholesale_pce'],
|
||||
'purchase_price' => $items['purchase_price'],
|
||||
'issuing_price' => $items['issuing_price'],
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$export = ExportGoodsExcelServer::export($title, $data, ROOT_PATH .'/uploads/excel/', '商品-' . time() . '.xlsx');
|
||||
return $export;
|
||||
} catch (\Exception $e) {
|
||||
self::$error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,6 +65,7 @@
|
||||
<div class="layui-inline">
|
||||
<button class="layui-btn layui-btn-sm layui-btn-normal" lay-submit lay-filter="search">查询</button>
|
||||
<button class="layui-btn layui-btn-sm layui-btn-primary" lay-submit lay-filter="clear-search">重置</button>
|
||||
<button class="layui-btn layui-btn-sm layuiadmin-btn-ad layui-btn-primary " lay-submit lay-filter="data-export">导出</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -96,7 +97,7 @@
|
||||
</script>
|
||||
|
||||
<script type="text/html" id="price-info">
|
||||
{{d.min_price}} ~ {{d.max_price}}
|
||||
{{d.min_price}} ~ {{d.max_price}}
|
||||
</script>
|
||||
|
||||
<script type="text/html" id="ratio">
|
||||
@ -131,7 +132,13 @@
|
||||
</div>
|
||||
|
||||
<script>
|
||||
layui.use(['table', 'form', 'element'], function(){
|
||||
layui.config({
|
||||
base: "/static/admin/js/layui_exts/",
|
||||
}).extend({
|
||||
excel: 'excel',
|
||||
});
|
||||
|
||||
layui.use(['table', 'form', 'element', 'excel'], function(){
|
||||
var $ = layui.$
|
||||
,form = layui.form
|
||||
,table = layui.table
|
||||
@ -160,6 +167,21 @@
|
||||
});
|
||||
});
|
||||
|
||||
// 导出数据
|
||||
form.on('submit(data-export)', function (data) {
|
||||
var field = data.field;
|
||||
like.ajax({
|
||||
url: '{:url("goods.goods/export")}'
|
||||
, data: field
|
||||
, type: 'get'
|
||||
, success: function (res) {
|
||||
if (res.code == 1) {
|
||||
window.location.href = res.data.url;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
//事件
|
||||
var active = {
|
||||
add: function(){
|
||||
@ -347,16 +369,16 @@
|
||||
submit.trigger('click');
|
||||
}
|
||||
,cancel: function(index, layero){
|
||||
var window_index = index;
|
||||
layer.confirm('商品未保存,确定关闭吗?', {
|
||||
time: 0, //不自动关闭
|
||||
btn: ['确定', '取消'],
|
||||
yes: function(index){
|
||||
layer.close(index);
|
||||
layer.close(window_index);
|
||||
}
|
||||
});
|
||||
return false;
|
||||
var window_index = index;
|
||||
layer.confirm('商品未保存,确定关闭吗?', {
|
||||
time: 0, //不自动关闭
|
||||
btn: ['确定', '取消'],
|
||||
yes: function(index){
|
||||
layer.close(index);
|
||||
layer.close(window_index);
|
||||
}
|
||||
});
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user