系統默認提供了獲取當前用戶的api方法
https://localhost:44364/api/identity/my-profile
手工實現方法:abp后台獲取當前用戶需要在AppService應用層注入CurrentUser currentUser,如本例
using System; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; using Volo.Abp.Users; namespace Shop { public class BookAppService : CrudAppService<Book, BookDto, Guid, PagedAndSortedResultRequestDto, CreateUpdateBookDto, CreateUpdateBookDto>, IBookAppService { private readonly CurrentUser _currentUser; public BookAppService(IRepository<Book, Guid> repository, CurrentUser currentUser) : base(repository) { _currentUser = currentUser; } public CurrentUser GetUser() { var currentUser = _currentUser; return currentUser; } } }
Vue前台獲取方式,創建user.vue
<template> <div> <el-button @click="btnCurrentUser">當前登錄用戶</el-button> <hr/> {{CurrentUser}} </div> </template> <script> export default { data(){ return{ CurrentUser:'' } }, methods: { btnCurrentUser: function () { this.$axios.get('https://localhost:44327/api/app/book/user') .then(res => { this.CurrentUser = res.data }) } } } </script>
router目錄index.js中增加路由規則
import Vue from 'vue' import VueRouter from 'vue-router' import Home from '../views/Login.vue' Vue.use(VueRouter) const routes = [ { path: '/', name: 'Home', component: Home }, { path: '/about', name: 'About', component: () => import('../views/About.vue') }, { path: '/user', name: 'user', component: () => import('../views/user.vue') } ] const router = new VueRouter({ routes }) export default router
前台顯示
后台中也可以根據CurrentUser獲取用戶id等詳情