Vue.component('service-component', { props: { isLogin: { type: Boolean, required: true } }, data() { return { services: [] // 服务列表 }; }, methods: { /** * 初始化服务管理模块 */ init: async function() { if (!this.isLogin) return; try { // 获取服务状态 await this.fetchServiceStatus(); } catch (error) { console.error('初始化服务管理失败:', error); this.popErrorMsg('初始化服务管理失败,请刷新重试'); } }, /** * 获取服务状态 */ fetchServiceStatus: async function() { let index = layer.load(2, { shade: [0.3, '#0000001f'] }); try { const { data: statusRes } = await axios.get('/api/web/system-web-service/get-service-status'); if (!statusRes.success) { this.popWarningMsg(statusRes.msg || '获取服务状态失败'); layer.close(index); return; } if (statusRes.data && statusRes.data.processList) { // 使用API返回的完整进程列表 this.services = statusRes.data.processList.map(proc => { // 计算可读的内存大小 const memory = this.formatMemorySize(proc.monit.memory); return { ...proc, // 保留原始属性 id: proc.name, // 为了兼容旧代码 isRunning: proc.status === 'online', displayStatus: proc.status === 'online' ? '运行中' : '已停止', displayMemory: memory, displayCpu: proc.monit.cpu.toFixed(1) + '%' }; }); layer.close(index); } else { this.popWarningMsg('服务状态数据格式不正确'); layer.close(index); } } catch (error) { console.error('获取服务状态失败:', error); this.popWarningMsg('获取服务状态失败,请重试'); layer.close(index); } }, /** * 格式化内存大小显示 * @param {number} bytes - 内存字节数 * @returns {string} 格式化后的内存大小 */ formatMemorySize: function(bytes) { if (bytes === 0) return '0 B'; const units = ['B', 'KB', 'MB', 'GB', 'TB']; const i = Math.floor(Math.log(bytes) / Math.log(1024)); return (bytes / Math.pow(1024, i)).toFixed(2) + ' ' + units[i]; }, /** * 服务操作 - 启动服务 * @param {Object} service - 服务对象 */ startService: async function(service) { if (!this.isLogin) { this.popWarningMsg('请先登录'); this.$emit('show-login'); return; } let acceptOrReject = await new Promise(resolve => { layer.confirm('确认启动' + service.name + '服务', { 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 index = layer.load(2, { shade: [0.3, '#0000001f'] }); const { data: startRes } = await axios.post('/api/web/system-web-service/start-service', { serviceName: service.name }); layer.close(index); if (!startRes.success) { this.popErrorMsg(startRes.msg || `启动${service.name}失败`); return; } this.popSuccessMsg(`${service.name}启动成功`); // 重新获取最新状态 await this.fetchServiceStatus(); } catch (error) { console.error(`启动${service.name}失败:`, error); this.popErrorMsg(`启动${service.name}失败,请重试`); layer.closeAll('loading'); } }, /** * 服务操作 - 停止服务 * @param {Object} service - 服务对象 */ stopService: async function(service) { if (!this.isLogin) { this.popWarningMsg('请先登录'); this.$emit('show-login'); return; } let acceptOrReject = await new Promise(resolve => { layer.confirm('确认停止' + service.name + '服务', { 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 index = layer.load(2, { shade: [0.3, '#0000001f'] }); const { data: stopRes } = await axios.post('/api/web/system-web-service/stop-service', { serviceName: service.name }); layer.close(index); if (!stopRes.success) { this.popErrorMsg(stopRes.msg || `停止${service.name}失败`); return; } this.popSuccessMsg(`${service.name}停止成功`); // 重新获取最新状态 await this.fetchServiceStatus(); } catch (error) { console.error(`停止${service.name}失败:`, error); this.popErrorMsg(`停止${service.name}失败,请重试`); layer.closeAll('loading'); } }, /** * 服务操作 - 重启服务 * @param {Object} service - 服务对象 */ restartService: async function(service) { if (!this.isLogin) { this.popWarningMsg('请先登录'); this.$emit('show-login'); return; } let acceptOrReject = await new Promise(resolve => { layer.confirm('确认重启' + service.name + '服务', { 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; } let index = layer.load(2, { shade: [0.3, '#0000001f'] }); try { const { data: restartRes } = await axios.post('/api/web/system-web-service/restart-service', { serviceName: service.name }); layer.close(index); if (!restartRes.success) { this.popErrorMsg(restartRes.msg || `重启${service.name}失败`); return; } this.popSuccessMsg(`${service.name}重启成功`); // 重新获取最新状态 await this.fetchServiceStatus(); } catch (error) { console.error(`重启${service.name}失败:`, error); this.popErrorMsg(`重启${service.name}失败,请重试`); layer.closeAll('loading'); } }, /** * 检查服务状态图标 * @param {Object} service - 服务对象 * @returns {string} - 服务状态图标类名 */ adminGetServiceStatusIcon: function(service) { return service.status === 'online' ? 'layui-icon-console tl-rtc-app-service-icon-running' : 'layui-icon-close-fill tl-rtc-app-service-icon-stopped'; }, /** * 获取服务状态类名 * @param {Object} service - 服务对象 * @returns {string} - 服务状态类名 */ adminGetServiceStatusClass: function(service) { return service.status === 'online' ? 'tl-rtc-app-status-running' : 'tl-rtc-app-status-stopped'; } }, mounted() { this.init(); }, template: `
PID: {{ service.pid || '-' }}
内存占用: {{ service.displayMemory }}
CPU占用: {{ service.displayCpu }}