Ant Design Pro V4.5 登录成功后用户名的显示问题(typescript版)

Posted シ゛甜虾

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ant Design Pro V4.5 登录成功后用户名的显示问题(typescript版)相关的知识,希望对你有一定的参考价值。

如果后台使用的Abp,登录成功后登录信息中只有userId没有发现用户名,虽然Token中包含了用户名信息,目前我还不知道如何获取,占时可以通过从服务器请求来实现用户信息获取

 我这里修改services中的user.ts即可修改显示的用户名

只需要吧服务器获取的用户名替换掉这个即可,我这里在models中的user.ts中引用的这个文件

我们在      const response = yield call(queryCurrent);之后获取name修改下即可

const userName = yield call(getUserName);

response.name = userName.result;

全部代码

import { Effect, Reducer } from 'umi';

import { queryCurrent, query as queryUsers } from '@/services/user';
import { getUserName } from '@/services/login';

export interface CurrentUser {
  avatar?: string;
  name?: string;
  title?: string;
  group?: string;
  signature?: string;
  tags?: {
    key: string;
    label: string;
  }[];
  userid?: string;
  unreadCount?: number;
}

export interface UserModelState {
  currentUser?: CurrentUser;
}

export interface UserModelType {
  namespace: 'user';
  state: UserModelState;
  effects: {
    fetch: Effect;
    fetchCurrent: Effect;
  };
  reducers: {
    saveCurrentUser: Reducer<UserModelState>;
    changeNotifyCount: Reducer<UserModelState>;
  };
}

const UserModel: UserModelType = {
  namespace: 'user',

  state: {
    currentUser: {},
  },

  effects: {
    *fetch(_, { call, put }) {
      const response = yield call(queryUsers);
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchCurrent(_, { call, put }) {
      const response = yield call(queryCurrent);
      const userName = yield call(getUserName);
      response.name = userName.result;
      yield put({
        type: 'saveCurrentUser',
        payload: response,userName
      });
    },
  },

  reducers: {
    saveCurrentUser(state, action) {
      return {
        ...state,
        currentUser: action.payload || {},
      };
    },
    changeNotifyCount(
      state = {
        currentUser: {},
      },
      action,
    ) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          notifyCount: action.payload.totalCount,
          unreadCount: action.payload.unreadCount,
        },
      };
    },
  },
};

export default UserModel;

服务器请求代码 

export async function getUserName() {
  return request(`/api/services/app/UserMenuRolesService/GetUserName`, {
      method: 'GET'
  });
}

服务器代码

        public string GetUserName()
        {
            var user = _userAppService.GetAsync(new Abp.Application.Services.Dto.EntityDto<long>() { Id = (long)_abpSession.UserId }).Result;
            if(user!=null)
            {
                return user.UserName;
            }
            else
            {
                return "未找到该用户名";
            }
        }

好了,用户名改变了,这里面还可以根据你们自己的需求获取其他信息

以上是关于Ant Design Pro V4.5 登录成功后用户名的显示问题(typescript版)的主要内容,如果未能解决你的问题,请参考以下文章

Ant Design Pro V4.5 获取和设置ProFormSelect选项(typescript版)

Ant Design Pro V4.5 从服务器请求菜单(typescript版)

Ant Design Pro V4.5 从服务器请求菜单(typescript版)

Ant Design Pro V4.5 从服务器请求菜单图标不显示解决

Ant Design Pro V4.5 从服务器请求数据的坑(typescript版)

Ant Design Pro V4.5 从服务器请求数据的坑(typescript版)#导入MD文档