注:微信小程序对接发货功能
文档地址: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"){
}
}
}