From f212043917beb4ab5747940ecdb69d4b728c2453 Mon Sep 17 00:00:00 2001 From: yubao Date: Thu, 14 Dec 2017 20:36:56 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=92=E8=89=B2=E6=9D=BF=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OpenAuth.App/OpenAuth.App.csproj | 4 +- OpenAuth.App/Request/QueryRoleListReq.cs | 7 + .../Response/{RoleVM.cs => RoleView.cs} | 8 +- OpenAuth.App/RoleApp.cs | 101 +++++++++++ .../Controllers/RoleManagerController.cs | 66 +++++++ OpenAuth.Mvc/OpenAuth.Mvc.csproj | 3 + OpenAuth.Mvc/Views/RoleManager/Index.cshtml | 69 ++++++++ OpenAuth.Mvc/js/roles.js | 167 ++++++++++++++++++ OpenAuth.UnitTest/TestOrg.cs | 94 +++++----- 9 files changed, 467 insertions(+), 52 deletions(-) create mode 100644 OpenAuth.App/Request/QueryRoleListReq.cs rename OpenAuth.App/Response/{RoleVM.cs => RoleView.cs} (86%) create mode 100644 OpenAuth.App/RoleApp.cs create mode 100644 OpenAuth.Mvc/Controllers/RoleManagerController.cs create mode 100644 OpenAuth.Mvc/Views/RoleManager/Index.cshtml create mode 100644 OpenAuth.Mvc/js/roles.js diff --git a/OpenAuth.App/OpenAuth.App.csproj b/OpenAuth.App/OpenAuth.App.csproj index 233d8374..cf753824 100644 --- a/OpenAuth.App/OpenAuth.App.csproj +++ b/OpenAuth.App/OpenAuth.App.csproj @@ -114,10 +114,12 @@ + + @@ -137,7 +139,7 @@ - + diff --git a/OpenAuth.App/Request/QueryRoleListReq.cs b/OpenAuth.App/Request/QueryRoleListReq.cs new file mode 100644 index 00000000..953b9f4f --- /dev/null +++ b/OpenAuth.App/Request/QueryRoleListReq.cs @@ -0,0 +1,7 @@ +namespace OpenAuth.App.Request +{ + public class QueryRoleListReq : PageReq + { + public string orgId { get; set; } + } +} diff --git a/OpenAuth.App/Response/RoleVM.cs b/OpenAuth.App/Response/RoleView.cs similarity index 86% rename from OpenAuth.App/Response/RoleVM.cs rename to OpenAuth.App/Response/RoleView.cs index c7ee0bb7..5ea3c5ce 100644 --- a/OpenAuth.App/Response/RoleVM.cs +++ b/OpenAuth.App/Response/RoleView.cs @@ -17,7 +17,7 @@ using OpenAuth.Repository.Domain; namespace OpenAuth.App.Response { - public partial class RoleVM + public partial class RoleView { /// /// 用户ID @@ -56,12 +56,12 @@ namespace OpenAuth.App.Response /// public bool Checked { get; set; } - public static implicit operator RoleVM(Role role) + public static implicit operator RoleView(Role role) { - return role.MapTo(); + return role.MapTo(); } - public static implicit operator Role(RoleVM rolevm) + public static implicit operator Role(RoleView rolevm) { return rolevm.MapTo(); } diff --git a/OpenAuth.App/RoleApp.cs b/OpenAuth.App/RoleApp.cs new file mode 100644 index 00000000..c7197475 --- /dev/null +++ b/OpenAuth.App/RoleApp.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using OpenAuth.App.Request; +using OpenAuth.App.Response; +using OpenAuth.App.SSO; +using OpenAuth.Repository.Domain; + + +namespace OpenAuth.App +{ + public class RoleApp : BaseApp + { + public RevelanceManagerApp ReleManagerApp { get; set; } + + /// + /// 加载当前登录用户可访问的一个部门及子部门全部角色 + /// + public TableData Load(QueryRoleListReq request) + { + var loginUser = AuthUtil.GetCurrentUser(); + + string cascadeId = ".0."; + if (!string.IsNullOrEmpty(request.orgId)) + { + var org = loginUser.Orgs.SingleOrDefault(u => u.Id == request.orgId); + cascadeId = org.CascadeId; + } + + var ids = loginUser.Orgs.Where(u => u.CascadeId.Contains(cascadeId)).Select(u => u.Id).ToArray(); + var roleIds = ReleManagerApp.Get(Define.ROLEORG, false, ids); + + var roles = UnitWork.Find(u => roleIds.Contains(u.Id)) + .OrderBy(u => u.Name) + .Skip((request.page - 1) * request.limit) + .Take(request.limit); + + var records = Repository.GetCount(u => roleIds.Contains(u.Id)); + + + var userviews = new List(); + foreach (var role in roles) + { + RoleView uv = role; + var orgs = LoadByRole(role.Id); + uv.Organizations = string.Join(",", orgs.Select(u => u.Name).ToList()); + uv.OrganizationIds = string.Join(",", orgs.Select(u => u.Id).ToList()); + userviews.Add(uv); + } + + return new TableData + { + count = records, + data = userviews, + }; + } + + public void AddOrUpdate(RoleView view) + { + if (string.IsNullOrEmpty(view.OrganizationIds)) + throw new Exception("请为用户分配机构"); + Role role = view; + if (string.IsNullOrEmpty(view.Id)) + { + if (UnitWork.IsExist(u => u.Account == view.Name)) + { + throw new Exception("用户账号已存在"); + } + role.CreateTime = DateTime.Now; + Repository.Add(role); + view.Id = role.Id; //要把保存后的ID存入view + } + else + { + UnitWork.Update(u => u.Id == view.Id, u => new User + { + Name = role.Name, + Status = role.Status + }); + } + string[] orgIds = view.OrganizationIds.Split(',').ToArray(); + + ReleManagerApp.DeleteBy(Define.ROLEORG, role.Id); + ReleManagerApp.AddRelevance(Define.ROLEORG, orgIds.ToLookup(u => role.Id)); + } + + /// + /// 加载角色的所有机构 + /// + public IEnumerable LoadByRole(string roleId) + { + var result = from userorg in UnitWork.Find(null) + join org in UnitWork.Find(null) on userorg.SecondId equals org.Id + where userorg.FirstId == roleId && userorg.Key == Define.ROLEORG + select org; + return result; + } + + + } +} \ No newline at end of file diff --git a/OpenAuth.Mvc/Controllers/RoleManagerController.cs b/OpenAuth.Mvc/Controllers/RoleManagerController.cs new file mode 100644 index 00000000..97026f53 --- /dev/null +++ b/OpenAuth.Mvc/Controllers/RoleManagerController.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Web.Http; +using System.Web.Mvc; +using Infrastructure; +using OpenAuth.App; +using OpenAuth.App.Request; +using OpenAuth.App.Response; +using OpenAuth.Mvc.Models; + +namespace OpenAuth.Mvc.Controllers +{ + public class RoleManagerController : BaseController + { + public RoleApp App { get; set; } + + // + // GET: /UserManager/ + [Authenticate] + public ActionResult Index() + { + return View(); + } + + //添加或修改组织 + [System.Web.Mvc.HttpPost] + public string AddOrUpdate(RoleView view) + { + try + { + App.AddOrUpdate(view); + + } + catch (Exception ex) + { + Result.Code = 500; + Result.Message = ex.Message; + } + return JsonHelper.Instance.Serialize(Result); + } + + /// + /// 加载组织下面的所有用户 + /// + public string Load([FromUri]QueryRoleListReq request) + { + return JsonHelper.Instance.Serialize(App.Load(request)); + } + + [System.Web.Mvc.HttpPost] + public string Delete(string[] ids) + { + try + { + App.Delete(ids); + } + catch (Exception e) + { + Result.Code = 500; + Result.Message = e.Message; + } + + return JsonHelper.Instance.Serialize(Result); + } + } +} \ No newline at end of file diff --git a/OpenAuth.Mvc/OpenAuth.Mvc.csproj b/OpenAuth.Mvc/OpenAuth.Mvc.csproj index a04ea0b5..57a4c126 100644 --- a/OpenAuth.Mvc/OpenAuth.Mvc.csproj +++ b/OpenAuth.Mvc/OpenAuth.Mvc.csproj @@ -155,6 +155,7 @@ + @@ -194,6 +195,7 @@ + @@ -351,6 +353,7 @@ + diff --git a/OpenAuth.Mvc/Views/RoleManager/Index.cshtml b/OpenAuth.Mvc/Views/RoleManager/Index.cshtml new file mode 100644 index 00000000..6468469c --- /dev/null +++ b/OpenAuth.Mvc/Views/RoleManager/Index.cshtml @@ -0,0 +1,69 @@ +@section header +{ + +} + + +
+
    + + + + + + + + + + +
    角色名称所属部门创建时间
    +
    + + + + + + + \ No newline at end of file diff --git a/OpenAuth.Mvc/js/roles.js b/OpenAuth.Mvc/js/roles.js new file mode 100644 index 00000000..c4f04d6e --- /dev/null +++ b/OpenAuth.Mvc/js/roles.js @@ -0,0 +1,167 @@ +layui.config({ + base: "/js/" +}).use(['form','vue', 'ztree', 'layer', 'jquery', 'table','droptree','openauth'], function () { + var form = layui.form, + //layer = (parent == undefined || parent.layer === undefined )? layui.layer : parent.layer, + layer = layui.layer, + $ = layui.jquery; + var table = layui.table; + var openauth = layui.openauth; + layui.droptree("/UserSession/GetOrgs", "#Organizations", "#OrganizationIds"); + + //主列表加载,可反复调用进行刷新 + var config= {}; //table的参数,如搜索key,点击tree的id + var mainList = function (options) { + if (options != undefined) { + $.extend(config, options); + } + table.reload('mainList', { + url: '/RoleManager/Load', + where: config + }); + } + //左边树状机构列表 + var ztree = function () { + var url = '/UserSession/GetOrgs'; + var zTreeObj; + var setting = { + view: { selectedMulti: false }, + data: { + key: { + name: 'Name', + title: 'Name' + }, + simpleData: { + enable: true, + idKey: 'Id', + pIdKey: 'ParentId', + rootPId: "" + } + }, + callback: { + onClick: function (event, treeId, treeNode) { + mainList({ orgId: treeNode.Id }); + } + } + }; + var load = function () { + $.getJSON(url, function (json) { + zTreeObj = $.fn.zTree.init($("#tree"), setting); + var newNode = { Name: "根节点", Id: null,ParentId:"" }; + json.push(newNode); + zTreeObj.addNodes(null, json); + mainList({ orgId: "" }); + zTreeObj.expandAll(true); + }); + }; + load(); + return { + reload: load + } + }(); + + //添加(编辑)对话框 + var editDlg = function() { + var vm = new Vue({ + el: "#formEdit" + }); + var update = false; //是否为更新 + var show = function (data) { + var title = update ? "编辑信息" : "添加"; + layer.open({ + title: title, + area: ["500px", "400px"], + type: 1, + content: $('#divEdit'), + success: function() { + vm.$set('$data', data); + }, + end: mainList + }); + var url = "/RoleManager/AddOrUpdate"; + if (update) { + url = "/RoleManager/AddOrUpdate"; //暂时和添加一个地址 + } + //提交数据 + form.on('submit(formSubmit)', + function(data) { + $.post(url, + data.field, + function(data) { + layer.msg(data.Message); + }, + "json"); + return false; + }); + } + return { + add: function() { //弹出添加 + update = false; + show({ + Id: '' + }); + }, + update: function(data) { //弹出编辑框 + update = true; + show(data); + } + }; + }(); + + //监听表格内部按钮 + table.on('tool(list)', function (obj) { + var data = obj.data; + if (obj.event === 'detail') { //查看 + layer.msg('ID:' + data.Id + ' 的查看操作'); + } + }); + + + //监听页面主按钮操作 + var active = { + btnDel: function () { //批量删除 + var checkStatus = table.checkStatus('mainList') + , data = checkStatus.data; + openauth.del("/RoleManager/Delete", + data.map(function (e) { return e.Id; }), + mainList); + } + , btnAdd: function () { //添加 + editDlg.add(); + } + , btnEdit: function () { //编辑 + var checkStatus = table.checkStatus('mainList') + , data = checkStatus.data; + if (data.length != 1) { + layer.msg("请选择编辑的行,且同时只能编辑一行"); + return; + } + editDlg.update(data[0]); + } + + , search: function () { //搜索 + mainList({ key: $('#key').val() }); + } + , btnRefresh: function() { + mainList(); + } + , btnAccessModule: function () { + var index = layer.open({ + title: "为用户分配模块", + type: 2, + area: ['800px', '500px'], + content: "/ModuleManager/Assign", + success: function(layero, index) { + + } + }); + } + }; + + $('.toolList .layui-btn').on('click', function () { + var type = $(this).data('type'); + active[type] ? active[type].call(this) : ''; + }); + + //监听页面主按钮操作 end +}) \ No newline at end of file diff --git a/OpenAuth.UnitTest/TestOrg.cs b/OpenAuth.UnitTest/TestOrg.cs index f6d1057d..e1f6e7f0 100644 --- a/OpenAuth.UnitTest/TestOrg.cs +++ b/OpenAuth.UnitTest/TestOrg.cs @@ -1,47 +1,47 @@ -using System; -using System.Diagnostics; -using Infrastructure; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using OpenAuth.App; -using OpenAuth.Repository.Domain; - -namespace OpenAuth.UnitTest -{ - [TestClass] - public class TestOrg : TestBase - { - private OrgManagerApp _app; - - public TestOrg() - { - _app = AutofacExt.GetFromFac(); - } - - - [TestMethod] - public void Add() - { - try - { - for (int i = 0; i < 2; i++) - { - var random = new Random(); - int val = random.Next(); - _app.Add(new Org - { - Id = string.Empty, - Name = "test" + val, - CreateTime = DateTime.Now, - ParentId = null, - ParentName = "", - SortNo = 1 - }); - } - } - catch (Exception e) - { - Console.WriteLine(e.Message); - } - } - } -} +using System; +using System.Diagnostics; +using Infrastructure; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using OpenAuth.App; +using OpenAuth.Repository.Domain; + +namespace OpenAuth.UnitTest +{ + [TestClass] + public class TestOrg : TestBase + { + private OrgManagerApp _app; + + public TestOrg() + { + _app = AutofacExt.GetFromFac(); + } + + + [TestMethod] + public void Add() + { + try + { + for (int i = 0; i < 2; i++) + { + var random = new Random(); + int val = random.Next(); + _app.Add(new Org + { + Id = string.Empty, + Name = "test" + val, + CreateTime = DateTime.Now, + ParentId = null, + ParentName = "", + SortNo = 1 + }); + } + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + } + } +}