Compare commits

..

2 Commits

Author SHA1 Message Date
tk
a736bc7b92 【类 型】:fix
【主	题】:根据回调里面 的description字段 设置发布“订单更新”主题
【描	述】:
	[原因]:此字段记录的商铺id 所以 飞机操作地面端 会根据自己账号所在商铺 订阅到自己商铺的提示更新主题
	[过程]:
	[影响]:
【结	束】

# 类型 包含:
# feat:新功能(feature)
# fix:修补bug
# docs:文档(documentation)
# style: 格式(不影响代码运行的变动)
# refactor:重构(即不是新增功能,也不是修改bug的代码变动)
# test:增加测试
# chore:构建过程或辅助工具的变动
2024-06-12 18:11:34 +08:00
tk
c60e68348f 【类 型】:feat
【主	题】:地面控制端增加发送mqtt功能函数 更改订单时向小程序端订阅的“更新订单主题” 发送信息
【描	述】:
	[原因]:小程序端可以实时刷新订单情况
	[过程]:发送小程序用户id对应的主题  订阅到订单有变更时 小程序端刷新订单
	[影响]:
【结	束】

# 类型 包含:
# feat:新功能(feature)
# fix:修补bug
# docs:文档(documentation)
# style: 格式(不影响代码运行的变动)
# refactor:重构(即不是新增功能,也不是修改bug的代码变动)
# test:增加测试
# chore:构建过程或辅助工具的变动
2024-06-12 17:59:16 +08:00
5 changed files with 50 additions and 8 deletions

View File

@ -61,7 +61,7 @@ class PayController extends PublicController
//设置获取签名的订单参数
$orderParameter = [
'out_trade_no' => $order['order_sn'],
'description' => $order['shop_id'], //商品名称
'description' => $order['shop_id'], //这个字段用 商铺id 方便分类查询腾讯的支付订单,还有在回调时候发送“订单更新主题”的子主题区分
'amount' => [
'total' => $order['total_price'] * 100, //单位:分
'currency' => 'CNY',
@ -85,7 +85,7 @@ class PayController extends PublicController
{
// 实例化 Yansongda Pay
$result = Pay::wechat($this->config)->callback();
// 地面终端 和 小程序端的提示刷新信息
$this->publish('refreshQuestList/2dc23dcfecc05fb1', 1);
try {
// 验证成功,处理业务逻辑
@ -97,6 +97,9 @@ class PayController extends PublicController
// 更新订单
$orderDb->where($where)->data($data)->save();
}
// 构建发布主题 并向地面终端提示刷新信息
$topic = 'refreshQuestList/' . $result['resource']['ciphertext']['description'];
$this->publish($topic, 1);
} catch (\Exception $e) {
// 捕获并记录可能的异常
error_log('支付回调处理错误:' . $e->getMessage());

View File

@ -9,7 +9,3 @@ function makeTopicPrefix($openid)
{
return substr(md5($openid . date("j")), 0, 8); //用户id+当天日 加密 截取前8位
}
function demo()
{
echo "hello";
}

View File

@ -8,7 +8,6 @@ class IndexController extends Controller
{
public function index()
{
demo();
$this->display();
}
}

View File

@ -5,6 +5,10 @@ namespace MpApi\Controller;
use Think\Image;
use Org\ImageController\ImageController;
use PhpMqtt\Client\MqttClient;
use PhpMqtt\Client\Exceptions\MqttClientException;
use PhpMqtt\Client\ConnectionSettings;
class PlaneController extends PublicController
{
public function index()
@ -571,7 +575,10 @@ class PlaneController extends PublicController
} elseif ($_REQUEST['state'] == 'back') {
$data['back'] = $_REQUEST['val']; //改变订单状态
}
if ($orderDb->where($where)->save($data)) {
if ($orderDb->where($where)->save($data)) { //修改数据
$topicPrefix = makeTopicPrefix($order['openid']); //小程序端用户订阅主题的前缀 ps:订单对应的用户的openid算出来的
// 提醒小程序端 刷新订单列表
$this->publish('refreshOrderList/' . $topicPrefix, 1);
echo json_encode(array('status' => 1, 'msg' => '订单修改成功'));
} else {
echo json_encode(array('status' => 0, 'msg' => '订单修改失败'));

View File

@ -6,6 +6,10 @@ use Think\Controller;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use PhpMqtt\Client\MqttClient;
use PhpMqtt\Client\Exceptions\MqttClientException;
use PhpMqtt\Client\ConnectionSettings;
class PublicController extends Controller
{
//****************
@ -148,4 +152,37 @@ class PublicController extends Controller
echo json_encode(array('status' => 0, 'msg' => '登录异常'));
}
}
/**
* @description: 连接mqtt服务器 并向指定主题发布信息
* @param string $topic mqtt主题
* @param string $msg 要发布的主题
*/
protected function publish($topic, $msg)
{
$server = C('mqtt')['server']; // MQTT 代理地址
$port = C('mqtt')['port']; // MQTT 代理端口
$clientId = C('mqtt')['client_id']; // 客户端 ID
$username = C('mqtt')['username']; // 用户名(如果需要)
$password = C('mqtt')['password']; // 密码(如果需要)
try {
// 创建连接设置对象
$settings = (new ConnectionSettings)->setUsername($username)->setPassword($password);
// 实例化 MQTT 客户端
$mqtt = new MqttClient($server, $port, $clientId);
// 连接到 MQTT 代理
$mqtt->connect($settings);
// 检查是否连接成功
if ($mqtt->isConnected()) {
// 发布消息到指定的主题 ps:主题 信息 qos
$mqtt->publish($topic, $msg, 0);
// 断开连接
$mqtt->disconnect();
} else {
echo "连接失败";
}
} catch (MqttClientException $e) { //连接失败
echo "Could not connect to MQTT broker: " . $e->getMessage();
}
}
}