私域操盘手 - 设备关联微信的微信数据内容由原先的模拟数据填充业务逻辑,调整部分页面的数据展示

This commit is contained in:
柳清爽
2025-05-15 18:16:12 +08:00
parent 72fa1247d8
commit 82dc6940f7
6 changed files with 72 additions and 25 deletions

View File

@@ -5,8 +5,9 @@ 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;
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;
@@ -47,10 +48,10 @@ class GetDeviceDetailV1Controller extends BaseController
protected function parseExtraForBattery(string $extra): int
{
if (!empty($extra)) {
$extra = json_decode($extra, true);
$extra = json_decode($extra);
if (is_array($extra) && isset($extra['battery'])) {
return intval($extra['battery']);
if ($extra && isset($extra->battery)) {
return intval($extra->battery);
}
}
@@ -83,6 +84,25 @@ class GetDeviceDetailV1Controller extends BaseController
return ArrHelper::getValue('autoAddFriend,autoReply,momentsSync,aiChat', [], 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');
}
/**
* 统计设备登录微信的好友
*
@@ -92,24 +112,43 @@ class GetDeviceDetailV1Controller extends BaseController
*/
protected function getTotalFriend(int $deviceId): int
{
$companyId = $this->getUserInfo('companyId');
$ownerWechatId = DeviceWechatLogin::where(compact('companyId', 'deviceId'))->order('createTime desc')->value('wechatId');
$ownerWechatId = $this->getDeviceLatestWechatLogin($deviceId);
if ($ownerWechatId) {
return WechatFriendShipModel::where(['ownerWechatId' => $ownerWechatId])->count();
return WechatFriendShipModel::where(
[
'companyId' => $this->getUserInfo('companyId'),
'ownerWechatId' => $ownerWechatId
]
)
->count();
}
return 0;
}
/**
* TODO 获取设备绑定微信的消息总数
* 获取设备绑定微信的消息总数
*
* @param int $deviceId
* @return int
*/
protected function getThirtyDayMsgCount(): int
protected function getThirtyDayMsgCount(int $deviceId): int
{
$ownerWechatId = $this->getDeviceLatestWechatLogin($deviceId);
if ($ownerWechatId) {
$activity = (string)WechatCustomerModel::where(
[
'wechatId' => $ownerWechatId,
'companyId' => $this->getUserInfo('companyId')
]
)
->value('activity');
return json_decode($activity)->totalMsgCount ?? 0;
}
return 0;
}
@@ -134,7 +173,7 @@ class GetDeviceDetailV1Controller extends BaseController
$device['battery'] = $this->parseExtraForBattery($device['extra']);
$device['features'] = $this->getTaskConfig($id);
$device['totalFriend'] = $this->getTotalFriend($id);
$device['thirtyDayMsgCount'] = $this->getThirtyDayMsgCount();
$device['thirtyDayMsgCount'] = $this->getThirtyDayMsgCount($id);
// 设备最后活跃时间为设备状态更新时间
$device['lastUpdateTime'] = date('Y-m-d H:i:s', $device['lastUpdateTime']);
@@ -147,6 +186,7 @@ class GetDeviceDetailV1Controller extends BaseController
/**
* 获取设备详情
*
* @return \think\response\Json
*/
public function index()
@@ -158,9 +198,9 @@ class GetDeviceDetailV1Controller extends BaseController
$this->checkUserDevicePermission($id);
}
$resultSet = $this->getDeviceInfo($id);
return ResponseHelper::success($resultSet);
return ResponseHelper::success(
$this->getDeviceInfo($id)
);
} catch (\Exception $e) {
return ResponseHelper::error($e->getMessage(), $e->getCode());
}

View File

@@ -4,6 +4,7 @@ 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\WechatFriendShip as WechatFriendShipModel;
use app\cunkebao\controller\BaseController;
@@ -73,10 +74,10 @@ class GetDeviceListV1Controller extends BaseController
->field([
'd.id', 'd.imei', 'd.memo', 'd.alive',
'l.wechatId',
'wa.nickname', 'wa.alias', '0 totalFriend'
'a.nickname', 'a.alias', '0 totalFriend'
])
->leftJoin('device_wechat_login l', 'd.id = l.deviceId')
->leftJoin('wechat_account wa', 'l.wechatId = wa.wechatId')
->leftJoin('device_wechat_login l', 'd.id = l.deviceId and l.alive =' . DeviceWechatLoginModel::ALIVE_WECHAT_ACTIVE)
->leftJoin('wechat_account a', 'l.wechatId = a.wechatId')
->order('d.id desc');
foreach ($where as $key => $value) {
@@ -105,7 +106,13 @@ class GetDeviceListV1Controller extends BaseController
$sections = $item->toArray();
if ($item->wechatId) {
$sections['totalFriend'] = WechatFriendShipModel::where(['ownerWechatId' => $item->wechatId])->count();
$sections['totalFriend'] = WechatFriendShipModel::where(
[
'ownerWechatId' => $item->wechatId,
'companyId' => $this->getUserInfo('companyId')
]
)
->count();
}
array_push($resultSets, $sections);