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

 }

小程序

//点击确认收货按钮。
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"){
        
      }
    }
}    

你可能感兴趣的:(php,微信小程序,开发语言)