C2CSettingState

最近更新时间:2025-09-12 18:14:52

我的收藏

概述

useC2CSettingState 是一个 Vue 3 Composition API Hook,用于管理单聊(C2C)设置相关的状态和业务操作。它提供了用户信息、聊天设置状态的响应式数据,以及置顶、免打扰、备注等核心功能的操作方法。该 Hook 会自动监听当前会话变化,并同步更新相关状态。

什么时候使用 C2CSettingState?

当目前的 ChatSetting 组件能力无法满足您的特定需求,或者您希望基于底层数据重新开发一个自定义的单聊设置组件时,可以使用 useC2CSettingState() 组合式函数来获取原始的数据源和相关的操作方法,进而进行灵活的定制和开发。

返回值速查表

字段
类型
描述
userID
Ref<string | undefined>
当前聊天用户的 ID
nick
Ref<string | undefined>
用户昵称
avatar
Ref<string | undefined>
用户头像 URL
signature
Ref<string | undefined>
用户个性签名
remark
Ref<string | undefined>
用户备注名
isMuted
Ref<boolean | undefined>
是否开启免打扰
isPinned
Ref<boolean | undefined>
是否置顶会话
isContact
Ref<boolean | undefined>
是否为好友关系
(value: boolean) => void
设置会话置顶状态
(value: boolean) => void
设置会话免打扰状态
(remark: string) => void
设置用户备注

详细介绍

userID

类型:Ref<string | undefined>
详细说明:当前单聊会话中对方用户的唯一标识符,默认值为 undefined

nick

类型:Ref<string | undefined>
详细说明:当前单聊用户的昵称,从用户资料中获取。当没有激活的单聊会话或正在加载时,该值为 undefined

avatar

类型:Ref<string | undefined>
详细说明:当前单聊用户的头像 URL 地址。当没有激活的单聊会话或正在加载时,该值为 undefined

signature

类型:Ref<string | undefined>
详细说明:当前单聊用户的个性签名。当没有激活的单聊会话或正在加载时,该值为 undefined

remark

类型:Ref<string | undefined>
详细说明:当前用户对聊天对象设置的备注名。当没有激活的单聊会话或正在加载时,该值为 undefined

isMuted

类型:Ref<boolean | undefined>
详细说明:当前会话是否开启免打扰模式,开启后不会收到消息通知。当没有激活的单聊会话或正在加载时,该值为 undefined

isPinned

类型:Ref<boolean | undefined>
详细说明:当前会话是否置顶显示,置顶的会话会在会话列表顶部显示。当没有激活的单聊会话或正在加载时,该值为 undefined

isContact

类型:Ref<boolean | undefined>
详细说明:当前聊天用户是否为好友关系,会自动检查双向好友关系。当没有激活的单聊会话或正在加载时,该值为 undefined

setChatPinned

类型:(value: boolean) => void
详细说明:设置当前会话的置顶状态,传入 true 置顶会话,传入 false 取消置顶。

setChatMuted

类型:(value: boolean) => void
详细说明:设置当前会话的免打扰状态,传入 true 开启免打扰,传入 false 关闭免打扰。

setUserRemark

类型:(remark: string) => void
详细说明:设置当前聊天用户的备注名,用于个性化显示用户名称。

使用示例

下面将展示如何使用 C2CChatSettingState 实现一个单聊的设置页面。
<template>
<div class="c2c-chat-setting">
<!-- 用户信息卡片 -->
<div class="user-card">
<div class="user-avatar">
<img
:src="avatar"
:alt="displayName"
class="avatar-image"
>
</div>

<div class="user-info">
<h2 class="user-name">
{{ displayName }}
</h2>
<p class="user-id">
ID: {{ userID }}
</p>
<p v-if="signature" class="user-signature">
{{ signature }}
</p>
<div class="contact-status">
<span class="status-badge" :class="contactStatusClass">
{{ contactStatusText }}
</span>
</div>
</div>
</div>

<!-- 备注设置 -->
<div class="setting-section">
<h3 class="section-title">
Remark Setting
</h3>
<div class="remark-setting">
<div class="input-group">
<label for="remark">remark:</label>
<input
id="remark"
v-model="remarkInput"
type="text"
placeholder="enter your remark"
maxlength="20"
class="remark-input"
>
<button
:disabled="!remarkInput.trim() || remarkInput === remark"
class="save-btn"
@click="handleSaveRemark"
>
Save
</button>
</div>
<p v-if="remark" class="current-remark">
current remark:<strong>{{ remark }}</strong>
</p>
</div>
</div>

<!-- 聊天设置 -->
<div class="setting-section">
<h3 class="section-title">
Chat Setting
</h3>

<!-- 置顶设置 -->
<div class="setting-item">
<div class="setting-info">
<span class="setting-label">Pin</span>
<span class="setting-desc">The chat will be displayed at the top of the list after being pinned</span>
</div>
<label class="switch">
<input
type="checkbox"
:checked="isPinned"
@change="handlePinnedChange"
>
<span class="switch-slider" />
</label>
</div>

