Files
huangjingfen/pro_v3.5.1/app/controller/admin/v1/user/UserLabel.php
panchengyong 7acbf45ff7 new files
2026-03-07 22:29:07 +08:00

311 lines
10 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\controller\admin\v1\user;
use app\controller\admin\AuthController;
use app\services\user\label\UserLabelCateServices;
use app\services\user\label\UserLabelRelationServices;
use app\services\user\label\UserLabelServices;
use crmeb\services\wechat\config\WorkConfig;
use crmeb\services\wechat\Work;
use think\annotation\Inject;
/**
* 用户标签控制器
* Class UserLabel
* @package app\controller\admin\v1\user
*/
class UserLabel extends AuthController
{
/**
* @var UserLabelServices
*/
#[Inject]
protected UserLabelServices $services;
/**
* 标签列表
* @return mixed
*/
public function index()
{
$where = $this->request->getMore([
['label_cate', ''],
['label_name', ''],
['label_type', ''],
['status', ''],
]);
$where['type'] = 0;
return $this->success($this->services->getList($where));
}
/**
* 获取带分类的用户标签列表
* @param UserLabelCateServices $userLabelCateServices
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function tree_list(UserLabelCateServices $userLabelCateServices)
{
[$label_type] = $this->request->getMore([
['label_type', ''],
], true);
$cate = $userLabelCateServices->getLabelCateAll();
$data = [];
$label = [];
if ($cate) {
foreach ($cate as $value) {
$data[] = [
'id' => $value['id'] ?? 0,
'value' => $value['id'] ?? 0,
'label_cate' => 0,
'label_name' => $value['name'] ?? '',
'label' => $value['name'] ?? '',
'store_id' => $value['store_id'] ?? 0,
'type' => $value['type'] ?? 1,
];
}
$label = $this->services->getList(['type' => 0, 'label_type' => $label_type]);
$label = $label['list'] ?? [];
if ($label) {
foreach ($label as &$item) {
$item['label'] = $item['label_name'];
$item['value'] = $item['id'];
}
}
}
return $this->success($this->services->get_tree_children($data, $label));
}
/**
* 添加修改标签表单
* @return mixed
* @throws \FormBuilder\Exception\FormBuilderException
*/
public function add()
{
[$id, $label_cate] = $this->request->getMore([
['id', 0],
['label_cate', 0]
], true);
return $this->success($this->services->add((int)$id, 0, 0, $label_cate));
}
/**
* 保存标签表单数据
* @param int $id
* @return mixed
*/
public function save()
{
$data = $this->request->postMore([
['id', 0],
['label_cate', 0],
['label_name', ''],//标签名称数组
['label_type', 1],//标签类型,1:手动;2 自动
['is_product', 0],//商品条件,1 开启 0 关闭
['is_property', 0],//资产条件,1 开启 0 关闭
['is_trade', 0],//交易条件,1 开启0 关闭
['is_customer', 0],//客户条件,1 开启 0 关闭
['is_condition', 1],//条件类型,1 条件满足任一,2全部条件满足
['product', []],//商品条件
['property', []],//资产条件
['trade', []],//交易条件
['customer', []],//客户条件
['status', 1],//状态
]);
// if (!$data['label_name'] = trim($data['label_name'])) return $this->fail('会员标签不能为空!');
$label_name = is_array($data['label_name']) ? $data['label_name'] : [$data['label_name']];
foreach ($label_name as $value) {
if ($this->services->fieldExists('label_name', $value, (int)$data['id'])) {
return $this->fail("会员标签{$value}已存在!");
}
}
$this->services->saveData((int)$data['id'], $data);
return $this->success('保存成功');
}
/**
* 删除
* @param $id
* @throws \Exception
*/
public function delete()
{
[$id] = $this->request->getMore([
['id', 0],
], true);
if (!$id) return $this->fail('数据不存在');
$this->services->delLabel((int)$id);
return $this->success('刪除成功!');
}
/**
* 标签分类
* @param UserLabelCateServices $services
* @return mixed
*/
public function getUserLabel(UserLabelCateServices $services, $uid)
{
[$uids, $all, $where] = $this->request->postMore([
['uids', []],
['all', 0],
['where', ""],
], true);
if (count($uids) == 1) {
$uid = $uids[0] ?? 0;
}
return $this->success($services->getUserLabel((int)$uid));
}
/**
* 设置用户标签
* @param UserLabelRelationServices $services
* @param $uid
* @return mixed
*/
public function setUserLabel(UserLabelRelationServices $services, $uid)
{
[$labels, $unLabelIds] = $this->request->postMore([
['label_ids', []],
['un_label_ids', []]
], true);
if (!count($labels) && !count($unLabelIds)) {
return $this->fail('请先添加用户标签');
}
if ($services->setUserLable($uid, $labels) && $services->unUserLabel($uid, $unLabelIds)) {
return $this->success('设置成功');
} else {
return $this->fail('设置失败');
}
}
/**
* 同步客户标签
* @param WorkConfig $config
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function synchroWorkLabel(WorkConfig $config, Work $work)
{
if (!$config->corpId) {
return $this->fail('请先配置企业微信ID');
}
$config = $work->getAppConfig(WorkConfig::TYPE_USER);
// if (empty($config['secret'])) {
// return $this->fail('请先配置企业微信客户secret');
// }
if ($this->services->authWorkClientLabel()) {
return $this->success('已加入消息队列,进行同步。请稍等片刻');
} else {
return $this->fail('加入消息队列失败');
}
}
/**
* 获取标签详情信息
* @param int $id 标签ID
* @return mixed
*/
public function info($id)
{
if (!$id) {
return $this->fail('数据不存在');
}
$info = $this->services->getInfo($id);
return $this->success($info);
}
/**
* 批量处理用户标签检查
* @return mixed
*/
public function batchProcess()
{
[$labelIds, $action] = $this->request->postMore([
['label_ids', []],
['action', 'update']
], true);
if (empty($labelIds)) {
return $this->fail('请选择要处理的标签');
}
try {
/** @var \app\services\user\label\UserLabelBatchProcessServices $batchProcessServices */
$batchProcessServices = app()->make(\app\services\user\label\UserLabelBatchProcessServices::class);
$result = $batchProcessServices->processLabelChange($labelIds, $action);
if ($result) {
return $this->success('批量处理任务已启动,正在后台处理中,请稍后查看处理结果');
} else {
return $this->fail('批量处理任务启动失败');
}
} catch (\Throwable $e) {
return $this->fail('批量处理失败:' . $e->getMessage());
}
}
/**
* 获取批量处理状态
* @return mixed
*/
public function batchStatus()
{
try {
/** @var \app\services\user\label\UserLabelBatchProcessServices $batchProcessServices */
$batchProcessServices = app()->make(\app\services\user\label\UserLabelBatchProcessServices::class);
$status = $batchProcessServices->getBatchProcessStatus();
return $this->success($status);
} catch (\Throwable $e) {
return $this->fail('获取处理状态失败:' . $e->getMessage());
}
}
/**
* 触发所有自动标签的用户检查
* @return mixed
*/
public function processAllAutoLabels()
{
try {
// 获取所有自动标签
$autoLabels = $this->services->getLabelList([
'label_type' => 2, // 自动标签
'status' => 1 // 启用状态
]);
if (empty($autoLabels)) {
return $this->fail('没有找到启用的自动标签');
}
$labelIds = array_column($autoLabels, 'id');
/** @var \app\services\user\label\UserLabelBatchProcessServices $batchProcessServices */
$batchProcessServices = app()->make(\app\services\user\label\UserLabelBatchProcessServices::class);
$result = $batchProcessServices->processLabelChange($labelIds, 'check_all');
if ($result) {
return $this->success('所有自动标签检查任务已启动,共处理 ' . count($labelIds) . ' 个标签,正在后台处理中');
} else {
return $this->fail('批量处理任务启动失败');
}
} catch (\Throwable $e) {
return $this->fail('处理失败:' . $e->getMessage());
}
}
}