username@email.com
2021-12-09 d14b82fec13361486c49165371b5dee1b7089c09
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using CommonToolsCore;
using DTO;
using IServices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using zhengcaioa.IService;
using zhengcaioa.Models;
 
namespace zhengcaioa.Controllers
{
    public class PltAuthController : Controller
    {
        private readonly ILogger<PltAuthController> _logger;
        private readonly IPltRoleService _pltRoleService;
        private readonly IUserService _userService;
        private readonly ISysCodeService _sysCodeService;
        private readonly IPltPageService _pltPageService;
 
 
 
        public PltAuthController(ILogger<PltAuthController> logger, IPltRoleService pltRoleService, IUserService userService, ISysCodeService sysCodeService, IPltPageService pltPageService)
        {
            _logger = logger;
            _pltRoleService = pltRoleService;
            _userService = userService;
            _sysCodeService = sysCodeService;
            _pltPageService = pltPageService;
        }
 
 
        [CheckLogin]
        public ActionResult Search()
        {
            #region 获取所有角色
            var listRole = _pltRoleService.listRole();
            ViewBag.listRole = listRole;
            #endregion
            return View();
        }
 
        [CheckLogin]
        public JsonResult GetPageByRole(string roleid = "")
        {
            var liststr = new List<string>();
            // var listPageAll = new List<TreeAuthPage>();
            #region 读取缓存权限
            //CacheHelper.RemoveCache("listPage");
            // = CacheHelper.GetCache("listPage") as List<TreeAuthPage>;
 
            //CacheHelper.RemoveCache("listPage");//清除权限缓存
            //var listPageAll = CacheHelperNetCore.CacheValue("listPage") as List<TreeAuthPage>;
            //if (listPageAll == null || listPageAll.Count == 0)
            //{
                var listPage = _pltPageService.GePagetList();
                var listP = new List<TreeAuthPage>();
                if (listPage.Count > 0)
                {
                    listPage.ForEach(p =>
                    {
                        listP.Add(new TreeAuthPage()
                        {
                            id = p.PageID,
                            pId = p.PageSuperior,
                            name = p.PageName
                        });
                    });
                }
                var listPP = GetTreeAuthPage(listP);
                var listPageAll = listPP;
                //CacheHelperNetCore.CacheInsert("listPage", listPP);
            //}
            //CacheHelper.SetCache("listPage", listPP);
 
            #endregion
            #region 获取所有权限
 
            #endregion
 
            #region 获取角色配置的权限
            var listAuth = _pltPageService.GetauthList(roleid);
            listPageAll.ForEach(p =>
            {
                bool b = false;
                if (listAuth.Count(a => a.ItemId == p.id) > 0)
                {
                    b = true;
                }
                liststr.Add("{ id:'" + p.id + "', pId:'" + p.pId + "', name:'" + p.name + "', open:" + (string.IsNullOrEmpty(p.pId)) + ",checked:" + b + "}");
            });
            #endregion
            return Json(new { sJson = "[" + string.Join(",", liststr) + "]" });
        }
 
        /// <summary>
        /// 递归去掉没有一级菜单的数据
        /// </summary>
        /// <param name="listP"></param>
        /// <returns></returns>
        /// 
        [CheckLogin]
        public List<TreeAuthPage> GetTreeAuthPage(List<TreeAuthPage> listP)
        {
            listP.ForEach(p =>
            {
                if (!string.IsNullOrEmpty(p.pId)&& p.pId!="0")
                {
                    var nCount = listP.Count(pp => pp.id == p.pId);
                    if (nCount == 0)
                    {
                        listP.Remove(p);
                        GetTreeAuthPage(listP);
                    }
                }
            });
            return listP;
        }
 
        /// <summary>
        /// 保存角色权限操作
        /// </summary>
        /// <param name="roleid"></param>
        /// <param name="pageid"></param>
        /// <returns></returns>
        /// 
        [CheckLogin]
        public string Save(string roleid = "", string pageid = "")
        {
            var curentuser = JsonConvert.DeserializeObject<PltUser>(HttpContext.Session.GetString("User"));
            ViewData["curentuser"] = curentuser;
            var result = new ResultEntity();
            #region 数据验证
            if (string.IsNullOrWhiteSpace(roleid))
            {
                result.Result = false;
                result.Message = "请选择角色";
                return JsonConvert.SerializeObject(result);
            }
            if (string.IsNullOrWhiteSpace(pageid))
            {
                result.Result = false;
                result.Message = "请选择权限";
                return JsonConvert.SerializeObject(result);
            }
            #endregion
            return JsonConvert.SerializeObject(_pltPageService.SaveManyEntity(roleid, pageid, curentuser.Id));
        }
    }
}