【操盘手】 流量分发功能提交

This commit is contained in:
wong
2025-06-04 11:38:43 +08:00
parent 1f2a1dab00
commit 296464bff9
9 changed files with 199 additions and 86 deletions

View File

@@ -194,9 +194,7 @@ export default function EditTrafficDistributionPage({ params }: { params: Promis
<ChevronLeft className="h-5 w-5" />
</Button>
<h1 className="text-lg font-bold"></h1>
<Button variant="ghost" size="icon" className="ml-auto">
<Settings className="h-5 w-5" />
</Button>
</div>
<StepIndicator currentStep={currentStep} steps={steps} />
</div>

View File

@@ -17,7 +17,7 @@ export default function BasicInfoStep({ onNext, initialData = {} }: BasicInfoSte
const [formData, setFormData] = useState({
name: initialData.name ?? `流量分发 ${format(new Date(), "yyyyMMdd HHmm")}`,
distributeType: initialData.distributeType ?? "1",
maxPerDay: initialData.maxPerDay ?? 50,
maxPerDay: initialData.maxPerDay ?? 100,
timeType: initialData.timeType ?? "2",
startTime: initialData.startTime ?? "09:00",
endTime: initialData.endTime ?? "18:00",
@@ -69,7 +69,7 @@ export default function BasicInfoStep({ onNext, initialData = {} }: BasicInfoSte
<span className="text-gray-500 text-sm">()</span>
</Label>
</div>
<div className="flex items-center space-x-2">
{/* <div className="flex items-center space-x-2">
<RadioGroupItem value="2" id="priority" />
<Label htmlFor="priority" className="cursor-pointer">
优先级分配 <span className="text-gray-500 text-sm">(按客服优先级顺序分配)</span>
@@ -80,7 +80,7 @@ export default function BasicInfoStep({ onNext, initialData = {} }: BasicInfoSte
<Label htmlFor="ratio" className="cursor-pointer">
比例分配 <span className="text-gray-500 text-sm">(按设定比例分配流量)</span>
</Label>
</div>
</div> */}
</RadioGroup>
</div>

View File

@@ -103,18 +103,19 @@ export default function TargetSettingsStep({ onNext, onBack, initialData = {}, s
</div>
{/* 设备选择弹窗 */}
<Dialog open={deviceDialogOpen} onOpenChange={setDeviceDialogOpen}>
<DialogContent className="max-w-xl w-full p-0">
<DialogTitle className="text-lg font-bold text-center mb-4"></DialogTitle>
<div className="p-6 pt-0">
<DialogContent className="max-w-xl w-full p-0 rounded-2xl shadow-2xl max-h-[80vh]">
<DialogTitle className="text-lg font-bold text-center py-3 border-b"></DialogTitle>
<div className="p-6 pt-4">
{/* 搜索和筛选 */}
<div className="flex items-center gap-2 mb-4">
<Input
placeholder="搜索设备IMEI/备注/微信号"
value={search}
onChange={e => setSearch(e.target.value)}
className="flex-1"
className="flex-1 rounded-lg border-gray-200 focus:border-blue-500 focus:ring-2 focus:ring-blue-100"
/>
<select
className="border rounded px-2 py-1 text-sm"
className="border rounded-lg px-3 py-2 text-sm bg-gray-50 focus:border-blue-500"
value={statusFilter}
onChange={e => setStatusFilter(e.target.value)}
>
@@ -123,7 +124,8 @@ export default function TargetSettingsStep({ onNext, onBack, initialData = {}, s
<option value="offline">线</option>
</select>
</div>
<div className="max-h-72 overflow-y-auto divide-y">
{/* 设备列表 */}
<div className="max-h-[500px] overflow-y-auto space-y-2">
{loading ? (
<div className="text-center text-gray-400 py-8">...</div>
) : filteredDevices.length === 0 ? (
@@ -132,11 +134,15 @@ export default function TargetSettingsStep({ onNext, onBack, initialData = {}, s
filteredDevices.map(device => (
<label
key={device.id}
className="flex items-center px-2 py-3 cursor-pointer hover:bg-gray-50"
className={`
flex items-center gap-3 p-4 rounded-xl border
${selectedDeviceIds.includes(String(device.id)) ? "border-blue-500 bg-blue-50" : "border-gray-200 bg-white"}
hover:border-blue-400 transition-colors cursor-pointer
`}
>
<input
type="checkbox"
className="mr-3 accent-blue-500"
className="accent-blue-500 scale-110"
checked={selectedDeviceIds.includes(String(device.id))}
onChange={() => {
setSelectedDeviceIds(prev =>
@@ -147,17 +153,26 @@ export default function TargetSettingsStep({ onNext, onBack, initialData = {}, s
}}
/>
<div className="flex-1">
<div className="font-medium text-base">{device.memo || device.nickname || device.name}</div>
<div className="font-semibold text-base">{device.memo || device.nickname || device.name}</div>
<div className="text-xs text-gray-500">IMEI: {device.imei}</div>
<div className="text-xs text-gray-500">: {device.wechatId || '--'}{device.nickname || '--'}</div>
<div className="text-xs text-gray-400">: {device.wechatId || '--'}{device.nickname || '--'}</div>
</div>
<span className={`ml-2 text-xs ${device.alive === 1 ? 'text-green-600' : 'text-gray-400'}`}>{device.alive === 1 ? '在线' : '离线'}</span>
<span className="flex items-center gap-1 text-xs font-medium">
<span className={`w-2 h-2 rounded-full ${device.alive === 1 ? 'bg-green-500' : 'bg-gray-300'}`}></span>
<span className={device.alive === 1 ? 'text-green-600' : 'text-gray-400'}>
{device.alive === 1 ? '在线' : '离线'}
</span>
</span>
</label>
))
)}
</div>
<div className="flex justify-end mt-6">
<Button className="w-full" onClick={handleDialogConfirm}>
{/* 确认按钮 */}
<div className="flex justify-center mt-8">
<Button
className="w-4/5 py-3 rounded-full text-base font-bold shadow-md"
onClick={handleDialogConfirm}
>
</Button>
</div>

View File

@@ -113,25 +113,29 @@ export default function TrafficPoolStep({ onSubmit, onBack, initialData = {}, de
</div>
</div>
<Dialog open={dialogOpen} onOpenChange={setDialogOpen}>
<DialogContent className="max-w-xl w-full p-0">
<DialogTitle className="text-lg font-bold text-center mb-4"></DialogTitle>
<div className="p-6 pt-0">
<DialogContent className="max-w-xl w-full p-0 rounded-2xl shadow-2xl max-h-[80vh]">
<DialogTitle className="text-lg font-bold text-center py-3 border-b"></DialogTitle>
<div className="p-6 pt-4">
{/* 搜索栏 */}
<div className="relative mb-4">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-400" />
<Input
placeholder="搜索流量池"
value={searchTerm}
onChange={e => setSearchTerm(e.target.value)}
className="pl-10"
className="pl-10 rounded-lg border-gray-200 focus:border-blue-500 focus:ring-2 focus:ring-blue-100"
/>
</div>
<div className="overflow-y-auto max-h-96">
{/* 流量池列表 */}
<div className="overflow-y-auto max-h-[400px] space-y-3">
{pagedPools.map((pool) => (
<Card
<div
key={pool.label}
className={`flex items-center justify-between rounded-xl shadow-sm border transition-colors duration-150 mb-4 cursor-pointer
className={`
flex items-center justify-between rounded-xl shadow-sm border transition-colors duration-150 cursor-pointer
${selectedPools.includes(pool.label) ? "border-blue-500 bg-blue-50" : "border-gray-200 bg-white"}
hover:border-blue-400`}
hover:border-blue-400
`}
onClick={() => togglePool(pool.label)}
>
<div className="flex items-center space-x-3 p-4 flex-1">
@@ -143,27 +147,35 @@ export default function TrafficPoolStep({ onSubmit, onBack, initialData = {}, de
<p className="text-sm text-gray-500">{poolDescMap[pool.label] || ""}</p>
</div>
</div>
<div className="flex items-center space-x-3 pr-4">
<span className="text-sm text-gray-500">{pool.count} </span>
<Checkbox
checked={selectedPools.includes(pool.label)}
onCheckedChange={() => togglePool(pool.label)}
onClick={e => e.stopPropagation()}
/>
</div>
</Card>
<span className="text-sm text-gray-500 mr-4">{pool.count} </span>
<input
type="checkbox"
className="accent-blue-500 scale-125 mr-6"
checked={selectedPools.includes(pool.label)}
onChange={e => {
e.stopPropagation();
togglePool(pool.label);
}}
onClick={e => e.stopPropagation()}
/>
</div>
))}
</div>
{/* 分页按钮 */}
{totalPages > 1 && (
<div className="flex justify-center items-center gap-2 mt-4">
<div className="flex justify-center items-center gap-2 mt-6">
<Button size="sm" variant="outline" disabled={currentPage === 1} onClick={() => setCurrentPage(p => Math.max(1, p - 1))}></Button>
<span className="text-sm text-gray-500"> {currentPage} / {totalPages} </span>
<Button size="sm" variant="outline" disabled={currentPage === totalPages} onClick={() => setCurrentPage(p => Math.min(totalPages, p + 1))}></Button>
</div>
)}
<div className="flex justify-end mt-6">
<Button className="w-full" onClick={() => setDialogOpen(false)} disabled={selectedPools.length === 0}>
{/* 确认按钮 */}
<div className="flex justify-center mt-8">
<Button
className="w-4/5 py-3 rounded-full text-base font-bold shadow-md"
onClick={() => setDialogOpen(false)}
disabled={selectedPools.length === 0}
>
</Button>
</div>

View File

@@ -141,9 +141,6 @@ export default function NewTrafficDistribution() {
<ChevronLeft className="h-5 w-5" />
</Button>
<h1 className="text-lg font-bold"></h1>
<Button variant="ghost" size="icon" className="ml-auto">
<Settings className="h-5 w-5" />
</Button>
</div>
<StepIndicator currentStep={currentStep} steps={steps} />

View File

@@ -240,8 +240,8 @@ export default function TrafficDistributionPage() {
<div>
<h3 className="font-medium text-lg">{plan.name}</h3>
<div className="flex items-center space-x-2 mt-1">
<Badge variant={plan.status === "active" ? "success" : "secondary"}>
{plan.status === "active" ? "进行中" : "已暂停"}
<Badge variant={plan.status == 1 ? "success" : "secondary"}>
{plan.status == 1 ? "进行中" : "已暂停"}
</Badge>
{plan.config.pools.map((group, index) => (
<Badge key={index} variant="outline">
@@ -292,8 +292,8 @@ export default function TrafficDistributionPage() {
<div className="bg-white">
<div className="grid grid-cols-3">
<div className="p-3 text-center border-r border-gray-200">
<div className="text-lg font-semibold">{plan.config.total.dailyAverage}</div>
<div className="text-xs text-gray-500 mt-1"></div>
<div className="text-lg font-semibold">{plan.config.total.totalAccounts}</div>
<div className="text-xs text-gray-500 mt-1"></div>
</div>
<div className="p-3 text-center border-r border-gray-200">
<div className="text-lg font-semibold">{plan.config.total.deviceCount}</div>
@@ -322,7 +322,7 @@ export default function TrafficDistributionPage() {
<div className="p-3 bg-gray-50 text-sm text-gray-500 flex items-center justify-between">
<div className="flex items-center">
<Clock className="h-4 w-4 mr-1" />
<span>: {plan.lastUpdated}</span>
<span>: {plan.config.lastUpdated}</span>
</div>
<div>: {plan.creatorName}</div>
</div>

View File

@@ -649,24 +649,24 @@ class WebSocketController extends BaseController
// 消息拼接 msgType(1:文本 3:图片 43:视频 47:动图表情包gif、其他表情包 49:小程序/其他:图文、文件)
// 当前type 为文本、图片、动图表情包的时候content为string, 其他情况为对象 {type: 'file/link/...', url: '', title: '', thunmbPath: '', desc: ''}
$result = [
"cmdType" => "CmdSendMessage",
"content" => $dataArray['content'],
"msgSubType" => 0,
"msgType" => $dataArray['msgType'],
"seq" => time(),
"wechatAccountId" => $dataArray['wechatAccountId'],
"wechatChatroomId" => 0,
"wechatFriendId" => $dataArray['wechatFriendId'],
];
$result = [
"cmdType" => "CmdSendMessage",
"content" => $dataArray['content'],
"msgSubType" => 0,
"msgType" => $dataArray['msgType'],
"seq" => time(),
"wechatAccountId" => $dataArray['wechatAccountId'],
"wechatChatroomId" => 0,
"wechatFriendId" => $dataArray['wechatFriendId'],
];
$result = json_encode($result);
$this->client->send($result);
$message = $this->client->receive();
$message = json_decode($message, 1);
//关闭WS链接
$this->client->close();
//Log::write('WS个人消息发送');
$result = json_encode($result);
$this->client->send($result);
$message = $this->client->receive();
$message = json_decode($message, 1);
//关闭WS链接
$this->client->close();
//Log::write('WS个人消息发送');
return $message;
}

View File

@@ -188,7 +188,7 @@ class WorkbenchController extends Controller
$list = Workbench::where($where)
->with($with)
->field('id,name,type,status,autoStart,userId,createTime,updateTime')
->field('id,companyId,name,type,status,autoStart,userId,createTime,updateTime')
->order('id', 'desc')
->page($page, $limit)
->select()
@@ -260,12 +260,50 @@ class WorkbenchController extends Controller
$item->config = $item->trafficConfig;
$item->config->devices = json_decode($item->config->devices, true);
$item->config->pools = json_decode($item->config->pools, true);
$config_item = Db::name('workbench_traffic_config_item')->where(['workbenchId' => $item->id])->order('id DESC')->find();
$item->config->lastUpdated = !empty($config_item) ? date('Y-m-d H:i',$config_item['createTime']) : '--';
//统计
$labels = $item->config->pools;
$totalUsers = Db::table('s2_wechat_friend')->alias('wf')
->join(['s2_company_account' => 'sa'], 'sa.id = wf.accountId', 'left')
->join(['s2_wechat_account' => 'wa'], 'wa.id = wf.wechatAccountId', 'left')
->where([
['wf.isDeleted', '=', 0],
['sa.departmentId', '=', $item->companyId]
])
->whereIn('wa.currentDeviceId', $item->config->devices)
->field('wf.id,wf.wechatAccountId,wf.wechatId,wf.labels,sa.userName,wa.currentDeviceId as deviceId')
->where(function ($q) use ($labels) {
foreach ($labels as $label) {
$q->whereOrRaw("JSON_CONTAINS(wf.labels, '\"{$label}\"')");
}
})->count();
$totalAccounts = Db::table('s2_company_account')
->alias('a')
->where(['a.departmentId' => $item->companyId, 'a.status' => 0])
->whereNotLike('a.userName', '%_offline%')
->whereNotLike('a.userName', '%_delete%')
->group('a.id')
->count();
$todayStart = strtotime(date('Y-m-d 00:00:00'));
$todayEnd = strtotime(date('Y-m-d 23:59:59'));
$dailyAverage = Db::name('workbench_traffic_config_item')
->where('workbenchId', $item->id)
->whereTime('createTime', 'between', [$todayStart, $todayEnd])
->count();
if($dailyAverage > 0){
$dailyAverage = $dailyAverage / $totalAccounts;
}
$item->config->total = [
'dailyAverage' => 0,
'dailyAverage' => intval($dailyAverage),
'totalAccounts' => $totalAccounts,
'deviceCount' => count($item->config->devices),
'poolCount' => count($item->config->pools),
'dailyAverage' => $item->config->maxPerDay,
'totalUsers' => $item->config->maxPerDay * count($item->config->devices) * count($item->config->pools)
'totalUsers' => $totalUsers >> 0
];
}
unset($item->trafficConfig,$item->traffic_config);
@@ -1175,7 +1213,7 @@ class WorkbenchController extends Controller
foreach ($labels as $label) {
$friendCount = Db::table('s2_wechat_friend')
->whereIn('ownerWechatId',$wechatIds)
->where('labels', 'like', '%'.$label.'%')
->where('labels', 'like', '%"'.$label.'"%')
->count();
$newLabel[] = [
'label' => $label,

View File

@@ -10,6 +10,7 @@ use think\facade\Cache;
use app\cunkebao\model\Workbench;
use app\cunkebao\model\WorkbenchTrafficConfig;
use think\Db;
use app\api\controller\AutomaticAssign;
class WorkbenchTrafficDistributeJob
{
@@ -69,31 +70,83 @@ class WorkbenchTrafficDistributeJob
return;
}
// 获取账号userName不包含offline和delete
// 获取当天未超额的可用账号
$todayStart = strtotime(date('Y-m-d 00:00:00'));
$todayEnd = strtotime(date('Y-m-d 23:59:59'));
$accounts = Db::table('s2_company_account')
->where(['departmentId' => $workbench->companyId, 'status' => 0])
->whereNotLike('userName', '%_offline%')
->whereNotLike('userName', '%_delete%')
->field('id,userName,realName')
->alias('a')
->where(['a.departmentId' => $workbench->companyId, 'a.status' => 0])
->whereNotLike('a.userName', '%_offline%')
->whereNotLike('a.userName', '%_delete%')
->leftJoin('workbench_traffic_config_item wti', "wti.wechatAccountId = a.id AND wti.workbenchId = {$workbench->id} AND wti.createTime BETWEEN {$todayStart} AND {$todayEnd}")
->field('a.id,a.userName,a.realName,COUNT(wti.id) as todayCount')
->group('a.id')
->having('todayCount <= ' . $config['maxPerDay'])
->select();
$accountNum = count($accounts);
if ($accountNum < 2) {
Log::info("流量分发工作台 {$workbench->id} 账号少于3");
if ($accountNum < 1) {
Log::info("流量分发工作台 {$workbench->id} 可分配账号少于1");
return;
}
$automaticAssign = new AutomaticAssign();
do {
$friends = $this->getFriendsByLabels($workbench, $config, $page, $pageSize);
if (empty($friends) || count($friends) == 0) {
Log::info("流量分发工作台 {$workbench->id} 没有可分配的好友");
break;
}
$i = 0;
$accountNum = count($accounts);
foreach ($friends as $friend) {
if ($accountNum == 0) {
Log::info("流量分发工作台 {$workbench->id} 所有账号今日分配已满");
break 2;
}
if ($i >= $accountNum) {
$i = 0;
}
$account = $accounts[$i];
// 获取可分配好友
$friends = $this->getFriendsByLabels($workbench, $config, $page, $pageSize);
if (empty($friends) || count($friends) == 0) {
Log::info("流量分发工作台 {$workbench->id} 没有可分配的好友");
return;
}
// 如果该账号今天分配的记录数加上本次分配的记录数超过最大限制
if (($account['todayCount'] + $pageSize) >= $config['maxPerDay']) {
// 查询该客服账号当天分配记录数
$todayCount = Db::name('workbench_traffic_config_item')
->where('workbenchId', $workbench->id)
->where('wechatAccountId', $account['id'])
->whereBetween('createTime', [$todayStart, $todayEnd])
->count();
if ($todayCount >= $config['maxPerDay']) {
unset($accounts[$i]);
$accounts = array_values($accounts);
$accountNum = count($accounts);
$i++;
continue;
}
}
// TODO: 在这里实现分发逻辑
print_r($friends);
exit;
// 执行切换好友命令
$automaticAssign->allotWechatFriend([
'wechatFriendId' => $friend['id'],
'toAccountId' => $account['id']
], true);
// 写入分配记录表
Db::name('workbench_traffic_config_item')->insert([
'workbenchId' => $workbench->id,
'deviceId' => $friend['deviceId'],
'wechatFriendId' => $friend['id'],
'wechatAccountId' => $account['id'],
'createTime' => time()
]);
Log::info("流量分发工作台 {$workbench->id} 好友[{$friend['id']}]分配给客服[{$account['id']}] 成功");
$i++;
}
break;
$page++;
} while (true);
Log::info("流量分发工作台 {$workbench->id} 执行分发逻辑完成");
}
/**
* 检查是否在流量分发时间范围内
* @param WorkbenchAutoLike $config
@@ -146,7 +199,7 @@ class WorkbenchTrafficDistributeJob
$q->whereOrRaw("JSON_CONTAINS(wf.labels, '\"{$label}\"')");
}
});
$list = $query->page($page, $pageSize)->select();
$list = $query->page($page, $pageSize)->order('wf.id DESC')->select();
return $list;
}