后端代码
async userActivityExport(ctx) {const limit = ctx.query.limit || 2const offset = ctx.query.offset || 0const UserActivity = ctx.module.db().entity('userActivity')const findOption = {}const ret = await UserActivity.findMany_(findOption)const firtCol = Object.keys(ret.data[0])let data = [firtCol, ...ret.data.map(item => Object.values(item))]data = data.map(row => {return row.map(cell => {if(isNaN(cell) && cell.hasOwnProperty('replace')) {cell.replace(/[\r\n]+/g, '')}return cell}).join(',')}).join('\n');this.send(ctx, data)}
前端代码
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><script src="./node_modules/axios/dist/axios.min.js"></script><script>axios.get('xxx').then((response) => {// 测试数据// const data = "id,mobile,email,displayName,password,passwordSalt,isEmailVerified,emailVerifiedTime,isMobileVerified,mobileVerifiedTime,avatar,status,lastLoginTime,currentLoginTime,createdAt,updatedAt,statusUnverifiedTimestamp,statusActiveTimestamp,statusSuspendedTimestamp,statusDeletedTimestamp,profile\n44,+8614333333333,26640947562@proton.me,张三,58f9f85a6414ca9fc9606adb9d0723436d578e71b76294cd77edf5f1df6e4fdb,qyxdtpzakz592kjt,,,,,,unverified,,,Fri Jun 28 2024 01:22:48 GMT+0800 (中国标准时间),,,,,,50\n46,+8614333323333,266409472562@proton.me,22,b80e43fafc280a156faf1c21e9ab08e5154ce1e153695c26e7aebeaa55198c4f,rdq2f2bolcn0oi43,,,,,,unverified,,,Fri Jun 28 2024 02:14:31 GMT+0800 (中国标准时间),,,,,,52\n30,,test2@wisley.ai,唯思狸,f2453005acb9c86b9ed34da965a8eaaaaef299e19278d1a645987bbdb1d74765,7unndrfe2of65dav,,,,,,active,,,Thu Jun 27 2024 06:14:06 GMT+0800 (中国标准时间),,,,,,25\n49,,,222,d64d255836ebd12af63daf01842f0d1a896cdfc3e7e720f59c1d613248ad0df4,vyvfbnzs8zli695v,,,,,,unverified,,,Fri Jun 28 2024 02:16:08 GMT+0800 (中国标准时间),,,,,,56\n50,,,bbbaaa,e6473a62b203919e5389cea2b3bc01b85f9ffaa0670a9eedaff0d59c9ed80613,cvyrny165kq36khf,,,,,,unverified,,,Fri Jun 28 2024 02:32:43 GMT+0800 (中国标准时间),,,,,,57\n51,,,222,ad5ab031d65c07c71f050191a0564b4ec0fa2359e52a64548e4a2a732910a3bf,0q60wm57rxkl57pz,,,,,,unverified,,,Fri Jun 28 2024 02:41:24 GMT+0800 (中国标准时间),,,,,,60\n52,,,3444,dd93fabbc6dff7914d4e689275005b4015ea0bc5eb5790958a17eefdfbcd7561,jieu5ska2ruhetji,,,,,,unverified,,,Fri Jun 28 2024 02:51:55 GMT+0800 (中国标准时间),,,,,,62\n53,,,bbbaaa1,b5a92e309749d1ce7d59bf63bf99b780e1427a3ec7c9aa7e8af8034073b96da7,dq4y4vb9wi7yt6sa,,,,,,unverified,,,Fri Jun 28 2024 02:52:42 GMT+0800 (中国标准时间),,,,,,65\n54,,,4442244,d0b6bd7a774c8ccabdee1e03a9a564e81bfdeac174f6a68fdb6072280cc811bf,zayrowwz04bydh4u,,,,,,unverified,,,Fri Jun 28 2024 02:54:27 GMT+0800 (中国标准时间),,,,,,68\n55,,,bbbaaa1,98dec860ff6f74b61e4604ebd00e4ab0bb8401286f6106379b47fd2982295f61,1swpe4a2e19adawx,,,,,,unverified,,,Fri Jun 28 2024 03:03:16 GMT+0800 (中国标准时间),,,,,,69\n56,,,bbbaaa1,9104ef30d326dd0a5eb13d1941fcb6666115d1c36fc50be62646962696d9f21d,k7gsrxyxyxeuyqh5,,,,,,unverified,,,Fri Jun 28 2024 03:05:55 GMT+0800 (中国标准时间),,,,,,70\n57,,,bbbaaa1,f2b57e80f12448b8041470473b3253b604e7f1b762ad33e413b915f3d7c9335f,cn3k7w9gpsdeo4jz,,,,,,unverified,,,Fri Jun 28 2024 03:07:15 GMT+0800 (中国标准时间),,,,,,71\n24,+8613632262320,developerhjj@163.com,hjj,fc6ebd338acb7820b1b2c678f387c4561829493028441c8bdaada9d2b91421cf,gqybre77i47epxul,,,,,,active,,,Wed Jun 26 2024 06:11:37 GMT+0800 (中国标准时间),Fri Jun 28 2024 08:30:37 GMT+0800 (中国标准时间),,,,,17\n23,+8613632262321,2664097562@qq.com,kyushu,fc6ebd338acb7820b1b2c678f387c4561829493028441c8bdaada9d2b91421cf,gqybre77i47epxul,,,,,,active,,,Wed Jun 26 2024 06:11:37 GMT+0800 (中国标准时间),Fri Jun 28 2024 08:30:37 GMT+0800 (中国标准时间),,,,,16\n48,+8613277777777,,22,e41f345e3a5fb3fefe9349d646545de5c9789e89b2a08fac153f8df98a9f550e,hd7vudyv7mu0x3xz,,,,,,active,,,Fri Jun 28 2024 02:15:49 GMT+0800 (中国标准时间),Mon Jul 01 2024 08:17:49 GMT+0800 (中国标准时间),,Thu Jan 01 1970 00:00:00 GMT+0800 (中国标准时间),,,54\n58,,2664097521@proton.me,张三,14221ca51620c029487bbc832e2501a7410a152a7b77d3cf447cabb2edf80e52,9klyxt5v08u5bqhp,,,,,,unverified,,,Fri Jun 28 2024 03:17:22 GMT+0800 (中国标准时间),Mon Jul 01 2024 09:58:25 GMT+0800 (中国标准时间),Thu Jan 01 1970 00:00:00 GMT+0800 (中国标准时间),,,,72";// 创建并下载文件const filename = 'data.csv';const blob = new Blob([response.data.data], { type: 'text/csv;charset=utf-8;' });if (navigator.msSaveBlob) {// 兼容IEnavigator.msSaveBlob(blob, filename);} else {const link = document.createElement('a');if (link.download !== undefined) {// 使用download属性,支持大多数浏览器const url = URL.createObjectURL(blob);link.setAttribute('href', url);link.setAttribute('download', filename);link.style.visibility = 'hidden';document.body.appendChild(link);link.click();document.body.removeChild(link);}}}).catch((error) => {console.log(error);});</script></body>
</html>