私域操盘手模块调整

This commit is contained in:
柳清爽
2025-04-09 10:35:28 +08:00
parent 9d6f62b670
commit 899bac425b
209 changed files with 88 additions and 40408 deletions

View File

@@ -0,0 +1,60 @@
<?php
namespace app\cunkebao\model;
use think\Model;
class BaseModel extends Model
{
// 数据插入 duplicate
public function insertAllDuplicate($data, $duplicate = null, $limit = null)
{
if (!empty($duplicate)) {
if (is_array($duplicate)) {
$temp = [];
foreach ($duplicate as $key => $item) {
if (is_integer($key)) {
$temp[] = "{$item} = VALUES({$item})";
} else {
$temp[] = "{$key} = {$item}";
}
}
$duplicate = implode(',', $temp);
}
}
if (!empty($limit)) {
// 分批写入 自动启动事务支持
$this->startTrans();
try {
// array_chunk 函数把数组分割为新的数组块。
//其中每个数组的单元数目由 size 参数决定。最后一个数组的单元数目可能会少几个。
//可选参数 preserve_key 是一个布尔值,它指定新数组的元素是否有和原数组相同的键(用于关联数组),还是从 0 开始的新数字键(用于索引数组)。默认是分配新的键。
$array = array_chunk($data, $limit, true);
$count = 0;
foreach ($array as $item) {
$sql = $this->fetchSql(true)->insertAll($item);
$sql = preg_replace("/INSERT\s*INTO\s*/", "INSERT IGNORE INTO ", $sql);
if (is_string($duplicate)) {
$sql = $sql . ' ON DUPLICATE KEY UPDATE ' . $duplicate;
}
$count += $this->execute($sql); // 获取影响函数
}
// 提交事务
$this->commit();
} catch (\Exception $e) {
$this->rollback();
throw $e;
}
return $count;
} else {
$sql = $this->fetchSql(true)->insertAll($data);
$sql = preg_replace("/INSERT\s*INTO\s*/", "INSERT IGNORE INTO ", $sql);
if (is_string($duplicate)) {
$sql = $sql . ' ON DUPLICATE KEY UPDATE ' . $duplicate;
}
return $this->execute($sql);
}
}
}

View File

