Zory 6 days ago
parent
commit
80af03fc1e

+ 8 - 14
app/controller/mini/Confirm.php

@@ -34,7 +34,7 @@ class Confirm extends Base
                 "order.require" => "请求参数错误"
             ],'post');
             if (!is_array($param)) return error($param);
-            $order = (new SaasOrderLife)->where("order_id",$param['order'])->where("open_id",$request->user['open_id'])->with(['goods' => function($query){
+            $order = (new SaasOrderLife)->where("certificate_id",$param['order'])->where("open_id",$request->user['open_id'])->with(['goods' => function($query){
                 $query->field("out_id,product_img");
             },'store' => function($query){
                 $query->field("store_id,service_mobile");
@@ -68,14 +68,14 @@ class Confirm extends Base
     {
         try {
             $param = $request->post();
-            $orderLife = (new SaasOrderLife)->where("order_id",$param['order'])->with(['store','agent'])->findOrEmpty();
+            $orderLife = (new SaasOrderLife)->where("certificate_id",$param['order'])->with(['store','agent'])->findOrEmpty();
             if ($orderLife->isEmpty()) return errorTrans("empty.data");
-            $order = (new SaasOrder)->where("order_id",$param['order'])->where("status","in",[1,2,4])->findOrEmpty();
+            $order = (new SaasOrder)->where("certificate_id",$param['order'])->where("status","in",[1,2,4])->findOrEmpty();
             if (!$order->isEmpty()) return error("请勿重复提交");
 //            $param['service_type'] = $orderLife['store']['service_type'];
             $address = !empty($param['address']) ? json_decode($param['address'],true): [];
             $param['order_sn'] = CodeExtend::random(18,1,date("md"));
-            $param['order_id'] = $param['order'];
+            $param['order_id'] = $orderLife['order_id'];
             $param['open_id'] = $request->user['open_id'];
             $param['uuid'] = $request->user['id'];
             $param['store_id'] = $orderLife['store_id'];
@@ -88,18 +88,10 @@ class Confirm extends Base
             $param['mobile'] = $address['mobile']??'';
             $param['region'] = $address['region']??'';
             $param['address'] = $address['address']??'';
-            if (empty($orderLife['certificate_id'])) {
-                $orderDetail = (new DouyinOrder)->config($this->getDyConfig())->token()->getOrderDetail([$param['order']]);
-                echo getDateFull()."===获取券码返回\n";
-                print_r($orderDetail);
-                if (!isset($orderDetail['data']['certificate_info_list'][0]['certificate_id'])) return error("订单数据异常!");
-                $param['certificate_id'] = $orderDetail['data']['certificate_info_list'][0]['certificate_id'];
-            } else {
-                $param['certificate_id'] = $orderLife['certificate_id'];
-            }
+            $param['certificate_id'] = $param['order']??'';
             $param['lock_key'] = strtoupper(CodeExtend::random(18,3));
             // 锁码
-            $resp = (new DouyinOrder)->config($this->getDyConfig())->token()->orderLock(2,$param['order'],$param['certificate_id'],$param['lock_key']);
+            $resp = (new DouyinOrder)->config($this->getDyConfig())->token()->orderLock(2,$orderLife['order_id'],$param['order'],$param['lock_key']);
             echo getDateFull()."===锁码返回\n";
             print_r($resp);
             if ($resp['err_no'] <> 0) return error("订单数据异常");
@@ -114,6 +106,8 @@ class Confirm extends Base
             if (!$state) return error("处理失败");
             return success("提交成功");
         }  catch (\Throwable $throwable) {
+            echo $throwable->getFile()."\n";
+            echo $throwable->getLine()."\n";
             return error($throwable->getMessage());
         }
     }

+ 8 - 7
app/controller/mini/Order.php

@@ -19,7 +19,6 @@ use LinFly\Annotation\Route\Route;
 use Shopwwi\WebmanAuth\Auth as AuthMode;
 use support\Request;
 use support\Response;
-use function DI\string;
 
 
 #[Controller(prefix: "/api/dy/order"),Middleware(DyMiddleware::class)]
@@ -37,7 +36,7 @@ class Order extends Base
                 "order.require"     => "请求参数错误",
             ]);
             if (!is_array($param)) return error($param);
-            $order = (new SaasOrderLife)->where("order_id",$param['order'])->findOrEmpty();
+            $order = (new SaasOrderLife)->where("certificate_id",$param['order'])->findOrEmpty();
             if ($order->isEmpty()) return error("订单数据已失效");
             return success("ok",$order->toArray());
         } catch (\Throwable $throwable) {
@@ -72,17 +71,19 @@ class Order extends Base
             }
             // 同步已经下单的订单
             if (!empty($param['account_id'])) {
-                $this->asyncDyOrder($param['account_id'],$openId);
+                $this->asyncDyOrder($param['account_id'],$openId,$param['order_id']);
             }
             $order = (new SaasOrderLife)->where("open_id",$openId)->where('order_id',$param['order_id'])->with(['goods' => function($query){
                 $query->field("out_id,product_img");
             },'store' => function($query){
                 $query->field("store_id,service_mobile");
-            }])->append(['expire_at'])->withAttr(['expire_at' => function($data,$resp){
+            }])->append(['expire_at','order'])->withAttr(['expire_at' => function($data,$resp){
                 return date("Y-m-d",strtotime($resp['expire_at']));
-            }])->findOrEmpty();
+            },'order' => function($data,$resp){
+                return (new SaasOrder)->where("certificate_id",$resp['certificate_id'])->where("express_status","in",[0,1,2,4])->field("order_sn,order_id")->findOrEmpty();
+            }])->select();
             if ($order->isEmpty()) return error("订单尚未同步");
