设备关联微信
This commit is contained in:
@@ -10,6 +10,7 @@ Route::group('v1/', function () {
|
||||
|
||||
// 设备管理相关
|
||||
Route::group('devices', function () {
|
||||
Route::get(':id/related-accounts', 'app\\devices\\controller\\Device@getRelatedAccounts'); // 设备关联微信账号路由
|
||||
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 +29,6 @@ Route::group('v1/', function () {
|
||||
Route::put('refresh', 'app\\devices\\controller\\DeviceWechat@refresh'); // 刷新设备微信状态
|
||||
Route::post('transfer-friends', 'app\\devices\\controller\\DeviceWechat@transferFriends'); // 微信好友转移
|
||||
});
|
||||
|
||||
|
||||
})->middleware(['jwt']);
|
||||
@@ -181,12 +181,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 +271,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 +293,7 @@ class Device extends Controller
|
||||
|
||||
// 验证IMEI是否已存在
|
||||
$exists = DeviceModel::where('imei', $data['imei'])->where('isDeleted', 0)->find();
|
||||
|
||||
if ($exists) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
@@ -314,10 +303,12 @@ class Device extends Controller
|
||||
|
||||
// 设置设备公司ID
|
||||
$data['companyId'] = $userInfo['companyId'];
|
||||
|
||||
$data['id'] = time();
|
||||
|
||||
// 添加设备
|
||||
$id = DeviceModel::addDevice($data);
|
||||
|
||||
|
||||
// 此处调用底层API
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '添加成功',
|
||||
@@ -465,4 +456,72 @@ 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()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user