@@ -0,0 +1,208 @@
<?php
namespace app\cunkebao\model;
use think\Model;
use think\Db;
/**
* 设备模型类
*/
class Device extends Model
{
// 设置表名
protected $name = 'device';
/**
* 获取设备总数
* @param array $where 查询条件
* @return int 设备总数
*/
public static function getDeviceCount($where = [])
{
// 默认只统计未删除的设备
if (!isset($where['isDeleted']) && !isset($where['d.isDeleted'])) {
$where['isDeleted'] = 0;
}
// 确定是否使用了表别名
$hasAlias = false;
foreach ($where as $key => $value) {
if (strpos($key, '.') !== false) {
$hasAlias = true;
break;
}
}
// 如果使用了表别名,则需要使用查询构造器
if ($hasAlias) {
return self::alias('d')->where($where)->count();
} else {
return self::where($where)->count();
}
}
/**
* 获取设备列表
* @param array $where 查询条件
* @param string $order 排序方式
* @param int $page 页码
* @param int $limit 每页数量
* @return \think\Paginator 分页对象
*/
public static function getDeviceList($where = [], $order = 'd.id desc', $page = 1, $limit = 10)
{
// 默认只查询未删除的设备
if (!isset($where['isDeleted'])) {
$where['d.isDeleted'] = 0;
}
// 构建查询对象
$query = self::alias('d')
->field(['d.id', 'd.imei', 'd.memo', 'w.wechatId', 'd.alive', 'w.totalFriend'])
->leftJoin('tk_wechat_account w', 'd.imei = w.imei COLLATE utf8mb4_unicode_ci');
// 处理查询条件
foreach ($where as $key => $value) {
// 处理特殊的exp表达式条件
if (is_numeric($key) && is_array($value) && isset($value[0]) && $value[0] === 'exp') {
// 直接添加原始SQL表达式
$query->whereExp('', $value[1]);
continue;
}
// 处理普通条件
$query->where($key, $value);
}
// 返回分页结果
return $query->order($order)
->paginate($limit, false, ['page' => $page]);
}
/**
* 获取设备详情
* @param int $id 设备ID
* @return array|null 设备信息
*/
public static function getDeviceInfo($id)
{
// 查询设备基础信息与关联的微信账号信息
$device = self::alias('d')
->field([
'd.id', 'd.imei', 'd.memo', 'd.alive', 'd.taskConfig', 'd.lastUpdateTime',
'w.id as wechatId', 'w.thirtyDayMsgCount', 'w.totalFriend', 'd.extra'
])
->leftJoin('tk_wechat_account w', 'd.imei = w.imei')
->where('d.id', $id)
->where('d.isDeleted', 0)
->find();
// 如果设备存在,处理额外信息
if ($device) {
// 解析电量信息
$battery = 0;
if (!empty($device['extra'])) {
$extra = json_decode($device['extra'], true);
if (is_array($extra) && isset($extra['battery'])) {
$battery = intval($extra['battery']);
}
}
$device['battery'] = $battery;
// 解析taskConfig字段获取功能开关
$features = [
'autoAddFriend' => false,
'autoReply' => false,
'contentSync' => false,
'aiChat' => false
];
if (!empty($device['taskConfig'])) {
$taskConfig = json_decode($device['taskConfig'], true);
if (is_array($taskConfig)) {
// 映射taskConfig中的字段到前端需要的features
$features['autoAddFriend'] = isset($taskConfig['autoAddFriend']) ? (bool)$taskConfig['autoAddFriend'] : false;
$features['autoReply'] = isset($taskConfig['autoReply']) ? (bool)$taskConfig['autoReply'] : false;
$features['contentSync'] = isset($taskConfig['momentsSync']) ? (bool)$taskConfig['momentsSync'] : false;
$features['aiChat'] = isset($taskConfig['aiChat']) ? (bool)$taskConfig['aiChat'] : false;
}
}
$device['features'] = $features;
unset($device['extra']);
unset($device['taskConfig']);
// 格式化最后活跃时间
$device['lastUpdateTime'] = !empty($device['lastUpdateTime']) ? date('Y-m-d H:i:s', strtotime($device['lastUpdateTime'])) : date('Y-m-d H:i:s');
// 确保totalFriend和thirtyDayMsgCount有值防止NULL
$device['totalFriend'] = intval($device['totalFriend'] ?? 0);
$device['thirtyDayMsgCount'] = intval($device['thirtyDayMsgCount'] ?? 0);
}
return $device;
}
/**
* 添加设备
* @param array $data 设备数据
* @return int 新增设备ID
*/
public static function addDevice($data)
{
$device = new self();
$device->allowField(true)->save($data);
return $device->id;
}
/**
* 更新设备
* @param int $id 设备ID
* @param array $data 设备数据
* @return bool 更新结果
*/
public static function updateDevice($id, $data)
{
return self::where('id', $id)
->where('isDeleted', 0)
->update($data);
}
/**
* 删除设备(软删除)
* @param int $id 设备ID
* @return bool 删除结果
*/
public static function deleteDevice($id)
{
return self::where('id', $id)
->update([
'isDeleted' => 1,
'deleteTime' => date('Y-m-d H:i:s', time())
]);
}
/**
* 按设备品牌统计数量
* @return array 统计结果
*/
public static function countByBrand()
{
return self::where('isDeleted', 0)
->group('brand')
->field('brand, count(*) as count')
->select();
}
/**
* 按设备在线状态统计数量
* @return array 统计结果
*/
public static function countByStatus()
{
return self::where('isDeleted', 0)
->group('alive')
->field('alive, count(*) as count')
->select();
}
}

View File

