Browse Source

修改优化订单导出

jun 4 tháng trước cách đây
mục cha
commit
e1e3b4af49
2 tập tin đã thay đổi với 124 bổ sung98 xóa
  1. 122 96
      app/Http/Controllers/Admin/Orders.php
  2. 2 2
      composer.lock

+ 122 - 96
app/Http/Controllers/Admin/Orders.php

@@ -411,59 +411,79 @@ class Orders extends Auth{
 		if( $startTime )		$map[] = ['orders_product.insert_time','>=',strtotime($startTime)];
 		if( $endTime )			$map[] = ['orders_product.insert_time','<=',strtotime($endTime)];
 		if( $status )			$map[] = ['orders_product.status','=',$status];
-		// 查询数据
-		$list					= $OrdersProduct->query()
-									->join('custom','orders_product.custom_uid','=','custom.uid')
-									->join('orders_addr','orders_addr.order_id','=','orders_product.order_id');
-        if (isset($shopIds))    $list->whereIn('orders_product.business_id',$shopIds);
+        // 当前登录的角色数据
+        $session                = session('userRule') ? session('userRule') : ['business_id'=>0,'menu_type'=>0,'data_type'=>0];
+        // 是否存在对应的商业公司ID
+        if ( $session['business_id'] ) $map[] = ['orders_product.business_id','=',$session['business_id']];
+        // 数据类型
+        $shopIds				= ($session['menu_type'] == 1 && $session['data_type'] == 2)  ? Business::query()->where('leader_uid',$session['admin_uid'])->pluck('id')->toArray() : [];
+        // 查询数据
+        $list					= $OrdersProduct->query()
+            ->join('custom','orders_product.custom_uid','=','custom.uid')
+            ->join('orders_addr','orders_addr.order_id','=','orders_product.order_id');
+        if ( $shopIds )    		$list =   $list->whereIn('orders_product.business_id',$shopIds);
         $list                   = $list->where($map)
-									->orderByDesc('orders_product.id')
-									->select([
-										'orders_product.id as id',
-										'orders_product.order_id',
-										'orders_product.custom_uid',
-										'orders_product.product_id',
-										'orders_product.product_name',
-										'orders_product.sku_attr_names as product_spec',
-										'orders_product.product_thumb',
-										'orders_product.buy_num',
-										'orders_product.price_total',
-										'orders_product.coupon_total',
-										'orders_product.pay_total',
-										'orders_product.status',
-										'orders_product.insert_time',
-										'custom.username as custom_name','custom.weiban_extid as weiban_extid',
-										'orders_addr.contact_name','orders_addr.contact_shop','orders_addr.shop_type','orders_addr.contact_phone','orders_addr.contact_province','orders_addr.contact_city','orders_addr.contact_area','orders_addr.contact_addr'
-									])->get()->toArray();
-		// 返回结果
-		$data						= [];
-		// 循环处理数据
-		foreach ($list as $value) {
-			// id转编号
-			$value['order_id']		= $Model->idToCode($value['order_id']);
-			$value['status']		= $Model->getState($value['status'],'state');
-			$value['custom_uid']	= $Custom->idToCode($value['custom_uid']);
-			$value['product_price']	= $value['buy_num'] ? ($value['price_total'] / $value['buy_num']) : $value['buy_num'];
-			$value['pay_price']		= $value['buy_num'] ? ($value['pay_total'] / $value['buy_num']) : $value['buy_num'];
-			// 重组
-			$data[$value['order_id']]['order_id'] = $value['order_id'];
-			$data[$value['order_id']]['custom_uid'] = $value['custom_uid'];
-			$data[$value['order_id']]['custom_name'] = $value['custom_name'];
-			$data[$value['order_id']]['weiban_extid'] = $value['weiban_extid'];
-			$data[$value['order_id']]['status'] = $value['status'];
-			$data[$value['order_id']]['insert_time'] = $value['insert_time'];
-			// 地址
-			$data[$value['order_id']]['contact_name'] = $value['contact_name'];
-			$data[$value['order_id']]['contact_phone'] = $value['contact_phone'];
-			$data[$value['order_id']]['contact_province'] = $value['contact_province'];
-			$data[$value['order_id']]['contact_city'] = $value['contact_city'];
-			$data[$value['order_id']]['contact_area'] = $value['contact_area'];
-			$data[$value['order_id']]['contact_addr'] = $value['contact_addr'] .($value['contact_shop'] ? '【'.$value['contact_shop'].'】' : '');
-			$data[$value['order_id']]['shop_type'] 	  = $value['shop_type'] ? $Shoptype->getOne($value['shop_type'],'name') : '';
-			// 子订单
-			$data[$value['order_id']]['product'][] 	  = ['product_id'=>$value['product_id'] ? $Product->idToCode($value['product_id']) : '— —','product_name'=>$value['product_name'],'product_spec'=>$value['product_spec'],'product_thumb'=>$value['product_thumb'],'product_price'=>$value['product_price'],'pay_price'=>$value['pay_price'],'buy_num'=>$value['buy_num'],'pay_total'=>$value['pay_total'],'price_total'=>$value['price_total'],'coupon_total'=>$value['coupon_total']];
-		}
-
+            ->orderByDesc('id')
+            ->select([
+                'orders_product.id as id',
+                'orders_product.order_id',
+                'orders_product.custom_uid',
+                'custom.username as custom_name',
+                'orders_product.status',
+                'orders_addr.contact_name',
+                'orders_addr.contact_shop',
+                'orders_addr.contact_phone',
+                'orders_addr.contact_province',
+                'orders_addr.contact_city',
+                'orders_addr.contact_area',
+                'orders_addr.contact_addr',
+                'orders_addr.shop_type',
+                'orders_product.product_id',
+                'orders_product.product_name',
+                'orders_product.sku_attr_names as product_spec',
+                'orders_product.product_thumb',
+                'orders_product.buy_num',
+                'orders_product.price_total',
+                'orders_product.coupon_total',
+                'orders_product.pay_total',
+                'orders_product.insert_time',
+                'custom.weiban_extid as weiban_extid',
+            ])
+            ->get()->toArray();
+        $data       =   [];
+        // 循环处理数据
+        foreach ($list as $key => $value) {
+            // id转编号
+            $value['order_id']		= $Model->idToCode($value['order_id']);
+            $value['status']		= $Model->getState($value['status'],'state');
+            $value['custom_uid']	= $Custom->idToCode($value['custom_uid']);
+            $value['product_price']	= $value['buy_num'] ? ($value['price_total'] / $value['buy_num']) : $value['buy_num'];
+            $value['pay_price']		= $value['buy_num'] ? ($value['pay_total'] / $value['buy_num']) : $value['buy_num'];
+            // 重组
+            $data[$key]             = [
+                'order_id'		    => $value['order_id'],
+                'custom_uid'		=> $value['custom_uid'],
+                'custom_name'		=> $value['custom_name'],
+                'status'		    => $value['status'],
+                'contact_name'		=> $value['contact_name'],
+                'contact_phone'		=> $value['contact_phone'],
+                'contact_province'	=> $value['contact_province'],
+                'contact_city'		=> $value['contact_city'],
+                'contact_area'		=> $value['contact_area'],
+                'contact_addr'		=> $value['contact_addr'],
+                'shop_type'		    => $value['shop_type'] ? $Shoptype->getOne($value['shop_type'],'name') : '',
+                'product_id'		=> $value['product_id'] ? $Product->idToCode($value['product_id']) : '— —',
+                'product_name'		=> $value['product_name'],
+                'product_spec'		=> $value['product_spec'],
+                'product_price'		=> $value['product_price'],
+                'pay_price'		    => $value['pay_price'],
+                'buy_num'		    => $value['buy_num'],
+                'coupon_total'		=> $value['coupon_total'],
+                'pay_total'		    => $value['pay_total'],
+                'weiban_extid'		=> $value['weiban_extid'],
+                'insert_time'		=> date('Y-m-d H:i:s',$value['insert_time']),
+            ];
+        }
 		try {
 			// 去下载
 			$this->toDown($data);
@@ -478,50 +498,56 @@ class Orders extends Auth{
 	 * 去下载
 	 */
 	private function  toDown($data){
-		// 创建新的电子表格对象
-		$spreadsheet			= new Spreadsheet();
-		// 设置合并单元格的行和列,例如合并A1到B2的单元格
-		$sheet					= $this->setStyle($spreadsheet);
-		// 从第二行写入
-		$row					= 2;
-		// 循环写入
-		foreach ($data as $value) {
-			// 循环产品
-			foreach ($value['product'] as $v) {
-				// 单元格内容写入
-				$sheet->setCellValue('A'.$row, $value['order_id']);
-				$sheet->setCellValue('B'.$row, $value['custom_uid']);
-				//避免 = + - 识别成公式
-				$sheet->setCellValueExplicit('C'.$row, $value['custom_name'],DataType::TYPE_STRING);
-				$sheet->setCellValue('D'.$row, $value['status']);
-				$sheet->setCellValue('E'.$row, $value['contact_name']);
-				$sheet->setCellValue('F'.$row, $value['contact_phone']);
-				$sheet->setCellValue('G'.$row, $value['contact_province']);
-				$sheet->setCellValue('H'.$row, $value['contact_city']);
-				$sheet->setCellValue('I'.$row, $value['contact_area']);
-				$sheet->setCellValue('J'.$row, $value['contact_addr']);
-				$sheet->setCellValue('K'.$row, $value['shop_type']);
-				$sheet->setCellValue('L'.$row, $v['product_id']);
-				$sheet->setCellValue('M'.$row, $v['product_name']);
-				$sheet->setCellValue('N'.$row, $v['product_spec']);
-				$sheet->setCellValue('O'.$row, $v['product_price']);
-				$sheet->setCellValue('P'.$row, $v['pay_price']);
-				$sheet->setCellValue('Q'.$row, $v['buy_num']);
-				$sheet->setCellValue('R'.$row, $v['coupon_total']);
-				$sheet->setCellValue('S'.$row, $v['pay_total']);
-				$sheet->setCellValue('T'.$row, $value['weiban_extid']);
-				$sheet->setCellValue('U'.$row, date('Y-m-d H:i:s',$value['insert_time']));
-				// 函数自增
-				$row++;
-			}
-		}
-		// 创建内容
-		$writer 				= IOFactory::createWriter($spreadsheet, 'Xlsx');
-		header('Pragma: public');
-		header('Content-type:application/vnd.ms-excel');
-		header('Content-Disposition: inline;filename=下载订单.xlsx');
-		// 输出数据流
-		return $writer->save('php://output');
+        try {
+            $config     = [
+                'path' =>public_path().'/uploads/' // xlsx文件保存路径
+            ];
+            $excel      = new \Vtiful\Kernel\Excel($config);
+            $header     =   [
+                '订单ID',
+                '客户ID',
+                '客户昵称',
+                '订单状态',
+                '收货人',
+                '收货人手机号',
+                '省',
+                '市',
+                '区县',
+                '收货地址',
+                '终端类型',
+                '产品编码',
+                '产品名称',
+                '产品规格',
+                '产品单价',
+                '折后单价',
+                '产品数量',
+                '优惠金额',
+                '产品金额',
+                '微伴ID',
+                '下单时间',
+            ];
+            $filePath = $excel->fileName('tutorial01.xlsx', 'sheet1')
+                ->header($header)
+                ->data($data)
+                ->output();
+            $filename   =   '订单下载.xlsx';
+            header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+            header('Content-Disposition: attachment;filename="' . $filename . '"');
+            header('Content-Length: ' . filesize($filePath));
+            header('Content-Transfer-Encoding: binary');
+            header('Cache-Control: must-revalidate');
+            header('Cache-Control: max-age=0');
+            header('Pragma: public');
+            ob_clean();
+            flush();
+            if (copy($filePath, 'php://output') === false) {
+                dd('下载出错');
+            }
+            @unlink($filePath);
+            exit();
+        }catch (\Exception  $exception) {
+            return $exception->getMessage();
+        }
 	}
 
 	/**

+ 2 - 2
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "b0a46dcf3e2d7533be5df979f332f5ab",
+    "content-hash": "9c2736fcb6ae3efb111fdc710bf0e9af",
     "packages": [
         {
             "name": "adbario/php-dot-notation",
@@ -10812,5 +10812,5 @@
         "php": "^7.3|^8.0"
     },
     "platform-dev": [],
-    "plugin-api-version": "2.6.0"
+    "plugin-api-version": "2.3.0"
 }