This commit is contained in:
Teo
2025-09-09 09:40:08 +08:00
parent 910b24019e
commit 4cd7efcfe4
34 changed files with 1898 additions and 71 deletions

View File

@ -10,6 +10,10 @@ export const setToken = (access_token: string) => (tokenStorage.value = access_t
export const removeToken = () => (tokenStorage.value = null);
export const getGoToken = () => {
return getLocal('goToken');
};
export const getDockSocketUrl = () => {
return getLocal('dockSocketUrl');
};

212
src/utils/exportExcel.ts Normal file
View File

@ -0,0 +1,212 @@
import { exportSalary } from '@/api/project/projectTeam';
import { exportDataToExcel } from '@/utils/exportDataToExcel.js';
export function useExcelExport() {
// 数据整合函数
const exportConfig = (obj, proxy, times) => {
// 如果导出前要处理数据,需要深克隆一份表格数据,然后进行处理
let time = proxy.parseTime(new Date(times), '{y}年{m}月'); // 当前年月
let header1 = ['建筑施工企业现场人员考勤表(' + time + ''];
let projectName = '项目部名称:' + obj.projectName;
let teamName = '班组类别:' + obj.teamName;
let header2 = [];
let header3 = [projectName + ' ' + teamName];
const header = ['序号', '姓名/日期', '身份证号'];
let columnsWidth = [6, 10, 26]; // 表格宽度
let obj1 = { index: '', name: '', identity_card: '' };
let fields = ['index', 'row', 'identity_card'];
// 计算当前月天数
let years = times.split('-')[0];
let month = times.split('-')[1];
let listLength = new Date(years, month, 0).getDate();
for (let index = 0; index < listLength; index++) {
obj1['day' + index] = '';
fields.push('day' + index);
header.push(index + 1);
columnsWidth.push(4);
}
header.push('合计');
fields.push('sum');
header.push('是否离场');
fields.push('type');
header.push('签字');
fields.push('sign');
// 上方共用
let data = [];
if (!(obj.team && obj.team.length)) return;
obj.team.forEach((item, index) => {
let obj = { row: '', index: '', identity_card: '' };
for (let key in item.attendance) {
let j = parseInt(key.split('-')[2]) - 1;
obj['day' + j] = item.attendance[key];
}
obj.row = item.row;
obj.index = index + 1;
obj.identity_card = item.identity_card;
let start = ''; // 从表格那一列哪一行开始
let end = ''; // 从表格那一列哪一行结束
let cols = 5 + index; // 行数
start = 'D' + cols;
// 判断当月天数 获取表格中最后一天的列标识
if (listLength == 28) {
end = 'AE' + cols;
} else if (listLength == 29) {
end = 'AF' + cols;
} else if (listLength == 30) {
end = 'AG' + cols;
} else {
end = 'AH' + cols;
}
obj.sum = { formula: 'SUM(' + start + ':' + end + ')', result: 0 };
obj.sign = '';
obj.type = item.type ? '离场' : '未离场';
data.push(obj);
});
const merges = [
// 单元格合并
{ row: 0, col: 0, rowspan: 1, colspan: listLength + 5 },
{ row: 2, col: 0, rowspan: 1, colspan: listLength + 5 },
{ row: obj.team.length + 5, col: 0, rowspan: 1, colspan: 3 },
{ row: obj.team.length + 5, col: 3, rowspan: 1, colspan: 10 },
{ row: obj.team.length + 5, col: 13, rowspan: 1, colspan: 10 },
{ row: obj.team.length + 5, col: 23, rowspan: 1, colspan: 10 }
];
data.push({});
// 表格尾部标题
data.push({
index: '制表人:',
day0: '班组负责人',
day10: '项目负责人',
day20: '制表日期'
});
data.push({
index: '注:',
row: '1、本考勤表必须按照每日实际工时进行填写'
});
data.push({
row: '2、本考勤表作为工资计发的重要依据。'
});
const config = {
data,
fields,
headers: [header1, header2, header3, header],
merges,
attrs: [],
view: [],
columnsWidth,
sheetName: obj.teamName
};
// 设置全表单元格边框,居中布局
config.attrs.push({
rowStart: 3,
rowEnd: config.data.length - 1,
colStart: 0,
colEnd: config.fields.length - 1,
attr: {
alignment: { vertical: 'middle', horizontal: 'center' },
border: {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
}
}
});
// 设置表头字体加粗
config.attrs.push({
rowStart: 0,
rowEnd: 0,
colStart: 0,
colEnd: config.fields.length - 1,
attr: {
alignment: { vertical: 'middle', horizontal: 'center' },
font: {
bold: true,
size: '16'
}
}
});
config.attrs.push({
rowStart: 2,
rowEnd: 2,
colStart: 0,
colEnd: config.fields.length - 1,
attr: {
alignment: { vertical: 'middle', horizontal: 'center' }
}
});
return config;
};
// 导出函数
const exportExcel = async (obj, proxy, name = undefined) => {
try {
const res = await exportSalary(obj);
if (res.code === 0 && res.data.AttendanceAllOne) {
let config = [];
let AttendanceAllOne = res.data.AttendanceAllOne;
// 处理每个表格
AttendanceAllOne.forEach((item) => {
let datas = exportConfig(item, proxy, obj.years);
if (datas) {
config.push(datas);
}
});
// 获取项目名
let projectName = '';
if (name) {
projectName = res.data.AttendanceAllOne[0].projectName + '-' + name;
} else {
projectName = res.data.AttendanceAllOne[0].projectName;
}
let year = obj.years.split('-')[0];
let month = obj.years.split('-')[1];
// 导出Excel
exportDataToExcel(config, `${projectName}-${year}${month}月考勤表.xlsx`);
} else if (res.code === 0) {
ElMessage({
message: '暂无考勤数据',
type: 'warning'
});
} else {
ElMessage({
message: res.message || '导出失败',
type: 'error'
});
}
} catch (error) {
ElMessage({
message: '导出过程出错',
type: 'error'
});
console.error('Excel导出错误:', error);
}
};
return {
exportConfig,
exportExcel
};
}