微信小程序对接发货功能

注:微信小程序对接发货功能

文档地址:https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order-shipping/order-shipping.html

php代码

common.php

php 复制代码
use think\Config;
use think\Db;
use fast\Http;
use think\Cache;

if(!function_exists('getAccessToken')){
    //获取token
    function getAccessToken()
    {
        $site = Config::get("site");
        $appId = '';
        if(array_key_exists('WX_AppID',$site)){
            $appId = $site['WX_AppID'];
        }
        $appSecret = '';
        if(array_key_exists('WX_AppSecret',$site)){
            $appSecret = $site['WX_AppSecret'];
        }
        $cacheKey = $appId . '@access_token';
        if (!Cache::get($cacheKey)) {
            // 请求API获取 access_token
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
            $result = Http::get($url);
            $data = json_decode($result, true);
            // return $data['access_token'];
            // 写入缓存
            Cache::set($cacheKey, $data['access_token'], 5000);    // 7000
        }
        return Cache::get($cacheKey);
    }
}

if(!function_exists('getWxSendOrderStatus')){
    //获取发货订单信息
    function getWxSendOrderStatus($transaction_id)
    {
        $token = getAccessToken();
        $url = "https://api.weixin.qq.com/wxa/sec/order/get_order?access_token=" . $token;
        $data = [
            'transaction_id' => $transaction_id
        ];
        $data = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        $result = Http::post($url, $data);
        $result = json_decode($result, true);

        return $result;
    }
}

if(!function_exists('set_jump_path')){
    //设置微信发货后,消息跳转地址,不设置为默认
    function set_jump_path()
    {
        $token = getAccessToken();
        $url = "https://api.weixin.qq.com/wxa/sec/order/set_msg_jump_path?access_token=" . $token;
        $data = [
            'path' => 'page_zhanghushezhi/myOrder/myOrder?conmen=3', //待收货订单列表页面
        ];
        $data = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        Http::post($url, $data);
    }
}

if(!function_exists('sendDelivery')){
    //发货 物流15天自动确认,虚拟商品隔天自动确认
    function sendDelivery($order, $logistics_type=3)
    {
        set_jump_path();

        $token = getAccessToken();

        $express_name = "";
        $express_no = "";
        if ($logistics_type == 1) {
            $express_name = $order['express_name'];
            $express_no = $order['express_no'];
        }

        $data = [
            'order_key' => [
                'order_number_type' => 2,   //订单单号类型,用于确认需要上传详情的订单。枚举值1,使用下单商户号和商户侧单号;枚举值2,使用微信支付单号。
                'transaction_id' => $order['transaction_id']
            ],
            'logistics_type' => $logistics_type,//物流模式,发货方式枚举值:1、实体物流配送采用快递公司进行实体物流配送形式 2、同城配送 3、虚拟商品,虚拟商品,例如话费充值,点卡等,无实体配送形式 4、用户自提
            'delivery_mode' => 1,   //发货模式,发货模式枚举值:1、UNIFIED_DELIVERY(统一发货)2、SPLIT_DELIVERY(分拆发货) 示例值: UNIFIED_DELIVERY
            'shipping_list' => [
                [
                    'tracking_no' => $express_no,
                    'express_company' => $express_name,
                    'item_desc' => $order['item_desc'] ?? "订单发货信息"
                ]
            ],
            'upload_time' => date('Y-m-d\TH:i:sP', time()),
            'payer' => [
                'openid' => $order['openid']
            ]
        ];

        $urlss = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=" . $token;
        $data = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        $results = Http::post($urlss, $data);

        $results = json_decode($results, true);
        return $results;
    }
}

商家发货

php 复制代码
    /**
     * 店铺对订单发货
     *
     * @ApiMethod (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @param string $id  订单id
     */
    public function fahuo()
    {
        try {

            $user_id = $this->user_id;
            $shop_id = $this->shop_id;    // 商家id      
            $id = $this->request->param('id', '');
            if (!$id) {
                $this->error('订单id不能为空');
            }
            $LitestoreorderModel = new LitestoreorderModel();
            $order = $LitestoreorderModel->getOrderDetail($id, false);
            if ($LitestoreorderModel->hasError()) {
                $this->error($LitestoreorderModel->getError());
            }
            if ($order['shop_id'] != $shop_id) {
                $this->error('订单错误');
            }

            if ($order->fahuo($id) !== false) {
                if($order['paytype'] == 2){
                    // 获取微信发货订单信息
                    $wxorder = getWxSendOrderStatus($order['transaction_id']);
                    if($wxorder['errcode'] != 0){
                        $this->error('获取微信订单失败');
                    }
                    $order_state = $wxorder['order']['order_state']; //订单状态枚举:(1) 待发货;(2) 已发货;(3) 确认收货;(4) 交易完成;(5) 已退款。
                    if($order_state == 1){
                        $data = [
                            'transaction_id'=>$order['transaction_id'],
                            'openid'=>$this->openid,
                            'item_desc'=>'订单商品',
                        ];
                        $results = sendDelivery($data);
                        if ($results['errcode'] == 0) {
                            $this->success('发货成功!');
                        } else {
                            $this->error("发货失败:" . $results['errmsg']);
                        }
                    }
                }
                $this->success('发货成功');
            }
            $this->error($order->getError());
        } catch (Exception $e) {

            $this->error($e->getMessage());
        }
    }

小程序确认收货

js 复制代码
//点击确认收货按钮。
wx.openBusinessView({
  businessType: 'weappOrderConfirm',
  extraData: {
    merchant_id: merchant_id,
    merchant_trade_no: order_no,
    transaction_id: transaction_id
  },
  success() {
    
  },
  fail() {
    
  },
  complete() {
  }
});

首页app.js里的onShow

js 复制代码
onShow(options) {
    if(options.referrerInfo && options.referrerInfo.extraData && options.referrerInfo.extraData.req_extradata){
      let t_status = options.referrerInfo.extraData.status
      let req_extradata = options.referrerInfo.extraData.req_extradata
      if(t_status=="success"){
        
      }
    }
}    
相关推荐
icebreaker18 小时前
Weapp-vite:原生模式之外,多一种 Vue SFC 选择
前端·vue.js·微信小程序
icebreaker18 小时前
重走 Vue 长征路 Weapp-vite:编译链路与 Wevu 运行时原理拆解
前端·vue.js·微信小程序
BingoGo1 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack1 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端
BingoGo2 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php
JaguarJack2 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php·服务端
JaguarJack3 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端
BingoGo3 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
php
大米饭消灭者4 天前
Taro是怎么实现一码多端的【底层原理】
微信小程序·taro
JaguarJack4 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·php·laravel