From 6dcc78153a8adb9420fcab37d02c6949e018c2fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E6=B8=85=E7=88=BD?= Date: Wed, 16 Apr 2025 13:51:29 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=81=E5=9F=9F=E6=8A=84=E7=9B=98=E6=89=8B?= =?UTF-8?q?=20-=20=E8=AE=BE=E5=A4=87=E5=88=97=E8=A1=A8=E8=BF=94=E5=B7=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/application/cunkebao/config/route.php | 2 +- .../device/GetDeviceListV1Controller.php | 162 ++++++++++++++++++ Server/application/cunkebao/model/Device.php | 37 ---- 3 files changed, 163 insertions(+), 38 deletions(-) create mode 100644 Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php diff --git a/Server/application/cunkebao/config/route.php b/Server/application/cunkebao/config/route.php index f2c0ba54..f69dc1df 100644 --- a/Server/application/cunkebao/config/route.php +++ b/Server/application/cunkebao/config/route.php @@ -12,7 +12,7 @@ Route::group('v1/', function () { Route::group('devices', function () { Route::get(':id/related-accounts', 'app\\cunkebao\\controller\\Device@getRelatedAccounts'); // 设备关联微信账号路由 Route::get(':id/handle-logs', 'app\\cunkebao\\controller\\Device@handleLogs'); // 获取设备操作记录 - Route::get('', 'app\\cunkebao\\controller\\Device@index'); // 获取设备列表 + Route::get('', 'app\\cunkebao\\controller\\device\\GetDeviceListV1Controller@index'); // 获取设备列表 Route::get('count', 'app\\cunkebao\\controller\\Device@count'); // 获取设备总数 Route::get(':id', 'app\\cunkebao\\controller\\Device@read'); // 获取设备详情 Route::post('', 'app\\cunkebao\\controller\\Device@save'); // 添加设备 diff --git a/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php b/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php new file mode 100644 index 00000000..857015af --- /dev/null +++ b/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php @@ -0,0 +1,162 @@ +user = request()->userInfo;; + } + + /** + * 构建查询条件 + * + * @param array $params + * @return array + */ + protected function makeWhere(array $params = []): array + { + $where = []; + + // 关键词搜索(同时搜索IMEI和备注) + if (!empty($keyword = Request::param('keyword'))) { + $where[] = ['exp', "d.imei LIKE '%{$keyword}%' OR d.memo LIKE '%{$keyword}%'"]; + } + + // 设备在线状态 + if (is_numeric($alive = Request::param('alive'))) { + $where['d.alive'] = $alive; + } + + $where['d.companyId'] = $this->user['companyId']; + $where['d.deleteTime'] = 0; + + return array_merge($params, $where); + } + + /** + * 获取指定用户的所有设备ID + * + * @return array + */ + protected function makeDeviceIdsWhere(): array + { + $deviceIds = DeviceUserModel::where( + $this->user['id'], + $this->user['companyId'] + ) + ->column('deviceId'); + + if (empty($deviceIds)) { + throw new \Exception('请联系管理员绑定设备', 403); + } + + $where['d.id'] = ['in', $deviceIds]; + + return $where; + } + + /** + * 获取设备列表 + * @param array $where 查询条件 + * @param int $page 页码 + * @param int $limit 每页数量 + * @return \think\Paginator 分页对象 + */ + protected function getDeviceList($where, $page = 1, $limit = 10) + { + $query = DeviceModel::alias('d') + ->field(['d.id', 'd.imei', 'd.memo', 'l.wechatId', 'd.alive', '0 totalFriend']) + ->leftJoin('device_wechat_login l', 'd.id = l.deviceId'); + + foreach ($where as $key => $value) { + if (is_numeric($key) && is_array($value) && isset($value[0]) && $value[0] === 'exp') { + $query->whereExp('', $value[1]); + continue; + } + + $query->where($key, $value); + } + + return $query->paginate($limit, false, ['page' => $page]); + } + + /** + * 统计微信好友 + * + * @param object $list + * @return array + */ + protected function countFriend(object $list): array + { + $result = []; + + foreach ($list->items() as $item) { + $section = $item->toArray(); + + if ($item->wechatId) { + $section['totalFriend'] = WechatFriend::where(['ownerWechatId' => $section['wechatId']])->count(); + } + + array_push($result, $section); + } + + return $result; + } + + /** + * 获取设备列表 + * @return \think\response\Json + */ + public function index() + { + try { + $page = (int)Request::param('page', 1); + $limit = (int)Request::param('limit', 10); + + if ($this->user['isAdmin'] == 1) { + $where = $this->makeWhere(); + $result = $this->getDeviceList($where, $page, $limit); + } else { + $where = $this->makeWhere($this->makeDeviceIdsWhere()); + $result = $this->getDeviceList($where, $page, $limit); + } + + return json([ + 'code' => 200, + 'msg' => '获取成功', + 'data' => [ + 'list' => $this->countFriend($result), + 'total' => $result->total(), + ] + ]); + } catch (\Exception $e) { + return json([ + 'code' => $e->getCode(), + 'msg' => $e->getMessage() + ]); + } + } +} \ No newline at end of file diff --git a/Server/application/cunkebao/model/Device.php b/Server/application/cunkebao/model/Device.php index a1e3402f..0fd236da 100644 --- a/Server/application/cunkebao/model/Device.php +++ b/Server/application/cunkebao/model/Device.php @@ -41,44 +41,7 @@ class Device extends Model } } - /** - * 获取设备列表 - * @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['deleteTime'])) { - $where['d.deleteTime'] = 0; - } - // 构建查询对象 - $query = self::alias('d') - ->field(['d.id', 'd.imei', 'd.memo', 'l.wechatId', 'd.alive', '0 totalFriend']) - ->leftJoin('device_wechat_login l', 'd.id = l.deviceId'); - - // 处理查询条件 - 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