Browse Source

fix:token

super-yimizi 1 month ago
parent
commit
70a6e7280d

+ 27 - 5
application/api/controller/Order.php

@@ -16,7 +16,8 @@ use app\common\Service\ParentOrderService;
 use app\common\Enum\OrderEnum;
 use app\common\Service\CartService;
 use app\common\Service\Pay\PayOperService;
-
+use app\common\library\easywechatPlus\WechatMiniProgramShop;
+use app\common\facade\Wechat;
 /**
  * 订单接口
  */
@@ -466,14 +467,35 @@ class Order extends Base
         $this->success('获取成功', $info);
     }
 
+    /**
+     * 
+     * @return void
+     */
     public  function getWechatMiniProgramOrderDelivery(){
           $orderId  = $this->request->post('order_id');
           if(empty($orderId )){
             $this->error('请上传正确的参数');
           }
-          $arrReturn = [];
-          $arrReturn['errcode'] = 0;
-          $arrReturn['order_state'] = 3;
-          $this->success('获取成功', $arrReturn);
+          //查询 订单信息
+          $order = OrderService::getByOrderId($orderId);
+          if (empty($order)) {
+              $this->error('订单不存在');
+          }
+          // 查询支付信息
+          $payInfo = PayOperService::getPayInfoByOrderId($orderId, 1);
+          if (empty($payInfo)) {
+              $this->error('支付信息不存在或未找到微信支付订单号');
+          }
+          $wechatService  = new WechatMiniProgramShop(Wechat::miniProgram());
+          $result = $wechatService->getOrderShippingStatus($payInfo->transaction_id);
+          
+          // 根据微信接口返回数据重新组织
+          $response = [
+              'errcode' => $result['errcode'] ?? 0,
+              'errmsg' => $result['errmsg'] ?? 'ok',
+              'order_state' => $result['order']['order_state'] ?? 0
+          ];
+          
+          $this->success('获取成功', $response);
     }
 }

+ 0 - 1
application/common/Service/Pay/PayOperService.php

@@ -445,7 +445,6 @@ class PayOperService
         $pays = PayModel::where('order_type', $orderType)
             ->field('id,pay_type,pay_fee,real_fee,transaction_id,paid_time')
             ->where('order_id', $orderId)
-            ->order('id', 'desc')
             ->find();
         
         return $pays;

+ 3 - 3
application/common/facade/Wechat.php

@@ -44,9 +44,9 @@ class Wechat extends Base
         $defaultConfig = self::defaultConfig();
         $officialAccount = shop_config('wechat.wechat_official_account', false);
         $config = array_merge($defaultConfig, [
-            'app_id'  => $officialAccount['app_id'],
-            'secret'  => $officialAccount['secret'],
-            'token'  => $officialAccount['token'],
+            'app_id'   => $officialAccount['app_id'],
+            'secret'   => $officialAccount['secret'],
+            'token'    => $officialAccount['token'],
             'aes_key'  => $officialAccount['aes_key'],
         ]);
         $app = new \EasyWeChat\OfficialAccount\Application($config);

+ 65 - 1
application/common/library/easywechatPlus/WechatMiniProgramShop.php

@@ -2,7 +2,7 @@
 
 namespace app\common\library\easywechatPlus;
 use app\common\Service\Order\ShippingInfo\OrderShippingInfo;
-use app\commom\model\data\WechatExpress;
+use app\common\model\data\WechatExpress;
 use app\common\exception\BusinessException;
 use app\common\facade\HttpClient;
 use think\Cache;
@@ -262,6 +262,70 @@ class WechatMiniProgramShop extends EasywechatPlus
         return $result;
     }
 
+      /**
+     * 获取微信delivery数据
+     *
+     * @param boolean $is_force
+     * @return void
+     */
+    public function getOrderStatus()
+    {
+    
+        $access_token = $this->getAccessToken();
+        $get_delivery_url = "https://api.weixin.qq.com/wxa/sec/order/get_order";
+
+        $result = HttpClient::request('post', $get_delivery_url, [
+            'body' => '{}',
+            'query' => ["access_token" => $access_token['access_token']],
+            'headers' => ['Content-Type' => 'application/json']
+        ]);
+
+        $result = $result->getBody()->getContents();
+        $result = json_decode($result, true);
+
+        if ($result['errcode'] != 0) {
+            throw new BusinessException('获取微信 delivery 列表失败: errcode:' . $result['errcode'] . '; errmsg:' . $result['errmsg']);
+        }
+
+
+        return $result;
+    }
+
+    /**
+     * 查询订单发货状态
+     * 根据微信文档:https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order-shipping/order-shipping.html#%E4%B8%89%E3%80%81%E6%9F%A5%E8%AF%A2%E8%AE%A2%E5%8D%95%E5%8F%91%E8%B4%A7%E7%8A%B6%E6%80%81
+     *
+     * @param string $transactionId 微信支付订单号
+     * @return array 查询结果
+     * @throws BusinessException
+     */
+    public function getOrderShippingStatus($transactionId = "")
+    {
+        $access_token = $this->getAccessToken();
+        $get_order_url = "https://api.weixin.qq.com/wxa/sec/order/get_order";
+
+        $requestData = [
+            'transaction_id' => $transactionId
+        ];
+
+        $result = HttpClient::request('post', $get_order_url, [
+            'body' => json_encode($requestData),
+            'query' => ["access_token" => $access_token['access_token']],
+            'headers' => ['Content-Type' => 'application/json']
+        ]);
+
+        $result = $result->getBody()->getContents();
+        $result = json_decode($result, true);
+
+        if ($result['errcode'] != 0) {
+            throw new BusinessException('查询订单发货状态失败: errcode:' . $result['errcode'] . '; errmsg:' . $result['errmsg']);
+        }
+
+        return $result;
+    }
+
+  
+
 
     /**
      * 方法转发到 easywechat