huning 1 month ago
parent
commit
f544cfc854
1 changed files with 69 additions and 17 deletions
  1. 69 17
      new-react-admin-ui/src/adapters/ruoyiDataProvider.ts

+ 69 - 17
new-react-admin-ui/src/adapters/ruoyiDataProvider.ts

@@ -113,17 +113,21 @@ const request = async <T>(url: string, options: RequestInit & { isToken?: boolea
     
     
     // 若依后台返回格式处理
     // 若依后台返回格式处理
     if (data.code !== 200) {
     if (data.code !== 200) {
-      // 保存错误信息到sessionStorage
-      const errorInfo = {
-        status: data.code,
-        message: data.msg || '请求失败',
-        resource: url,
-        action: options.method || 'GET'
-      };
-      sessionStorage.setItem('errorInfo', JSON.stringify(errorInfo));
+      // 对于权限错误(403),保存错误信息并重定向到错误页面
+      if (data.code === 403) {
+        const errorInfo = {
+          status: data.code,
+          message: data.msg || '权限不足',
+          resource: url,
+          action: options.method || 'GET'
+        };
+        sessionStorage.setItem('errorInfo', JSON.stringify(errorInfo));
+        
+        // 重定向到错误页面
+        window.location.href = '/error';
+      }
       
       
-      // 重定向到错误页面
-      window.location.href = '/error';
+      // 对于其他错误,直接抛出异常,让React Admin处理
       throw new Error(data.msg || '请求失败');
       throw new Error(data.msg || '请求失败');
     }
     }
 
 
@@ -152,6 +156,14 @@ export const ruoyiDataProvider: RuoyiDataProvider = {
     const { page = 1, perPage = 10 } = params.pagination || {};
     const { page = 1, perPage = 10 } = params.pagination || {};
     const { field, order } = params.sort || {};
     const { field, order } = params.sort || {};
     
     
+    // 解析层级化的资源名称,例如 "system/users" -> "users"
+    const getFlatResourceName = (resource: string): string => {
+      const parts = resource.split('/');
+      return parts[parts.length - 1]; // 取最后一部分作为扁平化资源名
+    };
+    
+    const flatResource = getFlatResourceName(resource);
+
     // 映射排序字段:将React Admin的默认字段名映射为若依后端的字段名
     // 映射排序字段:将React Admin的默认字段名映射为若依后端的字段名
     const getMappedSortField = (resource: string, field: string) => {
     const getMappedSortField = (resource: string, field: string) => {
       const fieldMappings: Record<string, Record<string, string>> = {
       const fieldMappings: Record<string, Record<string, string>> = {
@@ -167,7 +179,7 @@ export const ruoyiDataProvider: RuoyiDataProvider = {
       return fieldMappings[resource]?.[field] || field;
       return fieldMappings[resource]?.[field] || field;
     };
     };
     
     
-    const mappedField = field ? getMappedSortField(resource, field) : undefined;
+    const mappedField = field ? getMappedSortField(flatResource, field) : undefined;
     
     
     const queryParams = new URLSearchParams({
     const queryParams = new URLSearchParams({
       pageNum: page.toString(),
       pageNum: page.toString(),
@@ -176,7 +188,7 @@ export const ruoyiDataProvider: RuoyiDataProvider = {
       ...params.filter,
       ...params.filter,
     });
     });
 
 
-    switch (resource) {
+    switch (flatResource) {
       case 'users': {
       case 'users': {
         const data = await request<UserPageResult>(`/system/user/list?${queryParams}`);
         const data = await request<UserPageResult>(`/system/user/list?${queryParams}`);
         // 将userId映射为id,以满足React Admin的数据格式要求
         // 将userId映射为id,以满足React Admin的数据格式要求
@@ -282,7 +294,15 @@ export const ruoyiDataProvider: RuoyiDataProvider = {
 
 
   // 获取单条记录
   // 获取单条记录
   getOne: async (resource: string, params: GetOneParams): Promise<GetOneResult> => {
   getOne: async (resource: string, params: GetOneParams): Promise<GetOneResult> => {
-    switch (resource) {
+    // 解析层级化的资源名称
+    const getFlatResourceName = (resource: string): string => {
+      const parts = resource.split('/');
+      return parts[parts.length - 1];
+    };
+    
+    const flatResource = getFlatResourceName(resource);
+    
+    switch (flatResource) {
       case 'users': {
       case 'users': {
         const data = await request<any>(`/system/user/${params.id}`);
         const data = await request<any>(`/system/user/${params.id}`);
         // 将userId映射为id,以满足React Admin的数据格式要求
         // 将userId映射为id,以满足React Admin的数据格式要求
@@ -336,7 +356,15 @@ export const ruoyiDataProvider: RuoyiDataProvider = {
 
 
   // 创建记录
   // 创建记录
   create: async (resource: string, params: CreateParams): Promise<CreateResult> => {
   create: async (resource: string, params: CreateParams): Promise<CreateResult> => {
-    switch (resource) {
+    // 解析层级化的资源名称
+    const getFlatResourceName = (resource: string): string => {
+      const parts = resource.split('/');
+      return parts[parts.length - 1];
+    };
+    
+    const flatResource = getFlatResourceName(resource);
+    
+    switch (flatResource) {
       case 'users': {
       case 'users': {
         // 确保roleIds参数以数组格式发送
         // 确保roleIds参数以数组格式发送
         const requestData = {
         const requestData = {
@@ -381,7 +409,15 @@ export const ruoyiDataProvider: RuoyiDataProvider = {
 
 
   // 更新记录
   // 更新记录
   update: async (resource: string, params: UpdateParams): Promise<UpdateResult> => {
   update: async (resource: string, params: UpdateParams): Promise<UpdateResult> => {
-    switch (resource) {
+    // 解析层级化的资源名称
+    const getFlatResourceName = (resource: string): string => {
+      const parts = resource.split('/');
+      return parts[parts.length - 1];
+    };
+    
+    const flatResource = getFlatResourceName(resource);
+    
+    switch (flatResource) {
       case 'users': {
       case 'users': {
         // 确保roleIds参数以数组格式发送
         // 确保roleIds参数以数组格式发送
         const requestData = {
         const requestData = {
@@ -429,7 +465,15 @@ export const ruoyiDataProvider: RuoyiDataProvider = {
 
 
   // 删除记录
   // 删除记录
   delete: async (resource: string, params: DeleteParams): Promise<DeleteResult> => {
   delete: async (resource: string, params: DeleteParams): Promise<DeleteResult> => {
-    switch (resource) {
+    // 解析层级化的资源名称
+    const getFlatResourceName = (resource: string): string => {
+      const parts = resource.split('/');
+      return parts[parts.length - 1];
+    };
+    
+    const flatResource = getFlatResourceName(resource);
+    
+    switch (flatResource) {
       case 'users': {
       case 'users': {
         await request<Result>(`/system/user/${params.id}`, {
         await request<Result>(`/system/user/${params.id}`, {
           method: 'DELETE',
           method: 'DELETE',
@@ -466,7 +510,15 @@ export const ruoyiDataProvider: RuoyiDataProvider = {
 
 
   // 批量获取记录 - 由于后台没有批量接口,通过循环调用单个接口实现
   // 批量获取记录 - 由于后台没有批量接口,通过循环调用单个接口实现
   getMany: async (resource: string, params: GetManyParams): Promise<GetManyResult> => {
   getMany: async (resource: string, params: GetManyParams): Promise<GetManyResult> => {
-    switch (resource) {
+    // 解析层级化的资源名称
+    const getFlatResourceName = (resource: string): string => {
+      const parts = resource.split('/');
+      return parts[parts.length - 1];
+    };
+    
+    const flatResource = getFlatResourceName(resource);
+    
+    switch (flatResource) {
       case 'users': {
       case 'users': {
         // 循环调用单个用户接口获取多个用户信息
         // 循环调用单个用户接口获取多个用户信息
         const promises = params.ids.map(id => 
         const promises = params.ids.map(id =>