@@ -0,0 +1,100 @@
<?php
namespace app\cunkebao\model;
use think\Model;
/**
* 设备操作日志模型类
*/
class DeviceHandleLog extends Model
{
// 设置表名
protected $name = 'device_handle_log';
/**
* 添加设备操作日志
* @param array $data 日志数据
* @return int 新增日志ID
*/
public static function addLog($data)
{
$log = new self();
$log->allowField(true)->save($data);
return $log->id;
}
/**
* 获取设备操作日志列表
* @param array $where 查询条件
* @param string $order 排序方式
* @param int $page 页码
* @param int $limit 每页数量
* @return \think\Paginator 分页对象
*/
public static function getLogList($where = [], $order = 'createTime desc', $page = 1, $limit = 10)
{
return self::where($where)
->order($order)
->paginate($limit, false, ['page' => $page]);
}
/**
* 根据IMEI获取设备操作日志
* @param string $imei 设备IMEI
* @param int $companyId 租户ID
* @param int $limit 获取条数
* @return array 日志记录
*/
public static function getLogsByImei($imei, $companyId = null, $limit = 10)
{
$query = self::where('imei', $imei);
if ($companyId !== null) {
$query->where('companyId', $companyId);
}
return $query->order('createTime', 'desc')
->limit($limit)
->select();
}
/**
* 根据用户ID获取操作日志
* @param int $userId 用户ID
* @param int $companyId 租户ID
* @param int $page 页码
* @param int $limit 每页数量
* @return \think\Paginator 分页对象
*/
public static function getLogsByUser($userId, $companyId = null, $page = 1, $limit = 10)
{
$query = self::where('userId', $userId);
if ($companyId !== null) {
$query->where('companyId', $companyId);
}
return $query->order('createTime', 'desc')
->paginate($limit, false, ['page' => $page]);
}
/**
* 记录设备操作日志的便捷方法
* @param string $imei 设备IMEI
* @param int $userId 操作用户ID
* @param string $content 操作内容
* @param int $companyId 租户ID
* @return int 日志ID
*/
public static function recordLog($imei, $userId, $content, $companyId = null)
{
$data = [
'imei' => $imei,
'userId' => $userId,
'content' => $content,
'companyId' => $companyId
];
return self::addLog($data);
}
}

View File

