diff --git a/api/app/lib/controllers/member/index.js b/api/app/lib/controllers/member/index.js index 59bf603..1b04d2d 100644 --- a/api/app/lib/controllers/member/index.js +++ b/api/app/lib/controllers/member/index.js @@ -1,5 +1,6 @@ 'use strict'; const moment = require('moment') +const fs = require('fs'); async function add (ctx) { try { @@ -381,15 +382,89 @@ async function vacateStatistics (ctx) { async function exportData (ctx) { try { const { models } = ctx.fs.dc; - const { judgeHoliday, memberList } = ctx.app.fs.utils - const { keywordTarget, keyword, limit, page, state } = ctx.query + const { simpleExcelDown, memberList } = ctx.app.fs.utils + const { keywordTarget, keyword, limit, page, state, keys = '' } = ctx.query const userRes = await memberList({ keywordTarget, keyword, limit, page, state }) const tableAttributes = models['Member'].tableAttributes + const optionKeys = keys.split(',') - ctx.status = 200; + let exportD = [] + userRes.forEach(u => { + let existUser = exportD.find(r => r.pepUserId == u.pepUserId) + if (existUser) { + if (u.depId && !existUser.departmrnt.some(d => d.id == u.depId)) { + existUser.departmrnt.push({ + id: u.depId, + name: u.depName + }) + } + if (u.roleId && !existUser.role.some(r => r.id == u.roleId)) { + existUser.role.push({ + id: u.roleId, + name: u.roleName + }) + } + } else { + let obj = {} + for (let k in u) { + let nextKey = k.replace('hrMember.', '') + .replace('member.', '') + if (nextKey.includes('_')) { + nextKey = nextKey.toLowerCase() + .replace( + /(_)[a-z]/g, + (L) => L.toUpperCase() + ) + .replace(/_/g, '') + } + obj[nextKey] = u[k] + } + exportD.push({ + ...obj, + departmrnt: u.depId ? [{ + id: u.depId, + name: u.depName + }] : [], + role: u.roleId ? [{ + id: u.roleId, + name: u.roleName + }] : [], + del: undefined, + pepuserid: undefined + }) + } + }) + let preHeader = [{ + title: '姓名', + key: 'userName', + }] + let header = [].concat(preHeader) + for (let k in tableAttributes) { + const comment = tableAttributes[k].comment + if (k != 'id' && comment) { + if ([].includes(k)) { + // 截住不想导出的字段 + continue + } + header.push({ + title: comment || '-', + key: k, + // index: tableAttributes[k].index, + }) + } + } + + const fileName = `人员信息_${moment().format('YYYYMMDDHHmmss')}` + '.csv' + const filePath = await simpleExcelDown({ data: exportD, header, fileName: fileName }) + const fileData = fs.readFileSync(filePath); + + ctx.status = 200; + ctx.set('Content-Type', 'application/x-xls'); + ctx.set('Content-disposition', 'attachment; filename=' + encodeURI(fileName)); + ctx.body = fileData; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.status = 400; diff --git a/api/app/lib/routes/member/index.js b/api/app/lib/routes/member/index.js index 2d84b3b..6bd9b0f 100644 --- a/api/app/lib/routes/member/index.js +++ b/api/app/lib/routes/member/index.js @@ -26,4 +26,7 @@ module.exports = function (app, router, opts) { app.fs.api.logAttr['POST/add/members/bulk'] = { content: '导入员工信息', visible: true }; router.post('/add/members/bulk', member.addMembersBulk); + + app.fs.api.logAttr['GET/members/export'] = { content: '导出员工信息', visible: true }; + router.get('/members/export', member.exportData); }; \ No newline at end of file diff --git a/api/app/lib/utils/member.js b/api/app/lib/utils/member.js index 2559d9e..f5b2806 100644 --- a/api/app/lib/utils/member.js +++ b/api/app/lib/utils/member.js @@ -99,7 +99,7 @@ module.exports = function (app, opts) { ON hrOvertime.pep_user_id = member.pep_user_id WHERE - member.pep_user_id > 0 + member.del = 0 ${keywordTarget == 'role' && keyword ? ` AND user.id IN ( SELECT user_role.user @@ -138,6 +138,8 @@ module.exports = function (app, opts) { ${whereOption.length ? `WHERE ${whereOption.join(' AND ')}` : ''} `).toPromise() + + return userRes }