Exemplo n.º 1
0
        /// <summary>
        /// 新建或编辑菜单
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public async Task <WebResponseContent> Save(Sys_Menu menu)
        {
            WebResponseContent webResponse = new WebResponseContent();

            bool result = true;;

            try
            {
                if (menu == null)
                {
                    return(webResponse.Error("没有获取到提交的参数"));
                }

                webResponse = menu.ValidationEntity(x => new { x.MenuName, x.TableName });
                if (!webResponse.Status)
                {
                    return(webResponse);
                }

                if (menu.Menu_Id <= 0)
                {
                    menu.SetCreateDefaultVal();
                    repository.Add(menu);
                }
                else
                {
                    menu.SetModifyDefaultVal();
                    repository.Update(menu, p => new
                    {
                        p.ParentId,
                        p.MenuName,
                        p.Url,
                        p.Auth,
                        p.OrderNo,
                        p.Icon,
                        p.Enable,
                        p.TableName,
                        p.ModifyDate,
                        p.Modifier
                    });
                }
                await repository.SaverChangesAsync();

                if (result)
                {
                    _menuVersionn = DateTime.Now.ToString("yyyyMMddHHMMssfff");
                    _menus        = null;
                }
                webResponse.OK("保存成功", menu);
            }
            catch (Exception ex)
            {
                webResponse.Error(ex.Message);
            }
            finally
            {
                Logger.Info($"表:{menu.TableName},菜单:{menu.MenuName},权限{menu.Auth},{(webResponse.Status ? "成功" : "失败")}{webResponse.Message}");
            }
            return(webResponse);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 新建或编辑菜单
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public async Task <WebResponseContent> Save(Sys_Menu menu)
        {
            WebResponseContent webResponse = new WebResponseContent();

            if (menu == null)
            {
                return(webResponse.Error("没有获取到提交的参数"));
            }
            if (menu.Menu_Id > 0 && menu.Menu_Id == menu.ParentId)
            {
                return(webResponse.Error("父级ID不能是当前菜单的ID"));
            }
            try
            {
                webResponse = menu.ValidationEntity(x => new { x.MenuName, x.TableName });
                if (!webResponse.Status)
                {
                    return(webResponse);
                }
                if (menu.TableName != "/" && menu.TableName != ".")
                {
                    Sys_Menu sysMenu = await repository.FindAsyncFirst(x => x.TableName == menu.TableName);

                    if (sysMenu != null)
                    {
                        if ((menu.Menu_Id > 0 && sysMenu.Menu_Id != menu.Menu_Id) ||
                            menu.Menu_Id <= 0)
                        {
                            return(webResponse.Error($"视图/表名【{menu.TableName}】已被其他菜单使用"));
                        }
                    }
                }

                if (menu.Menu_Id <= 0)
                {
                    repository.Add(menu.SetCreateDefaultVal());
                }
                else
                {
                    //2020.05.07新增禁止选择上级角色为自己
                    if (menu.Menu_Id == menu.ParentId)
                    {
                        return(WebResponseContent.Instance.Error($"父级id不能为自己"));
                    }
                    if (repository.Exists(x => x.ParentId == menu.Menu_Id && menu.ParentId == x.Menu_Id))
                    {
                        return(WebResponseContent.Instance.Error($"不能选择此父级id,选择的父级id与当前菜单形成依赖关系"));
                    }
                    repository.Update(menu.SetModifyDefaultVal(), p => new
                    {
                        p.ParentId,
                        p.MenuName,
                        p.Url,
                        p.Auth,
                        p.OrderNo,
                        p.Icon,
                        p.Enable,
                        p.TableName,
                        p.ModifyDate,
                        p.Modifier
                    });
                }
                await repository.SaveChangesAsync();

                _menuVersionn = DateTime.Now.ToString("yyyyMMddHHMMssfff");
                _menus        = null;
                webResponse.OK("保存成功", menu);
            }
            catch (Exception ex)
            {
                webResponse.Error(ex.Message);
            }
            finally
            {
                Logger.Info($"表:{menu.TableName},菜单:{menu.MenuName},权限{menu.Auth},{(webResponse.Status ? "成功" : "失败")}{webResponse.Message}");
            }
            return(webResponse);
        }