Vue.component('company-component', { props: { isLogin: { type: Boolean, required: true } }, data() { return { companyTable: null, // 企业表格实例 companyDialog: null, // 企业编辑对话框 companyForm: { // 企业表单数据 id: 0, // 0表示新增,其他表示编辑 code: '', name: '', address: '', phone: '', email: '', website: '', logo: '', description: '', expiredStatus: false, authStatus: false, createTime: '' }, companySearch: { // 企业搜索条件 keyword: '', } }; }, methods: { /** * 初始化企业管理模块 */ init: async function() { if (!this.isLogin) return; try { await this.initCompanyTable(); this.$nextTick(() => { form.render(); }); } catch (error) { console.error('初始化企业管理失败:', error); this.popErrorMsg('初始化企业管理失败,请刷新重试'); } }, /** * 初始化企业管理表格 */ initCompanyTable: function() { if (this.companyTable) { this.companyTable.reload(); return; } const that = this; this.companyTable = table.render({ elem: '#company-table', url: '/api/web/system-web-company/get-company-list', method: 'get', where: { keyword: this.companySearch.keyword }, page: true, cols: [[ {field: 'id', title: 'ID', width: 60, sort: true, fixed: 'left'}, {field: 'code', title: '邀请码', width: 120, fixed: 'left'}, {field: 'name', title: '企业名称', width: 250}, {field: 'phone', title: '联系电话', width: 150}, {field: 'email', title: '企业邮箱', width: 150}, {field: 'website', title: '企业网站', width: 150}, {field: 'description', title: '企业描述', width: 180}, // { // field: 'authStatus', // title: '认证状态', // width: 100, // templet: function(d){ // return ` // // `; // } // }, { field: 'createTime', title: '创建时间', sort: true, templet: function(d) { return window.util.timeAgo(d.createTime); } }, {fixed: 'right', title: '操作', toolbar: '#company-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(company-table)', function(obj) { const data = obj.data; const event = obj.event; if (event === 'edit') { that.showEditCompanyDialog(data); } else if (event === 'del') { that.deleteCompany(data.id); } }); // 监听认证状态切换 form.on('switch(company-auth-filter)', function(data) { const companyId = parseInt(data.value); const checked = data.elem.checked; that.updateCompanyAuthStatus(companyId, checked); }); }, /** * 搜索企业 */ searchCompanies: function() { if (!this.companyTable) { this.initCompanyTable(); return; } this.companyTable.reload({ where: { keyword: this.companySearch.keyword }, page: { curr: 1 } }); }, /** * 显示添加企业对话框 */ showAddCompanyDialog: function() { // 重置表单 this.companyForm = { id: 0, code: '', name: '', address: '', phone: '', email: '', website: '', logo: '', description: '', expiredStatus: false, authStatus: false }; this.showCompanyDialog('添加企业'); }, /** * 显示编辑企业对话框 * @param {Object} company 企业数据 */ showEditCompanyDialog: function(company) { // 填充表单 this.companyForm = { id: company.id, code: company.code, name: company.name, address: company.address || '', phone: company.phone || '', email: company.email || '', website: company.website || '', logo: company.logo || '', description: company.description || '', authStatus: company.authStatus || false, expiredStatus: company.expiredStatus || false }; this.showCompanyDialog('编辑企业'); }, /** * 显示企业对话框(添加/编辑) * @param {string} title 对话框标题 */ showCompanyDialog: function(title) { const that = this; let isAdd = this.companyForm.id === 0; this.companyDialog = layer.open({ type: 1, title: title, shadeClose: 1, area: ['630px', '660px'], content: `
`, btn: ['确认', '取消'], success: function(layero) { form.render(null, 'company-form'); layero.css({ 'border-radius': '6px', }); form.on('switch', function(data){ if(data.elem.name === 'authStatus'){ that.companyForm.authStatus = data.elem.checked; }else if(data.elem.name === 'expiredStatus'){ that.companyForm.expiredStatus = data.elem.checked; } }); }, yes: function() { // 获取表单数据 const formData = { id: that.companyForm.id, code: $('input[name="code"]').val(), name: $('input[name="name"]').val(), address: $('input[name="address"]').val(), phone: $('input[name="phone"]').val(), email: $('input[name="email"]').val(), website: $('input[name="website"]').val(), logo: $('input[name="logo"]').val(), description: $('textarea[name="description"]').val(), authStatus: that.companyForm.authStatus, expiredStatus: that.companyForm.expiredStatus }; // 保存企业信息 that.saveCompany(formData); } }); }, /** * 保存企业信息 * @param {Object} formData 表单数据 */ saveCompany: async function(formData) { try { if (!formData.name) { this.popWarningMsg('企业名称不能为空'); return; } let index = layer.load(2, { shade: [0.3, '#0000001f'] }); const isEdit = formData.id > 0; const apiUrl = isEdit ? '/api/web/system-web-company/update-company' : '/api/web/system-web-company/add-company'; const { data: saveRes } = await axios.post(apiUrl, formData); layer.close(index); if (!saveRes.success) { this.popErrorMsg(saveRes.msg || (isEdit ? '更新企业失败' : '创建企业失败')); return; } this.popSuccessMsg(isEdit ? '更新企业成功' : '创建企业成功'); // 关闭对话框 if (this.companyDialog) { layer.close(this.companyDialog); } // 刷新表格 this.companyTable.reload(); } catch (error) { console.error('保存企业失败:', error); this.popErrorMsg('保存企业失败,请重试'); layer.closeAll('loading'); } }, /** * 删除企业 * @param {number} id 企业ID */ deleteCompany: async function(id) { const that = this; let acceptOrReject = await new Promise(resolve => { layer.confirm('确定要删除该企业吗?此操作不可恢复!', { shadeClose: 1, success: function(layero, index){ layero.css({ 'border-radius': '6px', }); } }, function (index) { layer.close(index); resolve(true); }, function (index) { resolve(false); }); }); if(!acceptOrReject){ return; } try { let loadingIndex = layer.load(2, { shade: [0.3, '#0000001f'] }); const { data: delRes } = await axios.post('/api/web/system-web-company/delete-company', { id }); layer.close(loadingIndex); if (!delRes.success) { this.popErrorMsg(delRes.msg || '删除企业失败'); return; } this.popSuccessMsg('删除企业成功'); // 刷新表格 this.companyTable.reload(); } catch (error) { console.error('删除企业失败:', error); this.popErrorMsg('删除企业失败,请重试'); layer.closeAll('loading'); } }, /** * 更新企业认证状态 * @param {number} id 企业ID * @param {boolean} status 认证状态 */ updateCompanyAuthStatus: async function(id, status) { try { let index = layer.load(2, { shade: [0.3, '#0000001f'] }); const { data: updateRes } = await axios.post('/api/web/system-web-company/update-auth-status', { id, status }); layer.close(index); if (!updateRes.success) { this.popErrorMsg(updateRes.msg || '更新认证状态失败'); // 更新失败时,重新加载表格以恢复原状态 this.companyTable.reload(); return; } this.popSuccessMsg('更新认证状态成功'); } catch (error) { console.error('更新认证状态失败:', error); this.popErrorMsg('更新认证状态失败,请重试'); layer.closeAll('loading'); // 错误时重新加载表格以恢复原状态 this.companyTable.reload(); } } }, mounted() { this.init(); }, template: `
企业管理
` });