Skip to content
关注公众号,获取新课通知

导出订单


安装导出excel扩展包 扩展包地址:http://packagist.p2hp.com/packages/jianyan74/php-excel

shell
composer require jianyan74/php-excel

控制器 app/controller/admin/Order.php

php
protected $excludeValidateCheck = [
	// ...
	'excelexport'
];

	// 导出订单
    public function excelexport(){
    	
    	$param = request()->param();
        $tab = getValByKey('tab',$param,'all');
        $model = $this->M;
        // 订单类型
        switch ($tab) {
        	case 'nopay': // 待付款
        		$model = $this->M->where('closed',0)
        						->whereNull('payment_method');
        		break;
        	case 'noship': // 待发货
        		$model = $this->M->where('closed',0)
        						->whereNotNull('payment_method')
        						->where('ship_status','pending')
        						->where('refund_status','pending');
        		break;
        	case 'shiped': // 已发货
        		$model = $this->M->where('closed',0)
        						->whereNotNull('payment_method')
        						->where('ship_status','delivered')
        						->where('refund_status','pending');
        		break;
        	case 'received': // 已收货
        		$model = $this->M->where('closed',0)
        						->whereNotNull('payment_method')
        						->where('ship_status','received')
        						->where('refund_status','pending');
        		break;
        	case 'finish': // 已完成
        		$model = $this->M->where('closed',0)
        						->whereNotNull('payment_method')
        						->where('ship_status','received')
        						->where('refund_status','pending');
        		break;
        	case 'closed': // 已关闭
        		$model = $this->M->where('closed',1);
        		break;
        	case 'refunding': // 退款中
        		$model = $this->M->where('closed',0)
        						->where('refund_status','applied');
        		break;
        }
        // 搜索条件
        if (array_key_exists('starttime',$param) && array_key_exists('endtime',$param)) {
        	$model = $model->whereTime('create_time', 'between', [$param['starttime'], $param['endtime']]);
        }
        
        $list = $model->with(['orderItems.goodsItem','user'])
		        ->order([ 'id'=>'desc' ])
				->select();
		
		$arr = [];
		$list->each(function($item) use(&$arr){
			// 联系方式
			$address = $item->address ="地址:".$item->address->province.$item->address->city.$item->address->district.$item->address->address." \n 姓名:".$item->address->name." \n 手机:".$item->address->phone;
			// 订单商品
			$order_items = '';
			foreach ($item->order_items as $val){
				$order_items .= '商品:'.$val['goods_item']['title']."\n ";
				$order_items .= '数量:'.$val['num']."\n ";
				$order_items .= '价格:'.$val['price']."\n\n ";
			}
			// 支付情况
			$pay = '未支付';
			switch ($item->payment_method) {
				case 'wechat':
					$pay = "支付方式:微信支付 \n 支付时间:".date('Y-m-d H:m:s',$item->paid_time);
					break;
				case 'wechat':
					$pay = "支付宝支付 \n 支付时间:".date('Y-m-d H:m:s',$item->paid_time);
					break;
			}
			// 发后状态
			$ship = '待发货';
			if ($item->ship_status && $item->ship_data) {
				$ship = "快递公司:".$item->ship_data->express_company." \n快递单号:".$item->ship_data->express_no." \n发货时间:".date('Y-m-d H:m:s',$item->ship_data->express_time);
			}
			
			$arr[] = [
				'id'=>$item->id,
				'no'=>$item->no,
				'address'=>$item->address,
				'order_items'=>$order_items,
				'pay'=>$pay,
				'ship'=>$ship,
				'create_time'=>$item->create_time
			];
		});
    	
    	// [名称, 字段名, 类型, 类型规则]
		$header = [
		    ['订单ID', 'id', 'text'],
		    ['订单号', 'no', 'text'],
		    ['收货地址', 'address'], // 规则不填默认text
		    ['商品', 'order_items'],
		    ['支付情况', 'pay'],
		    ['发货情况', 'ship'],
		    ['下单时间', 'create_time'],
		];
		
		// 简单使用
		return \jianyan\excel\Excel::exportData($arr, $header);
		
		// 定制 默认导出xlsx 支持 : xlsx/xls/html/csv
		// return Excel::exportData($list, $header, '测试', 'xlsx');
    }

路由 router/admin.php

php
Route::post('order/excelexport','admin.Order/excelexport');