Files
cunkebao_v3/Server/application/cunkebao/controller/traffic/GetPotentialListWithInCompanyV1Controller.php

276 lines
9.6 KiB
PHP
Raw Normal View History

<?php
namespace app\cunkebao\controller\traffic;
use app\common\model\TrafficPool as TrafficPoolModel;
use app\common\model\TrafficSource as TrafficSourceModel;
use app\common\model\WechatFriendShip as WechatFriendShipModel;
use app\cunkebao\controller\BaseController;
use library\ResponseHelper;
2025-07-23 16:00:01 +08:00
use think\Db;
/**
* 流量池控制器
*/
class GetPotentialListWithInCompanyV1Controller extends BaseController
{
/**
* 构建查询条件
*
* @param array $params
* @return array
*/
protected function makeWhere(array $params = []): array
{
if (!empty($keyword = $this->request->param('keyword'))) {
2025-07-29 17:04:00 +08:00
$where[] = ['p.identifier|wa.nickname|wa.phone|wa.wechatId|wa.alias', 'like', '%' . $keyword . '%'];
}
// 状态筛选
2025-07-23 16:00:01 +08:00
if ($status = $this->request->param('addStatus')) {
$where['s.status'] = $status;
} else {
$where['s.status'] = array('<>', TrafficSourceModel::STATUS_PASSED);
}
// 来源的筛选
2025-07-23 16:00:01 +08:00
if ($fromd = $this->request->param('packageId')) {
2025-07-29 17:04:00 +08:00
if ($fromd != -1) {
2025-07-23 16:00:01 +08:00
$where['tsp.id'] = $fromd;
2025-07-29 17:04:00 +08:00
} else {
$where[] = ['tsp.id', null];
2025-07-23 16:00:01 +08:00
}
}
2025-07-23 16:00:01 +08:00
if ($device = $this->request->param('device')) {
$where['d.deviceId'] = $device;
}
$where['s.companyId'] = $this->getUserInfo('companyId');
return array_merge($where, $params);
}
/**
* 获取流量池列表
*
* @param array $where
* @return \think\Paginator
*/
2025-07-23 16:00:01 +08:00
protected function getPoolListByCompanyId(array $where)
{
$query = TrafficPoolModel::alias('p')
->field(
[
2025-07-29 17:04:00 +08:00
'p.id', 'p.identifier', 'p.mobile', 'p.wechatId', 'p.identifier',
's.fromd', 's.status', 's.createTime', 's.companyId', 's.sourceId', 's.type',
2025-07-23 16:00:01 +08:00
'wa.nickname', 'wa.avatar', 'wa.gender', 'wa.phone',
]
)
2025-07-29 17:04:00 +08:00
->join('traffic_source s', 'p.identifier=s.identifier', 'left')
->join('wechat_account wa', 'p.identifier=wa.wechatId', 'left')
->join('traffic_source_package_item tspi', 'p.identifier = tspi.identifier AND s.companyId = tspi.companyId', 'left')
->join('traffic_source_package tsp', 'tspi.packageId=tsp.id', 'left')
->join('device_wechat_login d', 's.sourceId=d.wechatId', 'left')
2025-07-23 16:00:01 +08:00
->order('p.id DESC,s.id DESC')
->group('p.identifier');
foreach ($where as $key => $value) {
if (is_numeric($key) && is_array($value) && isset($value[0]) && $value[0] === 'exp') {
$query->whereExp('', $value[1]);
continue;
}
if (is_array($value)) {
$query->where($key, ...$value);
continue;
}
$query->where($key, $value);
}
2025-07-29 17:04:00 +08:00
$result = $query->paginate($this->request->param('limit/d', 10), false, ['page' => $this->request->param('page/d', 1)]);
2025-07-23 16:00:01 +08:00
$list = $result->items();
$total = $result->total();
foreach ($list as &$item) {
//流量池筛选
$package = Db::name('traffic_source_package_item')->alias('tspi')
->join('traffic_source_package p', 'tspi.packageId=p.id AND tspi.companyId=p.companyId')
2025-07-29 17:04:00 +08:00
->where(['tspi.companyId' => $item->companyId, 'tspi.identifier' => $item->identifier])
2025-07-23 16:00:01 +08:00
->column('p.name');
$package2 = Db::name('traffic_source_package_item')->alias('tspi')
->join('traffic_source_package p', 'tspi.packageId=p.id')
2025-07-29 17:04:00 +08:00
->where(['tspi.companyId' => $item->companyId, 'tspi.identifier' => $item->identifier, 'p.isSys' => 1])
2025-07-23 16:00:01 +08:00
->column('p.name');
$packages = array_merge($package, $package2);
$item['packages'] = $packages;
2025-07-29 17:04:00 +08:00
if ($item->type == 1) {
2025-07-23 16:00:01 +08:00
$tag = Db::name('wechat_friendship')->where(['wechatId' => $item->wechatId])->column('tags');
$tags = [];
foreach ($tag as $k => $v) {
2025-07-29 17:04:00 +08:00
$v = json_decode($v, true);
if (!empty($v)) {
$tags = array_merge($tags, $v);
}
2025-07-23 16:00:01 +08:00
}
$item['tags'] = $tags;
}
}
unset($item);
$data = ['list' => $list, 'total' => $total];
return json_encode($data, JSON_UNESCAPED_UNICODE);
}
/**
* 获取流量池列表
*
* @return \think\response\Json
*/
public function index()
{
try {
2025-07-29 17:04:00 +08:00
$result = $this->getPoolListByCompanyId($this->makeWhere());
2025-07-23 16:00:01 +08:00
$result = json_decode($result, true);
return ResponseHelper::success(
[
2025-07-29 17:04:00 +08:00
'list' => $result['list'],
2025-07-23 16:00:01 +08:00
'total' => $result['total'],
]
);
} catch (\Exception $e) {
return ResponseHelper::error($e->getMessage(), $e->getCode());
}
}
2025-07-23 16:00:01 +08:00
2025-07-29 17:04:00 +08:00
public function getUser()
{
$userId = $this->request->param('userId', '');
if (empty($userId)) {
return json_encode(['code' => 500, 'msg' => '用户id不能为空']);
}
$item = TrafficPoolModel::alias('p')
->field(
[
'p.id', 'p.identifier', 'p.mobile', 'p.wechatId', 'p.identifier',
's.fromd', 's.status', 's.createTime', 's.companyId', 's.sourceId', 's.type',
'wa.nickname', 'wa.avatar', 'wa.gender', 'wa.phone',
]
)
->join('traffic_source s', 'p.identifier=s.identifier', 'left')
->join('wechat_account wa', 'p.identifier=wa.wechatId', 'left')
->join('traffic_source_package_item tspi', 'p.identifier = tspi.identifier AND s.companyId = tspi.companyId', 'left')
->join('traffic_source_package tsp', 'tspi.packageId=tsp.id', 'left')
->join('device_wechat_login d', 's.sourceId=d.wechatId', 'left')
->order('p.id DESC,s.id DESC')
->where(['p.id' => $userId])
->group('p.identifier')
->find();
//流量池筛选
$package = Db::name('traffic_source_package_item')->alias('tspi')
->join('traffic_source_package p', 'tspi.packageId=p.id AND tspi.companyId=p.companyId')
->where(['tspi.companyId' => $item->companyId, 'tspi.identifier' => $item->identifier])
->column('p.name');
$package2 = Db::name('traffic_source_package_item')->alias('tspi')
->join('traffic_source_package p', 'tspi.packageId=p.id')
->where(['tspi.companyId' => $item->companyId, 'tspi.identifier' => $item->identifier, 'p.isSys' => 1])
->column('p.name');
$packages = array_merge($package, $package2);
$item['packages'] = $packages;
if ($item->type == 1) {
$tag = Db::name('wechat_friendship')->where(['wechatId' => $item->wechatId])->column('tags');
$tags = [];
foreach ($tag as $k => $v) {
$v = json_decode($v, true);
if (!empty($v)) {
$tags = array_merge($tags, $v);
}
}
$item['tags'] = $tags;
}
2025-07-23 16:00:01 +08:00
2025-07-29 17:04:00 +08:00
}
2025-07-23 16:00:01 +08:00
/**
* 用户旅程
* @return false|string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getUserJourney()
{
2025-07-29 17:04:00 +08:00
$page = $this->request->param('page', 1);
$pageSize = $this->request->param('pageSize', 10);
$userId = $this->request->param('userId', '');
if (empty($userId)) {
2025-07-23 16:00:01 +08:00
return json_encode(['code' => 500, 'msg' => '用户id不能为空']);
}
$query = Db::name('user_portrait')
->field('id,type,trafficPoolId,remark,count,createTime,updateTime')
->where(['trafficPoolId' => $userId]);
$total = $query->count();
$list = $query->order('createTime desc')
2025-07-29 17:04:00 +08:00
->page($page, $pageSize)
2025-07-23 16:00:01 +08:00
->select();
2025-07-29 17:04:00 +08:00
foreach ($list as $k => $v) {
$list[$k]['createTime'] = date('Y-m-d H:i:s', $v['createTime']);
$list[$k]['updateTime'] = date('Y-m-d H:i:s', $v['updateTime']);
2025-07-23 16:00:01 +08:00
}
2025-07-29 17:04:00 +08:00
return ResponseHelper::success(['list' => $list, 'total' => $total]);
}
2025-07-23 16:00:01 +08:00
2025-07-29 17:04:00 +08:00
public function getUserTags()
{
$userId = $this->request->param('userId', '');
if (empty($userId)) {
return json_encode(['code' => 500, 'msg' => '用户id不能为空']);
}
$data = Db::name('traffic_pool')->alias('tp')
->join(['s2_wechat_friend' => 'wf'], 'tp.wechatId=wf.wechatId', 'left')
->where(['tp.id' => $userId])
->order('tp.createTime desc')
->column('wf.id,wf.labels,wf.siteLabels');
$tags = [];
$siteLabels = [];
foreach ($data as $k => $v) {
$tag = json_decode($v['labels'], true);
$tag2 = json_decode($v['siteLabels'], true);
if (!empty($tag)) {
$tags = array_merge($tags, $tag);
}
if (!empty($tag2)) {
$siteLabels = array_merge($siteLabels, $tag2);
}
}
$tags = array_unique($tags);
$tags = array_values($tags);
$siteLabels = array_unique($siteLabels);
$siteLabels = array_values($siteLabels);
return ResponseHelper::success(['wechat' => $tags, 'siteLabels' => $siteLabels]);
2025-07-23 16:00:01 +08:00
}
2025-07-29 17:04:00 +08:00
}