Files
cunkebao_v3/Server/application/chukebao/controller/SettingsKefuController.php
Manus AI 5517457929 sync: 以本地为准同步全量变更至 GitHub
含四端需求文档、前端/后端/超管/触客宝迭代及部署脚本更新;未拉取远程。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-30 04:24:56 +08:00

130 lines
4.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\chukebao\controller;
use app\common\service\KefuAvatarSyncService;
use library\ResponseHelper;
use think\Db;
/**
* 模块 I · 设置页聚合(需求六 §6.4
* 聚合层在 chukebao内部调 cunkebao/共享表;触客宝不拼多个异构接口。
*/
class SettingsKefuController extends BaseController
{
/** GET /v1/kefu/settings/profile */
public function profile()
{
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
$accountId = $this->getUserInfo('s2_accountId');
$user = Db::name('users')->where('id', $userId)->find();
$companyName = Db::name('company')->where('id', $companyId)->value('name');
// 该客服管理的微信号
$wechatAccounts = [];
if (!empty($accountId)) {
$ids = Db::table('s2_wechat_friend')
->where(['accountId' => $accountId, 'isDeleted' => 0])
->group('wechatAccountId')
->column('wechatAccountId');
if (!empty($ids)) {
$wechatAccounts = Db::table('s2_wechat_account')
->whereIn('id', $ids)
->field('id,nickname,alias')
->select();
}
}
return ResponseHelper::success([
'avatar' => $user['avatar'] ?? '',
'nickname' => $user['username'] ?? '',
'jobNo' => (string)($user['s2_accountId'] ?? ''),
'companyId' => $companyId,
'companyName' => $companyName ?: ('项目 ' . $companyId),
'wechatAccounts' => $wechatAccounts,
]);
}
/**
* POST /v1/kefu/settings/avatar
* body: { avatar } — 写入 ck_users.avatar 并回写 s2_company_account.avatar
*/
public function saveAvatar()
{
$userId = (int)$this->getUserInfo('id');
$companyId = (int)$this->getUserInfo('companyId');
$accountId = (int)$this->getUserInfo('s2_accountId');
$avatar = trim((string)$this->request->param('avatar', ''));
if ($avatar === '') {
return ResponseHelper::error('avatar 不能为空');
}
Db::name('users')->where('id', $userId)->update([
'avatar' => $avatar,
'updateTime' => time(),
]);
$synced = false;
if ($accountId > 0) {
$synced = KefuAvatarSyncService::syncToS2Account($companyId, $accountId, $avatar);
}
return ResponseHelper::success([
'avatar' => $avatar,
'synced' => $synced,
'accountId' => $accountId,
], '已保存');
}
/** GET /v1/kefu/settings/ai */
public function getAi()
{
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
$row = Db::name('ai_settings')->where(['userId' => $userId, 'companyId' => $companyId])->find();
$tk = [];
if ($row && !empty($row['config'])) {
$cfg = json_decode($row['config'], true) ?: [];
$tk = $cfg['touchkebao'] ?? [];
}
return ResponseHelper::success([
'defaultEnabled' => $tk['defaultEnabled'] ?? false,
'takeoverDelay' => $tk['takeoverDelay'] ?? 30,
'defaultKbId' => $tk['defaultKbId'] ?? null,
'costTip' => $tk['costTip'] ?? true,
]);
}
/** POST /v1/kefu/settings/ai */
public function saveAi()
{
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
$tk = [
'defaultEnabled' => (bool)$this->request->param('defaultEnabled', false),
'takeoverDelay' => (int)$this->request->param('takeoverDelay', 30),
'defaultKbId' => $this->request->param('defaultKbId', null),
'costTip' => (bool)$this->request->param('costTip', true),
];
$row = Db::name('ai_settings')->where(['userId' => $userId, 'companyId' => $companyId])->find();
if ($row) {
$cfg = json_decode($row['config'], true) ?: [];
$cfg['touchkebao'] = $tk;
Db::name('ai_settings')->where('id', $row['id'])
->update(['config' => json_encode($cfg, 256), 'updateTime' => time()]);
} else {
Db::name('ai_settings')->insert([
'companyId' => $companyId,
'userId' => $userId,
'config' => json_encode(['touchkebao' => $tk], 256),
'createTime' => time(),
'updateTime' => time(),
]);
}
return ResponseHelper::success(true, '已保存');
}
}