@@ -0,0 +1,190 @@
<?php
namespace app\cunkebao\model;
use think\Model;
/**
* 设备微信登录记录模型类
*/
class DeviceWechatLogin extends Model
{
// 设置表名
protected $name = 'device_wechat_login';
/**
* 查询设备关联的微信ID列表
* @param int $deviceId 设备ID
* @param int $companyId 公司/租户ID
* @return array 微信ID列表
*/
public static function getDeviceWechatIds($deviceId, $companyId = null)
{
$query = self::where('deviceId', $deviceId);
// 如果提供了公司ID则添加对应的条件
if ($companyId !== null) {
$query->where('companyId', $companyId);
}
// 提取微信ID
$records = $query->select();
$wechatIds = [];
foreach ($records as $record) {
if (!empty($record['wechatId'])) {
$wechatIds[] = $record['wechatId'];
}
}
return $wechatIds;
}
/**
* 根据微信ID查询关联的设备
* @param string $wechatId 微信ID
* @param int $companyId 公司/租户ID
* @return array 设备ID列表
*/
public static function getWechatDeviceIds($wechatId, $companyId = null)
{
$query = self::where('wechatId', $wechatId);
// 如果提供了公司ID则添加对应的条件
if ($companyId !== null) {
$query->where('companyId', $companyId);
}
// 提取设备ID
$records = $query->select();
$deviceIds = [];
foreach ($records as $record) {
if (!empty($record['deviceId'])) {
$deviceIds[] = $record['deviceId'];
}
}
return $deviceIds;
}
/**
* 添加设备微信登录记录
* @param int $deviceId 设备ID
* @param string $wechatId 微信ID
* @param int $companyId 公司/租户ID
* @return int 新增记录ID
*/
public static function addRecord($deviceId, $wechatId, $companyId)
{
// 检查是否已存在相同记录
$exists = self::where('deviceId', $deviceId)
->where('wechatId', $wechatId)
->where('companyId', $companyId)
->find();
if ($exists) {
return $exists['id'];
}
// 创建新记录
$model = new self();
$model->deviceId = $deviceId;
$model->wechatId = $wechatId;
$model->companyId = $companyId;
$model->save();
return $model->id;
}
/**
* 删除设备微信登录记录
* @param int $deviceId 设备ID
* @param string $wechatId 微信ID为null时删除设备所有记录
* @param int $companyId 公司/租户ID为null时不限公司
* @return bool 删除结果
*/
public static function removeRecord($deviceId, $wechatId = null, $companyId = null)
{
$query = self::where('deviceId', $deviceId);
if ($wechatId !== null) {
$query->where('wechatId', $wechatId);
}
if ($companyId !== null) {
$query->where('companyId', $companyId);
}
return $query->delete();
}
/**
* 关联Device模型
* @return \think\model\relation\BelongsTo
*/
public function device()
{
return $this->belongsTo('Device', 'deviceId');
}
/**
* 获取设备关联的微信账号信息
* @param int $deviceId 设备ID
* @param int $companyId 公司/租户ID
* @return array 微信账号信息列表
*/
public static function getDeviceRelatedAccounts($deviceId, $companyId = null)
{
// 获取设备关联的微信ID列表
$wechatIds = self::getDeviceWechatIds($deviceId, $companyId);
if (empty($wechatIds)) {
return [];
}
// 查询微信账号信息
$accounts = \think\Db::name('wechat_account')
->alias('wa')
->field([
'wa.id',
'wa.wechatId',
'wa.accountNickname',
'wa.nickname',
'wa.accountUserName',
'wa.avatar',
'wa.gender',
'wa.wechatAlive',
'wa.status',
'wa.totalFriend',
'wa.createTime',
'wa.updateTime'
])
->whereIn('wa.wechatId', $wechatIds)
->where('wa.isDeleted', 0)
->select();
// 处理结果数据
$result = [];
foreach ($accounts as $account) {
// 计算最后活跃时间
$lastActive = date('Y-m-d H:i:s', max($account['updateTime'], $account['createTime']));
// 格式化数据
$result[] = [
'id' => $account['id'],
'wechatId' => $account['wechatId'],
'nickname' => $account['accountNickname'] ?: $account['nickname'] ?: '未命名微信',
'accountUserName' => $account['accountUserName'],
'avatar' => $account['avatar'],
'gender' => intval($account['gender']),
'status' => intval($account['status']),
'statusText' => intval($account['status']) === 1 ? '可加友' : '已停用',
'wechatAlive' => intval($account['wechatAlive']),
'wechatAliveText' => intval($account['wechatAlive']) === 1 ? '正常' : '异常',
'totalFriend' => intval($account['totalFriend']),
'lastActive' => $lastActive
];
}
return $result;
}
}

View File

