147 lines
5.5 KiB
PHP
147 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace app\chukebao\controller;
|
|
|
|
use library\ResponseHelper;
|
|
use think\Db;
|
|
|
|
/**
|
|
* 模块 F · 设备 / 微信号(需求五 · 需求七 §7.3 · 需求二)
|
|
*/
|
|
class DeviceWechatController extends BaseController
|
|
{
|
|
/**
|
|
* 2130 设备 + 微信号树
|
|
* GET /v1/kefu/device-wechat/overview
|
|
*/
|
|
public function overview()
|
|
{
|
|
$companyId = $this->getUserInfo('companyId');
|
|
try {
|
|
$devices = Db::name('device')
|
|
->where(['companyId' => $companyId, 'deleteTime' => 0])
|
|
->field('id,imei,memo,model,brand,alive,extra')
|
|
->order('id desc')
|
|
->select();
|
|
|
|
$result = [];
|
|
foreach ($devices as $d) {
|
|
$extra = !empty($d['extra']) ? (json_decode($d['extra'], true) ?: []) : [];
|
|
// 该设备当前登录的微信号
|
|
$wechatIds = Db::name('device_wechat_login')
|
|
->where('deviceId', $d['id'])
|
|
->where('companyId', $companyId)
|
|
->group('wechatId')
|
|
->column('wechatId');
|
|
$wechats = [];
|
|
if (!empty($wechatIds)) {
|
|
$wechats = Db::table('s2_wechat_account')
|
|
->whereIn('wechatId', $wechatIds)
|
|
->field('id,nickname,alias,avatar,wechatId')
|
|
->select();
|
|
}
|
|
$memo = (string)($d['memo'] ?? '');
|
|
$sourceTag = '';
|
|
foreach (['老坑爹', '黑科技', '暗黑', '魔兽'] as $k) {
|
|
if (strpos($memo, $k) !== false) {
|
|
$sourceTag = $k;
|
|
break;
|
|
}
|
|
}
|
|
$result[] = [
|
|
'deviceId' => (int)$d['id'],
|
|
'imei' => $d['imei'],
|
|
'memo' => $memo,
|
|
'model' => trim(($d['brand'] ?? '') . ' ' . ($d['model'] ?? '')),
|
|
'battery' => $extra['battery'] ?? null,
|
|
'online' => isset($d['alive']) ? (bool)$d['alive'] : ($extra['online'] ?? null),
|
|
'sourceTag' => $sourceTag,
|
|
'wechats' => $wechats,
|
|
];
|
|
}
|
|
return ResponseHelper::success(['list' => $result]);
|
|
} catch (\Exception $e) {
|
|
return ResponseHelper::error('获取设备失败:' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 设备详情
|
|
* GET /v1/kefu/device/detail?deviceId=
|
|
*/
|
|
public function detail()
|
|
{
|
|
$companyId = $this->getUserInfo('companyId');
|
|
$deviceId = (int)$this->request->param('deviceId', 0);
|
|
if (!$deviceId) {
|
|
return ResponseHelper::error('deviceId 缺失', 400);
|
|
}
|
|
$d = Db::name('device')
|
|
->where(['id' => $deviceId, 'companyId' => $companyId])
|
|
->field('id,imei,memo,model,brand,alive,extra')
|
|
->find();
|
|
if (!$d) {
|
|
return ResponseHelper::error('设备不存在或无权限', 404);
|
|
}
|
|
$extra = !empty($d['extra']) ? (json_decode($d['extra'], true) ?: []) : [];
|
|
return ResponseHelper::success([
|
|
'deviceId' => (int)$d['id'],
|
|
'imei' => $d['imei'],
|
|
'memo' => $d['memo'],
|
|
'model' => trim(($d['brand'] ?? '') . ' ' . ($d['model'] ?? '')),
|
|
'battery' => $extra['battery'] ?? null,
|
|
'online' => isset($d['alive']) ? (bool)$d['alive'] : ($extra['online'] ?? null),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 微信字典(需求二)
|
|
* GET /v1/kefu/wechat-dict?wechatAccountId= 或 ?wechatFriendId=
|
|
*/
|
|
public function dict()
|
|
{
|
|
$wechatAccountId = (int)$this->request->param('wechatAccountId', 0);
|
|
$wechatFriendId = (int)$this->request->param('wechatFriendId', 0);
|
|
|
|
if ($wechatFriendId) {
|
|
$row = Db::table('s2_wechat_friend')
|
|
->where('id', $wechatFriendId)
|
|
->field('wechatId,alias,nickname,conRemark,region,phone,signature')
|
|
->find();
|
|
if (!$row) {
|
|
return ResponseHelper::error('好友不存在', 404);
|
|
}
|
|
return ResponseHelper::success([
|
|
'wechatId' => $row['alias'] ?: $row['wechatId'],
|
|
'alias' => $row['alias'],
|
|
'nickname' => $row['nickname'],
|
|
'remark' => $row['conRemark'],
|
|
'items' => array_values(array_filter([
|
|
$row['region'] ? ['label' => '地区', 'value' => $row['region']] : null,
|
|
$row['phone'] ? ['label' => '手机', 'value' => $row['phone']] : null,
|
|
$row['signature'] ? ['label' => '签名', 'value' => $row['signature']] : null,
|
|
])),
|
|
]);
|
|
}
|
|
|
|
if ($wechatAccountId) {
|
|
$row = Db::table('s2_wechat_account')
|
|
->where('id', $wechatAccountId)
|
|
->field('wechatId,alias,nickname,avatar')
|
|
->find();
|
|
if (!$row) {
|
|
return ResponseHelper::error('微信号不存在', 404);
|
|
}
|
|
return ResponseHelper::success([
|
|
'wechatId' => $row['alias'] ?: $row['wechatId'],
|
|
'alias' => $row['alias'],
|
|
'nickname' => $row['nickname'],
|
|
'remark' => '',
|
|
'items' => [],
|
|
]);
|
|
}
|
|
|
|
return ResponseHelper::error('缺少 wechatAccountId 或 wechatFriendId', 400);
|
|
}
|
|
}
|