不会写代码的程序猿-蜀

不会写代码的程序猿-蜀

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

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

<?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; // 返回数据
    }

}
10-26 16:36