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"){
        
      }
    }
}    
相关推荐
Jam-Young6 分钟前
Python中的面向对象编程,类,对象,封装,继承,多态
开发语言·python
myloveasuka6 分钟前
类与对象(1)
开发语言·c++
江梦寻16 分钟前
思科模拟器路由器配置实验
开发语言·网络·网络协议·学习·计算机网络
代码小鑫17 分钟前
A034-基于Spring Boot的供应商管理系统的设计与实现
java·开发语言·spring boot·后端·spring·毕业设计
奋飞安全31 分钟前
初试js反混淆
开发语言·javascript·ecmascript
guoruijun_2012_431 分钟前
fastadmin多个表crud连表操作步骤
android·java·开发语言
浪里个浪的102434 分钟前
【C语言】计算3x3矩阵每行的最大值并存入第四列
c语言·开发语言·矩阵
@东辰41 分钟前
【golang-技巧】-自定义k8s-operator-by kubebuilder
开发语言·golang·kubernetes
乐悠小码1 小时前
数据结构------队列(Java语言描述)
java·开发语言·数据结构·链表·队列
史努比.1 小时前
Pod控制器
java·开发语言