禾匠旧版对接微信小程序发货系统(发货信息管理 接口)

最近小程序如果是商家交易需要再小程序后台点击一下发货,特别麻烦,但是旧版的禾匠又没有这个功能,所以只能手动增加这个功能,但是每一个版本发货逻辑都不一样,大家只能自己手动去兼容一下,下面只是写了一个简单商家配送的发货的方式,暂时其他没有加上(tpwe8888)

复制代码
<?php

namespace app\forms\common\order;

use app\core\response\ApiCode;
use app\forms\common\order\send\CitySendForm;
use app\forms\common\order\send\ExpressSendForm;
use app\forms\common\order\send\NoExpressSendForm;
use app\forms\common\order\send\OtherCitySendForm;
use app\models\Model;
use app\models\Order;
use app\models\PaymentOrder;
use app\models\PayType;
use app\models\UserInfo;
use app\models\PaymentOrderUnion;

class OrderSendForm extends Model
{
    public $order_id;
    public $is_express;
    public $accessToken;
    public function rules()
    {
        return [
            [['order_id', 'is_express'], 'required'],
            [['order_id', 'is_express'], 'integer'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'order_id' => '订单ID',
            'is_express' => '发货方式',
        ];
    }

    //发货
    public function save()
    {
        if (!$this->validate()) {
            return $this->getErrorResponse();
        }

        try {
            $order = Order::findOne([
                'id' => $this->order_id,
                'is_delete' => 0,
                'mall_id' => \Yii::$app->mall->id,
            ]);

            if (!$order) {
                throw new \Exception('订单不存在');
            }
            
           
            if ($order->send_type == 0 || $order->send_type == 1) {
                // 快递配送
                switch ($this->is_express) {
                    // 快递配送
                    case 1:
                        $form = new ExpressSendForm();
                        break;
                    // 自定义物流
                    case 2:
                        $form = new NoExpressSendForm();
                        break;
                    default:
                        throw new \Exception('发货方式异常');
                }
            } elseif ($order->send_type == 2) {
                // 同城配送
               
                switch ($this->is_express) {
                    // 第三方配送
                    case 1:
                        $form = new OtherCitySendForm();
                        break;
                    // 商家配送
                    case 2:
                        $this->sendWechatOrder($order);
                        $form = new CitySendForm();
                        break;
                    default:
                        throw new \Exception('发货方式异常');
                }
            } else {
                throw new \Exception('订单数据异常');
            }

            $form->attributes = \Yii::$app->request->post();
            return $form->send();
        } catch (\Exception $exception) {
            return [
                'code' => ApiCode::CODE_ERROR,
                'msg' => $exception->getMessage(),
                'error' => [
                    'line' => $exception->getLine(),
                ],
            ];
        }
    }
    public function sendWechatOrder($order){
        $this->accessToken = \Yii::$app->getWechat()->getAccessToken();
        if (!$this->accessToken) {
            throw new \Exception('微信配置有误');
        }
        $paymentorder = PaymentOrder::findOne([
                'order_no' => $order->order_no,
            ]);
        $payset = PayType::findOne([
            'mall_id' => \Yii::$app->mall->id,
        ]);
        $mchid = $payset->mchid;
        $payment_order_union = PaymentOrderUnion::findOne([
            'id'=>$paymentorder->payment_order_union_id,
            ]);
        $out_trade_no = $payment_order_union->order_no;
        $title = $payment_order_union->title;
        $user = UserInfo::findOne(['user_id'=>$order->user_id]);
        $openid = $user->platform_user_id;
        $api = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token={$this->accessToken}";
        $upload_time = date('Y-m-d\TH:i:s.vP');
        $arrayData = [
            "order_key" => [
                "order_number_type" => 1,
                "mchid" => $mchid,
                'out_trade_no'=>$out_trade_no
            ],
            "delivery_mode" => 1,
            "logistics_type" => 2,
            "shipping_list" => [
                [
                    "item_desc" => $title,
                    "contact" => [
                        "receiver_contact" => substr_replace($order->mobile, '****', 3, 4)
                    ]
                ]
            ],
            "upload_time" => $upload_time,
            "payer" => [
                "openid" => $openid
            ]
          ];
    
        $res = $this->httpPost($api, json_encode($arrayData,JSON_UNESCAPED_UNICODE));
    }
    public function httpPost($url,$data){
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS,  $data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, false); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
        $result = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            return 'Error POST'.curl_error($curl);
        }
        curl_close($curl); // 关键CURL会话
        return $result; // 返回数据
    }

}
相关推荐
小羊Yveesss4 小时前
2026年小程序商城的现状和发展趋势
小程序
智慧景区与市集主理人12 小时前
五一市集分账混乱?巨有科技智慧市集小程序实现统一收款、自动分账
大数据·科技·小程序
程序鉴定师15 小时前
深圳小程序制作哪家好?2026深度市场分析与选择指南?
大数据·小程序
河北清兮网络科技15 小时前
广告联盟全解析:从开发接入到运营优化,多视角拆解流量变现逻辑
小程序·app
张晓℡¹⁸⁰³⁷¹⁸²⁵⁵⁸16 小时前
海外盲盒APP玩法集合,海外盲盒多国语言
小程序·php
互联科技报16 小时前
订货商城选择哪个系统操作更简单?2026小白友好型选型指南
小程序
2501_9159214317 小时前
HTTPS前端劫持 新一代流量劫持解决方案
前端·网络协议·ios·小程序·https·uni-app·iphone
智塑未来17 小时前
2026企业数字化观察:小程序定制需求激增,如何避开选型坑?
小程序
肖有米XTKF864618 小时前
二二复制裂变小程序系统制度(双轨制公排模式)
人工智能·小程序·软件工程·团队开发
2601_9579665318 小时前
交友互动式盲盒小程序核心注意事项解析
小程序·小程序开发·盲盒小程序·盲盒系统·盲盒开发