存客宝应用接口初始化
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace app\cunkebao\controller\device;
|
||||
|
||||
use app\common\model\Device as DeviceModel;
|
||||
use app\common\model\DeviceTaskconf as DeviceTaskconfModel;
|
||||
use app\common\model\DeviceUser as DeviceUserModel;
|
||||
use app\common\model\User as UserModel;
|
||||
use app\cunkebao\controller\BaseController;
|
||||
use library\ResponseHelper;
|
||||
use think\Db;
|
||||
use app\api\controller\DeviceController as apiDevice;
|
||||
|
||||
/**
|
||||
* 设备管理控制器
|
||||
*/
|
||||
class DeleteDeviceV1Controller extends BaseController
|
||||
{
|
||||
/**
|
||||
* 删除设备关联用户信息
|
||||
*
|
||||
* @param int $deviceId
|
||||
* @return void
|
||||
*/
|
||||
protected function deleteDeviceUser(int $deviceId): void
|
||||
{
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
$deviceUser = DeviceUserModel::where(compact('companyId', 'deviceId'))->find();
|
||||
|
||||
// 有关联数据则删除
|
||||
if ($deviceUser) {
|
||||
if (!$deviceUser->delete()) {
|
||||
throw new \Exception('设备用户关联数据删除失败', 402);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除设备任务配置记录
|
||||
*
|
||||
* @param int $deviceId
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function deleteDeviceConf(int $deviceId): void
|
||||
{
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
$deviceConf = DeviceTaskconfModel::where(compact('companyId', 'deviceId'))->find();
|
||||
|
||||
// 有配置信息则删除
|
||||
if ($deviceConf) {
|
||||
if (!$deviceConf->delete()) {
|
||||
throw new \Exception('设备设置信息删除失败', 402);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除主设备信息
|
||||
*
|
||||
* @param int $id
|
||||
* @return DeviceModel
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function deleteDevice(int $id): void
|
||||
{
|
||||
$device = DeviceModel::where('companyId', $this->getUserInfo('companyId'))->find($id);
|
||||
|
||||
if (!$device) {
|
||||
throw new \Exception('设备不存在或无权限操作', 404);
|
||||
}
|
||||
|
||||
if (!$device->delete()) {
|
||||
throw new \Exception('设备删除失败', 402);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除存客宝设备数据
|
||||
*
|
||||
* @param int $id
|
||||
* @return $this
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function deleteCkbAbout(int $id): self
|
||||
{
|
||||
$apiDevice = new ApiDevice();
|
||||
$res = $apiDevice->delDevice($id);
|
||||
$res = json_decode($res, true);
|
||||
if ($res['code'] == 200){
|
||||
$this->deleteDevice($id);
|
||||
$this->deleteDeviceConf($id);
|
||||
$this->deleteDeviceUser($id);
|
||||
return $this;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO 删除存客宝设备数据
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
protected function deleteS2About(): self
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查用户权限,只有操盘手可以删除设备
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function checkPermission(): self
|
||||
{
|
||||
if ($this->getUserInfo('typeId') != UserModel::MASTER_USER) {
|
||||
throw new \Exception('您没有权限删除设备', 403);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除设备
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
$id = $this->request->param('id/d');
|
||||
|
||||
Db::startTrans();
|
||||
$this->checkPermission();
|
||||
$this->deleteCkbAbout($id)->deleteS2About($id);
|
||||
Db::commit();
|
||||
|
||||
return ResponseHelper::success();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
return ResponseHelper::error($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
namespace app\cunkebao\controller\device;
|
||||
|
||||
use app\api\controller\DeviceController as ApiDeviceController;
|
||||
use app\common\model\Device as DeviceModel;
|
||||
use app\common\model\User as UserModel;
|
||||
use app\cunkebao\controller\BaseController;
|
||||
use library\ResponseHelper;
|
||||
use think\Db;
|
||||
use think\facade\Cache;
|
||||
|
||||
/**
|
||||
* 设备控制器
|
||||
*/
|
||||
class GetAddResultedV1Controller extends BaseController
|
||||
{
|
||||
/**
|
||||
* 通过账号id 获取项目id。
|
||||
*
|
||||
* @param int $accountId
|
||||
* @return int
|
||||
*/
|
||||
protected function getCompanyIdByAccountId(int $accountId): int
|
||||
{
|
||||
return UserModel::where('s2_accountId', $accountId)->value('companyId');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目下的所有设备。
|
||||
*
|
||||
* @param int $companyId
|
||||
* @return array
|
||||
*/
|
||||
protected function getAllDevicesIdWithInCompany(int $companyId): array
|
||||
{
|
||||
return DeviceModel::where('companyId', $companyId)->column('id') ?: [0];
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行数据迁移。
|
||||
*
|
||||
* @param int $accountId
|
||||
* @return void
|
||||
*/
|
||||
protected function migrateData(int $accountId): void
|
||||
{
|
||||
$companyId = $this->getCompanyIdByAccountId($accountId);
|
||||
$deviceIds = $this->getAllDevicesIdWithInCompany($companyId) ?: [0];
|
||||
|
||||
// 从 s2_device 导入数据。
|
||||
$this->getNewDeviceFromS2_device($deviceIds, $companyId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 s2_device 导入数据。
|
||||
*
|
||||
* @param array $ids
|
||||
* @param int $companyId
|
||||
* @return void
|
||||
*/
|
||||
protected function getNewDeviceFromS2_device(array $ids, int $companyId): void
|
||||
{
|
||||
$ids = implode(',', $ids);
|
||||
|
||||
$sql = "INSERT INTO ck_device(`id`, `imei`, `model`, phone, operatingSystem, memo, alive, brand, rooted, xPosed, softwareVersion, extra, createTime, updateTime, deleteTime, companyId)
|
||||
SELECT
|
||||
d.id, d.imei, d.model, d.phone, d.operatingSystem, d.memo, d.alive, d.brand, d.rooted, d.xPosed, d.softwareVersion, d.extra, d.createTime, d.lastUpdateTime, d.deleteTime, a.departmentId AS companyId
|
||||
FROM s2_device d
|
||||
JOIN s2_company_account a ON d.currentAccountId = a.id
|
||||
WHERE isDeleted = 0 AND deletedAndStop = 0 AND d.id NOT IN ({$ids}) AND a.departmentId = {$companyId}
|
||||
ON DUPLICATE KEY UPDATE
|
||||
imei = VALUES(imei),
|
||||
model = VALUES(model),
|
||||
phone = VALUES(phone),
|
||||
operatingSystem = VALUES(operatingSystem),
|
||||
memo = VALUES(memo),
|
||||
alive = VALUES(alive),
|
||||
brand = VALUES(brand),
|
||||
rooted = VALUES(rooted),
|
||||
xPosed = VALUES(xPosed),
|
||||
softwareVersion = VALUES(softwareVersion),
|
||||
extra = VALUES(extra),
|
||||
updateTime = VALUES(updateTime),
|
||||
deleteTime = VALUES(deleteTime),
|
||||
companyId = VALUES(companyId)";
|
||||
|
||||
Db::query($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前设备数量
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function getCkbDeviceCount(): int
|
||||
{
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
$cacheKey = 'deviceNum_'.$companyId;
|
||||
$deviceNum = Cache::get($cacheKey);
|
||||
if (empty($deviceNum)) {
|
||||
$deviceNum = DeviceModel::where(['companyId' => $companyId])->count('*');
|
||||
Cache::set($cacheKey,$deviceNum,120);
|
||||
}
|
||||
return $deviceNum;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取添加的关联设备结果。
|
||||
*
|
||||
* @param int $accountId
|
||||
* @return bool
|
||||
*/
|
||||
protected function getAddResulted(int $accountId): bool
|
||||
{
|
||||
$deviceNum = $this->getCkbDeviceCount();
|
||||
$result = (new ApiDeviceController())->getlist(
|
||||
[
|
||||
'accountId' => $accountId,
|
||||
'pageIndex' => 0,
|
||||
'pageSize' => 100
|
||||
],
|
||||
true
|
||||
);
|
||||
$result = json_decode($result, true);
|
||||
$result = $result['data']['results'] ?? false;
|
||||
|
||||
if (empty($result)){
|
||||
return false;
|
||||
}else{
|
||||
if (count($result) > $deviceNum){
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
$cacheKey = 'deviceNum_'.$companyId;
|
||||
Cache::rm($cacheKey);
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取基础统计信息
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$accountId = $this->request->param('accountId/d');
|
||||
|
||||
if (empty($accountId)){
|
||||
return ResponseHelper::error('参数缺失');
|
||||
}
|
||||
|
||||
$isAdded = $this->getAddResulted($accountId);
|
||||
$isAdded && $this->migrateData($accountId);
|
||||
|
||||
return ResponseHelper::success(
|
||||
[
|
||||
'added' => $isAdded
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
|
||||
namespace app\cunkebao\controller\device;
|
||||
|
||||
use app\common\model\Device as DeviceModel;
|
||||
use app\common\model\DeviceTaskconf as DeviceTaskconfModel;
|
||||
use app\common\model\DeviceUser as DeviceUserModel;
|
||||
use app\common\model\DeviceWechatLogin as DeviceWechatLoginModel;
|
||||
use app\common\model\User as UserModel;
|
||||
use app\common\model\WechatCustomer as WechatCustomerModel;
|
||||
use app\common\model\WechatFriendShip as WechatFriendShipModel;
|
||||
use app\cunkebao\controller\BaseController;
|
||||
use Eison\Utils\Helper\ArrHelper;
|
||||
use library\ResponseHelper;
|
||||
|
||||
/**
|
||||
* 设备管理控制器
|
||||
*/
|
||||
class GetDeviceDetailV1Controller extends BaseController
|
||||
{
|
||||
/**
|
||||
* 检查用户是否有权限操作指定设备
|
||||
*
|
||||
* @param int $deviceId
|
||||
* @return void
|
||||
*/
|
||||
protected function checkUserDevicePermission(int $deviceId): void
|
||||
{
|
||||
$hasPermission = DeviceUserModel::where(
|
||||
[
|
||||
'deviceId' => $deviceId,
|
||||
'userId' => $this->getUserInfo('id'),
|
||||
'companyId' => $this->getUserInfo('companyId')
|
||||
]
|
||||
)
|
||||
->count() > 0;
|
||||
|
||||
if (!$hasPermission) {
|
||||
throw new \Exception('您没有权限查看该设备', 403);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析设备额外信息
|
||||
*
|
||||
* @param string $extra
|
||||
* @return int
|
||||
*/
|
||||
protected function parseExtraForBattery(string $extra): int
|
||||
{
|
||||
if (!empty($extra)) {
|
||||
$extra = json_decode($extra);
|
||||
|
||||
if ($extra && isset($extra->battery)) {
|
||||
return intval($extra->battery);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备最新登录微信的 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 int $deviceId
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function getWechatCustomerInfo(int $deviceId): array
|
||||
{
|
||||
$curstomer = WechatCustomerModel::field('activity,friendShip')
|
||||
->where(
|
||||
[
|
||||
'companyId' => $this->getUserInfo('companyId'),
|
||||
'wechatId' => $this->getDeviceLatestWechatLogin($deviceId)
|
||||
]
|
||||
)
|
||||
->find();
|
||||
|
||||
return $curstomer ? [
|
||||
'lastUpdateTime' => $curstomer->activity->lastActivityTime ?? '',
|
||||
'thirtyDayMsgCount' => $curstomer->activity->totalMsgCount ?? 0,
|
||||
'totalFriend' => $curstomer->friendShip->totalFriend ?? 0,
|
||||
] : [
|
||||
'lastUpdateTime' => '',
|
||||
'thirtyDayMsgCount' => 0,
|
||||
'totalFriend' => 0,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备详情
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
protected function getDeviceInfo(int $id): array
|
||||
{
|
||||
// 查询设备基础信息与关联的微信账号信息
|
||||
$device = DeviceModel::alias('d')
|
||||
->field([
|
||||
'd.id', 'd.imei', 'd.memo', 'd.alive', 'd.extra'
|
||||
])
|
||||
->find($id);
|
||||
|
||||
if (empty($device)) {
|
||||
throw new \Exception('设备不存在', 404);
|
||||
}
|
||||
|
||||
$device->battery = $this->parseExtraForBattery($device->extra);
|
||||
|
||||
// 删除冗余字段
|
||||
unset($device->extra);
|
||||
|
||||
return $device->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备详情
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
$id = $this->request->param('id/d');
|
||||
|
||||
if ($this->getUserInfo('isAdmin') != UserModel::ADMIN_STP) {
|
||||
$this->checkUserDevicePermission($id);
|
||||
}
|
||||
|
||||
return ResponseHelper::success(
|
||||
$this->getDeviceInfo($id) + $this->getWechatCustomerInfo($id)
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
return ResponseHelper::error($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function isUpdataWechat()
|
||||
{
|
||||
$id = $this->request->param('id/d');
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
$newWechat = DeviceWechatLoginModel::alias('a')
|
||||
->field('b.*')
|
||||
->join('wechat_account b', 'a.wechatId = b.wechatId')
|
||||
->where(['a.deviceId' => $id,'a.isTips' => 0,'a.companyId' => $companyId])
|
||||
->order('a.id', 'desc')
|
||||
->find();
|
||||
if (empty($newWechat)){
|
||||
return ResponseHelper::success('','该设备绑定的微信无需迁移',201);
|
||||
}
|
||||
|
||||
$oldWechat = DeviceWechatLoginModel::alias('a')
|
||||
->field('b.*')
|
||||
->join('wechat_account b', 'a.wechatId = b.wechatId')
|
||||
->where(['a.companyId' => $companyId])
|
||||
->where('a.deviceId' ,'<>', $id)
|
||||
->order('a.id', 'desc')
|
||||
->find();
|
||||
if (empty($oldWechat)){
|
||||
return ResponseHelper::success('','该设备绑定的微信无需迁移',201);
|
||||
}else{
|
||||
DeviceWechatLoginModel::where(['deviceId' => $id,'isTips' => 0,'companyId' => $companyId])->update(['isTips' => 1]);;
|
||||
return ResponseHelper::success(['newWechat' => $newWechat,'oldWechat' => $oldWechat]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace app\cunkebao\controller\device;
|
||||
|
||||
use app\common\model\DeviceHandleLog;
|
||||
use app\common\model\DeviceUser as DeviceUserModel;
|
||||
use app\common\model\User as UserModel;
|
||||
use app\cunkebao\controller\BaseController;
|
||||
use library\ResponseHelper;
|
||||
|
||||
/**
|
||||
* 设备管理控制器
|
||||
*/
|
||||
class GetDeviceHandleLogsV1Controller 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询设备操作记录,并关联用户表获取操作人信息
|
||||
*
|
||||
* @param int $deviceId
|
||||
* @return \think\Paginator
|
||||
*/
|
||||
protected function getHandleLogs(int $deviceId): \think\Paginator
|
||||
{
|
||||
return DeviceHandleLog::alias('l')
|
||||
->field([
|
||||
'l.id', 'l.content', 'l.createTime',
|
||||
'u.username'
|
||||
])
|
||||
->leftJoin('users u', 'l.userId = u.id')
|
||||
->where('l.deviceId', $deviceId)
|
||||
->order('l.createTime desc')
|
||||
->paginate($this->request->param('limit/d', 10), false, ['page' => $this->request->param('page/d', 1)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备操作记录
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
$deviceId = $this->request->param('id/d');
|
||||
|
||||
if ($this->getUserInfo('isAdmin') != UserModel::ADMIN_STP) {
|
||||
$this->checkUserDevicePermission($deviceId);
|
||||
}
|
||||
|
||||
$logs = $this->getHandleLogs($deviceId);
|
||||
|
||||
return ResponseHelper::success(
|
||||
[
|
||||
'total' => $logs->total(),
|
||||
'list' => $logs->items()
|
||||
]
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
return ResponseHelper::error($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
|
||||
namespace app\cunkebao\controller\device;
|
||||
|
||||
use app\common\model\Device as DeviceModel;
|
||||
use app\common\model\DeviceUser as DeviceUserModel;
|
||||
use app\common\model\DeviceWechatLogin as DeviceWechatLoginModel;
|
||||
use app\common\model\User as UserModel;
|
||||
use app\common\model\WechatCustomer as WechatCustomerModel;
|
||||
use app\common\model\WechatFriendShip as WechatFriendShipModel;
|
||||
use app\cunkebao\controller\BaseController;
|
||||
use library\ResponseHelper;
|
||||
|
||||
/**
|
||||
* 设备管理控制器
|
||||
*/
|
||||
class GetDeviceListV1Controller extends BaseController
|
||||
{
|
||||
/**
|
||||
* 构建查询条件
|
||||
*
|
||||
* @param array $params
|
||||
* @return array
|
||||
*/
|
||||
protected function makeWhere(array $params = []): array
|
||||
{
|
||||
// 关键词搜索(同时搜索IMEI和备注)
|
||||
if (!empty($keyword = $this->request->param('keyword'))) {
|
||||
$where[] = ['exp', "d.imei LIKE '%{$keyword}%' OR d.memo LIKE '%{$keyword}%'"];
|
||||
}
|
||||
|
||||
// 设备在线状态
|
||||
if (is_numeric($alive = $this->request->param('alive'))) {
|
||||
$where['d.alive'] = $alive;
|
||||
}
|
||||
|
||||
$where['d.companyId'] = $this->getUserInfo('companyId');
|
||||
|
||||
return array_merge($params, $where);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定用户的所有设备ID
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function makeDeviceIdsWhere(): array
|
||||
{
|
||||
$deviceIds = DeviceUserModel::where(
|
||||
[
|
||||
'userId' => $this->getUserInfo('id'),
|
||||
'companyId' => $this->getUserInfo('companyId')
|
||||
]
|
||||
)
|
||||
->column('deviceId');
|
||||
|
||||
if (empty($deviceIds)) {
|
||||
throw new \Exception('请联系管理员绑定设备', 403);
|
||||
}
|
||||
|
||||
$where['d.id'] = array('in', $deviceIds);
|
||||
|
||||
return $where;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备列表
|
||||
*
|
||||
* @param array $where 查询条件
|
||||
* @return \think\Paginator 分页对象
|
||||
*/
|
||||
protected function getDeviceList(array $where): \think\Paginator
|
||||
{
|
||||
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
$query = DeviceModel::alias('d')
|
||||
->field([
|
||||
'd.id', 'd.imei', 'd.memo', 'd.alive',
|
||||
'l.wechatId',
|
||||
'a.nickname', 'a.alias', 'a.avatar', '0 totalFriend'
|
||||
])
|
||||
->join('(SELECT MAX(id) AS id, deviceId FROM ck_device_wechat_login WHERE companyId='.$companyId.' GROUP BY deviceId) dwl_max','dwl_max.deviceId = d.id')
|
||||
->join('device_wechat_login l','l.id = dwl_max.id')
|
||||
->join('wechat_account a', 'l.wechatId = a.wechatId')
|
||||
->order('d.id desc');
|
||||
|
||||
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($this->request->param('limit/d', 10), false, ['page' => $this->request->param('page/d', 1)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备最新登录微信的 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(
|
||||
[
|
||||
//'companyId' => $this->getUserInfo('companyId'),
|
||||
'wechatId' => $wechatId
|
||||
]
|
||||
)
|
||||
->find();
|
||||
|
||||
return $curstomer->friendShip->totalFriend ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计微信好友
|
||||
*
|
||||
* @param \think\Paginator $list
|
||||
* @return array
|
||||
*/
|
||||
protected function countFriend(\think\Paginator $list): array
|
||||
{
|
||||
$resultSets = [];
|
||||
|
||||
foreach ($list->items() as $item) {
|
||||
$wechatId = $this->getDeviceLatestWechatLogin($item->id);
|
||||
|
||||
$item->totalFriend = $wechatId ? $this->getWechatCustomerInfo($wechatId) : 0;
|
||||
|
||||
array_push($resultSets, $item->toArray());
|
||||
}
|
||||
|
||||
return $resultSets;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备列表
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
if ($this->getUserInfo('isAdmin') == UserModel::ADMIN_STP) {
|
||||
$where = $this->makeWhere();
|
||||
$result = $this->getDeviceList($where);
|
||||
}else {
|
||||
//$where = $this->makeWhere( $this->makeDeviceIdsWhere() );
|
||||
$where = $this->makeWhere();
|
||||
$result = $this->getDeviceList($where);
|
||||
}
|
||||
|
||||
return ResponseHelper::success(
|
||||
[
|
||||
'list' => $this->countFriend($result),
|
||||
'total' => $result->total(),
|
||||
]
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
return ResponseHelper::error($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace app\cunkebao\controller\device;
|
||||
|
||||
use app\common\model\DeviceTaskconf as DeviceTaskconfModel;
|
||||
use app\common\model\DeviceUser as DeviceUserModel;
|
||||
use app\common\model\User as UserModel;
|
||||
use app\cunkebao\controller\BaseController;
|
||||
use Eison\Utils\Helper\ArrHelper;
|
||||
use library\ResponseHelper;
|
||||
|
||||
/**
|
||||
* 设备管理控制器
|
||||
*/
|
||||
class GetDeviceTaskConfigV1Controller extends BaseController
|
||||
{
|
||||
/**
|
||||
* 检查用户是否有权限操作指定设备
|
||||
*
|
||||
* @param int $deviceId
|
||||
* @return void
|
||||
*/
|
||||
protected function checkUserDevicePermission(int $deviceId): void
|
||||
{
|
||||
$hasPermission = DeviceUserModel::where(
|
||||
[
|
||||
'deviceId' => $deviceId,
|
||||
'userId' => $this->getUserInfo('id'),
|
||||
'companyId' => $this->getUserInfo('companyId')
|
||||
]
|
||||
)
|
||||
->count() > 0;
|
||||
|
||||
if (!$hasPermission) {
|
||||
throw new \Exception('您没有权限查看该设备', 403);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析taskConfig字段获取功能开关
|
||||
*
|
||||
* @param int $deviceId
|
||||
* @return int[]
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function getTaskConfig(int $deviceId): array
|
||||
{
|
||||
$conf = DeviceTaskconfModel::alias('c')
|
||||
->field([
|
||||
'c.autoAddFriend', 'c.autoReply', 'c.momentsSync', 'c.aiChat'
|
||||
])
|
||||
->where(
|
||||
[
|
||||
'companyId' => $this->getUserInfo('companyId'),
|
||||
'deviceId' => $deviceId
|
||||
]
|
||||
)
|
||||
->find();
|
||||
|
||||
// 未配置时赋予默认关闭的状态
|
||||
return !is_null($conf) ? $conf->toArray() : ArrHelper::getValue('autoAddFriend,autoReply,momentsSync,aiChat', [], 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备详情
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
$id = $this->request->param('id/d');
|
||||
|
||||
if ($this->getUserInfo('isAdmin') != UserModel::ADMIN_STP) {
|
||||
$this->checkUserDevicePermission($id);
|
||||
}
|
||||
|
||||
return ResponseHelper::success(
|
||||
$this->getTaskConfig($id)
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
return ResponseHelper::error($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace app\cunkebao\controller\device;
|
||||
|
||||
use app\cunkebao\controller\BaseController;
|
||||
|
||||
/**
|
||||
* 设备管理控制器
|
||||
*/
|
||||
class PostAddDeviceV1Controller extends BaseController
|
||||
{
|
||||
/**
|
||||
* 添加设备
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
exit('暂未支持');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace app\cunkebao\controller\device;
|
||||
|
||||
use app\cunkebao\controller\BaseController;
|
||||
use library\ResponseHelper;
|
||||
|
||||
/**
|
||||
* 设备管理控制器
|
||||
*/
|
||||
class RefreshDeviceDetailV1Controller extends BaseController
|
||||
{
|
||||
/**
|
||||
* 刷新设备
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
// TODO: 实现实际刷新设备状态的功能
|
||||
return ResponseHelper::success();
|
||||
} catch (\Exception $e) {
|
||||
return ResponseHelper::error($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace app\cunkebao\controller\device;
|
||||
|
||||
use app\common\model\Device as DeviceModel;
|
||||
use app\common\model\DeviceHandleLog as DeviceHandleLogModel;
|
||||
use app\common\model\DeviceTaskconf;
|
||||
use app\common\model\DeviceUser as DeviceUserModel;
|
||||
use app\common\model\User as UserModel;
|
||||
use app\cunkebao\controller\BaseController;
|
||||
use library\ResponseHelper;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 设备管理控制器
|
||||
*/
|
||||
class UpdateDeviceTaskConfigV1Controller extends BaseController
|
||||
{
|
||||
/**
|
||||
* 先获取设备信息,确认设备存在且未删除
|
||||
*
|
||||
* @param int $deviceId
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function checkDeviceExists(int $deviceId)
|
||||
{
|
||||
$where = [
|
||||
'deviceId' => $deviceId,
|
||||
'companyId' => $this->getUserInfo('companyId'),
|
||||
];
|
||||
|
||||
$device = DeviceModel::find($where);
|
||||
|
||||
if (!$device) {
|
||||
throw new \Exception('设备不存在或已删除', 404);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查用户是否有权限操作指定设备
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加设备操作日志
|
||||
*
|
||||
* @param int $deviceId
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function addHandleLog(int $deviceId): void
|
||||
{
|
||||
$data = $this->request->post();
|
||||
$content = null;
|
||||
|
||||
if (isset($data['autoAddFriend']))/**/ $content = $data['autoAddFriend'] ? '开启自动添加好友' : '关闭自动添加好友';
|
||||
if (isset($data['autoReply']))/* */ $content = $data['autoReply'] ? '开启自动回复' : '关闭自动回复';
|
||||
if (isset($data['momentsSync']))/* */ $content = $data['momentsSync'] ? '开启朋友圈同步' : '关闭朋友圈同步';
|
||||
if (isset($data['aiChat']))/* */ $content = $data['aiChat'] ? '开启AI会话' : '关闭AI会话';
|
||||
|
||||
if (empty($content)) {
|
||||
throw new \Exception('参数错误', 400);
|
||||
}
|
||||
|
||||
DeviceHandleLogModel::addLog(
|
||||
[
|
||||
'deviceId' => $deviceId,
|
||||
'content' => $content,
|
||||
'userId' => $this->getUserInfo('id'),
|
||||
'companyId' => $this->getUserInfo('companyId'),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新设备taskConfig字段
|
||||
*
|
||||
* @param int $deviceId
|
||||
* @return void
|
||||
*/
|
||||
protected function setTaskconf(int $deviceId): void
|
||||
{
|
||||
$data = $this->request->post();
|
||||
$conf = DeviceTaskconf::where('deviceId', $deviceId)->find();
|
||||
|
||||
if ($conf) {
|
||||
DeviceTaskconf::where('deviceId', $deviceId)->update($data);
|
||||
} else {
|
||||
DeviceTaskconf::create(array_merge($data, [
|
||||
'companyId' => $this->getUserInfo('companyId'),
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新设备任务配置
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$id = $this->request->param('deviceId/d');
|
||||
|
||||
$this->checkDeviceExists($id);
|
||||
|
||||
if ($this->getUserInfo('isAdmin') != UserModel::ADMIN_STP) {
|
||||
$this->checkUserDevicePermission($id);
|
||||
}
|
||||
|
||||
try {
|
||||
Db::startTrans();
|
||||
|
||||
$this->addHandleLog($id);
|
||||
$this->setTaskconf($id);
|
||||
|
||||
Db::commit();
|
||||
|
||||
return ResponseHelper::success();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
return ResponseHelper::error($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user