Browse Source

fix: 修复用户导出失败的bug

pull/193/head
cp3hnu 10 months ago
parent
commit
22e2cec9f2
3 changed files with 46 additions and 32 deletions
  1. +5
    -8
      react-ui/src/pages/System/User/index.tsx
  2. +11
    -7
      react-ui/src/services/system/user.ts
  3. +30
    -17
      react-ui/src/utils/downloadfile.ts

+ 5
- 8
react-ui/src/pages/System/User/index.tsx View File

@@ -4,7 +4,6 @@ import { getRoleList } from '@/services/system/role';
import {
addUser,
changeUserStatus,
exportUser,
getDeptTree,
getUser,
getUserList,
@@ -13,6 +12,7 @@ import {
updateAuthRole,
updateUser,
} from '@/services/system/user';
import { downloadXlsx } from '@/utils/downloadfile';
import {
DeleteOutlined,
DownOutlined,
@@ -132,15 +132,12 @@ const handleRemoveOne = async (selectedRow: API.System.User) => {

/**
* 导出数据
*
*
*/
const handleExport = async () => {
const handleExport = async (deptId: string) => {
const hide = message.loading('正在导出');
try {
await exportUser();
await downloadXlsx('/api/system/user/export', 'POST', { data: { deptId: deptId } });
hide();
message.success('导出成功');
return true;
} catch (error) {
hide();
@@ -470,7 +467,7 @@ const UserTableList: React.FC = () => {
key="export"
hidden={!access.hasPerms('system:user:export')}
onClick={async () => {
handleExport();
handleExport(selectDept.id);
}}
>
<PlusOutlined />{' '}
@@ -581,7 +578,7 @@ const UserTableList: React.FC = () => {
/>
<AuthRoleForm
onSubmit={async (values: any) => {
const success = await updateAuthRole(values);
const success = await updateAuthRole(currentRow!.userId, values.roleIds);
if (success) {
setAuthRoleModalVisible(false);
setSelectedRows([]);


+ 11
- 7
react-ui/src/services/system/user.ts View File

@@ -60,8 +60,9 @@ export async function removeUser(ids: string, options?: { [key: string]: any })
// 导出用户信息
export function exportUser(params?: API.System.UserListParams, options?: { [key: string]: any }) {
return request<API.Result>(`/api/system/user/export`, {
method: 'GET',
params,
method: 'POST',
data: params,
skipValidating: true,
...(options || {}),
});
}
@@ -126,16 +127,19 @@ export function uploadAvatar(data: any) {

// 查询授权角色
export function getAuthRole(userId: number) {
return request('/system/user/authRole/' + userId, {
return request('/api/system/user/authRole/' + userId, {
method: 'get',
});
}

// 保存授权角色
export function updateAuthRole(data: Record<string, any>) {
return request('/system/user/authRole', {
method: 'put',
params: data,
export function updateAuthRole(userId: number, data: Record<string, any>) {
return request(`/api/system/user/authRole/${userId}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json;charset=UTF-8',
},
data: data,
});
}



+ 30
- 17
react-ui/src/utils/downloadfile.ts View File

@@ -1,19 +1,21 @@
import { request } from '@umijs/max';

const mimeMap = {
/** MimeType */
export const mimeMap = {
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
zip: 'application/zip',
};

/**
* 解析blob响应内容并下载
* @param {*} res blob响应内容
* @param {String} mimeType MIME类型
* @param res - blob响应内容
* @param mimeType - MIME类型
*/
export function resolveBlob(res: any, mimeType: string) {
const aLink = document.createElement('a');
const blob = new Blob([res.data], { type: mimeType });
// //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
// 从response的headers中获取filename,
// 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
const patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*');
// console.log(res);
const contentDisposition = decodeURI(res.headers['content-disposition']);
@@ -29,6 +31,11 @@ export function resolveBlob(res: any, mimeType: string) {
document.body.removeChild(aLink);
}

/**
* 下载 Zip 文件
* @param url - url 地址
* @param options - 请求参数
*/
export function downLoadZip(url: string, params?: any) {
request(url, {
method: 'GET',
@@ -40,24 +47,30 @@ export function downLoadZip(url: string, params?: any) {
});
}

export async function downLoadXlsx(url: string, params: any, fileName: string) {
/**
* 下载 XLSX 文件
* @param url - url 地址
* @param method - 请求方式
* @param options - 请求选项
*/
export async function downloadXlsx(
url: string,
method: string = 'GET',
options?: Record<string, any>,
) {
return request(url, {
...params,
method: 'POST',
method: method,
...options,
responseType: 'blob',
}).then((data) => {
const aLink = document.createElement('a');
const blob = data as any; // new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
aLink.style.display = 'none';
aLink.href = URL.createObjectURL(blob);
aLink.setAttribute('download', fileName); // 设置下载文件名称
document.body.appendChild(aLink);
aLink.click();
URL.revokeObjectURL(aLink.href); // 清除引用
document.body.removeChild(aLink);
getResponse: true,
}).then((res) => {
resolveBlob(res, mimeMap.xlsx);
});
}

/**
* @deprecated 无效
*/
export function download(fileName: string) {
window.location.href = `/api/common/download?fileName=${encodeURI(fileName)}&delete=${true}`;
}

Loading…
Cancel
Save