All files / src/views/logic Logs.logic.ts

100% Statements 200/200
100% Branches 16/16
100% Functions 4/4
100% Lines 200/200

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 2001x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 4x 5x 1x 1x 1x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x
/**
 * 操作日志业务逻辑模块
 * 提供操作日志的查询、筛选、导出等功能
 * 
 * @fileOverview 操作日志模块,基于Vue 3 Composition API实现
 * @author InspectionFlowSystem Team
 * @version 1.0.0
 * @since 2025-11-14
 */
 
import { ref, reactive } from "vue";
import { ElMessage } from 'element-plus';
import { logAPI } from '../../api/index';
 
/**
 * 操作日志数据接口
 * @interface Log
 * @description 定义操作日志的完整数据结构
 * @since 1.0.0
 */
export interface Log {
  /** 日志ID */
  id: number;
  /** 操作者ID */
  operator_id: number;
  /** 操作者用户名 */
  operator_name: string;
  /** 操作者工号 */
  operator_job_number: string;
  /** 操作类型 */
  action_type: string;
  /** 操作内容 */
  action_content: string;
  /** 操作时间 */
  action_time: string;
  /** 检测批次 */
  detect_batch: string;
  /** 创建时间 */
  created_at: string;
  /** 更新时间 */
  updated_at: string;
}
 
/**
 * 日志筛选条件接口
 * @interface LogFilter
 * @description 定义日志筛选条件的数据结构
 * @since 1.0.0
 */
export interface LogFilter {
  /** 开始日期 */
  start_date?: string;
  /** 结束日期 */
  end_date?: string;
  /** 用户ID */
  user_id?: number;
  /** 操作类型 */
  action_type?: string;
  /** 页码 */
  page: number;
  /** 每页数量 */
  limit: number;
}
 
/**
 * 操作日志列表响应式引用
 * @type {import('vue').Ref<Log[]>}
 * @description 存储所有操作日志的列表数据
 */
export const logs = ref<Log[]>([]);
 
/**
 * 日志筛选条件响应式引用
 * @type {import('vue').Reactive<LogFilter>}
 * @description 存储日志筛选条件
 */
export const logFilter = reactive<LogFilter>({
  start_date: '',
  end_date: '',
  user_id: undefined,
  action_type: '',
  page: 1,
  limit: 10
});
 
/**
 * 日志总数响应式引用
 * @type {import('vue').Ref<number>}
 * @description 存储日志总数
 */
export const totalLogs = ref<number>(0);
 
/**
 * 获取操作日志列表
 * 
 * @async
 * @function fetchLogs
 * @returns {void}
 * @description 从后端 API 获取操作日志列表,更新本地 logs 响应式数据
 * 
 * @throws {Error} 当API请求失败时会显示错误消息
 * @since 1.0.0
 */
export async function fetchLogs() {
  try {
    const params: any = {
      page: logFilter.page,
      limit: logFilter.limit
    };
    
    // 只添加非空的筛选条件
    if (logFilter.start_date) params.start_date = logFilter.start_date;
    if (logFilter.end_date) params.end_date = logFilter.end_date;
    if (logFilter.user_id !== undefined) params.user_id = logFilter.user_id;
    if (logFilter.action_type) params.action_type = logFilter.action_type;
    
    const response = await logAPI.getLogs(params);
    
    logs.value = response.data.logs;
    totalLogs.value = response.data.total;
  } catch (error) {
    console.error('获取操作日志列表失败:', error);
    ElMessage.error('获取操作日志列表失败');
  }
}
 
/**
 * 导出操作日志
 * 
 * @async
 * @function exportLogs
 * @returns {void}
 * @description 导出操作日志为指定格式的文件
 * 
 * @throws {Error} 当API请求失败时会显示错误消息
 * @since 1.0.0
 */
export async function exportLogs() {
  try {
    const params: any = {};
    
    // 只添加非空的筛选条件
    if (logFilter.start_date) params.start_date = logFilter.start_date;
    if (logFilter.end_date) params.end_date = logFilter.end_date;
    if (logFilter.user_id !== undefined) params.user_id = logFilter.user_id;
    if (logFilter.action_type) params.action_type = logFilter.action_type;
    
    // 设置导出格式
    params.format = 'csv';
    
    const response = await logAPI.exportLogs(params);
    
    // 创建下载链接
    const url = window.URL.createObjectURL(new Blob([response.data]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', '操作日志.csv');
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    
    ElMessage.success('日志导出成功!');
  } catch (error) {
    console.error('导出操作日志失败:', error);
    ElMessage.error('导出操作日志失败');
  }
}
 
/**
 * 重置筛选条件
 * 
 * @function resetFilter
 * @returns {void}
 * @description 重置日志筛选条件并重新获取日志列表
 * 
 * @since 1.0.0
 */
export function resetFilter() {
  logFilter.start_date = '';
  logFilter.end_date = '';
  logFilter.user_id = undefined;
  logFilter.action_type = '';
  logFilter.page = 1;
  fetchLogs();
}
 
/**
 * 处理页码变化
 * 
 * @function handlePageChange
 * @param {number} page - 新的页码
 * @returns {void}
 * @description 处理页码变化事件并重新获取日志列表
 * 
 * @since 1.0.0
 */
export function handlePageChange(page: number) {
  logFilter.page = page;
  fetchLogs();
}