PHP 微信小程序发货管理

PHP

php 复制代码
//获取token
public function getAccessToken($appId,$appSecret)
{
	$cacheKey = $this->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 = $this->get($url);
		$data = json_decode($result, true);
		// 写入缓存
		Cache::set($cacheKey, $data['access_token'], 5000);    // 7000
	}
	return Cache::get($cacheKey);
}
//获取发货订单信息
public function getWxSendOrderStatus($transaction_id)
{

    $token = $this->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 = curlPost($url, $data);

    $result = json_decode($result, true);

    return $result;

}

/**
返回数据 order_state 订单状态枚举:(1) 待发货;(2) 已发货;(3) 确认收货;(4) 交易完成;(5) 已退款。
array (
  'errcode' => 0,
  'errmsg' => 'ok',
  'order' => 
  array (
    'transaction_id' => '',
    'merchant_trade_no' => '',
    'description' => '订单发货信息',
    'paid_amount' => 900,
    'openid' => '',
    'trade_create_time' => 1704856177,
    'pay_time' => 1704856183,
    'order_state' => 4,
    'shipping' => 
    array (
      'delivery_mode' => 1,
      'shipping_list' => 
      array (
        0 => 
        array (
          'tracking_no' => '657',
          'express_company' => '德邦快递',
          'upload_time' => 1704856224,
          'goods_desc' => '订单发货信息',
        ),
      ),
      'logistics_type' => 1,
      'finish_shipping' => true,
      'upload_scene' => 2,
      'finish_shipping_count' => 1,
    ),
    'in_complaint' => false,
    'merchant_id' => '',
    'sub_merchant_id' => '0',
  ),
)
**/

//设置微信发货后,消息跳转地址,不设置为默认
public function set_jump_path()
{

    $token = $this->getAccessToken();

    $url = "https://api.weixin.qq.com/wxa/sec/order/set_msg_jump_path?access_token=" . $token;

    $data = [
        'path' => 'pages/order/index'
    ];
    $data = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

    curlPost($url, $data);

}
//发货 物流15天自动确认,虚拟商品隔天自动确认
public function sendDelivery($order)
{

     $this->set_jump_path();

     $token = $this->getAccessToken();
     $express_name = "";
     $express_no = "";
     if ($order['logistics_type'] == 1) {
         $express_name = $order['express_name'];
         $express_no = $order['express_no'];
     }

     $data = [
         'order_key' => [
             'order_number_type' => 2,
             'transaction_id' => $order['transaction_id']
         ],
         'logistics_type' => $order['logistics_type'],//物流模式,发货方式枚举值:1、实体物流配送采用快递公司进行实体物流配送形式 2、同城配送 3、虚拟商品,虚拟商品,例如话费充值,点卡等,无实体配送形式 4、用户自提
         'delivery_mode' => 1,
         '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 = curlPost($urlss, $data);

     $results = json_decode($results, true);
     if ($results['errcode'] == 0) {
         return true;
     } else {
         \think\Log::error("发货失败:" . $results['errmsg']);
         return false;
     }

 }

小程序

javascript 复制代码
//点击确认收货按钮。
wx.openBusinessView({
  businessType: 'weappOrderConfirm',
  extraData: {
    merchant_id: merchant_id,
    merchant_trade_no: order_no,
    transaction_id: transaction_id
  },
  success() {
    
  },
  fail() {
    
  },
  complete() {
  }
});
javascript 复制代码
首页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"){
        
      }
    }
}    
相关推荐
JaguarJack3 小时前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端
BingoGo4 小时前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
php
大米饭消灭者19 小时前
Taro是怎么实现一码多端的【底层原理】
微信小程序·taro
JaguarJack1 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·php·laravel
郑州光合科技余经理2 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1232 天前
matlab画图工具
开发语言·matlab
dustcell.2 天前
haproxy七层代理
java·开发语言·前端
norlan_jame2 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone2 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054962 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django