微信小程序对接发货功能

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

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

php代码

common.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;
    }
}

商家发货
在这里插入图片描述


    /**
     * 店铺对订单发货
     *
     * @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());
        }
    }

小程序确认收货

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

首页app.js里的onShow

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"){
        
      }
    }
}    

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot 微信小程序对接可以通过以下步骤完成: 1.注册微信公众平台账号并创建小程序,获取小程序的AppID和AppSecret。 2.创建一个Spring Boot项目,添加Spring Boot对WeChat SDK的依赖。 3.在项目中配置小程序的AppID和AppSecret,可以通过在配置文件中设置或者在代码中进行配置。 4.实现微信小程序的登录,可以通过WeChat SDK提供的接口获取用户的OpenID和SessionKey。 5.实现微信小程序的支付功能,可以通过WeChat SDK提供的接口实现支付功能。 6.实现微信小程序的模板消息推送,可以通过WeChat SDK提供的接口实现模板消息的发送功能。 7.实现微信小程序的二维码生成,可以通过WeChat SDK提供的接口生成二维码图片。 8.实现微信小程序的数据统计,可以通过WeChat SDK提供的接口获取小程序的访问数据和用户数据。 以上是基本的对接流程,具体实现还需要根据项目需求进行调整和扩展。 ### 回答2: Spring Boot对接微信小程序的方式主要有以下几个步骤: 1. 获取微信小程序的AppID和AppSecret,可以在微信公众平台上申请得到。 2. 在Spring Boot项目中添加依赖,例如引入Spring Boot的Web模块和微信的SDK等。可以使用Maven或Gradle进行依赖管理。 3. 创建微信小程序登录的接口,用于接收小程序的登录请求,获取小程序的code和encryptedData等参数。 4. 使用微信提供的SDK,使用AppID和AppSecret等参数调用微信的接口,获取访问令牌access_token和用户唯一标识openid等信息。 5. 返回访问令牌和用户信息给小程序。 6. 小程序接收到访问令牌和用户信息后,可以保存用户信息或进行其他业务逻辑处理。 7. 针对其他小程序的业务需求,可以继续对接微信提供的其他接口,如支付、模板消息等。 8. 在开发过程中,可以使用微信提供的工具进行API测试和调试,确保接口的正确性。 总结起来,对接微信小程序需要获取AppID和AppSecret,使用Spring Boot创建接口,调用微信接口获取访问令牌和用户信息,并进行相应的业务处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值