-            $order['order'] = (new SaasOrder)->where("order_id",$param['order_id'])->where("express_status","in",[0,1,2,4])->field("order_sn,order_id")->findOrEmpty();
+//            $order['order'] = (new SaasOrder)->where("order_id",$param['order_id'])->where("express_status","in",[0,1,2,4])->field("order_sn,order_id")->findOrEmpty();
             return successTrans("success.data",[
                 "user"      => get_object_vars((new AuthMode)->guard("member")->login($userIn)),
                 "order"     => $order
@@ -108,7 +109,7 @@ class Order extends Base
                 "keyword.default"   => ""
             ],'post');
             if (!is_array($param)) return error($param);
-            $order = (new SaasOrderLife)->where("order_id",$param['order'])->where("open_id",$request->user['open_id'])->findOrEmpty();
+            $order = (new SaasOrderLife)->where("certificate_id",$param['order'])->where("open_id",$request->user['open_id'])->findOrEmpty();
             if ($order->isEmpty()) return error("订单尚未同步");
             $location = explode(",",$param['location']);
             $where = "store_id={$order['store_id']} and agent_id={$order['agent_id']}";

+ 13 - 0
app/controller/mini/Test.php

@@ -9,6 +9,7 @@ use LinFly\Annotation\Route\Controller;
 use LinFly\Annotation\Route\Route;
 use support\Request;
 use support\Response;
+use function Symfony\Component\String\s;
 
 
 #[Controller(prefix: "/test")]
@@ -42,4 +43,16 @@ class Test extends Base
         }
     }
 
+
+    #[Route(path: "token",methods: "get")]
+    public function getToken(): Response
+    {
+        try {
+            $token = (new \app\extra\douyin\Base)->config($this->getDyConfig())->getAccessToken();
+            return success("ok",['token'=>$token]);
+        } catch (\Throwable $throwable) {
+            return error($throwable->getMessage());
+        }
+    }
+
 }

+ 46 - 44
app/extra/basic/Base.php

@@ -42,58 +42,60 @@ class Base
      * 同步订单-实时
      * @param string $account
      * @param string $openId
-     * @return void
      */
