Пример #1
0
        /// <summary>
        /// 新增\修改
        /// </summary>
        /// <param name="Dto"></param>
        /// <returns></returns>
        public async Task <Guid> SaveAsync(Sys_MenuDto Dto)
        {
            var model       = Dto.Model;
            var functionIds = Dto.FunctionIds;

            model = await this.InsertOrUpdateAsync(model);

            //
            await dbMenuFunction.DeleteAsync(w => w.MenuFunction_MenuID == model.Menu_ID);

            if (functionIds.Count > 0)
            {
                var _Sys_MenuFunctionList = await dbMenuFunction.ToListAsync(w => w.MenuFunction_MenuID == model.Menu_ID);

                foreach (var item in functionIds)
                {
                    var _Sys_MenuFunction = _Sys_MenuFunctionList.FirstOrDefault(w => w.MenuFunction_FunctionID == item);

                    var menufuncModel = new Sys_MenuFunction();
                    menufuncModel.MenuFunction_ID = _Sys_MenuFunction == null?Guid.NewGuid() : _Sys_MenuFunction.MenuFunction_ID;

                    menufuncModel.MenuFunction_FunctionID = item;
                    menufuncModel.MenuFunction_MenuID     = model.Menu_ID;

                    await dbMenuFunction.InsertAsync(menufuncModel);
                }
            }

            return(model.Menu_ID);
        }
        /// <summary>
        /// 写入操作日志
        /// </summary>
        public async Task InsertAppLogAsync()
        {
            var _QueryString = httpContext.Request.QueryString.ToString();
            var _ApiUrl      = httpContext.Request.Path;
            var _IP          = httpContext.Connection.RemoteIpAddress.ToString();

            //本机不记录
            // if (_IP == "::1") return;

            var body = string.Empty;
            var form = string.Empty;

            //body
            try
            {
                //读取 body 信息
                var reader = new StreamReader(httpContext.Request.Body);
                body = await reader.ReadToEndAsync();

                httpContext.Request.Body.Position = 0;//必须存在

                //Stream stream = httpContext.Request.Body;
                //byte[] buffer = new byte[httpContext.Request.ContentLength.Value];
                //await stream.ReadAsync(buffer, 0, buffer.Length);
                //body = Encoding.UTF8.GetString(buffer);
            }
            catch (Exception)
            {
            }
            //form
            try
            {
                //读取 表单 信息
                var _Form = httpContext.Request.Form;
                if (_Form != null)
                {
                    var _Dictionary = new Dictionary <string, object>();
                    foreach (var key in _Form.Keys)
                    {
                        _Dictionary[key] = _Form[key];
                    }

                    form = JsonConvert.SerializeObject(_Dictionary);
                }
            }
            catch (Exception) { }

            var appLogModel = new Sys_AppLog();

            appLogModel.AppLog_Api         = _ApiUrl;
            appLogModel.AppLog_IP          = _IP;
            appLogModel.AppLog_Form        = form;
            appLogModel.AppLog_QueryString = _QueryString;
            appLogModel.AppLog_FormBody    = body;
            appLogModel.AppLog_UserID      = info?.UserID;

            await dbAppLog.InsertAsync(appLogModel);
        }
        /// <summary>
        /// 新增\修改
        /// </summary>
        /// <param name="Dto"></param>
        /// <returns></returns>
        public async Task <Guid> SaveAsync(Sys_UserDto Dto)
        {
            var model   = Dto.Model;
            var roleIds = Dto.RoleIds;

            if (string.IsNullOrWhiteSpace(model.User_Pwd))
            {
                MessageBox.Show("密码不能为空!");
            }

            if (model.User_ID == Guid.Empty)
            {
                model.User_Pwd = string.IsNullOrWhiteSpace(model.User_Pwd) ? "123" : model.User_Pwd; //Tools.MD5Encrypt("123");
                model          = await this.InsertAsync(model);
            }
            else
            {
                await this.UpdateByIdAsync(model);
            }

            //变更用户角色
            if (roleIds.Count > 0)
            {
                var _Sys_UserRoleList = await dbUserRole.ToListAsync(w => w.UserRole_UserID == model.User_ID);

                await dbUserRole.DeleteAsync(w => w.UserRole_UserID == model.User_ID);

                foreach (var item in roleIds)
                {
                    var _Sys_UserRole = _Sys_UserRoleList.FirstOrDefault(w => w.UserRole_RoleID == item);

                    var userRoleModel = new Sys_UserRole();
                    userRoleModel.UserRole_ID = _Sys_UserRole == null?Guid.NewGuid() : _Sys_UserRole.UserRole_ID;

                    userRoleModel.UserRole_RoleID = item;
                    userRoleModel.UserRole_UserID = model.User_ID;
                    await dbUserRole.InsertAsync(userRoleModel);
                }
            }

            return(model.User_ID);
        }