Browse Source

导出接口的错误提示信息也改成文本流

wufeng 2 years ago
parent
commit
b4f4192b29
2 changed files with 63 additions and 55 deletions
  1. 1 1
      app/admin/controller/Order.php
  2. 62 54
      app/admin/logic/OrderLogic.php

+ 1 - 1
app/admin/controller/Order.php

@@ -29,7 +29,7 @@ class Order extends BaseController
     //导出
     public function export()
     {
-        $param = $this->request->only(['masterOrderCode' => '', 'orderCode' => '', 'status' => '', 'name' => '', 'username' => '', 'start_date' => '', 'end_date' => '', 'type' => ''], 'post');
+        $param = $this->request->only(['masterOrderCode' => '', 'orderCode' => '', 'status' => '', 'name' => '', 'username' => '', 'start_date' => '', 'end_date' => '', 'type' => '', 'token' => ''], 'post');
 
 
         return OrderLogic::export($param);

+ 62 - 54
app/admin/logic/OrderLogic.php

@@ -9,6 +9,7 @@ use app\model\MasterOrderModel;
 use app\model\OrderModel;
 use app\model\ServiceModel;
 use think\Exception;
+use think\exception\ValidateException;
 use think\facade\Db;
 use think\facade\Validate;
 use think\response\Json;
@@ -95,63 +96,70 @@ class OrderLogic extends BaseLogic
     //导出
     public static function export(array $data = []): Json
     {
-
-        $db = OrderModel::alias('a')
-            ->leftJoin('company b', 'b.id=a.company_id AND b.is_del=' . CommonModel::$del_normal)
-            ->leftJoin('card c', 'c.id=a.card_id AND c.is_del=' . CommonModel::$del_normal)
-            ->leftJoin('account d', 'd.id=a.uid AND d.is_del=' . CommonModel::$del_normal)
-            ->leftJoin('addr e', 'e.id=a.addr_id AND e.is_del=' . CommonModel::$del_normal)
-            ->where('a.is_del', CommonModel::$del_normal);
-
-        if ($data['masterOrderCode'] != '') $db->whereLike('a.masterOrderCode', '%' . $data['masterOrderCode'] . '%');
-        if ($data['orderCode'] != '') $db->whereLike('a.orderCode', '%' . $data['orderCode'] . '%');
-        if ($data['status'] != '') $db->where('a.status', $data['status']);
-        if ($data['name'] !== '') $db->whereLike('d.name', '%' . $data['name'] . '%');
-        if ($data['username']) $db->whereLike('d.username', '%' . $data['username'] . '%');
-        if (($data['start_date'] != '') && ($data['end_date'] != '')) $db->whereBetween('a.addtime', [$data['start_date'], $data['end_date']]);
-        if ($data['type'] !== '') $db->where('a.type', $data['type']);
-
-        $list = $db
-            ->field('a.id `订单ID`,a.masterOrderCode `主订单编号`,a.orderCode `订单编号`,a.type `订单类型`,b.title `业务公司`,c.title `卡类型`,d.name `购买用户名`,d.username `购买账号`,a.num `购买数量`,e.contactor `收货联系人`,e.mobile `收货联系电话`,a.status `状态`,a.post_name `物流公司`,a.post_code `物流单号`,a.addtime `创建时间`,e.addr_code,"" `收货地址`,e.addr,"" `商品编码`,"" `商品(服务)名称`,a.child_id')
-            ->order('a.addtime desc')
-            ->cursor();
-
-        $status = [
-            MasterOrderModel::$order_status_wait_pay => '待支付',
-            MasterOrderModel::$order_status_pay_fail => '支付失败',
-            MasterOrderModel::$order_status_not_deliver => '待发货',
-            MasterOrderModel::$order_status_deliver => '已发货',
-            MasterOrderModel::$order_status_receipt => '已收货',
-        ];
-        $type = [MasterOrderModel::$type_exchange_good => '兑换商品订单', MasterOrderModel::$type_shopping_good => '商城商品订单', MasterOrderModel::$type_service => '服务订单'];
-
         $da = [];
-        foreach ($list as $v) {
-            $v = $v->toArray();
-
-            if ($v['订单类型'] == MasterOrderModel::$type_service) $v['商品(服务)名称'] = ServiceModel::where(['is_del' => CommonModel::$del_normal, 'id' => $v['child_id']])->value('title', '');
-            else {
-                $good = GoodModel::where(['is_del' => CommonModel::$del_normal, 'id' => $v['child_id']])
-                    ->field('good_code,good_name')
-                    ->findOrEmpty()
-                    ->toArray();
-                $v['商品编码'] = $good['good_code'] ?? '';
-                $v['商品(服务)名称'] = $good['good_name'] ?? '';
-            };
-
-            $v['收货地址'] = $v['addr_code'] ? get_addr_name($v['addr_code']) . $v['addr'] : $v['addr'];
-            $v['状态'] = $status[$v['状态']] ?? '';
-            $v['订单类型'] = $type[$v['订单类型']] ?? '';
-
-            unset($v['addr_code']);
-            unset($v['addr']);
-            unset($v['child_id']);
-
-            $da[] = $v;
-        }
+        try {
 
-        if (empty($da)) $da[] = '没有可供导出的数据';
+            verify_token($data['token']);
+
+            $db = OrderModel::alias('a')
+                ->leftJoin('company b', 'b.id=a.company_id AND b.is_del=' . CommonModel::$del_normal)
+                ->leftJoin('card c', 'c.id=a.card_id AND c.is_del=' . CommonModel::$del_normal)
+                ->leftJoin('account d', 'd.id=a.uid AND d.is_del=' . CommonModel::$del_normal)
+                ->leftJoin('addr e', 'e.id=a.addr_id AND e.is_del=' . CommonModel::$del_normal)
+                ->where('a.is_del', CommonModel::$del_normal);
+
+            if ($data['masterOrderCode'] != '') $db->whereLike('a.masterOrderCode', '%' . $data['masterOrderCode'] . '%');
+            if ($data['orderCode'] != '') $db->whereLike('a.orderCode', '%' . $data['orderCode'] . '%');
+            if ($data['status'] != '') $db->where('a.status', $data['status']);
+            if ($data['name'] !== '') $db->whereLike('d.name', '%' . $data['name'] . '%');
+            if ($data['username']) $db->whereLike('d.username', '%' . $data['username'] . '%');
+            if (($data['start_date'] != '') && ($data['end_date'] != '')) $db->whereBetween('a.addtime', [$data['start_date'], $data['end_date']]);
+            if ($data['type'] !== '') $db->where('a.type', $data['type']);
+
+            $list = $db
+                ->field('a.id `订单ID`,a.masterOrderCode `主订单编号`,a.orderCode `订单编号`,a.type `订单类型`,b.title `业务公司`,c.title `卡类型`,d.name `购买用户名`,d.username `购买账号`,a.num `购买数量`,e.contactor `收货联系人`,e.mobile `收货联系电话`,a.status `状态`,a.post_name `物流公司`,a.post_code `物流单号`,a.addtime `创建时间`,e.addr_code,"" `收货地址`,e.addr,"" `商品编码`,"" `商品(服务)名称`,a.child_id')
+                ->order('a.addtime desc')
+                ->cursor();
+
+            $status = [
+                MasterOrderModel::$order_status_wait_pay => '待支付',
+                MasterOrderModel::$order_status_pay_fail => '支付失败',
+                MasterOrderModel::$order_status_not_deliver => '待发货',
+                MasterOrderModel::$order_status_deliver => '已发货',
+                MasterOrderModel::$order_status_receipt => '已收货',
+            ];
+            $type = [MasterOrderModel::$type_exchange_good => '兑换商品订单', MasterOrderModel::$type_shopping_good => '商城商品订单', MasterOrderModel::$type_service => '服务订单'];
+
+            foreach ($list as $v) {
+                $v = $v->toArray();
+
+                if ($v['订单类型'] == MasterOrderModel::$type_service) $v['商品(服务)名称'] = ServiceModel::where(['is_del' => CommonModel::$del_normal, 'id' => $v['child_id']])->value('title', '');
+                else {
+                    $good = GoodModel::where(['is_del' => CommonModel::$del_normal, 'id' => $v['child_id']])
+                        ->field('good_code,good_name')
+                        ->findOrEmpty()
+                        ->toArray();
+                    $v['商品编码'] = $good['good_code'] ?? '';
+                    $v['商品(服务)名称'] = $good['good_name'] ?? '';
+                };
+
+                $v['收货地址'] = $v['addr_code'] ? get_addr_name($v['addr_code']) . $v['addr'] : $v['addr'];
+                $v['状态'] = $status[$v['状态']] ?? '';
+                $v['订单类型'] = $type[$v['订单类型']] ?? '';
+
+                unset($v['addr_code']);
+                unset($v['addr']);
+                unset($v['child_id']);
+
+                $da[] = $v;
+            }
 
+            if (empty($da)) throw new Exception('没有可供导出的数据');
+        } catch (Exception $exception) {
+            $da[] = $exception->getMessage();
+        } catch (ValidateException $validateException) {
+            $da[] = $validateException->getError();
+        }
         $headerArr = array_keys($da[0]);
         excel_save('订单导出' . date('YmdHis'), $headerArr, $da);