130 lines
4.6 KiB
PHP
130 lines
4.6 KiB
PHP
<?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, '已保存');
|
||
}
|
||
}
|