微信小程序对接发货功能

server/2024/10/11 11:16:54/
注:微信小程序对接发货功能

文档地址:https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order-shipping/order-shipping.html

php代码

common.php

php">
use think\Config;
use think\Db;
use fast\Http;
use think\Cache;if(!function_exists('getAccessToken')){//获取tokenfunction 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;}
}

商家发货
在这里插入图片描述

php">/*** 店铺对订单发货** @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.statuslet req_extradata = options.referrerInfo.extraData.req_extradataif(t_status=="success"){}}
}    

http://www.ppmy.cn/server/46556.html

相关文章

FreeRTOS学习笔记【1】

本文章为本人学习FreeRTOS时的笔记,学习时使用 STM32 SPL库Keil开发环境。 之前发过这篇文章但不知为何在CSDN上MD格式无法显示,故重新发一次。(真不是水浏览量) 文章目录 操作系统启动步骤1.定义任务函数2.空闲任务与定时器任务堆栈函数实现3.定义任务…

Python函数式编程进阶:装饰器和闭包介绍

文章目录 Python函数式编程进阶:函数装饰器和闭包介绍一个简单的装饰器实现和行为表现装饰器通常会把函数替换成另一个函数Python导入模块时首先就会运行装饰器闭包__closure__属性可以查看闭包的自由变量总结 nonlocal声明 Python函数式编程进阶:函数装…

sql server 中的6种约束

一、约束定义 约束是用于定义和实施表的规则和限制,以确保数据的完整性和一致性。 即对一张表中的属性操作进行限制。 二、约束分类 通过定义约束,可以对数据库中的数据进行限制,以下是常见的约束: 1. 主键约束(Pr…

随笔(一)——项目代码优化

文章目录 前言一、if判断点对象赋值1.需求2.原本方法3.优化方法 二、数组的inclueles方法的使用1.需求2.原本方法3.优化方法 三、数组对象的按顺序渲染Object.entries0. Object.entries的基本使用1.需求2.原本方法3.优化方法4. 问题 前言 提示: 一、if判断点对象赋…

git远程仓库限额的解决方法——大文件瘦身

Git作为世界上最优秀的分布式版本控制工具,也是优秀的文件管理工具,它赋予了项目成员对项目进行远程协同开发能力,因此受到越来越多的行业从业人员的喜爱。很多优秀的项目管理平台,比如国内的Gitee,国外的Github&#…

slot 的用法

1.常规 slot是让一个组件预留插槽&#xff0c;用于扩展性设计。例如一个Window组件这么设计 <div class"window"><slot></slot></div> 那么使用者 <Window><span>xx</span></Window> 就等同于 <div class&…

IP代理池是什么?

从事跨境行业的朋友们总会有一个疑问&#xff0c;为什么自己所合作的IP代理商的IP在使用的过程中账号会有莫名封禁的问题&#xff0c;会不会是自己在使用的过程中错误的操作违反了平台的规则&#xff0c;其实不然有可能会是IP代理池纯净度不高的问题&#xff0c;有可能自己在使…

函数的创建和调用

自学python如何成为大佬(目录):https://blog.csdn.net/weixin_67859959/article/details/139049996?spm1001.2014.3001.5501 提到函数&#xff0c;大家会想到数学函数吧&#xff0c;函数是数学最重要的一个模块&#xff0c;贯穿整个数学学习过程。在Python中&#xff0c;函数…