• Vue Abp vNext获取当前登录用户


    系统默认提供了获取当前用户的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等详情

  • 相关阅读:
    全链路压测(1):认识全链路压测
    碎片式的技术笔记
    聊聊传统压测和全链路压测的区别
    生产全链路压测常态化方案
    C++选择文件打开方式的函数
    常用的一些 git 命令
    MyBatis 批量插入数据的 3 种方法!
    MyBatis Plus 批量数据插入功能,yyds!
    什么是可中断锁?有什么用?怎么实现?
    1.3w字,一文详解死锁!
  • 原文地址:https://www.cnblogs.com/liessay/p/13181584.html
Copyright © 2020-2023  润新知