no commit message
This commit is contained in:
@@ -138,6 +138,9 @@ class Bussiness extends Base
|
||||
|
||||
$res = $inquiryService->createInquiry($data);
|
||||
if($res['code'] == 1){
|
||||
\think\facade\Log::write('Bussiness/save: 开始推送未回价数量', 'push_debug');
|
||||
$result = \fetchAndPushUnreturnedPriceCount();
|
||||
\think\facade\Log::write('Bussiness/save: 推送结果: ' . ($result ? '成功' : '失败'), 'push_debug');
|
||||
return $this->buildSuccess();
|
||||
}
|
||||
return $this->buildFailed($res['code'], $res['msg']);
|
||||
|
||||
@@ -30,6 +30,19 @@ use app\model\ReportDetail as ReportDetailModel;
|
||||
|
||||
class Pending extends Base
|
||||
{
|
||||
/**
|
||||
* 获取未回价数量(return_price_status=2)
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function getUnreturnedPriceCount()
|
||||
{
|
||||
$Inquiry = new Inquiry();
|
||||
|
||||
// 查询 return_price_status = 2 的数量
|
||||
$count = $Inquiry->where('return_price_status', 2)->where('status', 1)->count();
|
||||
|
||||
return $this->buildSuccess(['count' => $count]);
|
||||
}
|
||||
|
||||
//询价编号前缀 - 住宅
|
||||
const INQUERY_NUMBER_RESIDENCE_PREFIX = "GZ01";
|
||||
|
||||
@@ -157,6 +157,7 @@ class InquiryService extends CommonService
|
||||
// $inquiry->type = $data['type']; // 2025-12-15 annnj 注释掉type字段
|
||||
$inquiry->is_multiple = count($data['details']) > 1;
|
||||
$inquiry->status = Inquiry::STATUS_CREATED;
|
||||
$inquiry->return_price_status = 2;
|
||||
$inquiry->create_time = date('Y-m-d H:i:s');
|
||||
$inquiry->update_time = date('Y-m-d H:i:s');
|
||||
$inquiry->sort = getSort($data['buss_user_id']);
|
||||
|
||||
@@ -1624,11 +1624,70 @@ function apiErrMsg($msg = '请勿异常操作', $code = 99999) {
|
||||
}
|
||||
|
||||
function writeLog($log = '', $level = "writelog") {
|
||||
$logConfig = Config::pull('log');
|
||||
$logConfig = \think\facade\Config::pull('log');
|
||||
$logConfig['level'][] = $level;
|
||||
$logConfig['apart_level'][] = $level;
|
||||
Log::init($logConfig);
|
||||
Log::write($log, $level);
|
||||
\think\facade\Log::init($logConfig);
|
||||
\think\facade\Log::write($log, $level);
|
||||
}
|
||||
|
||||
/**
|
||||
* 推送消息给所有WebSocket客户端
|
||||
* @param string $eventName 事件名称
|
||||
* @param array $data 推送的数据
|
||||
* @return bool
|
||||
*/
|
||||
function pushToAllClients($eventName, $data) {
|
||||
$socketUrl = 'http://127.0.0.1:2121';
|
||||
|
||||
$postData = [
|
||||
'type' => 'broadcast',
|
||||
'event' => $eventName,
|
||||
'data' => json_encode($data)
|
||||
];
|
||||
|
||||
$options = [
|
||||
'http' => [
|
||||
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
|
||||
'method' => 'POST',
|
||||
'content' => http_build_query($postData),
|
||||
'timeout' => 5
|
||||
]
|
||||
];
|
||||
|
||||
$context = stream_context_create($options);
|
||||
$result = file_get_contents($socketUrl, false, $context);
|
||||
|
||||
writeLog("Push result - event: $eventName, data: " . json_encode($data) . ", result: '$result'", 'push_log');
|
||||
|
||||
return $result === 'ok';
|
||||
}
|
||||
|
||||
/**
|
||||
* 推送未回价数量更新
|
||||
* @param int $count 未回价数量
|
||||
* @return bool
|
||||
*/
|
||||
function pushUnreturnedPriceCount($count) {
|
||||
return pushToAllClients('updateUnreturnedCount', ['count' => $count]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取未回价数量
|
||||
* @return int
|
||||
*/
|
||||
function getUnreturnedPriceCount() {
|
||||
$Inquiry = new \app\model\Inquiry();
|
||||
return $Inquiry->where('return_price_status', 2)->where('status', 1)->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取未回价数量并推送给所有客户端
|
||||
* @return bool
|
||||
*/
|
||||
function fetchAndPushUnreturnedPriceCount() {
|
||||
$count = getUnreturnedPriceCount();
|
||||
return pushUnreturnedPriceCount($count);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -16,10 +16,21 @@ class Push
|
||||
$last_online_count = 0;
|
||||
// 记录最后一次广播的在线页面数
|
||||
$last_online_page_count = 0;
|
||||
// 模拟推送的数值(这里可以从数据库或其他地方获取)
|
||||
$push_value = 0;
|
||||
// PHPSocketIO服务
|
||||
$sender_io = new SocketIO(2120);
|
||||
|
||||
// 定时任务:每3秒更新一次数值并推送给所有客户端
|
||||
\Workerman\Lib\Timer::add(3, function() use (&$push_value, $sender_io) {
|
||||
// 模拟数值变化(可以替换为实际的业务逻辑)
|
||||
$push_value = rand(100, 1000);
|
||||
// 向所有连接的客户端推送数值
|
||||
$sender_io->emit('push_value', ['value' => $push_value]);
|
||||
});
|
||||
|
||||
// 客户端发起连接事件时,设置连接socket的各种事件回调
|
||||
$sender_io->on('connection', function($socket){
|
||||
$sender_io->on('connection', function($socket) use (&$push_value){
|
||||
// 当客户端发来登录事件时触发
|
||||
$socket->on('login', function ($uid)use($socket){
|
||||
global $uidConnectionMap, $last_online_count, $last_online_page_count;
|
||||
|
||||
@@ -1,9 +1,144 @@
|
||||
<?php
|
||||
use think\Container;
|
||||
|
||||
// 加载基础文件\
|
||||
require __DIR__ . '/thinkphp/base.php';
|
||||
// 直接启动WebSocket服务,不通过thinkphp容器
|
||||
require __DIR__ . '/vendor/autoload.php';
|
||||
//
|
||||
// 执行应用并响应
|
||||
Container::get('app')->bind('push/Push/index')->run()->send();
|
||||
|
||||
use Workerman\Worker;
|
||||
use PHPSocketIO\SocketIO;
|
||||
|
||||
// 全局数组保存uid在线数据
|
||||
$uidConnectionMap = [];
|
||||
// 模拟推送的数值
|
||||
$push_value = rand(100, 1000);
|
||||
// PHPSocketIO服务
|
||||
$sender_io = new SocketIO(2120);
|
||||
|
||||
// 客户端发起连接事件
|
||||
$sender_io->on('connection', function($socket) use (&$push_value){
|
||||
// 客户端连接时立即发送当前数值
|
||||
$socket->emit('push_value', ['value' => $push_value]);
|
||||
|
||||
// 连接时自动推送最新的未回价数量
|
||||
try {
|
||||
// 使用PDO连接数据库查询最新数量
|
||||
$dsn = 'mysql:host=localhost;dbname=pgserver;charset=utf8';
|
||||
$pdo = new PDO($dsn, 'root', '');
|
||||
$stmt = $pdo->query('SELECT COUNT(*) as count FROM pg_inquiry WHERE return_price_status = 2');
|
||||
$result = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
$count = $result['count'] ?? 0;
|
||||
|
||||
// 推送未回价数量
|
||||
$socket->emit('updateUnreturnedCount', ['count' => $count]);
|
||||
error_log("WebSocket连接时推送未回价数量: $count");
|
||||
} catch (Exception $e) {
|
||||
error_log("WebSocket连接时获取未回价数量失败: " . $e->getMessage());
|
||||
}
|
||||
|
||||
$socket->on('login', function ($uid)use($socket){
|
||||
global $uidConnectionMap;
|
||||
if(isset($socket->uid)){
|
||||
return;
|
||||
}
|
||||
$uid = (string)$uid;
|
||||
if(!isset($uidConnectionMap[$uid])){
|
||||
$uidConnectionMap[$uid] = 0;
|
||||
}
|
||||
++$uidConnectionMap[$uid];
|
||||
$socket->join($uid);
|
||||
$socket->uid = $uid;
|
||||
});
|
||||
|
||||
$socket->on('disconnect', function () use($socket) {
|
||||
if(!isset($socket->uid)){
|
||||
return;
|
||||
}
|
||||
global $uidConnectionMap;
|
||||
if(--$uidConnectionMap[$socket->uid] <= 0){
|
||||
unset($uidConnectionMap[$socket->uid]);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 监听http端口用于推送和更新数值
|
||||
$sender_io->on('workerStart', function()use ($sender_io){
|
||||
$inner_http_worker = new Worker('http://0.0.0.0:2121');
|
||||
$inner_http_worker->onMessage = function($http_connection, $request_data)use ($sender_io){
|
||||
global $uidConnectionMap, $push_value;
|
||||
|
||||
// 添加跨域头
|
||||
$http_connection->header('Access-Control-Allow-Origin: *');
|
||||
$http_connection->header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
|
||||
$http_connection->header('Access-Control-Allow-Headers: Content-Type');
|
||||
|
||||
// 处理OPTIONS预检请求
|
||||
if(isset($request_data['server']['REQUEST_METHOD']) && $request_data['server']['REQUEST_METHOD'] === 'OPTIONS'){
|
||||
return $http_connection->send('ok');
|
||||
}
|
||||
|
||||
// Workerman HTTP Worker 的 $request_data 是一个数组
|
||||
// 包含 get, post, cookie, server 等键
|
||||
$msgContent = array();
|
||||
|
||||
// 解析 GET 参数
|
||||
if(isset($request_data['get']) && is_array($request_data['get'])){
|
||||
$msgContent = array_merge($msgContent, $request_data['get']);
|
||||
}
|
||||
|
||||
// 解析 POST 参数
|
||||
if(isset($request_data['post']) && is_array($request_data['post'])){
|
||||
$msgContent = array_merge($msgContent, $request_data['post']);
|
||||
}
|
||||
|
||||
// 调试:输出所有信息
|
||||
$logData = array(
|
||||
'msgContent' => $msgContent,
|
||||
'get' => $request_data['get'] ?? [],
|
||||
'post' => $request_data['post'] ?? []
|
||||
);
|
||||
error_log("WebSocket HTTP Request: " . json_encode($logData));
|
||||
|
||||
// 更新数值并推送给所有客户端
|
||||
if(isset($msgContent['type']) && $msgContent['type'] == 'update_value'){
|
||||
$push_value = isset($msgContent['value']) ? intval($msgContent['value']) : rand(100, 1000);
|
||||
$sender_io->emit('push_value', ['value' => $push_value]);
|
||||
return $http_connection->send('ok');
|
||||
}
|
||||
|
||||
// 广播事件给所有客户端(通用推送)
|
||||
if(isset($msgContent['type']) && $msgContent['type'] == 'broadcast'){
|
||||
$event = isset($msgContent['event']) ? $msgContent['event'] : '';
|
||||
$eventData = isset($msgContent['data']) ? $msgContent['data'] : '';
|
||||
|
||||
error_log("Broadcast check - event: '$event', data: '$eventData'");
|
||||
|
||||
if(!empty($event)){
|
||||
if(is_string($eventData)){
|
||||
$decoded = json_decode($eventData, true);
|
||||
$eventData = $decoded !== null ? $decoded : $eventData;
|
||||
}
|
||||
$sender_io->emit($event, $eventData);
|
||||
error_log("Broadcast sent - event: $event, data: " . print_r($eventData, true));
|
||||
return $http_connection->send('ok');
|
||||
}
|
||||
}
|
||||
|
||||
// 推送消息给指定用户或所有用户
|
||||
if(isset($msgContent['type']) && $msgContent['type'] == 'publish'){
|
||||
$to = isset($msgContent['to']) ? $msgContent['to'] : '';
|
||||
$content = isset($msgContent['content']) ? $msgContent['content'] : '';
|
||||
if($to){
|
||||
$sender_io->to($to)->emit('newMsg', $content);
|
||||
}else{
|
||||
$sender_io->emit('newMsg', $content);
|
||||
}
|
||||
if($to && !isset($uidConnectionMap[$to])){
|
||||
return $http_connection->send('offline');
|
||||
}else{
|
||||
return $http_connection->send('ok');
|
||||
}
|
||||
}
|
||||
return $http_connection->send('fail');
|
||||
};
|
||||
$inner_http_worker->listen();
|
||||
});
|
||||
|
||||
Worker::runAll();
|
||||
|
||||
Reference in New Issue
Block a user