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"){
        
      }
    }
}    
相关推荐
moringlightyn21 分钟前
c++11可变模版参数 emplace接口 新的类功能 lambda 包装器
开发语言·c++·笔记·其他·c++11·lambda·包装器
Laplaces Demon22 分钟前
Spring 源码学习(十四)—— HandlerMethodArgumentResolver
java·开发语言·学习
郝学胜-神的一滴26 分钟前
使用Linux系统函数递归遍历指定目录
linux·运维·服务器·开发语言·c++·软件工程
guygg8826 分钟前
Java 无锁方式实现高性能线程
java·开发语言
盛夏绽放33 分钟前
关于 uni-app 与原生微信小程序中的生命周期 —— 一次“生命旅程”的解读
微信小程序·小程序·uni-app
青衫码上行1 小时前
【从0开始学习Java | 第22篇】反射
java·开发语言·学习
一念&1 小时前
每日一个C语言知识:C 字符串
c语言·开发语言
流水线上的指令侠1 小时前
使用C#写微信小程序后端——电商微信小程序
微信小程序·小程序·c#·visual studio
知识分享小能手1 小时前
uni-app 入门学习教程,从入门到精通,uni-app 基础知识详解 (2)
前端·javascript·windows·学习·微信小程序·小程序·uni-app
0110_10241 小时前
tauri + rust的环境搭建---初始化以及构建
开发语言·后端·rust