schangxiang@126.com
2025-09-17 e8e8a06fc68a6a645ce32be2cc9c3aaa67a97d68
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
155
156
157
158
159
160
using FineUIPro;
using FineUIPro.iWareWms;
using iWareCommon.Common.Entity;
using iWareDataCore.RBAC.Entity;
using iWareDataCore.RBAC.EnumType;
using iWareDataCore.RBAC.Service;
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace iWareWms.View.RBAC.RoleContent
{
    public partial class RoleContent : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
 
        private void LoadData()
        {
 
            BindGridRoles();
            if (GridRole.Rows.Count > 0)
            {
                GridRole.SelectedRowIndex = 0;
            }
            BindGridMenu();
        }
 
        /// <summary>
        /// 查询所有角色
        /// </summary>
        private void BindGridRoles()
        {
            string msg;
            var roles = RoleService.GetInstance().QueryByParam(new QueryParam(),out msg);
            GridRole.DataSource = roles;
            GridRole.DataBind();
        }
 
        /// <summary>
        /// 查询所有菜单
        /// </summary>
        private void BindGridMenu()
        {
            try
            {
                string msg;
                var contents = ContentService.GetInstance().QueryByParam(new QueryParam {
                    Filter = new Dictionary<string, object> { { "Type", EContentType.用于BS端的菜单 } },
                    Order = new Dictionary<string, object> { { "Type", "ASC" }, { "ContentIndex", "ASC" } } }, out msg);
 
                if (!string.IsNullOrEmpty(msg))
                {
                    Alert.ShowInTop(msg);
                    return;
                }
                GridMenu.DataSource = contents;
                GridMenu.DataBind();
                int userRoleId = GetSelectedDataKeyID(GridRole);
                List<string> selectArray = new List<string>();
                if (userRoleId > -1)
                {
                    var res = RoleContentService.GetInstance().QueryByParam(new QueryParam { Filter = new Dictionary<string, object> { { "RoleId", userRoleId } } }, out msg);
                    for (var i = 0; i < res.Count; i++)
                    {
                        selectArray.Add(res[i].ContentId.ToString());
                    }
                    GridMenu.SelectedRowIDArray = selectArray.ToArray();
                }
                WriteLog("角色权限更新" + msg, "权限分配");
 
                
 
            }
            catch (Exception ex)
            {
                Alert.ShowInTop(ex.Message);
 
            }
        }
 
        protected void GridRole_RowClick(object sender, EventArgs e)
        {
            BindGridMenu();
        }
 
 
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            if (GridMenu.SelectedRowIndexArray.Length < 1)
            {
                Alert.ShowInTop("请至少选择一项");
                return;
            }
            int roleId = GetSelectedDataKeyID(GridRole);
            List<int> ids = new List<int>();
            var menuList = new List<RoleContentEntity>();
            for (int i = 0; i < GridMenu.SelectedRowIndexArray.Length; i++)
            {
                int index = GridMenu.SelectedRowIndexArray[i];
                menuList.Add(new RoleContentEntity { RoleId = roleId, ContentId = Convert.ToInt32(GridMenu.Rows[index].DataKeys[0]), Value = 5 });
            }
            string msg;
            var res = RoleContentService.GetInstance().Save( roleId, menuList, out msg);
 
            Alert.ShowInTop(string.IsNullOrEmpty(msg) ? "保存成功" : msg);
            
        }
 
        protected void GridMenu_RowClick(object sender, GridRowClickEventArgs e)
        {
            string msg;
            var contentDict = ContentService.GetInstance().ToDictionary(new QueryParam(), out msg);
            List<int> ids = GetSelectedDataKeyIDs(GridMenu);
            var content = contentDict.Values.ToList();
            object[] keys = GridMenu.DataKeys[e.RowIndex];
            var id = Convert.ToInt32(keys[0]);
            var parentId = Convert.ToInt32(keys[2]);
            if (!ids.Contains(id) && parentId == -1)
            {
                var contents = contentDict.Values.Where(x => x.ParentId == Convert.ToInt32(keys[0])).ToList();
                if (contents.Count > 0)
                {
                    contents.ForEach(x => ids.Remove(x.Id));
                }
            }
            else
            {
                if(Convert.ToInt32(keys[2])!=-1){
                    var contents = contentDict.Values.Where(x => x.Id == Convert.ToInt32(keys[2])).ToList();
                    ids.Add(contents[0].Id);
                }else{
                    var contents = contentDict.Values.Where(x => x.ParentId == Convert.ToInt32(keys[0])).ToList();
                    if (contents.Count > 0)
                    {
                        contents.ForEach(x => ids.Add(x.Id));
                    }
                }
            }
            
            GridMenu.SelectedRowIDArray = ids.ConvertAll<string>(x => x.ToString()).ToArray();
        }
 
        /// <summary>
        /// 改变contentType显示信息
        /// </summary>
        /// <param name="contentType"></param>
        /// <returns></returns>
        protected string GetContentType(object contentType)
        {
            return ((EContentType)contentType).ToString();
        }
    }
 
}