This commit is contained in:
xavier
2025-05-26 15:54:46 +08:00
parent 58f943acb0
commit 4f900ab7de
2 changed files with 113 additions and 8 deletions

View File

@@ -51,11 +51,11 @@ class TaskServer extends Server
// todo 临时测试,回头封装到类里调用,每个任务一个类
Timer::add(5, function () use ($current_worker_id, $process_count_for_status_0) {
if ($current_worker_id == self::PROCESS_COUNT - 1) {
// TODO 专门检查添加后的情况,是否通过
// TODO 专门检查添加后的情况,是否通过 使用独立的进程和定时器处理周期改为1min
$tasks = Db::name('task_customer')
->where('status', 1)
->limit(100)
->limit(50)
->select();
if ($tasks) {
// TODO 检查是否添加成功是否需要再次发送然后更新状态为2或3 ...
@@ -68,16 +68,55 @@ class TaskServer extends Server
$tasks = Db::name('task_customer')
->where('status', 0)
->whereRaw("id % $process_count_for_status_0 = {$current_worker_id}")
->limit(100)
->limit(50)
->select();
if ($tasks) {
// ... 更新状态为1然后处理再更新为2或3 ...
// ... 更新状态为1然后处理~~再更新为2或3~~ ...
foreach ($tasks as $task) {
// 查找 ck_customer_acquisition_task 表是否存在该任务然后拿到任务配置详情童谣的任务id需要缓存信息不要重复查询
$task_id = $task['task_id'];
// 先读取缓存
$task_info = cache('task_info_' . $task_id);
if (!$task_info) {
$task_info = Db::name('customer_acquisition_task')
->where('id', $task_id)
->find();
if ($task_info) {
cache('task_info_' . $task['task_id'], $task_info);
} else {
continue;
}
}
if (empty($task_info['status']) || empty($task_info['reqConf'])) {
continue;
}
// 先更新状态为1
Db::name('task_customer')
->where('id', $task['id'])
->update(['status' => 1]);
// todo 基于 $task['phone'] 和 $task_info['reqConf'] 进行处理
//通过conpany_id拿到设备/微信,判断这些微信的状态(是否在线,是否能加人)
// ~~// 更新状态为1 || 4~~
// // 更新状态为2
// Db::name('task_customer')
// ->where('id', $task['id'])
// ->update(['status' => 1]);
// 之后可能会更新为~~失败~~或者不处理 -- 失败一定是另一个进程/定时器在检查的
}
}
}
});
}
}

View File

@@ -141,6 +141,70 @@ class Adapter implements WeChatServiceInterface
return true;
}
// checkIfIsWeChatFriend
public function checkIfIsWeChatFriendByPhone(string $wxId, string $phone): bool
{
if (empty($wxId) || empty($phone)) {
// Avoid queries with empty essential parameters.
return false;
}
try {
// The SQL hint provided is:
// SELECT ownerWechatId, phone, passTime, createTime
// FROM `s2_wechat_friend`
// WHERE ownerWechatId = '您的微信ID' -- Corresponds to $wxId
// AND phone LIKE 'phone%' -- Corresponds to $phone . '%'
// ORDER BY createTime DESC;
// $friendRecord = Db::table('s2_wechat_friend')
// ->where('ownerWechatId', $wxId)
// ->where('phone', 'like', $phone . '%') // Match phone numbers starting with $phone
// ->order('createTime', 'desc') // Order by creation time as hinted
// ->find(); // Fetches the first matching record or null
$friendRecord = Db::table('s2_wechat_friend')
->where('ownerWechatId', $wxId)
->where('phone', 'like', $phone . '%') // Match phone numbers starting with $phone
->order('createTime', 'desc') // Order by creation time as hinted
// ->column('id');
->value('id');
// If a record is found, $friendRecord will not be empty.
return !empty($friendRecord);
} catch (\Exception $e) {
// Log the exception for diagnostics.
Log::error("Error in checkIfIsWeChatFriendByPhone (wxId: {$wxId}, phone: {$phone}): " . $e->getMessage());
// Return false in case of an error, indicating not a friend or unable to determine.
return false;
}
}
// getWeChatFriendPassTimeByPhone
public function getWeChatFriendPassTimeByPhone(string $wxId, string $phone): int
{
if (empty($wxId) || empty($phone)) {
return 0;
}
try {
// $passTime = Db::table('s2_wechat_friend')
// ->where('ownerWechatId', $wxId)
// ->where('phone', 'like', $phone . '%') // Match phone numbers starting with $phone
// ->order('createTime', 'desc') // Order by creation time as hinted
// ->value('passTime');
$record = Db::table('s2_wechat_friend')
->where('ownerWechatId', $wxId)
->where('phone', 'like', $phone . '%') // Match phone numbers starting with $phone
->field('id,createTime,passTime') // Order by creation time as hinted
->find();
return $record['passTime'] ?? $record['createTime'] ?? 0;
} catch (\Exception $e) {
Log::error("Error in getWeChatFriendPassTimeByPhone (wxId: {$wxId}, phone: {$phone}): " . $e->getMessage());
return 0;
}
}
/* todo 以上方法待实现,基于/参考 application/api/controller/WebSocketController.php 去实现 */
@@ -274,7 +338,7 @@ class Adapter implements WeChatServiceInterface
foreach ($cursor as $item) {
if (empty($item['deviceId']) || empty($item['wechatId'])) {
if (empty($item['deviceId']) || empty($item['wechatId']) || empty($item['companyId'])) {
continue;
}
@@ -282,6 +346,7 @@ class Adapter implements WeChatServiceInterface
$exists = Db::table('ck_device_wechat_login')
->where('deviceId', $item['deviceId'])
->where('wechatId', $item['wechatId'])
->where('companyId', $item['companyId'])
// ->where('createTime', $item['createTime'])
->find();
@@ -289,6 +354,7 @@ class Adapter implements WeChatServiceInterface
Db::table('ck_device_wechat_login')
->where('deviceId', $item['deviceId'])
->where('wechatId', $item['wechatId'])
->where('companyId', $item['companyId'])
->update(['alive' => $item['alive'], 'updateTime' => $item['updateTime']]);
} else {
$item['createTime'] = $item['updateTime'];