Files
zyt/server/app/adminapi/controller/qywx/CustomerController.php
T
2026-09-03 16:03:41 +08:00

127 lines
3.5 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace app\adminapi\controller\qywx;
use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\qywx\CustomerLists;
use app\adminapi\logic\auth\AuthLogic;
use app\adminapi\logic\qywx\CustomerLogic;
use app\adminapi\validate\qywx\CustomerValidate;
/**
* 企业微信客户管理控制器
*/
class CustomerController extends BaseAdminController
{
private const DELETE_PERMISSION = 'qywx.customer/delete';
/**
* @notes 客户列表
*/
public function lists()
{
return $this->dataLists(new CustomerLists());
}
/**
* @notes 同步企业微信客户
*/
public function sync()
{
$result = CustomerLogic::triggerBackgroundSync();
if ($result === false) {
return $this->fail(CustomerLogic::getError());
}
$msg = is_array($result) && isset($result['message']) ? (string) $result['message'] : '已提交同步';
return $this->success($msg, $result);
}
/**
* @notes 删除一条本地企业微信客户同步记录
*/
public function delete()
{
// 显式鉴权,避免权限菜单迁移漏执行时被通用中间件当成“未受控 URI”放行。
if (!$this->canDeleteCustomer()) {
return $this->fail('权限不足,无法删除企业微信客户');
}
$params = (new CustomerValidate())->post()->goCheck('delete');
if (!CustomerLogic::deleteCustomer((int) $params['id'])) {
return $this->fail(CustomerLogic::getError());
}
return $this->success('删除成功');
}
/**
* @notes 获取统计信息
*/
public function stats()
{
$stats = CustomerLogic::getStats();
return $this->data($stats);
}
/**
* @notes 获取标签维度统计(按 group 分组、按客户数倒序,供前端筛选下拉 + 标签面板共用)
*/
public function tagStats()
{
return $this->data(CustomerLogic::getTagStats());
}
/**
* @notes 今日进入分布(用于页面"今日新增"卡片的迷你柱/最近进入时间/渠道 Top5)
*/
public function todayArrival()
{
return $this->data(CustomerLogic::getTodayArrivalStats());
}
/**
* @notes 今日进入明细流水(每一次 add_external_contact 推送 = 一行,按时间倒序分页)
*/
public function todayArrivalList()
{
$pageNo = (int) $this->request->get('page_no', 1);
$pageSize = (int) $this->request->get('page_size', 20);
return $this->data(CustomerLogic::getTodayArrivalList($pageNo, $pageSize));
}
/**
* @notes 获取同步设置
*/
public function getSyncSettings()
{
$settings = CustomerLogic::getSyncSettings();
return $this->data($settings);
}
/**
* @notes 保存同步设置
*/
public function saveSyncSettings()
{
$params = (new CustomerValidate())->post()->goCheck('syncSettings');
$result = CustomerLogic::saveSyncSettings($params);
if ($result === false) {
return $this->fail(CustomerLogic::getError());
}
return $this->success('保存成功');
}
private function canDeleteCustomer(): bool
{
if ((int) ($this->adminInfo['root'] ?? 0) === 1) {
return true;
}
return in_array(self::DELETE_PERMISSION, AuthLogic::getAuthByAdminId($this->adminId), true);
}
}