操盘手端 - 设备关联账号返工
This commit is contained in:
@@ -10,7 +10,7 @@ Route::group('v1/', function () {
|
|||||||
|
|
||||||
// 设备管理相关
|
// 设备管理相关
|
||||||
Route::group('devices', function () {
|
Route::group('devices', function () {
|
||||||
Route::get(':id/related-accounts', 'app\\cunkebao\\controller\\Device@getRelatedAccounts'); // 设备关联微信账号路由
|
Route::get(':id/related-accounts', 'app\\cunkebao\\controller\\device\\GetRelatedAccountsV1Controller@index'); // 设备关联微信账号路由
|
||||||
Route::get(':id/handle-logs', 'app\\cunkebao\\controller\\Device@handleLogs'); // 获取设备操作记录
|
Route::get(':id/handle-logs', 'app\\cunkebao\\controller\\Device@handleLogs'); // 获取设备操作记录
|
||||||
Route::get('', 'app\\cunkebao\\controller\\device\\GetDeviceListV1Controller@index'); // 获取设备列表
|
Route::get('', 'app\\cunkebao\\controller\\device\\GetDeviceListV1Controller@index'); // 获取设备列表
|
||||||
Route::get('count', 'app\\cunkebao\\controller\\Device@count'); // 获取设备总数
|
Route::get('count', 'app\\cunkebao\\controller\\Device@count'); // 获取设备总数
|
||||||
|
|||||||
@@ -23,8 +23,6 @@ class BaseController extends Controller
|
|||||||
parent::initialize();
|
parent::initialize();
|
||||||
|
|
||||||
date_default_timezone_set('Asia/Shanghai');
|
date_default_timezone_set('Asia/Shanghai');
|
||||||
|
|
||||||
$this->user = request()->userInfo;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -36,10 +34,12 @@ class BaseController extends Controller
|
|||||||
*/
|
*/
|
||||||
protected function getUserInfo(string $column = '')
|
protected function getUserInfo(string $column = '')
|
||||||
{
|
{
|
||||||
if (!$this->user) {
|
$user = $this->request->userInfo;
|
||||||
|
|
||||||
|
if (!$user) {
|
||||||
throw new \Exception('未授权访问,缺少有效的身份凭证', 401);
|
throw new \Exception('未授权访问,缺少有效的身份凭证', 401);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $column ? $this->user[$column] : $this->user;
|
return $column ? $user[$column] : $user;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,6 @@ use app\common\model\Device as DeviceModel;
|
|||||||
use app\common\model\DeviceUser as DeviceUserModel;
|
use app\common\model\DeviceUser as DeviceUserModel;
|
||||||
use app\common\model\WechatFriend;
|
use app\common\model\WechatFriend;
|
||||||
use app\cunkebao\controller\BaseController;
|
use app\cunkebao\controller\BaseController;
|
||||||
use think\facade\Request;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设备管理控制器
|
* 设备管理控制器
|
||||||
@@ -23,12 +22,12 @@ class GetDeviceListV1Controller extends BaseController
|
|||||||
$where = [];
|
$where = [];
|
||||||
|
|
||||||
// 关键词搜索(同时搜索IMEI和备注)
|
// 关键词搜索(同时搜索IMEI和备注)
|
||||||
if (!empty($keyword = Request::param('keyword'))) {
|
if (!empty($keyword = $this->request->param('keyword'))) {
|
||||||
$where[] = ['exp', "d.imei LIKE '%{$keyword}%' OR d.memo LIKE '%{$keyword}%'"];
|
$where[] = ['exp', "d.imei LIKE '%{$keyword}%' OR d.memo LIKE '%{$keyword}%'"];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设备在线状态
|
// 设备在线状态
|
||||||
if (is_numeric($alive = Request::param('alive'))) {
|
if (is_numeric($alive = $this->request->param('alive'))) {
|
||||||
$where['d.alive'] = $alive;
|
$where['d.alive'] = $alive;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,8 +115,8 @@ class GetDeviceListV1Controller extends BaseController
|
|||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$page = (int)Request::param('page', 1);
|
$page = (int)$this->request->param('page', 1);
|
||||||
$limit = (int)Request::param('limit', 10);
|
$limit = (int)$this->request->param('limit', 10);
|
||||||
|
|
||||||
if ($this->getUserInfo('isAdmin') == 1) {
|
if ($this->getUserInfo('isAdmin') == 1) {
|
||||||
$where = $this->makeWhere();
|
$where = $this->makeWhere();
|
||||||
|
|||||||
@@ -0,0 +1,173 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\cunkebao\controller\device;
|
||||||
|
|
||||||
|
use app\common\model\DeviceUser as DeviceUserModel;
|
||||||
|
use app\common\model\DeviceWechatLogin as DeviceWechatLoginModel;
|
||||||
|
use app\common\model\WechatAccount as WechatAccountModel;
|
||||||
|
use app\common\model\WechatFriend;
|
||||||
|
use app\cunkebao\controller\BaseController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备管理控制器
|
||||||
|
*/
|
||||||
|
class GetRelatedAccountsV1Controller extends BaseController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 检查用户是否有权限操作指定设备
|
||||||
|
*
|
||||||
|
* @param int $deviceId
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function checkUserDevicePermission(int $deviceId): void
|
||||||
|
{
|
||||||
|
$where = [
|
||||||
|
'deviceId' => $deviceId,
|
||||||
|
'userId' => $this->getUserInfo('id'),
|
||||||
|
'companyId' => $this->getUserInfo('companyId')
|
||||||
|
];
|
||||||
|
|
||||||
|
$hasPermission = DeviceUserModel::where($where)->count() > 0;
|
||||||
|
|
||||||
|
if (!$hasPermission) {
|
||||||
|
throw new \Exception('您没有权限查看该设备', '403');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询设备关联的微信ID列表
|
||||||
|
*
|
||||||
|
* @param int $deviceId 设备ID
|
||||||
|
* @return array 微信ID列表
|
||||||
|
*/
|
||||||
|
protected function getDeviceWechatIds(int $deviceId): array
|
||||||
|
{
|
||||||
|
$where = [
|
||||||
|
'deviceId' => $deviceId,
|
||||||
|
'companyId' => $this->getUserInfo('companyId')
|
||||||
|
];
|
||||||
|
|
||||||
|
return DeviceWechatLoginModel::where($where)->group('wechatId')->column('wechatId');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过设备关联的微信号列表获取微信账号
|
||||||
|
*
|
||||||
|
* @param array $wechatIds
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getWechatAccountsByIds(array $wechatIds): array
|
||||||
|
{
|
||||||
|
return (array)WechatAccountModel::alias('a')
|
||||||
|
->field([
|
||||||
|
'a.wechatId', 'a.nickname', 'a.avatar', 'a.gender', 'a.createTime'
|
||||||
|
])
|
||||||
|
->whereIn('a.wechatId', $wechatIds)
|
||||||
|
->select()
|
||||||
|
->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过微信id获取微信最后活跃时间
|
||||||
|
*
|
||||||
|
* @param int $wechatId
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getWechatLastActiveTime($wechatId): string
|
||||||
|
{
|
||||||
|
return date('Y-m-d H:i:s', $wechatId ?: time());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加友状态
|
||||||
|
*
|
||||||
|
* @param string $wechatId
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getWechatStatusText(string $wechatId): string
|
||||||
|
{
|
||||||
|
return 1 ? '可加友' : '已停用';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 账号状态
|
||||||
|
*
|
||||||
|
* @param string $wechatId
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getWechatAliveText(string $wechatId): string
|
||||||
|
{
|
||||||
|
return 1 ? '正常' : '异常';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 统计微信好友
|
||||||
|
*
|
||||||
|
* @param string $wechatId
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
protected function countFriend(string $wechatId): int
|
||||||
|
{
|
||||||
|
return WechatFriend::where(['ownerWechatId' => $wechatId])->count();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取设备关联的微信账号信息
|
||||||
|
*
|
||||||
|
* @param int $deviceId 设备ID
|
||||||
|
* @return array 微信账号信息列表
|
||||||
|
*/
|
||||||
|
protected function getDeviceRelatedAccounts(int $deviceId)
|
||||||
|
{
|
||||||
|
// 获取设备关联的微信ID列表
|
||||||
|
$wechatIds = $this->getDeviceWechatIds($deviceId);
|
||||||
|
|
||||||
|
if (!empty($wechatIds)) {
|
||||||
|
$results = $this->getWechatAccountsByIds($wechatIds);
|
||||||
|
|
||||||
|
foreach ($results as &$account) {
|
||||||
|
$account['lastActive'] = $this->getWechatLastActiveTime($account['createTime']);
|
||||||
|
$account['statusText'] = $this->getWechatStatusText($account['wechatId']);
|
||||||
|
$account['totalFriend'] = $this->countFriend($account['wechatId']);
|
||||||
|
$account['wechatAliveText'] = $this->getWechatAliveText($account['wechatId']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $results;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取设备关联的微信账号
|
||||||
|
* @return \think\response\Json
|
||||||
|
*/
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$deviceId = $this->request->param('id/d');
|
||||||
|
|
||||||
|
if ($this->getUserInfo('isAdmin') != 1) {
|
||||||
|
$this->checkUserDevicePermission($deviceId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取设备关联的微信账号
|
||||||
|
$wechatAccounts = $this->getDeviceRelatedAccounts($deviceId);
|
||||||
|
|
||||||
|
return json([
|
||||||
|
'code' => 200,
|
||||||
|
'msg' => '获取成功',
|
||||||
|
'data' => [
|
||||||
|
'deviceId' => $deviceId,
|
||||||
|
'accounts' => $wechatAccounts,
|
||||||
|
'total' => count($wechatAccounts)
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return json([
|
||||||
|
'code' => $e->getCode(),
|
||||||
|
'msg' => $e->getMessage()
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,33 +11,7 @@ class DeviceWechatLogin extends Model
|
|||||||
// 设置表名
|
// 设置表名
|
||||||
protected $name = 'device_wechat_login';
|
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查询关联的设备
|
* 根据微信ID查询关联的设备
|
||||||
|
|||||||
Reference in New Issue
Block a user