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

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

}
相关推荐
2401_845937535 小时前
PHP一键约课高效健身智能健身管理系统小程序源码
微信·微信小程序·小程序·微信公众平台·微信开放平台
程序员入门进阶7 小时前
基于微信小程序的科创微应用平台设计与实现+ssm(lw+演示+源码+运行)
微信小程序·小程序
DreamByte16 小时前
Python Tkinter小程序
开发语言·python·小程序
说私域17 小时前
开源 AI 智能名片小程序:开启内容营销新境界
人工智能·小程序
汇匠源17 小时前
零工市场小程序:保障灵活就业
java·小程序·零工市场
哈尔滨财富通科技17 小时前
家居小程序有什么用?
小程序
程序员阿龙18 小时前
【2025】基于微信小程序的网上点餐系统设计与实现、基于微信小程序的智能网上点餐系统、微信小程序点餐系统设计、智能点餐系统开发、微信小程序网上点餐平台设计
微信小程序·小程序·毕业设计·订单管理·在线点餐·订单跟踪·在线支付
张人玉1 天前
微信小程序开发——比较两个数字大小
微信小程序·小程序
计算机学姐2 天前
基于微信小程序的食堂点餐预约管理系统
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis
罗_三金2 天前
微信小程序读写NFC标签(实现NFC标签快速拉起小程序)实战
前端·javascript·微信小程序·小程序