<!-- 免打扰设置 -->
<div class="setting-item">
<div class="setting-info">
<span class="setting-label">Mute</span>
<span class="setting-desc">After being muted, you will not receive messages from this chat</span>
</div>
<label class="switch">
<input
type="checkbox"
:checked="isMuted"
@change="handleMutedChange"
>
<span class="switch-slider" />
</label>
</div>
</div>

<!-- 快速操作 -->
<div class="setting-section">
<h3 class="section-title">
Quick Actions
</h3>
<div class="quick-actions">
<button
class="action-btn work-mode"
:class="{ active: isWorkMode }"
@click="setWorkMode"
>
<span class="btn-icon">💼</span>
<span class="btn-text">Work Mode</span>
<span class="btn-desc">Pinned + Mute</span>
</button>

<button
class="action-btn normal-mode"
:class="{ active: isNormalMode }"
@click="setNormalMode"
>
<span class="btn-icon">💬</span>
<span class="btn-text">Normal Mode</span>
<span class="btn-desc">Receive messages normally</span>
</button>

<button
class="action-btn important-mode"
:class="{ active: isImportantMode }"
@click="setImportantMode"
>
<span class="btn-icon"></span>
<span class="btn-text">Important Contact</span>
<span class="btn-desc">Only display at the top</span>
</button>
</div>
</div>

<!-- 操作反馈 -->
<div
v-if="feedbackMessage"
class="feedback-message"
:class="feedbackType"
>
{{ feedbackMessage }}
</div>
</div>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue';
import { useC2CSettingState } from '@tencentcloud/chat-uikit-vue3';

// 使用 C2C 设置状态
const {
userID,
nick,
avatar,
signature,
remark,
isMuted,
isPinned,
isContact,
setChatPinned,
setChatMuted,
setUserRemark,
} = useC2CSettingState();

// 本地状态
const remarkInput = ref('');
const feedbackMessage = ref('');
const feedbackType = ref<'success' | 'error' | 'info'>('info');

// 计算属性
const displayName = computed(() => remark.value || nick.value || 'unknown user');

const contactStatusClass = computed(() => isContact.value ? 'friend' : 'stranger');

const contactStatusText = computed(() => isContact.value ? 'Friend' : 'Stranger');

// 模式判断
const isWorkMode = computed(() => isPinned.value && isMuted.value);

const isNormalMode = computed(() => !isPinned.value && !isMuted.value);

const isImportantMode = computed(() => isPinned.value && !isMuted.value);

// 监听备注变化
watch(remark, (newRemark) => {
remarkInput.value = newRemark || '';
}, { immediate: true });

// 显示反馈消息
const showFeedback = (message: string, type: 'success' | 'error' | 'info' = 'info') => {
feedbackMessage.value = message;
feedbackType.value = type;

setTimeout(() => {
feedbackMessage.value = '';
}, 3000);
};

// 事件处理
const handleSaveRemark = () => {
const newRemark = remarkInput.value.trim();
if (!newRemark) {
showFeedback('Please enter the remark name', 'error');
return;
}

if (newRemark === remark.value) {
showFeedback('The remark name has not changed', 'info');
return;
}

try {
setUserRemark(newRemark);
showFeedback(`The remark has been updated to: ${newRemark}`, 'success');
} catch (error) {
showFeedback('The remark update failed, please try again', 'error');
}
};

const handlePinnedChange = (event: Event) => {
const target = event.target as HTMLInputElement;
try {
setChatPinned(target.checked);
showFeedback(target.checked ? 'The chat has been pinned' : 'The chat has been unpinned', 'success');
} catch (error) {
showFeedback('The pinned setting failed, please try again', 'error');
}
};

const handleMutedChange = (event: Event) => {
const target = event.target as HTMLInputElement;
try {
setChatMuted(target.checked);
showFeedback(target.checked ? 'The chat has been muted' : 'The chat has been unmuted', 'success');
} catch (error) {
showFeedback('The muted setting failed, please try again', 'error');
}
};

// 快速模式设置
const setWorkMode = () => {
try {
setChatPinned(true);
setChatMuted(true);
showFeedback('The chat has been switched to work mode', 'success');
} catch (error) {
showFeedback('The work mode setting failed, please try again', 'error');
}
};

const setNormalMode = () => {
try {
setChatPinned(false);
setChatMuted(false);
showFeedback('The chat has been switched to normal mode', 'success');
} catch (error) {
showFeedback('The normal mode setting failed, please try again', 'error');
}
};

const setImportantMode = () => {
try {
setChatPinned(true);
setChatMuted(false);
showFeedback('The chat has been set as an important contact', 'success');
} catch (error) {
showFeedback('The important contact setting failed, please try again', 'error');
}
};
</script>
示例效果:


相关文档

交流与反馈

如遇任何问题,可联系 官网售后 反馈,享有专业工程师的支持,解决您的难题。