|
@@ -0,0 +1,79 @@
|
|
|
+<?php
|
|
|
+namespace app\common\service;
|
|
|
+
|
|
|
+use app\common\model\Orders;
|
|
|
+use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
|
|
+use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
+use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
+
|
|
|
+class OrderExport{
|
|
|
+ public static function export($list){
|
|
|
+ $excel=new Spreadsheet();
|
|
|
+ $header=[
|
|
|
+ [
|
|
|
+ 'name'=>'ID',
|
|
|
+ 'value'=>function($model){
|
|
|
+ return $model['id'];
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'name'=>'订单号',
|
|
|
+ 'value'=>function($model){
|
|
|
+ return $model['order_no'];
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'name'=>'状态',
|
|
|
+ 'value'=>function($model){
|
|
|
+ return Orders::getStatus()[$model['status']];
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'name'=>'下单用户',
|
|
|
+ 'value'=>function($model){
|
|
|
+ return $model['user']['nickname'];
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'name'=>'商品信息',
|
|
|
+ 'value'=>function($model){
|
|
|
+ $goods=[];
|
|
|
+ foreach ($model['info'] as $info){
|
|
|
+ $goods[]="商品:{$info['goods_name']},规格:{$info['sku_name']},数量:{$info['num']},安装量:{$info['num_install']},金额:{$info['amount_pay']}";
|
|
|
+ }
|
|
|
+ return implode("\n",$goods);
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+ $headers=[];
|
|
|
+ foreach ($header as $value){
|
|
|
+ if(is_callable($value['value']) && self::visible($value)){
|
|
|
+ $headers[]=$value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $sheet=$excel->getActiveSheet();
|
|
|
+ $headerPoint='A';
|
|
|
+ foreach ($headers as $key=>$value){
|
|
|
+ $sheet->setCellValue(sprintf("%s%d",$headerPoint,1),$value['name']);
|
|
|
+ $vk=2;
|
|
|
+ foreach ($list as $_key=>$_value){
|
|
|
+ $val=$value['value']($_value);
|
|
|
+ $sheet->setCellValueExplicit(sprintf("%s%d", $headerPoint, $vk), $val,DataType::TYPE_STRING);
|
|
|
+ $vk++;
|
|
|
+ }
|
|
|
+ $headerPoint++;
|
|
|
+ }
|
|
|
+
|
|
|
+ $sheet->getColumnDimension('A')->setAutoSize(true);
|
|
|
+ $xlsx=new Xlsx($excel);
|
|
|
+ header('Content-Type: application/vnd.ms-excel;charset=UTF-8');
|
|
|
+ $xlsx->save('php://output');
|
|
|
+ return response()
|
|
|
+ ->data('')
|
|
|
+ ->contentType('application/vnd.ms-excel');
|
|
|
+ }
|
|
|
+
|
|
|
+ protected static function visible($item){
|
|
|
+ return empty($item['hidden']) || !$item['hidden'];
|
|
|
+ }
|
|
|
+}
|