微信号

This commit is contained in:
柳清爽
2025-04-01 17:36:32 +08:00
parent 850d85ea4b
commit a2c9078ccf
7 changed files with 986 additions and 157 deletions

View File

@@ -0,0 +1,131 @@
<?php
namespace app\common\model;
use think\Model;
/**
* 公司账户模型类
*/
class CompanyAccount extends Model
{
/**
* 数据表名
* @var string
*/
protected $table = 'tk_company_account';
/**
* 主键
* @var string
*/
protected $pk = 'id';
/**
* 自动写入时间戳
* @var bool
*/
protected $autoWriteTimestamp = true;
/**
* 创建时间字段
* @var string
*/
protected $createTime = 'createTime';
/**
* 更新时间字段
* @var string
*/
protected $updateTime = 'updateTime';
/**
* 隐藏属性
* @var array
*/
protected $hidden = ['passwordMd5', 'passwordLocal', 'secret'];
/**
* 字段类型
* @var array
*/
protected $type = [
'id' => 'integer',
'tenantId' => 'integer',
'accountType' => 'integer',
'companyId' => 'integer',
'useGoogleSecretKey' => 'boolean',
'hasVerifyGoogleSecret' => 'boolean',
'lastLoginTime' => 'integer',
'createTime' => 'integer',
'updateTime' => 'integer'
];
/**
* 获取公司账户信息
* @param string $userName 用户名
* @param string $password 密码MD5加密后的
* @return array|null
*/
public static function getAccount($userName, $password)
{
// 查询账户
$account = self::where('userName', $userName)
->find();
if (!$account) {
return null;
}
// 验证密码
if ($account->passwordMd5 !== $password) {
return null;
}
// 更新登录信息
$account->lastLoginIp = request()->ip();
$account->lastLoginTime = time();
$account->save();
return [
'id' => $account->id,
'tenantId' => $account->tenantId,
'userName' => $account->userName,
'realName' => $account->realName,
'nickname' => $account->nickname,
'avatar' => $account->avatar,
'accountType' => $account->accountType,
'companyId' => $account->companyId,
'lastLoginIp' => $account->lastLoginIp,
'lastLoginTime' => $account->lastLoginTime
];
}
/**
* 通过租户ID获取账户信息
* @param int $companyId 租户ID
* @return array|null
*/
public static function getAccountByCompanyId($companyId)
{
// 查询账户
$account = self::where('companyId', $companyId)->find();
if (!$account) {
return null;
}
return [
'id' => $account->id,
'tenantId' => $account->tenantId,
'userName' => $account->userName,
'realName' => $account->realName,
'nickname' => $account->nickname,
'avatar' => $account->avatar,
'accountType' => $account->accountType,
'companyId' => $account->companyId,
'lastLoginIp' => $account->lastLoginIp,
'lastLoginTime' => $account->lastLoginTime
];
}
}

View File

@@ -1,6 +1,7 @@
<?php
namespace app\devices\controller;
use app\common\model\CompanyAccount;
use think\Controller;
use app\devices\model\WechatAccount;
use think\facade\Request;
@@ -88,6 +89,8 @@ class DeviceWechat extends Controller
public function index()
{
try {
// 获取登录用户信息
$userInfo = request()->userInfo;
// 获取查询条件
$where = [];
@@ -102,7 +105,7 @@ class DeviceWechat extends Controller
if (!empty($nickname)) {
$where['nickname|accountNickname'] = ['like', "%{$nickname}%"];
}
// 获取分页参数
$page = (int)Request::param('page', 1);
$limit = (int)Request::param('limit', 10);
@@ -110,22 +113,74 @@ class DeviceWechat extends Controller
// 获取排序参数
$sort = Request::param('sort', 'id');
$order = Request::param('order', 'desc');
// 获取在线微信账号列表
$list = WechatAccount::getOnlineWechatList($where, "{$sort} {$order}", $page, $limit);
// 公司账户表没有 companyId需要转换一下
$acountInfo = CompanyAccount::getAccountByCompanyId($userInfo['companyId']);
// 先用账号进行查询
$where['accountUserName'] = $acountInfo['userName'];
// 根据用户权限不同实现不同的查询逻辑
if ($userInfo['isAdmin'] == 1) {
// 管理员直接查询tk_wechat_account表
$list = WechatAccount::getOnlineWechatList($where, "{$sort} {$order}", $page, $limit);
} else {
// 非管理员先查询tk_device_user表
$deviceIds = Db::table('tk_device_user')
->where('companyId', $userInfo['companyId'])
->where('userId', $userInfo['id'])
->column('deviceId');
if (empty($deviceIds)) {
// 如果没有绑定设备,返回提示信息
return json([
'code' => 403,
'msg' => '请联系管理员绑定设备微信',
'data' => [
'total' => 0,
'list' => []
]
]);
}
// 获取这些设备关联的微信ID
$wechatIds = Db::table('tk_device_wechat_login')
->where('companyId', $userInfo['companyId'])
->whereIn('deviceId', $deviceIds)
->column('wechatId');
if (empty($wechatIds)) {
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'total' => 0,
'list' => []
]
]);
}
// 将微信ID添加到查询条件中
$where['id'] = ['in', $wechatIds];
// 查询微信账号
$list = WechatAccount::getOnlineWechatList($where, "{$sort} {$order}", $page, $limit);
}
// 处理返回数据
$data = [];
foreach ($list->items() as $item) {
// 计算今日可添加好友数量(这里使用一个示例算法,你可以根据实际需求修改)
$canAddFriendCount = 30 - (isset($item['yesterdayMsgCount']) ? intval($item['yesterdayMsgCount']) : 0);
$canAddFriendCount = 20 - Db::table('tk_friend_task')->where('wechatId', $item['wechatId'])->count('*');
if ($canAddFriendCount < 0) {
$canAddFriendCount = 0;
}
// 计算今日新增好友数量(示例数据,实际需要从数据库获取或通过其他方式计算)
// 这里只是一个示例,你需要根据实际情况替换
$todayNewFriendCount = mt_rand(0, 10); // 随机生成0-10的数字作为示例
$todayNewFriendCount = Db::table('tk_friend_task')->where('wechatId', $item['wechatId'])
->where('status', 3)
->count('*');
$data[] = [
'id' => $item['id'],

View File

@@ -0,0 +1,324 @@
<?php
namespace app\devices\model;
use think\Model;
/**
* 添加好友任务记录模型
*/
class FriendTask extends Model
{
/**
* 数据表名
* @var string
*/
protected $table = 'tk_friend_task';
/**
* 主键
* @var string
*/
protected $pk = 'id';
/**
* 自动写入时间戳
* @var bool
*/
protected $autoWriteTimestamp = true;
/**
* 创建时间字段
* @var string
*/
protected $createTime = 'createTime';
/**
* 更新时间字段
* @var string
*/
protected $updateTime = 'updateTime';
/**
* 字段类型
* @var array
*/
protected $type = [
'id' => 'integer',
'tenantId' => 'integer',
'operatorAccountId' => 'integer',
'status' => 'integer',
'wechatAccountId' => 'integer',
'createTime' => 'integer',
'updateTime' => 'integer'
];
/**
* 状态常量
*/
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

@@ -97,8 +97,6 @@ class WechatAccount extends Model
public static function getOnlineWechatList($where = [], $order = 'id desc', $page = 1, $limit = 10)
{
$condition = [
'wechatAlive' => 1,
'deviceAlive' => 1,
'isDeleted' => 0
];