2025-04-16 13:51:29 +08:00
|
|
|
|
<?php
|
2025-05-06 17:47:30 +08:00
|
|
|
|
|
2025-04-16 13:51:29 +08:00
|
|
|
|
namespace app\cunkebao\controller\device;
|
|
|
|
|
|
|
2025-04-16 16:10:37 +08:00
|
|
|
|
use app\common\model\Device as DeviceModel;
|
|
|
|
|
|
use app\common\model\DeviceUser as DeviceUserModel;
|
2025-05-15 18:16:12 +08:00
|
|
|
|
use app\common\model\DeviceWechatLogin as DeviceWechatLoginModel;
|
2025-05-08 10:39:53 +08:00
|
|
|
|
use app\common\model\User as UserModel;
|
2025-05-19 11:40:00 +08:00
|
|
|
|
use app\common\model\WechatCustomer as WechatCustomerModel;
|
2025-05-12 15:32:42 +08:00
|
|
|
|
use app\common\model\WechatFriendShip as WechatFriendShipModel;
|
2025-04-16 16:10:37 +08:00
|
|
|
|
use app\cunkebao\controller\BaseController;
|
2025-05-07 18:16:02 +08:00
|
|
|
|
use library\ResponseHelper;
|
2025-04-16 13:51:29 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设备管理控制器
|
|
|
|
|
|
*/
|
2025-04-16 16:10:37 +08:00
|
|
|
|
class GetDeviceListV1Controller extends BaseController
|
2025-04-16 13:51:29 +08:00
|
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 构建查询条件
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param array $params
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function makeWhere(array $params = []): array
|
|
|
|
|
|
{
|
|
|
|
|
|
// 关键词搜索(同时搜索IMEI和备注)
|
2025-04-17 10:15:26 +08:00
|
|
|
|
if (!empty($keyword = $this->request->param('keyword'))) {
|
2025-04-16 13:51:29 +08:00
|
|
|
|
$where[] = ['exp', "d.imei LIKE '%{$keyword}%' OR d.memo LIKE '%{$keyword}%'"];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 设备在线状态
|
2025-04-17 10:15:26 +08:00
|
|
|
|
if (is_numeric($alive = $this->request->param('alive'))) {
|
2025-04-16 13:51:29 +08:00
|
|
|
|
$where['d.alive'] = $alive;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-16 16:10:37 +08:00
|
|
|
|
$where['d.companyId'] = $this->getUserInfo('companyId');
|
2025-04-16 13:51:29 +08:00
|
|
|
|
|
|
|
|
|
|
return array_merge($params, $where);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取指定用户的所有设备ID
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function makeDeviceIdsWhere(): array
|
|
|
|
|
|
{
|
|
|
|
|
|
$deviceIds = DeviceUserModel::where(
|
2025-05-10 10:17:58 +08:00
|
|
|
|
[
|
|
|
|
|
|
'userId' => $this->getUserInfo('id'),
|
|
|
|
|
|
'companyId' => $this->getUserInfo('companyId')
|
|
|
|
|
|
]
|
2025-04-16 13:51:29 +08:00
|
|
|
|
)
|
|
|
|
|
|
->column('deviceId');
|
|
|
|
|
|
|
|
|
|
|
|
if (empty($deviceIds)) {
|
|
|
|
|
|
throw new \Exception('请联系管理员绑定设备', 403);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-10 10:17:58 +08:00
|
|
|
|
$where['d.id'] = array('in', $deviceIds);
|
2025-04-16 13:51:29 +08:00
|
|
|
|
|
|
|
|
|
|
return $where;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取设备列表
|
2025-04-17 15:33:49 +08:00
|
|
|
|
*
|
2025-04-16 13:51:29 +08:00
|
|
|
|
* @param array $where 查询条件
|
|
|
|
|
|
* @return \think\Paginator 分页对象
|
|
|
|
|
|
*/
|
2025-05-10 10:17:58 +08:00
|
|
|
|
protected function getDeviceList(array $where): \think\Paginator
|
2025-04-16 13:51:29 +08:00
|
|
|
|
{
|
|
|
|
|
|
$query = DeviceModel::alias('d')
|
2025-05-08 10:39:53 +08:00
|
|
|
|
->field([
|
|
|
|
|
|
'd.id', 'd.imei', 'd.memo', 'd.alive',
|
|
|
|
|
|
'l.wechatId',
|
2025-08-13 14:30:22 +08:00
|
|
|
|
'a.nickname', 'a.alias', 'a.avatar', '0 totalFriend'
|
2025-05-08 10:39:53 +08:00
|
|
|
|
])
|
2025-06-07 17:34:20 +08:00
|
|
|
|
->leftJoin('device_wechat_login l', 'd.id = l.deviceId and l.alive =' . DeviceWechatLoginModel::ALIVE_WECHAT_ACTIVE . ' and l.companyId = d.companyId')
|
2025-05-15 18:16:12 +08:00
|
|
|
|
->leftJoin('wechat_account a', 'l.wechatId = a.wechatId')
|
2025-04-17 17:18:35 +08:00
|
|
|
|
->order('d.id desc');
|
2025-04-16 13:51:29 +08:00
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-17 10:39:18 +08:00
|
|
|
|
return $query->paginate($this->request->param('limit/d', 10), false, ['page' => $this->request->param('page/d', 1)]);
|
2025-04-16 13:51:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-19 11:40:00 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取设备最新登录微信的 wechatId
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param int $deviceId
|
|
|
|
|
|
* @return string|null
|
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function getDeviceLatestWechatLogin(int $deviceId): ?string
|
|
|
|
|
|
{
|
|
|
|
|
|
return DeviceWechatLoginModel::where(
|
|
|
|
|
|
[
|
|
|
|
|
|
'companyId' => $this->getUserInfo('companyId'),
|
|
|
|
|
|
'deviceId' => $deviceId,
|
|
|
|
|
|
'alive' => DeviceWechatLoginModel::ALIVE_WECHAT_ACTIVE
|
|
|
|
|
|
]
|
|
|
|
|
|
)
|
|
|
|
|
|
->value('wechatId');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取设备绑定的客服信息
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string $wechatId
|
|
|
|
|
|
* @return int
|
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function getWechatCustomerInfo(string $wechatId): int
|
|
|
|
|
|
{
|
|
|
|
|
|
$curstomer = WechatCustomerModel::field('friendShip')
|
|
|
|
|
|
->where(
|
|
|
|
|
|
[
|
2025-06-16 15:40:45 +08:00
|
|
|
|
//'companyId' => $this->getUserInfo('companyId'),
|
2025-05-19 11:40:00 +08:00
|
|
|
|
'wechatId' => $wechatId
|
|
|
|
|
|
]
|
|
|
|
|
|
)
|
|
|
|
|
|
->find();
|
|
|
|
|
|
|
|
|
|
|
|
return $curstomer->friendShip->totalFriend ?? 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-16 13:51:29 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 统计微信好友
|
|
|
|
|
|
*
|
2025-04-17 14:34:31 +08:00
|
|
|
|
* @param \think\Paginator $list
|
2025-04-16 13:51:29 +08:00
|
|
|
|
* @return array
|
|
|
|
|
|
*/
|
2025-04-17 14:34:31 +08:00
|
|
|
|
protected function countFriend(\think\Paginator $list): array
|
2025-04-16 13:51:29 +08:00
|
|
|
|
{
|
2025-05-10 10:17:58 +08:00
|
|
|
|
$resultSets = [];
|
2025-04-16 13:51:29 +08:00
|
|
|
|
|
|
|
|
|
|
foreach ($list->items() as $item) {
|
2025-05-19 11:40:00 +08:00
|
|
|
|
$wechatId = $this->getDeviceLatestWechatLogin($item->id);
|
|
|
|
|
|
|
|
|
|
|
|
$item->totalFriend = $wechatId ? $this->getWechatCustomerInfo($wechatId) : 0;
|
2025-04-16 13:51:29 +08:00
|
|
|
|
|
2025-05-19 11:40:00 +08:00
|
|
|
|
array_push($resultSets, $item->toArray());
|
2025-04-16 13:51:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-10 10:22:55 +08:00
|
|
|
|
return $resultSets;
|
2025-04-16 13:51:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取设备列表
|
|
|
|
|
|
* @return \think\response\Json
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function index()
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
2025-05-08 10:39:53 +08:00
|
|
|
|
if ($this->getUserInfo('isAdmin') == UserModel::ADMIN_STP) {
|
2025-04-16 13:51:29 +08:00
|
|
|
|
$where = $this->makeWhere();
|
2025-04-17 10:39:18 +08:00
|
|
|
|
$result = $this->getDeviceList($where);
|
2025-05-10 10:17:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
$where = $this->makeWhere( $this->makeDeviceIdsWhere() );
|
2025-04-17 10:39:18 +08:00
|
|
|
|
$result = $this->getDeviceList($where);
|
2025-04-16 13:51:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-07 18:16:02 +08:00
|
|
|
|
return ResponseHelper::success(
|
|
|
|
|
|
[
|
2025-05-08 10:39:53 +08:00
|
|
|
|
'list' => $this->countFriend($result),
|
2025-04-16 13:51:29 +08:00
|
|
|
|
'total' => $result->total(),
|
|
|
|
|
|
]
|
2025-05-07 18:16:02 +08:00
|
|
|
|
);
|
2025-04-16 13:51:29 +08:00
|
|
|
|
} catch (\Exception $e) {
|
2025-05-07 18:16:02 +08:00
|
|
|
|
return ResponseHelper::error($e->getMessage(), $e->getCode());
|
2025-04-16 13:51:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|