含 AI 助手、抖音 OAuth、流量池推送、支付获客、SuperAdmin 设备与场景管理等本地迭代;以本地为准单向推送,未拉取远程。 Co-authored-by: Cursor <cursoragent@cursor.com>
150 lines
5.1 KiB
PHP
150 lines
5.1 KiB
PHP
<?php
|
||
|
||
namespace app\cunkebao\controller;
|
||
|
||
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 library\ResponseHelper;
|
||
use think\Db;
|
||
use think\facade\Log;
|
||
|
||
/**
|
||
* 设备微信统计与通讯录刷新
|
||
*/
|
||
class DeviceWechat extends BaseController
|
||
{
|
||
/** GET /v1/wechats/count */
|
||
public function count()
|
||
{
|
||
$companyId = (int)$this->getUserInfo('companyId');
|
||
$deviceIds = $this->resolveDeviceIds($companyId);
|
||
if (empty($deviceIds)) {
|
||
return ResponseHelper::success(['count' => 0]);
|
||
}
|
||
|
||
$total = DeviceWechatLoginModel::where('companyId', $companyId)
|
||
->where('deviceId', 'in', $deviceIds)
|
||
->distinct(true)
|
||
->count('wechatId');
|
||
|
||
return ResponseHelper::success(['count' => (int)$total]);
|
||
}
|
||
|
||
/** GET /v1/wechats/device-count */
|
||
public function deviceCount()
|
||
{
|
||
$companyId = (int)$this->getUserInfo('companyId');
|
||
$deviceIds = $this->resolveDeviceIds($companyId);
|
||
if (empty($deviceIds)) {
|
||
return ResponseHelper::success(['deviceCount' => 0]);
|
||
}
|
||
|
||
$count = DeviceWechatLoginModel::where('companyId', $companyId)
|
||
->where('deviceId', 'in', $deviceIds)
|
||
->where('alive', DeviceWechatLoginModel::ALIVE_WECHAT_ACTIVE)
|
||
->distinct(true)
|
||
->count('deviceId');
|
||
|
||
return ResponseHelper::success(['deviceCount' => (int)$count]);
|
||
}
|
||
|
||
/**
|
||
* PUT /v1/wechats/refresh
|
||
* 对比 s2 好友库数量;write=1 时轻量更新已存在好友 nickname/avatar
|
||
*/
|
||
public function refresh()
|
||
{
|
||
try {
|
||
$wechatAccountId = (int)$this->request->param('wechatAccountId/d', 0);
|
||
$deviceId = (int)$this->request->param('deviceId/d', 0);
|
||
$writeDb = (bool)$this->request->param('write/d', 0);
|
||
|
||
if ($wechatAccountId <= 0 && $deviceId > 0) {
|
||
$wechatAccountId = (int)Db::table('s2_wechat_account')
|
||
->where('currentDeviceId', $deviceId)
|
||
->value('id');
|
||
}
|
||
|
||
if ($wechatAccountId <= 0) {
|
||
return ResponseHelper::error('缺少 wechatAccountId 或 deviceId', 400);
|
||
}
|
||
|
||
$account = Db::table('s2_wechat_account')->where('id', $wechatAccountId)->find();
|
||
if (empty($account)) {
|
||
return ResponseHelper::error('微信账号不存在', 404);
|
||
}
|
||
|
||
$ownerWechatId = (string)($account['wechatId'] ?? '');
|
||
$dbCount = 0;
|
||
if ($ownerWechatId !== '') {
|
||
$dbCount = (int)Db::table('s2_wechat_friend')
|
||
->where('ownerWechatId', $ownerWechatId)
|
||
->where('isDeleted', 0)
|
||
->count();
|
||
}
|
||
|
||
$written = 0;
|
||
if ($writeDb && $wechatAccountId > 0) {
|
||
$written = $this->refreshFriendMetaFromDb($wechatAccountId);
|
||
}
|
||
|
||
return ResponseHelper::success([
|
||
'wechatAccountId' => $wechatAccountId,
|
||
'ownerWechatId' => $ownerWechatId,
|
||
'sdk_contact_count' => null,
|
||
'sdk_available' => false,
|
||
'db_friend_count' => $dbCount,
|
||
'write_db' => $writeDb,
|
||
'written' => $written,
|
||
'message' => '工作手机 SDK 未接入时仅返回库内好友统计;完整同步需配置工作手机 BFF',
|
||
'refreshed_at' => time(),
|
||
]);
|
||
} catch (\Throwable $e) {
|
||
Log::error('DeviceWechat refresh: ' . $e->getMessage());
|
||
return ResponseHelper::error($e->getMessage(), 500);
|
||
}
|
||
}
|
||
|
||
/** @return int[] */
|
||
private function resolveDeviceIds(int $companyId): array
|
||
{
|
||
$isAdmin = (int)$this->getUserInfo('isAdmin');
|
||
if ($isAdmin === UserModel::ADMIN_STP) {
|
||
return DeviceModel::where('companyId', $companyId)->column('id');
|
||
}
|
||
|
||
return DeviceUserModel::where([
|
||
'userId' => $this->getUserInfo('id'),
|
||
'companyId' => $companyId,
|
||
])->column('deviceId');
|
||
}
|
||
|
||
/** 从 s2 账号表回填已存在好友的头像/昵称(不写新好友) */
|
||
private function refreshFriendMetaFromDb(int $wechatAccountId): int
|
||
{
|
||
$account = Db::table('s2_wechat_account')->where('id', $wechatAccountId)->find();
|
||
if (empty($account)) {
|
||
return 0;
|
||
}
|
||
|
||
$patch = array_filter([
|
||
'nickname' => $account['nickname'] ?? null,
|
||
'avatar' => $account['avatar'] ?? null,
|
||
], static function ($v) {
|
||
return $v !== null && $v !== '';
|
||
});
|
||
|
||
if (empty($patch) || empty($account['wechatId'])) {
|
||
return 0;
|
||
}
|
||
|
||
return (int)Db::table('s2_wechat_friend')
|
||
->where('wechatAccountId', $wechatAccountId)
|
||
->where('ownerWechatId', $account['wechatId'])
|
||
->where('isDeleted', 0)
|
||
->update($patch);
|
||
}
|
||
}
|