Merge branch 'develop' of https://gitee.com/Tyssen/yi-shi into develop
This commit is contained in:
131
Server/application/common/model/CompanyAccount.php
Normal file
131
Server/application/common/model/CompanyAccount.php
Normal 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
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,6 +10,8 @@ Route::group('v1/', function () {
|
||||
|
||||
// 设备管理相关
|
||||
Route::group('devices', function () {
|
||||
Route::get(':id/related-accounts', 'app\\devices\\controller\\Device@getRelatedAccounts'); // 设备关联微信账号路由
|
||||
Route::get(':id/handle-logs', 'app\\devices\\controller\\Device@handleLogs'); // 获取设备操作记录
|
||||
Route::get('', 'app\\devices\\controller\\Device@index'); // 获取设备列表
|
||||
Route::get('count', 'app\\devices\\controller\\Device@count'); // 获取设备总数
|
||||
Route::get(':id', 'app\\devices\\controller\\Device@read'); // 获取设备详情
|
||||
@@ -28,4 +30,6 @@ Route::group('v1/', function () {
|
||||
Route::put('refresh', 'app\\devices\\controller\\DeviceWechat@refresh'); // 刷新设备微信状态
|
||||
Route::post('transfer-friends', 'app\\devices\\controller\\DeviceWechat@transferFriends'); // 微信好友转移
|
||||
});
|
||||
|
||||
|
||||
})->middleware(['jwt']);
|
||||
@@ -1,8 +1,10 @@
|
||||
<?php
|
||||
namespace app\devices\controller;
|
||||
|
||||
use app\devices\model\DeviceHandleLog;
|
||||
use think\Controller;
|
||||
use app\devices\model\Device as DeviceModel;
|
||||
use think\Db;
|
||||
use think\facade\Request;
|
||||
use app\common\util\JwtUtil;
|
||||
|
||||
@@ -181,12 +183,6 @@ class Device extends Controller
|
||||
try {
|
||||
// 获取登录用户信息
|
||||
$userInfo = request()->userInfo;
|
||||
if (empty($userInfo)) {
|
||||
return json([
|
||||
'code' => 401,
|
||||
'msg' => '未登录或登录已过期'
|
||||
]);
|
||||
}
|
||||
|
||||
// 获取设备ID
|
||||
$id = Request::param('id/d');
|
||||
@@ -277,13 +273,7 @@ class Device extends Controller
|
||||
try {
|
||||
// 获取登录用户信息
|
||||
$userInfo = request()->userInfo;
|
||||
if (empty($userInfo)) {
|
||||
return json([
|
||||
'code' => 401,
|
||||
'msg' => '未登录或登录已过期'
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
// 检查用户权限,只有管理员可以添加设备
|
||||
if ($userInfo['isAdmin'] != 1) {
|
||||
return json([
|
||||
@@ -305,6 +295,7 @@ class Device extends Controller
|
||||
|
||||
// 验证IMEI是否已存在
|
||||
$exists = DeviceModel::where('imei', $data['imei'])->where('isDeleted', 0)->find();
|
||||
|
||||
if ($exists) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
@@ -314,10 +305,34 @@ class Device extends Controller
|
||||
|
||||
// 设置设备公司ID
|
||||
$data['companyId'] = $userInfo['companyId'];
|
||||
|
||||
// 添加设备
|
||||
$id = DeviceModel::addDevice($data);
|
||||
|
||||
$data['id'] = time();
|
||||
|
||||
try {
|
||||
Db::startTrans();
|
||||
|
||||
// 添加设备
|
||||
$id = DeviceModel::addDevice($data);
|
||||
|
||||
// 添加设备操作记录
|
||||
DeviceHandleLog::addLog(
|
||||
[
|
||||
'imei' => $data['imei'],
|
||||
'userId' => $userInfo['id'],
|
||||
'content' => '添加设备',
|
||||
'companyId' => $userInfo['companyId'],
|
||||
]
|
||||
);
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '添加失败:' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
|
||||
// 此处调用底层API
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '添加成功',
|
||||
@@ -342,12 +357,6 @@ class Device extends Controller
|
||||
try {
|
||||
// 获取登录用户信息
|
||||
$userInfo = request()->userInfo;
|
||||
if (empty($userInfo)) {
|
||||
return json([
|
||||
'code' => 401,
|
||||
'msg' => '未登录或登录已过期'
|
||||
]);
|
||||
}
|
||||
|
||||
// 检查用户权限,只有管理员可以删除设备
|
||||
if ($userInfo['isAdmin'] != 1) {
|
||||
@@ -405,6 +414,9 @@ class Device extends Controller
|
||||
{
|
||||
// 获取请求参数
|
||||
$data = $this->request->post();
|
||||
|
||||
// 获取登录用户信息
|
||||
$userInfo = request()->userInfo;
|
||||
|
||||
// 验证参数
|
||||
if (empty($data['id'])) {
|
||||
@@ -445,14 +457,53 @@ class Device extends Controller
|
||||
if (!$hasUpdate) {
|
||||
return json(['code' => 200, 'msg' => '更新成功', 'data' => ['taskConfig' => $taskConfig]]);
|
||||
}
|
||||
|
||||
// 更新设备taskConfig字段
|
||||
$result = \app\devices\model\Device::where('id', $deviceId)
|
||||
->update([
|
||||
'taskConfig' => json_encode($taskConfig),
|
||||
'updateTime' => time()
|
||||
]);
|
||||
|
||||
try {
|
||||
Db::startTrans();
|
||||
|
||||
// 更新设备taskConfig字段
|
||||
$result = \app\devices\model\Device::where('id', $deviceId)
|
||||
->update([
|
||||
'taskConfig' => json_encode($taskConfig),
|
||||
'updateTime' => time()
|
||||
]);
|
||||
|
||||
if (isset($data['autoAddFriend'])) {
|
||||
$content = $data['autoAddFriend'] ? '开启自动添加好友' : '关闭自动添加好友';
|
||||
}
|
||||
|
||||
if (isset($data['autoReply'])) {
|
||||
$content = $data['autoReply'] ? '开启自动回复' : '关闭自动回复';
|
||||
}
|
||||
|
||||
if (isset($data['momentsSync'])) {
|
||||
$content = $data['momentsSync'] ? '开启朋友圈同步' : '关闭朋友圈同步';
|
||||
}
|
||||
|
||||
if (isset($data['aiChat'])) {
|
||||
$content = $data['aiChat'] ? '开启AI会话' : '关闭AI会话';
|
||||
}
|
||||
|
||||
// 添加设备操作记录
|
||||
DeviceHandleLog::addLog(
|
||||
[
|
||||
'imei' => $device['imei'],
|
||||
'deviceId' => $deviceId,
|
||||
'userId' => $userInfo['id'],
|
||||
'content' => $content,
|
||||
'companyId' => $userInfo['companyId'],
|
||||
]
|
||||
);
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '更新任务配置失败'
|
||||
]);
|
||||
}
|
||||
|
||||
if ($result) {
|
||||
return json([
|
||||
'code' => 200,
|
||||
@@ -465,4 +516,166 @@ class Device extends Controller
|
||||
return json(['code' => 500, 'msg' => '更新任务配置失败']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备关联的微信账号
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getRelatedAccounts()
|
||||
{
|
||||
try {
|
||||
// 获取登录用户信息
|
||||
$userInfo = request()->userInfo;
|
||||
|
||||
// 获取设备ID
|
||||
$deviceId = $this->request->param('id/d');
|
||||
if (empty($deviceId)) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '设备ID不能为空'
|
||||
]);
|
||||
}
|
||||
|
||||
// 检查用户是否有权限访问该设备
|
||||
if ($userInfo['isAdmin'] != 1) {
|
||||
// 非管理员需要检查是否有权限访问该设备
|
||||
$hasPermission = \app\common\model\DeviceUser::checkUserDevicePermission(
|
||||
$userInfo['id'],
|
||||
$deviceId,
|
||||
$userInfo['companyId']
|
||||
);
|
||||
|
||||
if (!$hasPermission) {
|
||||
return json([
|
||||
'code' => 403,
|
||||
'msg' => '您没有权限查看该设备'
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取设备信息,确认设备存在
|
||||
$device = DeviceModel::where('id', $deviceId)
|
||||
->where('isDeleted', 0)
|
||||
->find();
|
||||
|
||||
if (!$device) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '设备不存在或已删除'
|
||||
]);
|
||||
}
|
||||
|
||||
// 获取设备关联的微信账号
|
||||
$wechatAccounts = \app\devices\model\DeviceWechatLogin::getDeviceRelatedAccounts($deviceId, $userInfo['companyId']);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
'data' => [
|
||||
'deviceId' => $deviceId,
|
||||
'accounts' => $wechatAccounts,
|
||||
'total' => count($wechatAccounts)
|
||||
]
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '获取失败:' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备操作记录
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function handleLogs()
|
||||
{
|
||||
try {
|
||||
// 获取登录用户信息
|
||||
$userInfo = request()->userInfo;
|
||||
|
||||
// 获取设备ID
|
||||
$deviceId = $this->request->param('id/d');
|
||||
if (empty($deviceId)) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '设备ID不能为空'
|
||||
]);
|
||||
}
|
||||
|
||||
// 检查用户是否有权限访问该设备
|
||||
if ($userInfo['isAdmin'] != 1) {
|
||||
// 非管理员需要检查是否有权限访问该设备
|
||||
$hasPermission = \app\common\model\DeviceUser::checkUserDevicePermission(
|
||||
$userInfo['id'],
|
||||
$deviceId,
|
||||
$userInfo['companyId']
|
||||
);
|
||||
|
||||
if (!$hasPermission) {
|
||||
return json([
|
||||
'code' => 403,
|
||||
'msg' => '您没有权限查看该设备'
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取设备信息,确认设备存在
|
||||
$device = DeviceModel::where('id', $deviceId)
|
||||
->where('isDeleted', 0)
|
||||
->find();
|
||||
|
||||
if (!$device) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '设备不存在或已删除'
|
||||
]);
|
||||
}
|
||||
|
||||
// 获取分页参数
|
||||
$page = (int)Request::param('page', 1);
|
||||
$limit = (int)Request::param('limit', 10);
|
||||
|
||||
// 查询设备操作记录,并关联用户表获取操作人信息
|
||||
$logs = Db::table('tk_device_handle_log')
|
||||
->alias('l')
|
||||
->join('tk_users u', 'l.userId = u.id', 'left')
|
||||
->where('l.imei', $device['imei'])
|
||||
->where('l.companyId', $userInfo['companyId'])
|
||||
->field([
|
||||
'l.id',
|
||||
'l.content',
|
||||
'l.createTime',
|
||||
'u.username'
|
||||
])
|
||||
->order('l.createTime desc')
|
||||
->paginate($limit, false, ['page' => $page]);
|
||||
|
||||
// 格式化返回数据
|
||||
$items = [];
|
||||
foreach ($logs as $log) {
|
||||
$items[] = [
|
||||
'id' => $log['id'],
|
||||
'content' => $log['content'],
|
||||
'username' => $log['username'] ? $log['username'] : '未知用户',
|
||||
'createTime' => $log['createTime']
|
||||
];
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
'data' => [
|
||||
'total' => $logs->total(),
|
||||
'list' => $items
|
||||
]
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '获取失败:' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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'],
|
||||
|
||||
121
Server/application/devices/model/DeviceHandleLog.php
Normal file
121
Server/application/devices/model/DeviceHandleLog.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
namespace app\devices\model;
|
||||
|
||||
use think\Model;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 设备操作日志模型类
|
||||
*/
|
||||
class DeviceHandleLog extends Model
|
||||
{
|
||||
// 设置表名
|
||||
protected $name = 'device_handle_log';
|
||||
protected $prefix = 'tk_';
|
||||
|
||||
// 设置主键
|
||||
protected $pk = 'id';
|
||||
|
||||
// 自动写入时间戳
|
||||
protected $autoWriteTimestamp = 'datetime';
|
||||
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createTime';
|
||||
protected $updateTime = false;
|
||||
|
||||
// 定义字段类型
|
||||
protected $type = [
|
||||
'id' => 'integer',
|
||||
'userId' => 'integer',
|
||||
'deviceId' => 'integer',
|
||||
'companyId' => 'integer',
|
||||
'createTime' => 'datetime'
|
||||
];
|
||||
|
||||
/**
|
||||
* 添加设备操作日志
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
207
Server/application/devices/model/DeviceWechatLogin.php
Normal file
207
Server/application/devices/model/DeviceWechatLogin.php
Normal file
@@ -0,0 +1,207 @@
|
||||
<?php
|
||||
namespace app\devices\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 设备微信登录记录模型类
|
||||
*/
|
||||
class DeviceWechatLogin extends Model
|
||||
{
|
||||
// 设置表名
|
||||
protected $name = 'device_wechat_login';
|
||||
|
||||
// 设置主键
|
||||
protected $pk = 'id';
|
||||
|
||||
// 自动写入时间戳
|
||||
protected $autoWriteTimestamp = 'int';
|
||||
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createTime';
|
||||
|
||||
// 定义字段类型
|
||||
protected $type = [
|
||||
'id' => 'integer',
|
||||
'deviceId' => 'integer',
|
||||
'companyId' => 'integer',
|
||||
'createTime' => 'integer'
|
||||
];
|
||||
|
||||
/**
|
||||
* 查询设备关联的微信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;
|
||||
}
|
||||
}
|
||||
324
Server/application/devices/model/FriendTask.php
Normal file
324
Server/application/devices/model/FriendTask.php
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,8 @@ class WechatAccount extends Model
|
||||
'gender' => 'integer',
|
||||
'currentDeviceId' => 'integer',
|
||||
'isDeleted' => 'integer',
|
||||
'groupId' => 'integer'
|
||||
'groupId' => 'integer',
|
||||
'status' => 'integer'
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -96,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
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user