@@ -0,0 +1,286 @@
<?php
namespace app\cunkebao\model;
use think\Model;
/**
* 添加好友任务记录模型
*/
class FriendTask extends Model
{
/**
* 数据表名
* @var string
*/
protected $table = 'tk_friend_task';
/**
* 状态常量
*/
const STATUS_PENDING = 1; // 待处理
const STATUS_PROCESSING = 2; // 处理中
const STATUS_APPROVED = 3; // 已通过
const STATUS_REJECTED = 4; // 已拒绝
const STATUS_EXPIRED = 5; // 已过期
const STATUS_CANCELLED = 6; // 已取消
/**
* 获取状态文本
* @param int $status 状态码
* @return string 状态文本
*/
public static function getStatusText($status)
{
$statusMap = [
self::STATUS_PENDING => '待处理',
self::STATUS_PROCESSING => '处理中',
self::STATUS_APPROVED => '已通过',
self::STATUS_REJECTED => '已拒绝',
self::STATUS_EXPIRED => '已过期',
self::STATUS_CANCELLED => '已取消'
];
return isset($statusMap[$status]) ? $statusMap[$status] : '未知状态';
}
/**
* 获取好友任务列表
* @param array $where 查询条件
* @param string $order 排序条件
* @param int $page 页码
* @param int $limit 每页数量
* @return \think\Paginator
*/
public static function getTaskList($where = [], $order = 'createTime desc', $page = 1, $limit = 10)
{
return self::where($where)
->order($order)
->paginate($limit, false, ['page' => $page]);
}
/**
* 获取任务详情
* @param int $id 任务ID
* @return array|null
*/
public static function getTaskDetail($id)
{
return self::where('id', $id)->find();
}
/**
* 创建好友任务
* @param array $data 任务数据
* @return int|bool 任务ID或false
*/
public static function createTask($data)
{
// 确保必填字段存在
if (!isset($data['id'])) {
return false;
}
// 设置默认值
if (!isset($data['status'])) {
$data['status'] = self::STATUS_PENDING;
}
// 设置创建时间
$data['createTime'] = time();
$data['updateTime'] = time();
// 创建任务
$task = new self;
$task->allowField(true)->save($data);
return $task->id;
}
/**
* 更新任务信息
* @param int $id 任务ID
* @param array $data 更新数据
* @return bool
*/
public static function updateTask($id, $data)
{
// 更新时间
$data['updateTime'] = time();
return self::where('id', $id)->update($data);
}
/**
* 更新任务状态
* @param int $id 任务ID
* @param int $status 新状态
* @param string $remark 备注
* @return bool
*/
public static function updateTaskStatus($id, $status, $remark = '')
{
$data = [
'status' => $status,
'updateTime' => time()
];
if (!empty($remark)) {
$data['remark'] = $remark;
}
return self::where('id', $id)->update($data);
}
/**
* 取消任务
* @param int $id 任务ID
* @param string $remark 取消原因
* @return bool
*/
public static function cancelTask($id, $remark = '')
{
return self::updateTaskStatus($id, self::STATUS_CANCELLED, $remark);
}
/**
* 任务审批通过
* @param int $id 任务ID
* @param string $remark 备注信息
* @return bool
*/
public static function approveTask($id, $remark = '')
{
return self::updateTaskStatus($id, self::STATUS_APPROVED, $remark);
}
/**
* 任务拒绝
* @param int $id 任务ID
* @param string $remark 拒绝原因
* @return bool
*/
public static function rejectTask($id, $remark = '')
{
return self::updateTaskStatus($id, self::STATUS_REJECTED, $remark);
}
/**
* 根据微信账号ID获取任务列表
* @param int $wechatAccountId 微信账号ID
* @param array $status 状态数组,默认查询所有状态
* @param int $page 页码
* @param int $limit 每页数量
* @return \think\Paginator
*/
public static function getTasksByWechatAccount($wechatAccountId, $status = [], $page = 1, $limit = 10)
{
$where = ['wechatAccountId' => $wechatAccountId];
if (!empty($status)) {
$where['status'] = ['in', $status];
}
return self::getTaskList($where, 'createTime desc', $page, $limit);
}
/**
* 根据操作账号ID获取任务列表
* @param int $operatorAccountId 操作账号ID
* @param array $status 状态数组,默认查询所有状态
* @param int $page 页码
* @param int $limit 每页数量
* @return \think\Paginator
*/
public static function getTasksByOperator($operatorAccountId, $status = [], $page = 1, $limit = 10)
{
$where = ['operatorAccountId' => $operatorAccountId];
if (!empty($status)) {
$where['status'] = ['in', $status];
}
return self::getTaskList($where, 'createTime desc', $page, $limit);
}
/**
* 根据手机号/微信号查询任务
* @param string $phone 手机号/微信号
* @param int $tenantId 租户ID
* @return array
*/
public static function getTasksByPhone($phone, $tenantId = null)
{
$where = ['phone' => $phone];
if ($tenantId !== null) {
$where['tenantId'] = $tenantId;
}
return self::where($where)->select();
}
/**
* 获取统计数据
* @param int $tenantId 租户ID
* @param int $timeRange 时间范围(秒)
* @return array
*/
public static function getTaskStats($tenantId = null, $timeRange = 86400)
{
$where = [];
if ($tenantId !== null) {
$where['tenantId'] = $tenantId;
}
// 时间范围
$startTime = time() - $timeRange;
$where['createTime'] = ['>=', $startTime];
// 获取各状态的任务数量
$stats = [
'total' => self::where($where)->count(),
'pending' => self::where(array_merge($where, ['status' => self::STATUS_PENDING]))->count(),
'processing' => self::where(array_merge($where, ['status' => self::STATUS_PROCESSING]))->count(),
'approved' => self::where(array_merge($where, ['status' => self::STATUS_APPROVED]))->count(),
'rejected' => self::where(array_merge($where, ['status' => self::STATUS_REJECTED]))->count(),
'expired' => self::where(array_merge($where, ['status' => self::STATUS_EXPIRED]))->count(),
'cancelled' => self::where(array_merge($where, ['status' => self::STATUS_CANCELLED]))->count()
];
return $stats;
}
/**
* 任务处理结果统计
* @param int $tenantId 租户ID
* @param int $timeRange 时间范围(秒)
* @return array
*/
public static function getTaskResultStats($tenantId = null, $timeRange = 86400 * 30)
{
$where = [];
if ($tenantId !== null) {
$where['tenantId'] = $tenantId;
}
// 时间范围
$startTime = time() - $timeRange;
$where['createTime'] = ['>=', $startTime];
// 获取处理结果数据
$stats = [
'total' => self::where($where)->count(),
'approved' => self::where(array_merge($where, ['status' => self::STATUS_APPROVED]))->count(),
'rejected' => self::where(array_merge($where, ['status' => self::STATUS_REJECTED]))->count(),
'pending' => self::where(array_merge($where, ['status' => ['in', [self::STATUS_PENDING, self::STATUS_PROCESSING]]]))->count(),
'other' => self::where(array_merge($where, ['status' => ['in', [self::STATUS_EXPIRED, self::STATUS_CANCELLED]]]))->count()
];
// 计算成功率
$stats['approvalRate'] = $stats['total'] > 0 ? round($stats['approved'] / $stats['total'] * 100, 2) : 0;
return $stats;
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace app\cunkebao\model;
use think\Model;
/**
* 获客场景模型类
*/
class PlanScene extends Model
{
// 设置表名
protected $name = 'plan_scene';
/**
* 获取场景列表
*
* @param array $where 查询条件
* @param string $order 排序
* @param int $page 页码
* @param int $limit 每页数量
* @return array 场景列表和总数
*/
public static function getSceneList($where = [], $order = 'id desc', $page = 1, $limit = 10)
{
// 构建查询
$query = self::where($where);
// 计算总数
$total = $query->count();
// 分页查询数据
$list = $query->page($page, $limit)
->order($order)
->select();
return [
'list' => $list,
'total' => $total,
'page' => $page,
'limit' => $limit
];
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace app\cunkebao\model;
use think\Model;
/**
* 获客计划任务模型
*/
class PlanTask extends Model
{
// 设置表名
protected $name = 'plan_task';
}

View File

@@ -0,0 +1,11 @@
<?php
namespace app\cunkebao\model;
/**
* 流量池模型
*/
class TrafficPool extends BaseModel
{
// 设置表名
protected $name = 'traffic_pool';
}

View File

@@ -0,0 +1,11 @@
<?php
namespace app\cunkebao\model;
/**
* 流量来源模型
*/
class TrafficSource extends BaseModel
{
// 设置表名
protected $name = 'traffic_source';
}

View File

@@ -0,0 +1,32 @@
<?php
namespace app\cunkebao\model;
use think\Model;
/**
* 流量标签模型
*/
class TrafficTag extends Model
{
// 设置表名
protected $name = 'traffic_tag';
/**
* 获取标签列表,支持分页和搜索
*
* @param array $where 查询条件
* @param string $order 排序方式
* @param int $page 页码
* @param int $limit 每页数量
* @return \think\Paginator 分页对象
*/
public static function getTagsByCompany($where = [], $order = 'id desc', $page = 1, $limit = 200)
{
return self::where($where)
->where('deleteTime', 0) // 只查询未删除的记录
->order($order)
->paginate($limit, false, [
'page' => $page
]);
}
}

View File

@@ -0,0 +1,97 @@
<?php
namespace app\cunkebao\model;
use think\Model;
/**
* 微信账号模型类
*/
class WechatAccount extends Model
{
// 设置表名
protected $name = 'wechat_account';
/**
* 获取在线微信账号数量
*
* @param array $where 额外的查询条件
* @return int 微信账号数量
*/
public static function getOnlineWechatCount($where = [])
{
$condition = [
'deviceAlive' => 1,
'wechatAlive' => 1,
'isDeleted' => 0
];
// 合并额外条件
if (!empty($where)) {
$condition = array_merge($condition, $where);
}
return self::where($condition)->count();
}
/**
* 获取有登录微信的设备数量
*
* @param array $where 额外的查询条件
* @return int 设备数量
*/
public static function getDeviceWithWechatCount($where = [])
{
$condition = [
'deviceAlive' => 1,
'isDeleted' => 0
];
// 合并额外条件
if (!empty($where)) {
$condition = array_merge($condition, $where);
}
return self::where($condition)->count();
}
/**
* 获取在线微信账号列表
*
* @param array $where 额外的查询条件
* @param string $order 排序方式
* @param int $page 页码
* @param int $limit 每页数量
* @return \think\Paginator 分页对象
*/
public static function getOnlineWechatList($where = [], $order = 'id desc', $page = 1, $limit = 10)
{
$condition = [
'isDeleted' => 0
];
// 合并额外条件
if (!empty($where)) {
$condition = array_merge($condition, $where);
}
return self::where($condition)
->field([
'id',
'wechatId',
'accountNickname',
'nickname',
'accountUserName',
'avatar',
'wechatAlive',
'deviceAlive',
'totalFriend',
'maleFriend',
'femaleFriend',
'imei',
'deviceMemo',
'yesterdayMsgCount'
])
->order($order)
->paginate($limit, false, ['page' => $page]);
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace app\cunkebao\model;
use think\Model;
/**
* 微信好友模型类
*/
class WechatFriend extends Model
{
// 设置表名
protected $name = 'wechat_friend';
/**
* 根据微信账号ID获取好友列表
*
* @param string $ownerWechatId 所有者微信ID
* @param array $params 查询条件参数
* @param int $page 页码
* @param int $limit 每页数量
* @return array 好友列表和总数
*/
public static function getFriendsByWechatId($ownerWechatId, $params = [], $page = 1, $limit = 20)
{
// 构建基础查询
$query = self::where('ownerWechatId', $ownerWechatId)
->where('isDeleted', 0);
// 添加筛选条件(昵称、备注、微信号、标签)
if (!empty($params['keyword'])) {
$keyword = $params['keyword'];
$query->where(function($q) use ($keyword) {
$q->whereOr('nickname', 'like', "%{$keyword}%")
->whereOr('conRemark', 'like', "%{$keyword}%")
->whereOr('alias', 'like', "%{$keyword}%")
->whereOr("JSON_SEARCH(labels, 'one', '%{$keyword}%') IS NOT NULL");
});
}
// 计算总数
$total = $query->count();
// 分页查询数据
$friends = $query->page($page, $limit)
->order('createTime desc')
->field('wechatId, alias, avatar, labels, accountNickname, accountRealName, nickname, conRemark, gender, region')
->select();
return [
'list' => $friends,
'total' => $total,
'page' => $page,
'limit' => $limit
];
}
}