Vue.component('user-component', {
props: {
isLogin: {
type: Boolean,
required: true
}
},
data() {
return {
userTable: null, // 用户表格实例
userDialog: null, // 用户编辑对话框
userForm: { // 用户表单数据
id: 0, // 0表示新增,其他表示编辑
companyId: 0,
name: '',
password: '',
email: '',
mobile: '',
avatarUrl: '',
roleId: 0,
},
userSearch: { // 用户搜索条件
keyword: '',
},
// 角色选项
roleOptions: [
{ id: 1, name: '普通用户' },
{ id: 3, name: '演示用户' },
{ id: 20000, name: '官网用户' },
],
// 企业列表(用于选择用户所属企业)
companyOptions: []
};
},
methods: {
/**
* 初始化用户管理模块
*/
init: async function() {
if (!this.isLogin) return;
try {
// 获取企业列表(用于用户选择企业)
await this.fetchCompanyOptions();
// 初始化用户表格
await this.initUserTable();
this.$nextTick(() => {
form.render();
});
} catch (error) {
console.error('初始化用户管理失败:', error);
this.popErrorMsg('初始化用户管理失败,请刷新重试');
}
},
/**
* 获取企业列表选项
*/
fetchCompanyOptions: async function() {
try {
const { data: companyRes } = await axios.get('/api/web/system-web-company/get-company-list', {
params: { page: 1, limit: 100 }
});
if (!companyRes.success) {
this.popWarningMsg(companyRes.msg || '获取企业列表失败');
return;
}
if (companyRes.data && companyRes.data.list) {
this.companyOptions = companyRes.data.list.map(company => {
return {
id: company.id,
name: company.name
};
});
}
} catch (error) {
console.error('获取企业列表失败:', error);
this.popErrorMsg('获取企业列表失败,请重试');
}
},
/**
* 初始化用户管理表格
*/
initUserTable: function() {
if (this.userTable) {
this.userTable.reload();
return;
}
const that = this;
this.userTable = table.render({
elem: '#user-table',
url: '/api/web/system-web-user/get-user-list',
method: 'get',
where: {
keyword: this.userSearch.keyword
},
page: true,
cols: [[
{field: 'id', title: 'ID', width: 60, sort: true, fixed: 'left'},
{
field: 'avatarUrl',
title: '头像',
width: 60,
fixed: 'left',
templet: function(d) {
return d.avatarUrl ?
`` :
`
`;
}
},
{
field: 'name',
title: '用户名',
width: 150,
fixed: 'left',
templet: function(d) {
// 添加用户名点击事件,显示用户详情
return `
${d.name}
`;
}
},
{
field: 'companyName', title: '所属企业', width: 250,
templet: function(d) {
// 点击企业名称打开右侧侧边栏企业详情
if(d.companyId && d.companyName.length > 0){
return `${d.companyName}`;
}else{
return `
企业已删除
`
}
}
},
{
field: 'roleName',
title: '角色',
width: 100,
templet: function(d) {
return `${d.roleName}`;
}
},
{field: 'email', title: '邮箱', width: 150},
{field: 'mobile', title: '手机', width: 150},
{
field: 'createTime',
title: '注册时间',
sort: true,
templet: function(d) {
return window.util.timeAgo(d.createTime);
}
},
{fixed: 'right', title: '操作', toolbar: '#user-table-ops', width: 90}
]],
parseData: function(res) {
if (!res.success) {
this.popErrorMsg(res.msg || '获取用户列表失败');
return {
"code": 1,
"msg": res.msg || '获取用户列表失败',
"count": 0,
"data": []
};
}
return {
"code": 0,
"msg": "",
"count": res.data.count,
"data": res.data.list
};
},
});
// 监听表格工具条
table.on('tool(user-table)', function(obj) {
const data = obj.data;
const event = obj.event;
if (event === 'edit') {
that.showEditUserDialog(data);
} else if (event === 'del') {
that.deleteUser(data);
}
});
// 监听企业名称链接点击事件
$('.tl-rtc-app-user-area').on('click', '.tl-rtc-app-company-detail-link', function(e) {
e.preventDefault();
const companyId = $(this).data('id');
if (companyId) {
window.subModule.$emit('show-company-detail', companyId);
}
});
// 监听用户名称链接点击事件
$('.tl-rtc-app-user-area').on('click', '.tl-rtc-app-user-detail-link', function(e) {
e.preventDefault();
const userId = $(this).data('id');
const companyId = $(this).data('company-id');
if (userId && companyId) {
window.subModule.$emit('show-user-detail', companyId, userId);
}
});
},
/**
* 搜索用户
*/
searchUsers: function() {
if (!this.userTable) {
this.initUserTable();
return;
}
this.userTable.reload({
where: {
keyword: this.userSearch.keyword
},
page: {
curr: 1
}
});
},
/**
* 显示添加用户对话框
*/
showAddUserDialog: function() {
// 重置表单
this.userForm = {
id: 0,
companyId: this.companyOptions.length > 0 ? this.companyOptions[0].id : 0,
name: '',
password: '',
email: '',
mobile: '',
avatarUrl: '',
roleId: 1, // 默认为普通用户
};
this.showUserDialog('添加用户');
},
/**
* 显示编辑用户对话框
* @param {Object} user 用户数据
*/
showEditUserDialog: function(user) {
// 填充表单
this.userForm = {
id: user.id,
companyId: user.companyId || 0,
name: user.name || '',
password: '', // 编辑时不显示密码
email: user.email || '',
mobile: user.mobile || '',
avatarUrl: user.avatarUrl || '',
roleId: user.roleId || 1,
};
this.showUserDialog('编辑用户');
},
/**
* 显示用户对话框(添加/编辑)
* @param {string} title 对话框标题
*/
showUserDialog: function(title) {
const that = this;
const isAdd = this.userForm.id === 0;
// 构建企业下拉选项
let companyOptionsHtml = '';
this.companyOptions.forEach(company => {
const selected = company.id === this.userForm.companyId ? 'selected' : '';
companyOptionsHtml += ``;
});
// 构建角色下拉选项
let roleOptionsHtml = '';
this.roleOptions.forEach(role => {
const selected = role.id === this.userForm.roleId ? 'selected' : '';
roleOptionsHtml += ``;
});
this.userDialog = layer.open({
type: 1,
title: title,
shadeClose: 1,
area: ['630px', '660px'],
content: `