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

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

}
相关推荐
m0_694845579 小时前
Oh My Zsh 使用指南:Zsh 终端配置与插件管理教程
服务器·前端·小程序·开源·github
2501_9339072112 小时前
宁波本凡科技提供性价比高的智能解决方案
科技·微信小程序·小程序
一字白首14 小时前
进阶初学:微信小程序核心语法与配置实战DAY02
微信小程序·小程序
2601_9520137614 小时前
2024仿东郊微端家政预约上门小程序【亲测可用】家政管理系统抢单派单开源源码
小程序
CHU72903515 小时前
随时随地学新知——线上网课教学小程序前端功能详解
前端·小程序
职豚求职小程序16 小时前
京东26春招笔试题库宝藏小程序刷题yyds!
小程序
游戏开发爱好者816 小时前
如何使用Instruments和Keymob进行Swift应用性能优化分析
开发语言·ios·性能优化·小程序·uni-app·iphone·swift
2501_9160088916 小时前
移动应用上架到应用商店的完整指南:原理与详细步骤
android·ios·小程序·https·uni-app·iphone·webview
云起SAAS17 小时前
素材库微商品牌产品花店图文视频素材库抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·素材库微商品牌产品花店图文视频
源码ym7k资源17 小时前
家政服务小程序维修保洁美容美甲上门预约派单技师saas全开源app+h5+小程序-源码ym7K
小程序