62 lines
1.7 KiB
PHP
62 lines
1.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
header('Cache-Control: no-store');
|
|
|
|
require __DIR__ . '/im_web_bootstrap.php';
|
|
|
|
$payload = readJsonPayload();
|
|
$userId = trim((string)($payload['userId'] ?? ''));
|
|
$targetId = trim((string)($payload['targetId'] ?? $payload['friendUid'] ?? ''));
|
|
$alias = trim((string)($payload['alias'] ?? ''));
|
|
|
|
if ($userId === '') {
|
|
respond(400, '缺少用户ID');
|
|
}
|
|
if ($targetId === '') {
|
|
respond(400, '缺少联系人ID');
|
|
}
|
|
if ($targetId === $userId) {
|
|
respond(400, '不能给自己设置备注名');
|
|
}
|
|
if (mb_strlen($alias, 'UTF-8') > 32) {
|
|
respond(400, '备注名不能超过32个字');
|
|
}
|
|
|
|
try {
|
|
ensureWebImAppTables();
|
|
$imDb = imDb();
|
|
$exists = $imDb->prepare('select 1 from `t_user` where `_uid` = ? limit 1');
|
|
$exists->execute([$targetId]);
|
|
if (!$exists->fetchColumn()) {
|
|
respond(404, '没有找到该用户');
|
|
}
|
|
|
|
$friend = $imDb->prepare('select 1 from `t_friend` where `_uid` = ? and `_friend_uid` = ? limit 1');
|
|
$friend->execute([$userId, $targetId]);
|
|
$isContact = (bool)$friend->fetchColumn();
|
|
|
|
if ($isContact) {
|
|
requireImSuccess(
|
|
imAdminPost('/admin/friend/set_alias', [
|
|
'operator' => $userId,
|
|
'targetId' => $targetId,
|
|
'alias' => $alias,
|
|
]),
|
|
'备注名修改失败'
|
|
);
|
|
}
|
|
saveWebUserRemark(imAppDb(), $userId, $targetId, $alias);
|
|
|
|
respond(0, 'success', [
|
|
'contact' => [
|
|
'uid' => $targetId,
|
|
'alias' => $alias,
|
|
'isContact' => $isContact,
|
|
],
|
|
]);
|
|
} catch (Throwable $error) {
|
|
respond(500, $error->getMessage() ?: '备注名服务暂不可用');
|
|
}
|