通讯录-代码优化

main
ysn 3 days ago
parent e62dd368a0
commit 0c4b5cc03e
  1. 451
      src/views/contacts/index.vue

@ -1,30 +1,28 @@
<template> <template>
<div class="app-container addrbook-page"> <div class="app-container addrbook-page">
<!-- 组织架构树 --> <!-- 侧树区域 -->
<div class="addrbook-left"> <div class="addrbook-left">
<div <div
v-for="(item, idx) in leftTabList"
:key="idx"
class="latest-contacts" class="latest-contacts"
:class="{ active: isLatestContacts }" :class="{ active: item.flag && isLatestContacts }"
@click="showLatestContacts" @click="item.fn()"
> >
<i class="el-icon-time" /> <i :class="item.icon" />
<span>常用联系人</span> <span>{{ item.label }}</span>
</div>
<div class="latest-contacts">
<i class="el-icon-office-building" />
<span>组织架构</span>
</div> </div>
<el-tree <el-tree
ref="orgTree" ref="orgTree"
:data="treeData" :data="treeData"
:props="treeProps" :props="treeProps"
default-expand-all default-expand-all
:highlight-current="true" highlight-current
node-key="id" node-key="id"
@node-click="handleNodeClick" @node-click="handleNodeClick"
/> />
</div> </div>
<!-- 成员列表 --> <!-- 成员列表 -->
<div class="addrbook-center"> <div class="addrbook-center">
<div class="search-box"> <div class="search-box">
<el-input <el-input
@ -46,84 +44,73 @@
</div> </div>
<div v-loading="loading" class="member-list"> <div v-loading="loading" class="member-list">
<div <div
v-for="member in displayMembers" v-for="member in memberList"
:key="member.id" :key="member.id"
:class="[ class="member-item"
'member-item', :class="{
{ active: currentUser.id === member.id,
active: currentUser && currentUser.id === member.id, offline: !member.online,
offline: !member.online, }"
}, @click="setSelectUser(member)"
]"
@click="handleMemberClick(member)"
> >
<el-avatar <el-avatar
:size="32" :size="32"
:src="member.avatar | avatarFilter" :src="getAvatarUrl(member.avatar)"
icon="el-icon-user-solid" icon="el-icon-user-solid"
/> />
<span class="member-name">{{ member.name }}</span> <span class="member-name">{{ member.name }}</span>
<span v-if="member.online" class="online-dot" /> <span v-if="member.online" class="online-dot" />
</div> </div>
<el-empty <el-empty
v-if="!loading && displayMembers.length === 0" v-if="!loading && !memberList.length"
description="暂无成员" description="暂无成员"
/> />
</div> </div>
</div> </div>
<!-- 右列详情面板 -->
<!-- 右侧详情 -->
<div class="addrbook-right"> <div class="addrbook-right">
<div v-if="currentUser" class="detail-panel"> <div v-if="currentUser" class="detail-panel">
<div class="avatar-section"> <div class="avatar-section">
<el-avatar <el-avatar
:size="80" :size="80"
:src="currentUser.avatar | avatarFilter" :src="getAvatarUrl(currentUser.avatar)"
icon="el-icon-user-solid" icon="el-icon-user-solid"
/> />
</div> </div>
<div class="info-section"> <div class="info-section">
<div class="info-row"> <div v-for="info in userInfoList" :key="info.key" class="info-row">
<i class="el-icon-user" /> <i :class="info.icon" />
<span class="label">姓名</span> <span class="label">{{ info.label }}</span>
<el-tooltip :content="currentUser.name" placement="top"> <el-tooltip
<span class="value">{{ currentUser.name }}</span> :content="
</el-tooltip> info.key === 'online'
</div> ? currentUser.online
<div class="info-row"> ? '正常'
<i class="el-icon-message" /> : '离线'
<span class="label">邮箱</span> : currentUser[info.key]
<el-tooltip :content="currentUser.email || ''" placement="top"> "
<span class="value">{{ currentUser.email }}</span> placement="top"
</el-tooltip>
</div>
<div class="info-row">
<i class="el-icon-office-building" />
<span class="label">部门</span>
<el-tooltip :content="currentUser.full_group || ''" placement="top">
<span class="value">{{ currentUser.full_group }}</span>
</el-tooltip>
</div>
<div class="info-row">
<i class="el-icon-s-custom" />
<span class="label">职位</span>
<el-tooltip :content="currentUser.role || ''" placement="top">
<span class="value">{{ currentUser.role }}</span>
</el-tooltip>
</div>
<div class="info-row">
<i class="el-icon-phone" />
<span class="label">手机</span>
<span class="value">{{ currentUser.phone }}</span>
</div>
<div class="info-row">
<i class="el-icon-info" />
<span class="label">状态</span>
<span
class="value"
:class="currentUser.online ? 'online' : 'offline'"
> >
{{ currentUser.online ? "正常" : "离线" }} <span
</span> class="value"
:class="
info.key === 'online'
? currentUser.online
? 'online'
: 'offline'
: ''
"
>
{{
info.key === "online"
? currentUser.online
? "正常"
: "离线"
: currentUser[info.key]
}}
</span>
</el-tooltip>
</div> </div>
</div> </div>
<div class="action-section"> <div class="action-section">
@ -144,7 +131,6 @@
</template> </template>
<script> <script>
import EventBus from "@/utils/eventBus";
import { import {
getMessagesLatestContacts, getMessagesLatestContacts,
getGroupsList, getGroupsList,
@ -153,121 +139,106 @@ import {
} from "@/api/contacts/index.js"; } from "@/api/contacts/index.js";
export default { export default {
name: "Contacts", name: "Contacts",
filters: {
avatarFilter(avatar) {
if (!avatar) return "";
if (avatar.startsWith("http")) return avatar;
// MinIO 访 netConfig
return avatar;
},
},
data() { data() {
return { return {
treeData: [], treeData: [],
treeProps: { treeProps: { label: "name", children: "child" },
label: "name",
children: "children",
},
expandedKeys: [],
currentGroupId: null, currentGroupId: null,
currentStructureName: "", currentStructureName: "",
memberList: [], memberList: [],
offlineMemberList: [],
currentUser: null, currentUser: null,
searchText: "", searchText: "",
loading: false, loading: false,
isLatestContacts: false, isLatestContacts: false,
userGroupId: null, userGroupId: null,
//
leftTabList: [
{
label: "常用联系人",
icon: "el-icon-time",
flag: true,
fn: () => this.showLatestContacts(),
},
{
label: "组织架构",
icon: "el-icon-office-building",
flag: false,
fn: () => this.fetchOrgTree(),
},
],
//
userInfoList: [
{ label: "姓名", icon: "el-icon-user", key: "name" },
{ label: "邮箱", icon: "el-icon-message", key: "email" },
{ label: "部门", icon: "el-icon-office-building", key: "full_group" },
{ label: "职位", icon: "el-icon-s-custom", key: "role" },
{ label: "手机", icon: "el-icon-phone", key: "phone" },
{ label: "状态", icon: "el-icon-info", key: "online" },
],
}; };
}, },
computed: { computed: {
displayMembers() { MINIO_BASE() {
// 线线 Qt return this.$store.state.user.netConfig.MINIO_ENDPOINT_HTTPS || "";
const online = this.memberList.filter((m) => m.online);
const offline = this.memberList.filter((m) => !m.online);
return [...online, ...offline];
},
currentUserInfo() {
return this.$store.state.user.userInfo;
}, },
}, },
mounted() { mounted() {
//
this.showLatestContacts();
this.fetchOrgTree(); this.fetchOrgTree();
this.showLatestContacts();
}, },
methods: { methods: {
// Qt Cmd_addr_book_addr POST body //
getAvatarUrl(avatar) {
if (!avatar) return "";
return avatar.startsWith("http") ? avatar : `${this.MINIO_BASE}${avatar}`;
},
//
setSelectUser(user) {
this.currentUser = { ...user };
},
//
async fetchOrgTree() { async fetchOrgTree() {
try { try {
const res = await getGroupsList(); const { data } = await getGroupsList();
const list = res.data?.list || []; this.treeData =data.list
this.treeData = this.buildTree(list); if (!this.userGroupId) return;
// const parentKeys = this.findParentKeys(this.treeData, this.userGroupId);
if (this.userGroupId) { this.$nextTick(() => {
this.expandedKeys = this.findParentKeys( this.$refs.orgTree.setCurrentKey(this.userGroupId);
this.treeData, const node = this.findNodeById(this.treeData, this.userGroupId);
this.userGroupId if (node) {
); this.currentStructureName = node.name;
this.$nextTick(() => { this.fetchMembers(this.userGroupId);
this.$refs.orgTree.setCurrentKey(this.userGroupId); }
if (this.userGroupId) { });
this.fetchMembers(this.userGroupId); } catch (err) {
const node = this.findNodeById(this.treeData, this.userGroupId); console.error("获取组织架构失败", err);
if (node) {
this.currentStructureName = node.name;
}
}
});
}
} catch (e) {
console.error("获取组织架构失败", e);
} }
}, },
//
buildTree(list) {
return list.map((item) => ({
id: item.id,
name: item.name,
level: item.level,
children:
item.child && item.child.length > 0
? this.buildTree(item.child)
: undefined,
}));
},
//
findParentKeys(tree, targetId, parents = []) { findParentKeys(tree, targetId, parents = []) {
for (const node of tree) { for (const node of tree) {
if (node.id === targetId) { if (node.id === targetId) return [...parents, node.id];
return [...parents, node.id]; if (node.children.length) {
} const res = this.findParentKeys(node.children, targetId, [
if (node.children && node.children.length > 0) {
const found = this.findParentKeys(node.children, targetId, [
...parents, ...parents,
node.id, node.id,
]); ]);
if (found.length > 0) return found; if (res.length) return res;
} }
} }
return []; return [];
}, },
// ID
findNodeById(tree, id) { findNodeById(tree, id) {
for (const node of tree) { for (const node of tree) {
if (node.id === id) return node; if (node.id === id) return node;
if (node.children) { if (node.children) {
const found = this.findNodeById(node.children, id); const res = this.findNodeById(node.children, id);
if (found) return found; if (res) return res;
} }
} }
return null; return null;
}, },
//
// Qt OnListWidgetTreeMenuSwitch
handleNodeClick(data) { handleNodeClick(data) {
this.isLatestContacts = false; this.isLatestContacts = false;
this.currentStructureName = data.name; this.currentStructureName = data.name;
@ -275,157 +246,86 @@ export default {
this.currentUser = null; this.currentUser = null;
this.fetchMembers(data.id); this.fetchMembers(data.id);
}, },
//
// Qt GetAddrBookDetailInfo POST { group_id }
async fetchMembers(groupId) { async fetchMembers(groupId) {
this.loading = true; this.loading = true;
try { try {
const res = await getGroupsListUser({ group_id: groupId }); const { data } = await getGroupsListUser({ group_id: groupId });
const list = res.data?.list || []; this.memberList = data.list || [];
// Qt 线线 memberList computed this.memberList.length && this.setSelectUser(this.memberList[0]);
this.memberList = list.map((item) => ({ } catch (err) {
id: item.id, console.error("获取成员失败", err);
name: item.name,
role: item.role,
phone: item.phone,
status: item.status,
email: item.email,
group: item.group,
full_group: item.full_group,
avatar: item.avatar,
online: item.online === 1 || item.online === true,
}));
//
if (this.memberList.length > 0) {
this.handleMemberClick(this.memberList[0]);
}
console.log(this.memberList, "组成员");
} catch (e) {
console.error("获取成员列表失败", e);
this.memberList = []; this.memberList = [];
} finally { } finally {
this.loading = false; this.loading = false;
} }
}, },
//
// Qt SendLatestContactsSlot async showLatestContacts() {
showLatestContacts() {
this.isLatestContacts = true; this.isLatestContacts = true;
this.currentStructureName = "最近联系人"; this.currentStructureName = "最近联系人";
this.currentGroupId = null; this.currentGroupId = null;
this.currentUser = null; this.currentUser = null;
//
this.$refs.orgTree.setCurrentKey(null); this.$refs.orgTree.setCurrentKey(null);
this.fetchLatestContacts();
},
// Qt GetLatestContacts POST { scene: 1, page: 1, size: 65535 }
async fetchLatestContacts() {
this.loading = true; this.loading = true;
try { try {
const res = await getMessagesLatestContacts({ const { data } = await getMessagesLatestContacts({
scene: 1, scene: 1,
page: 1, page: 1,
size: 65535, size: 65535,
}); });
const list = res.data?.list || []; this.memberList = data.list || [];
this.memberList = list.map((item) => ({ this.memberList.length && this.setSelectUser(this.memberList[0]);
id: item.id, } catch (err) {
name: item.name, console.error("常用联系人异常", err);
role: item.role,
phone: item.phone,
status: item.status,
email: item.email,
group: item.group,
full_group: item.full_group,
avatar: item.avatar,
online: item.online === 1 || item.online === true,
}));
//
if (this.memberList.length > 0) {
this.handleMemberClick(this.memberList[0]);
}
} catch (e) {
console.error("获取最近联系人失败", e);
this.memberList = []; this.memberList = [];
} finally { } finally {
this.loading = false; this.loading = false;
} }
}, },
//
// Qt OnSecListMenuSwitch
handleMemberClick(member) {
this.currentUser = { ...member };
},
// Qt searchContactsClicked
async handleSearch() { async handleSearch() {
const text = this.searchText.trim(); const keyword = this.searchText.trim();
if (!text) { if (!keyword) return this.handleClearSearch();
if (this.isLatestContacts) {
this.fetchLatestContacts();
} else if (this.currentGroupId) {
this.fetchMembers(this.currentGroupId);
}
return;
}
this.loading = true; this.loading = true;
try { try {
const res = await searchUsers({ const { data } = await searchUsers({
search_key: text, search_key: keyword,
user_ids: [], user_ids: [],
page: 1, page: 1,
size: 10000, size: 10000,
}); });
const list = res.data?.list || [];
this.isLatestContacts = true; this.isLatestContacts = true;
this.currentStructureName = "搜索结果"; this.currentStructureName = "搜索结果";
this.currentGroupId = null; this.currentGroupId = null;
this.$refs.orgTree.setCurrentKey(null); this.$refs.orgTree.setCurrentKey(null);
this.memberList = list.map((item) => ({ this.memberList = (data.list || []).map((item) => ({
id: item.id, ...item,
name: item.name, online: [1, true].includes(item.online),
role: item.role,
phone: item.phone,
status: item.status,
email: item.email,
group: item.group,
full_group: item.full_group,
avatar: item.avatar,
online: item.online === 1 || item.online === true,
})); }));
} catch (e) { this.memberList.length && this.setSelectUser(this.memberList[0]);
console.error("搜索联系人失败", e); } catch (err) {
console.error("搜索失败", err);
} finally { } finally {
this.loading = false; this.loading = false;
} }
}, },
//
//
handleClearSearch() { handleClearSearch() {
this.searchText = ""; this.searchText = "";
if (this.isLatestContacts) { if (this.isLatestContacts) this.showLatestContacts();
this.fetchLatestContacts(); else if (this.currentGroupId) this.fetchMembers(this.currentGroupId);
} else if (this.currentGroupId) { else if (this.userGroupId) this.fetchMembers(this.userGroupId);
this.fetchMembers(this.currentGroupId);
} else if (this.userGroupId) {
this.fetchMembers(this.userGroupId);
}
}, },
// //
handleRefresh() { handleRefresh() {
if (this.isLatestContacts) { this.isLatestContacts
this.fetchLatestContacts(); ? this.showLatestContacts()
} else if (this.currentGroupId) { : this.fetchMembers(this.currentGroupId);
this.fetchMembers(this.currentGroupId);
}
}, },
//
// Qt SendMessag / SignalSendSelectedContactInfo
sendMessage() { sendMessage() {
if (!this.currentUser) return; if (!this.currentUser) return;
//
this.$router.push({ this.$router.push({
path: "/message", path: "/message",
query: { query: {
@ -444,12 +344,11 @@ export default {
display: flex; display: flex;
height: 100%; height: 100%;
} }
//
.addrbook-left { .addrbook-left {
width: 240px; width: 240px;
border-right: 1px solid #ebeef5; border-right: 1px solid #ebeef5;
overflow-y: auto; overflow-y: auto;
.latest-contacts { .latest-contacts {
display: flex; display: flex;
align-items: center; align-items: center;
@ -461,78 +360,61 @@ export default {
font-size: 14px; font-size: 14px;
color: #606266; color: #606266;
transition: all 0.2s; transition: all 0.2s;
i { i {
font-size: 16px; font-size: 16px;
} }
&:hover,
&:hover {
background: #e5f1fb;
color: #409eff;
}
&.active { &.active {
background: #e5f1fb; background: #e5f1fb;
color: #409eff; color: #409eff;
} }
} }
::v-deep .el-tree { ::v-deep .el-tree {
background: transparent; background: transparent;
.el-tree-node__content { .el-tree-node__content {
height: 40px; height: 40px;
} }
.el-tree-node:focus > .el-tree-node__content { .el-tree-node:focus > .el-tree-node__content {
background-color: #e5f1fb; background-color: #e5f1fb;
} }
.el-tree-node__content:hover { .el-tree-node__content:hover {
background-color: #f0f7ff; background-color: #f0f7ff;
} }
} }
} }
//
.addrbook-center { .addrbook-center {
width: 280px; width: 280px;
border-right: 1px solid #ebeef5; border-right: 1px solid #ebeef5;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
.search-box,
.center-header { .center-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 12px 16px; padding: 12px 16px;
border-bottom: 1px solid #ebeef5; border-bottom: 1px solid #ebeef5;
height: 61px; height: 61px;
}
.center-header {
display: flex;
align-items: center;
justify-content: space-between;
.structure-name { .structure-name {
font-size: 14px; font-size: 14px;
font-weight: 500; font-weight: 500;
color: #303133; color: #303133;
} }
.refresh-btn { .refresh-btn {
font-size: 16px; font-size: 16px;
color: #909399; color: #909399;
cursor: pointer; cursor: pointer;
&:hover { &:hover {
color: #409eff; color: #409eff;
} }
} }
} }
.search-box {
padding: 12px 16px;
border-bottom: 1px solid #ebeef5;
}
.member-list { .member-list {
flex: 1; flex: 1;
overflow-y: auto; overflow-y: auto;
.member-item { .member-item {
display: flex; display: flex;
align-items: center; align-items: center;
@ -540,31 +422,23 @@ export default {
padding: 8px 16px; padding: 8px 16px;
cursor: pointer; cursor: pointer;
transition: background 0.2s; transition: background 0.2s;
position: relative;
&:hover { &:hover {
background: #f5f7fa; background: #f5f7fa;
} }
&.active { &.active {
background: #e5f1fb; background: #e5f1fb;
} }
&.offline .member-name {
&.offline { color: #909399;
.member-name {
color: #909399;
}
} }
.member-name { .member-name {
font-size: 14px;
color: #303133;
flex: 1; flex: 1;
overflow: hidden; overflow: hidden;
text-overflow: ellipsis; text-overflow: ellipsis;
white-space: nowrap; white-space: nowrap;
font-size: 14px;
color: #303133;
} }
.online-dot { .online-dot {
width: 8px; width: 8px;
height: 8px; height: 8px;
@ -575,26 +449,22 @@ export default {
} }
} }
} }
//
.addrbook-right { .addrbook-right {
flex: 1; flex: 1;
background: #fff;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
.detail-panel { .detail-panel {
width: 100%; width: 100%;
max-width: 400px; max-width: 400px;
padding: 40px 24px; padding: 40px 24px;
.avatar-section { .avatar-section {
display: flex; display: flex;
justify-content: center; justify-content: center;
margin-bottom: 32px; margin-bottom: 32px;
} }
.info-section { .info-section {
.info-row { .info-row {
display: flex; display: flex;
@ -609,14 +479,12 @@ export default {
width: 20px; width: 20px;
text-align: center; text-align: center;
} }
.label { .label {
width: 70px; width: 70px;
font-size: 14px; font-size: 14px;
color: #909399; color: #909399;
flex-shrink: 0; flex-shrink: 0;
} }
.value { .value {
flex: 1; flex: 1;
font-size: 14px; font-size: 14px;
@ -625,30 +493,25 @@ export default {
text-overflow: ellipsis; text-overflow: ellipsis;
white-space: nowrap; white-space: nowrap;
text-align: right; text-align: right;
&.online { &.online {
color: #67c23a; color: #67c23a;
} }
&.offline { &.offline {
color: #909399; color: #909399;
} }
} }
} }
} }
.action-section { .action-section {
display: flex; display: flex;
gap: 16px; gap: 16px;
justify-content: center; justify-content: center;
margin-top: 32px; margin-top: 32px;
.el-button { .el-button {
min-width: 120px; min-width: 120px;
} }
} }
} }
.empty-detail { .empty-detail {
width: 100%; width: 100%;
height: 100%; height: 100%;

Loading…
Cancel
Save