-    protected function asyncDyOrder(string $account = "",string $openId = "")
+    protected function asyncDyOrder(string $account = "",string $openId = "",string $orderId = "")
     {
         // 同步已经下单的订单
         if (!empty($account)) {
+            if (!empty($orderId)) {
+                $order = (new SaasOrderLife)->where("order_id",$orderId)->findOrEmpty();
+                if (!$order->isEmpty()) return [];
+            }
             $resp = (new Client)->config($this->getDyConfig())->token()->queryOrder($account,$openId);
             $store = (new SaasStore)->where("store_id",$account)->findOrEmpty();
-            if (!empty($resp['data']['orders'])) {
-                $orderData = $orderLog = [];
-                foreach ($resp['data']['orders'] as $key=>$val) {
-                    if ($val['can_use']) {
-                        $orderEx = (new SaasOrderLife)->where("order_id",$val['order_id'])->findOrEmpty();
-                        $certificates = $val['certificates'][0];
-                        if ($orderEx->isEmpty()) {
-                            $orderData[$key] = [
-                                "open_id"   => $openId,
-                                "agent_id"  => $store['agent_id']??'',
-                                "store_id"  => $store['store_id']??'',
-                                "order_id"  => $val['order_id'],
-                                "pay_amount"    => $certificates['amount']['pay_amount'],
-                                "order_amount"  => $certificates['amount']['original_amount'],
-                                "expire_at"     => date("Y-m-d H:i:s",$certificates['expire_time']),
-                                "out_id"        => $certificates['sku_info']['sku_id'],
-                                "product_name"  => $certificates['sku_info']['title'],
-                                "groupon_type"  => $certificates['sku_info']['groupon_type'],
-                                "certificate_id"  => $certificates['certificate_id']??'',
-                                "start_time"    => date("Y-m-d H:i:s",$certificates['start_time']),
-                            ];
-                            $orderLog[$key] = [
-                                "order_id"  => $val['order_id'],
-                                "title"     => "发起订单",
-                                "remark"    => "下单并完成支付"
-                            ];
-                        } else {
-                            $orderEx->save([
-                                "open_id"       => $openId,
-                                "expire_at"     => date("Y-m-d H:i:s",$certificates['expire_time']),
-                                "out_id"        => $certificates['sku_info']['sku_id'],
-                                "product_name"  => $certificates['sku_info']['title'],
-                                "groupon_type"  => $certificates['sku_info']['groupon_type'],
-                                "certificate_id"  => $certificates['certificate_id']??'',
-                                "start_time"    => date("Y-m-d H:i:s",$certificates['start_time']),
-                            ]);
-                        }
-                    }
-                }
-                if (!empty($orderData)) {
-                    (new SaasOrderLife)->insertAll(array_values($orderData));
-                    (new SaasOrderLog)->insertAll(array_values($orderLog));
+            if (empty($resp['data']['orders'])) return [];
+            $lifeOrder = $resp['data']['orders'][0];
+            $orderData = $orderLog = [];
+            foreach ($lifeOrder['certificates'] as $key=>$val) {
+                $orderEx = (new SaasOrderLife)->where("order_id",$lifeOrder['order_id'])->where("certificate_id",$val['certificate_id'])->findOrEmpty();
+                if ($orderEx->isEmpty()) {
+                    $orderData[$key] = [
+                        "open_id"   => $openId,
+                        "agent_id"  => $store['agent_id']??'',
+                        "store_id"  => $store['store_id']??'',
+                        "order_id"  => $lifeOrder['order_id'],
+                        "pay_amount"    => $val['amount']['pay_amount'],
+                        "order_amount"  => $val['amount']['original_amount'],
+                        "expire_at"     => date("Y-m-d H:i:s",$val['expire_time']),
+                        "out_id"        => $val['sku_info']['sku_id'],
+                        "product_name"  => $val['sku_info']['title'],
+                        "groupon_type"  => $val['sku_info']['groupon_type'],
+                        "certificate_id"  => $val['certificate_id']??'',
+                        "status"        => 1,
+                        "start_time"    => date("Y-m-d H:i:s",$val['start_time']),
+                    ];
+                    $orderLog[$key] = [
+                        "order_id"  => $val['certificate_id'],
+                        "title"     => "发起订单",
+                        "remark"    => "下单并完成支付"
+                    ];
+                } else {
+                    $orderEx->save([
+                        "open_id"       => $openId,
+                        "expire_at"     => date("Y-m-d H:i:s",$val['expire_time']),
+                        "out_id"        => $val['sku_info']['sku_id'],
+                        "product_name"  => $val['sku_info']['title'],
+                        "groupon_type"  => $val['sku_info']['groupon_type'],
+                        "certificate_id"  => $val['certificate_id']??'',
+                        "start_time"    => date("Y-m-d H:i:s",$val['start_time']),
+                    ]);
                 }
             }
+            if (!empty($orderData)) {
+                (new SaasOrderLife)->insertAll(array_values($orderData));
+                (new SaasOrderLog)->insertAll(array_values($orderLog));
+            }
+            return [];
         }
     }
 

+ 3 - 0
app/extra/douyin/Base.php

@@ -3,6 +3,7 @@
 namespace app\extra\douyin;
 
 use support\think\Cache;
+use yzh52521\EasyHttp\Http;
 
 class Base
 {
@@ -59,6 +60,8 @@ class Base
 
     public function getAccessToken()
     {
+//        $resp = Http::asJson()->get("https://miniapi.jsshuita.com.cn/test/token")->array();
+//        return $resp['data']['token'];
         $accessToken = Cache::get($this->getTokenPrefix());
         if (!empty($accessToken)) return $accessToken;
         $result = (new AccessToken)->config($this->config)->getAccessTokenLine();