UserSessionController.cs 5.14 KB
// ***********************************************************************
// <summary>
// 获取登录用户的全部信息
// 所有和当前登录用户相关的操作都在这里
// </summary>
// ***********************************************************************

using Hh.Mes.Common.Request;
using Hh.Mes.Common.Tree;
using Hh.Mes.POJO.Response;
using Hh.Mes.POJO.ViewModel;
using Hh.Mes.Service.SystemAuth;
using Hh.Mes.Service.WebService.Base;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;

namespace WebMvc
{
    [Area("base")]
    public class UserSessionController : BaseController
    {
        private readonly AuthStrategyContext _authStrategyContext;
        private readonly SysDeptService sysDeptService;

        public UserSessionController(IAuth authUtil, SysDeptService sysDeptService) : base(authUtil)
        {
            _authStrategyContext = _authUtil.GetCurrentUser();
            this.sysDeptService = sysDeptService;
            this.sysDeptService.sysWebUser = _authStrategyContext.User;
        }

        public List<string> GetCurrentUser()
        {
            return new List<string> { _authStrategyContext.User.Account, _authStrategyContext.User.Name };
        }

        /// <summary>
        /// 获取登录用户可访问的所有模块,及模块的操作菜单
        /// </summary>
        public string GetModulesTree()
        {
            //OpenAuth原左边栏菜单
            var moduleTree = _authStrategyContext.Modules.GenerateTree(u => u.Id, u => u.ParentId);
            return Serialize(moduleTree);
        }

        /// <summary>
        /// datatable结构的模块列表
        /// </summary>
        /// <param name="modules"></param>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        public string GetModulesTable(QueryModules modules, PageReq pageRequest)
        {
            var query = _authStrategyContext.Modules;

            if (modules.parentId == null && modules.pId != null)
            {
                //当前是一级模块,就加载子模块
                query = query.Where(u => u.ParentId == modules.pId).ToList();
            }
            else if (modules.parentId != null && modules.pId != null)
            {
                //当前是小于二级的模块,就加载所有的子节点模块
                query = query.Where(u => u.CascadeId.Contains(modules.cascadeId)).ToList();
            }            
            else if (modules.parentId == null && modules.pId == null)
            {
                //当前是根模块,就加载父节点是“根节点”的模块
                query = query.Where(u => u.ParentName== "根节点").OrderBy(x => x.SortNo).ToList();
                return Serialize(new Response
                {
                    Result = query.ToList(),
                    Count = query.Count,
                });
            }

            var data = query.OrderBy(u => u.Id)
                            .Skip((pageRequest.page - 1) * pageRequest.limit)
                            .Take(pageRequest.limit);
            return Serialize(new Response
            {
                Result = data.ToList(),
                Count = query.Count(),
            });
        }

        /// <summary>
        /// 获取用户可访问的模块列表
        /// </summary>
        public string GetModules()
        {
            var moduleList = _authStrategyContext.Modules;
            return Serialize(moduleList);
        }

        /// <summary>
        /// 获取所有部门
        /// <para>用于树状结构</para>
        /// </summary>
        public string GetOrgs()
        {
            return Serialize(_authStrategyContext.Orgs);
        }

        /// <summary>
        /// 加载机构的全部下级机构
        /// </summary>
        /// <param name="orgId">机构ID</param>
        /// <param name="pageRequest">机构ID</param>
        /// <returns></returns>
        public string GetSubOrgs(int orgId, PageReq pageRequest)
        {
            string cascadeId = ".0.";
            if (orgId != 0)
            {
                var org = _authStrategyContext.Orgs.SingleOrDefault(u => u.Id == orgId);
                if (org == null)
                {
                    return Serialize(new Response
                    {
                        Message = "未找到指定的节点",
                        Code = 500,
                    });
                }
                cascadeId = org.CascadeId;
            }

            var query = _authStrategyContext.Orgs.Where(u => u.CascadeId.StartsWith(cascadeId));
            var data = query.OrderBy(u => u.Id)
                            .Skip((pageRequest.page - 1) * pageRequest.limit)
                            .Take(pageRequest.limit);

            return Serialize(new Response
            {
                Result = data.ToList(),
                Count = query.Count(),
            });
        }
    }
    /// <summary>
    /// 查询模块模型
    /// </summary>
    public class QueryModules
    {
        //父级id
        public int? parentId { get; set; }
        public int? pId { get; set; }
        public string cascadeId { get; set; }
    }
}