public static string GetRandomStr(int length, ContextString dbContext) { Random rd = new Random(); string str = "abcdefghijklmnopqrstuvwxyz0123456789"; bool isnewstr = true; string result = string.Empty; do { result = string.Empty; for (int i = 0; i < length; i++) { result += str[rd.Next(str.Length)]; } var count = dbContext.RandomStr.Where(i => i.Str == result).Count(); if (count <= 0) { dbContext.RandomStr.Add(new RandomStr { AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Str = result, UpdateTime = DateTime.Now }); if (dbContext.SaveChanges() > 0) { isnewstr = false; } } } while (isnewstr); return(result); }
public static bool CreateServerToIOT() { DbContextOptions <ContextString> options = new DbContextOptions <ContextString>(); ContextString dbContext = new ContextString(options); QianMuResult _Result = new QianMuResult(); var serveriot = dbContext.ServerIOT.FirstOrDefault(); if (serveriot == null) { var servermac = Method.GetServerMac().Replace(":", ""); //调用云端接口创建设备 var url = Method.MallSite + "API/IOT/AddFrontServer"; var data = new { ServerMac = servermac, MallCode = Method.CusID }; try { _Result = Method.PostMothsToObj(url, JsonHelper.SerializeJSON(data)); if (_Result.Code == "200") { IOTReturn _IOTReturn = new IOTReturn(); _IOTReturn = (IOTReturn)Newtonsoft.Json.JsonConvert.DeserializeObject(_Result.Data.ToString(), _IOTReturn.GetType()); dbContext.ServerIOT.Add(new Models.ServerIOT { AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Key = _IOTReturn.Key, Name = _IOTReturn.UserName, ServerMac = servermac, UpdateTime = DateTime.Now }); } if (dbContext.SaveChanges() >= 0) { return(true); } else { return(false); } } catch (Exception) { return(false); } } else { return(true); } }
/// <summary> /// 更新服务器状态 /// </summary> public static void UpdateDevState() { DateTime dt = DateTime.Now; try { QMLog qm = new QMLog(); qm.WriteLogToFile("", "Hangfire定时器执行"); DbContextOptions <ContextString> options = new DbContextOptions <ContextString>(); ContextString dbContext = new ContextString(options); var list = dbContext.Device.ToList(); foreach (var dev in list) { var de = Method.DeviceCommandList.Where(i => i.Code == dev.Code).FirstOrDefault(); if (de == null) { dev.DeviceOnline = false; dev.FrontOnline = false; } else { if (de.DevBreathTime >= dt.AddMinutes(-1)) { dev.DeviceOnline = true; } else { dev.DeviceOnline = false; } if (de.AppBreathTime >= dt.AddMinutes(-1)) { dev.FrontOnline = true; } else { dev.FrontOnline = false; } } } dbContext.Device.UpdateRange(list); dbContext.SaveChanges(); } catch (Exception e) { QMLog qm = new QMLog(); qm.WriteLogToFile("", e.ToString()); } }
public static string GetCusID(ContextString dbContext, string regkey) { var prj = dbContext.ProjectInfo.Where(i => i.RegKey == regkey).FirstOrDefault(); if (prj == null) { var url = Method.MallSite + "API/CDN/GetCusID"; QianMuResult qianMuResult = new QianMuResult(); var param = new { RegKey = regkey }; try { qianMuResult = Method.PostMothsToObj(url, JsonHelper.SerializeJSON(param)); if (qianMuResult.Code == "200") { dbContext.ProjectInfo.Add(new ProjectInfo { AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), CusID = qianMuResult.Data.ToString(), RegKey = regkey, UpdateTime = DateTime.Now }); if (dbContext.SaveChanges() > 0) { return(qianMuResult.Data.ToString()); } else { return(string.Empty); } } else { return(string.Empty); } } catch (Exception) { return(string.Empty); } } else { return(prj.CusID); } }
public async Task <IActionResult> ParkingSpaceDel(string code, string userName, [FromServices] ContextString dbContext) { QianMuResult _Result = new QianMuResult(); if (string.IsNullOrEmpty(userName)) { //检测用户登录情况 var uol = Method.GetLoginUserName(dbContext, this.HttpContext); if (string.IsNullOrEmpty(uol.UserName)) { _Result.Code = "401"; _Result.Msg = "请登陆后再进行操作"; _Result.Data = ""; return(Json(_Result)); } else { userName = uol.UserName; } } if (string.IsNullOrEmpty(code)) { _Result.Code = "510"; _Result.Msg = "请输入一个停车位编码"; _Result.Data = ""; return(Json(_Result)); } var ps = await dbContext.ParkingSpace.Where(i => i.Code == code && !i.IsDel).FirstOrDefaultAsync(); if (ps == null) { _Result.Code = "510"; _Result.Msg = "需要有效的停车位编码"; _Result.Data = ""; return(Json(_Result)); } ps.IsDel = true; ps.UpdateTime = DateTime.Now; dbContext.ParkingSpace.Update(ps); if (await dbContext.SaveChangesAsync() > 0) { _Result.Code = "200"; _Result.Msg = "删除成功"; _Result.Data = ""; var ip = Method.GetUserIp(this.HttpContext); dbContext.SysLog.Add(new SysLog { AccountName = userName, ModuleName = "停车场管理", LogMsg = userName + "删除了编号为:" + ps.Num + "的停车位", AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Type = "删除", IP = ip }); dbContext.SaveChanges(); } else { _Result.Code = "2"; _Result.Msg = "删除失败"; _Result.Data = ""; } return(Json(_Result)); }
public async Task <IActionResult> ParkingSpaceAdd(Input_ParkingSpace model, [FromServices] ContextString dbContext) { QianMuResult _Result = new QianMuResult(); Stream stream = HttpContext.Request.Body; byte[] buffer = new byte[HttpContext.Request.ContentLength.Value]; stream.Read(buffer, 0, buffer.Length); string inputStr = Encoding.UTF8.GetString(buffer); model = (Input_ParkingSpace)Newtonsoft.Json.JsonConvert.DeserializeObject(inputStr, model.GetType()); if (string.IsNullOrEmpty(model.UserName)) { //检测用户登录情况 var uol = Method.GetLoginUserName(dbContext, this.HttpContext); if (string.IsNullOrEmpty(uol.UserName)) { _Result.Code = "401"; _Result.Msg = "请登陆后再进行操作"; _Result.Data = ""; return(Json(_Result)); } else { model.UserName = uol.UserName; } } if (string.IsNullOrEmpty(model.ParkCode) || string.IsNullOrEmpty(model.Num) || string.IsNullOrEmpty(model.Xaxis) || string.IsNullOrEmpty(model.Yaxis) || string.IsNullOrEmpty(model.NavXaxis) || string.IsNullOrEmpty(model.NavYaxis)) { _Result.Code = "510"; _Result.Msg = "输入项中存在空值"; _Result.Data = ""; return(Json(_Result)); } var pk = await dbContext.ParkingLot.Where(i => i.Code == model.ParkCode && !i.IsDel).FirstOrDefaultAsync(); if (pk == null) { _Result.Code = "510"; _Result.Msg = "需要有效的停车场编码"; _Result.Data = ""; return(Json(_Result)); } model.Num = Regex.Replace(model.Num, ";", ";"); var numList = model.Num.Split(";"); var errorAlready = new ArrayList(); var spaceList = new List <ParkingSpace>(); foreach (var item in numList) { if (!string.IsNullOrEmpty(item)) { var numCount = await dbContext.ParkingSpace.Where(i => i.ParkCode == pk.Code && i.Num == item && !i.IsDel).CountAsync(); if (numCount > 0) { errorAlready.Add(item); } else { spaceList.Add(new ParkingSpace { AddTime = DateTime.Now, Num = item, ParkCode = pk.Code, Xaxis = model.Xaxis, Yaxis = model.Yaxis, NavXaxis = model.NavXaxis, NavYaxis = model.NavYaxis, Code = Guid.NewGuid().ToString(), IsDel = false, UpdateTime = DateTime.Now }); } } } //var pscount = await dbContext.ParkingSpace.Where(i => i.Num == model.Num && !i.IsDel).CountAsync(); //if (pscount > 0) if (errorAlready.Count > 0) { _Result.Code = "510"; _Result.Msg = "停车位编码:" + string.Join(";", (string[])errorAlready.ToArray(typeof(string))) + "已存在"; _Result.Data = ""; return(Json(_Result)); } dbContext.ParkingSpace.AddRange(spaceList); if (await dbContext.SaveChangesAsync() > 0) { _Result.Code = "200"; _Result.Msg = "添加成功"; _Result.Data = ""; var ip = Method.GetUserIp(this.HttpContext); dbContext.SysLog.Add(new SysLog { AccountName = model.UserName, ModuleName = "停车场管理", LogMsg = model.UserName + "添加停车位", AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Type = "创建", IP = ip }); dbContext.SaveChanges(); } else { _Result.Code = "2"; _Result.Msg = "添加失败"; _Result.Data = ""; } return(Json(_Result)); }
public async Task <IActionResult> ParkingLotAdd(string FloorCode, string userNmae, [FromServices] ContextString dbContext) { QianMuResult _Result = new QianMuResult(); if (string.IsNullOrEmpty(userNmae)) { //检测用户登录情况 var uol = Method.GetLoginUserName(dbContext, this.HttpContext); if (string.IsNullOrEmpty(uol.UserName)) { _Result.Code = "401"; _Result.Msg = "请登陆后再进行操作"; _Result.Data = ""; return(Json(_Result)); } else { userNmae = uol.UserName; } } if (string.IsNullOrEmpty(FloorCode)) { _Result.Code = "510"; _Result.Msg = "请输入一个楼层编码"; _Result.Data = ""; return(Json(_Result)); } var floor = await dbContext.Floor.Where(i => i.Code == FloorCode && !i.IsDel).FirstOrDefaultAsync(); if (floor == null) { _Result.Code = "510"; _Result.Msg = "需要有效的楼层ID"; _Result.Data = ""; return(Json(_Result)); } dbContext.ParkingLot.Add(new ParkingLot { AddTime = DateTime.Now, FloorCode = FloorCode, IsDel = false, Code = Guid.NewGuid().ToString(), UpdateTime = DateTime.Now }); if (await dbContext.SaveChangesAsync() > 0) { _Result.Code = "200"; _Result.Msg = "添加成功"; _Result.Data = ""; } else { _Result.Code = "2"; _Result.Msg = "添加失败"; _Result.Data = ""; } var ip = Method.GetUserIp(this.HttpContext); dbContext.SysLog.Add(new SysLog { AccountName = userNmae, ModuleName = "停车场管理", LogMsg = userNmae + "将楼层Code为:" + FloorCode + ",楼层名称为:" + floor.Name + "的楼层标记为停车场", AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Type = "修改", IP = ip }); dbContext.SaveChanges(); return(Json(_Result)); }
public async Task <IActionResult> ParkingLotEdit(Input_PLEdit model, [FromServices] ContextString dbContext) { QianMuResult _Result = new QianMuResult(); Stream stream = HttpContext.Request.Body; byte[] buffer = new byte[HttpContext.Request.ContentLength.Value]; stream.Read(buffer, 0, buffer.Length); string inputStr = Encoding.UTF8.GetString(buffer); model = (Input_PLEdit)Newtonsoft.Json.JsonConvert.DeserializeObject(inputStr, model.GetType()); if (string.IsNullOrEmpty(model.MallCode)) { //检测用户登录情况 var uol = Method.GetLoginUserName(dbContext, this.HttpContext); if (string.IsNullOrEmpty(uol.UserName)) { _Result.Code = "401"; _Result.Msg = "请登陆后再进行操作"; _Result.Data = ""; return(Json(_Result)); } else { model.UserName = uol.UserName; model.MallCode = uol.MallCode; } } if (model.FloorCodes.Count() <= 0) { var spacecount = await dbContext.MallBuilding.Where(i => i.MallCode == model.MallCode).Join( dbContext.Building.Where(i => !i.IsDel), mb => mb.BuildingCode, bu => bu.Code, (mb, bu) => bu).Join( dbContext.Floor.Where(i => !i.IsDel), bu => bu.Code, fl => fl.BuildingCode, (bu, fl) => fl).Join( dbContext.ParkingLot.Where(i => !i.IsDel), fl => fl.Code, pl => pl.FloorCode, (fl, pl) => pl).Join( dbContext.ParkingSpace.Where(i => !i.IsDel), pl => pl.Code, ps => ps.ParkCode, (pl, ps) => ps ).AsNoTracking().CountAsync(); //var spacecount = await dbContext.ParkingSpace.Where(i => !i.IsDel).CountAsync(); if (spacecount > 0) { _Result.Code = "510"; _Result.Msg = "Erro:有" + spacecount + "个停车位正在被使用不可将停车场清空"; _Result.Data = ""; return(Json(_Result)); } // var pllist = dbContext.ParkingLot.Where(i => !i.IsDel).ToList(); var pllist = await dbContext.MallBuilding.Where(i => i.MallCode == model.MallCode).Join( dbContext.Building.Where(i => !i.IsDel), mb => mb.BuildingCode, bu => bu.Code, (mb, bu) => bu).Join( dbContext.Floor.Where(i => !i.IsDel), bu => bu.Code, fl => fl.BuildingCode, (bu, fl) => fl).Join( dbContext.ParkingLot.Where(i => !i.IsDel), fl => fl.Code, pl => pl.FloorCode, (fl, pl) => pl).AsNoTracking().ToListAsync(); foreach (var pl in pllist) { pl.IsDel = true; pl.UpdateTime = DateTime.Now; } dbContext.ParkingLot.UpdateRange(pllist); await dbContext.SaveChangesAsync(); _Result.Code = "200"; _Result.Msg = "编辑成功"; _Result.Data = ""; return(Json(_Result)); } foreach (var item in model.FloorCodes) { if (string.IsNullOrEmpty(item)) { _Result.Code = "510"; _Result.Msg = "Erro:编码不可为空"; _Result.Data = ""; return(Json(_Result)); } var floor = await dbContext.Floor.Where(i => i.Code == item).AsNoTracking().FirstOrDefaultAsync(); if (floor == null) { _Result.Code = "510"; _Result.Msg = "需要有效的楼层编码"; _Result.Data = ""; return(Json(_Result)); } } // var pls = await dbContext.ParkingLot.Where(i => !i.IsDel).ToListAsync(); var pls = await dbContext.MallBuilding.Where(i => i.MallCode == model.MallCode).Join( dbContext.Building.Where(i => !i.IsDel), mb => mb.BuildingCode, bu => bu.Code, (mb, bu) => bu).Join( dbContext.Floor.Where(i => !i.IsDel), bu => bu.Code, fl => fl.BuildingCode, (bu, fl) => fl).Join( dbContext.ParkingLot.Where(i => !i.IsDel), fl => fl.Code, pl => pl.FloorCode, (fl, pl) => pl).AsNoTracking().ToListAsync(); bool haschange = false; foreach (var pl in pls) { //ID不在新的列表中 删除 if (model.FloorCodes.Where(i => i == pl.FloorCode).Count() <= 0) { var scount = await dbContext.ParkingSpace.Where(i => i.ParkCode == pl.Code && !i.IsDel).CountAsync(); if (scount > 0) { var floor = await dbContext.Floor.Where(i => i.Code == pl.FloorCode && !i.IsDel).FirstOrDefaultAsync(); var bu = await dbContext.Building.Where(i => i.Code == floor.BuildingCode && !i.IsDel).FirstOrDefaultAsync(); var plName = bu.Name + floor.Name; _Result.Code = "510"; _Result.Msg = "Erro:停车场" + plName + "有" + scount + "个停车位正在被使用,不可移除"; _Result.Data = ""; return(Json(_Result)); } pl.IsDel = true; pl.UpdateTime = DateTime.Now; dbContext.ParkingLot.Update(pl); haschange = true; } } foreach (var code in model.FloorCodes) { //ID不在旧的列表中 添加 if (pls.Where(i => i.FloorCode == code).Count() <= 0) { dbContext.ParkingLot.Add(new ParkingLot { AddTime = DateTime.Now, FloorCode = code, IsDel = false, Code = Guid.NewGuid().ToString(), UpdateTime = DateTime.Now }); haschange = true; } } if (!haschange) { _Result.Code = "200"; _Result.Msg = "无数据变更"; _Result.Data = ""; return(Json(_Result)); } if (await dbContext.SaveChangesAsync() > 0) { _Result.Code = "200"; _Result.Msg = "添加成功"; _Result.Data = ""; var ip = Method.GetUserIp(this.HttpContext); dbContext.SysLog.Add(new SysLog { AccountName = model.UserName, ModuleName = "停车场管理", LogMsg = model.UserName + "编辑停车场信息", AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Type = "修改", IP = ip }); dbContext.SaveChanges(); } else { _Result.Code = "2"; _Result.Msg = "添加失败"; _Result.Data = ""; } return(Json(_Result)); }
public async Task <IActionResult> ParkingLotDel(string code, string userNmae, [FromServices] ContextString dbContext) { QianMuResult _Result = new QianMuResult(); if (string.IsNullOrEmpty(userNmae)) { //检测用户登录情况 var uol = Method.GetLoginUserName(dbContext, this.HttpContext); if (string.IsNullOrEmpty(uol.UserName)) { _Result.Code = "401"; _Result.Msg = "请登陆后再进行操作"; _Result.Data = ""; return(Json(_Result)); } else { userNmae = uol.UserName; } } if (string.IsNullOrEmpty(code)) { _Result.Code = "510"; _Result.Msg = "请输入一个停车场编码"; _Result.Data = ""; return(Json(_Result)); } var pl = await dbContext.ParkingLot.Where(i => i.Code == code && !i.IsDel).FirstOrDefaultAsync(); if (pl == null) { _Result.Code = "510"; _Result.Msg = "需要有效的停车场ID"; _Result.Data = ""; return(Json(_Result)); } var count = await dbContext.ParkingSpace.Where(i => i.ParkCode == code && !i.IsDel).CountAsync(); if (count > 0) { _Result.Code = "510"; _Result.Msg = "该停车场有:" + count + "个停车位在使用不可移除"; _Result.Data = ""; return(Json(_Result)); } pl.IsDel = true; pl.UpdateTime = DateTime.Now; dbContext.ParkingLot.Update(pl); if (await dbContext.SaveChangesAsync() > 0) { _Result.Code = "200"; _Result.Msg = "删除成功"; _Result.Data = ""; var floor = dbContext.Floor.Where(i => i.Code == code).FirstOrDefault(); var ip = Method.GetUserIp(this.HttpContext); dbContext.SysLog.Add(new SysLog { AccountName = userNmae, ModuleName = "停车场管理", LogMsg = userNmae + "将楼层编码为:" + code + "楼层名称为:" + floor == null ? "" : floor.Name + "的楼层从停车场移除", AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Type = "修改", IP = ip }); dbContext.SaveChanges(); } else { _Result.Code = "2"; _Result.Msg = "删除失败"; _Result.Data = ""; } return(Json(_Result)); }
public async Task <IActionResult> ShopFormatAdd(Input_ShopFormatAdd modelsf, [FromServices] ContextString dbContext) { QianMuResult _Result = new QianMuResult(); Stream stream = HttpContext.Request.Body; byte[] buffer = new byte[HttpContext.Request.ContentLength.Value]; stream.Read(buffer, 0, buffer.Length); string inputStr = Encoding.UTF8.GetString(buffer); modelsf = (Input_ShopFormatAdd)Newtonsoft.Json.JsonConvert.DeserializeObject(inputStr, modelsf.GetType()); var model = modelsf.Parameter; if (string.IsNullOrEmpty(model.MallCode)) { //检测用户登录情况 var uol = Method.GetLoginUserName(dbContext, this.HttpContext); if (string.IsNullOrEmpty(uol.UserName)) { _Result.Code = "401"; _Result.Msg = "请登陆后再进行操作"; _Result.Data = ""; return(Json(_Result)); } else { model.MallCode = uol.MallCode; model.UserName = uol.UserName; } } if (string.IsNullOrEmpty(model.Name)) { _Result.Code = "510"; _Result.Msg = "请输入业态名称"; _Result.Data = ""; return(Json(_Result)); } var mall = await dbContext.Mall.Where(i => i.Code == model.MallCode).FirstOrDefaultAsync(); if (mall == null) { _Result.Code = "510"; _Result.Msg = "无效的商场编码:" + model.MallCode; _Result.Data = ""; return(Json(_Result)); } var sf = await dbContext.ShopFormat.Where(i => (i.Name == model.Name && i.IsDel == false && string.IsNullOrEmpty(i.ParentCode) && i.MallCode == model.MallCode)).FirstOrDefaultAsync(); if (sf != null) { _Result.Code = "510"; _Result.Msg = "业态名称已存在"; _Result.Data = ""; return(Json(_Result)); } foreach (var item in model.Child) { if (model.Child.Where(i => i.Name == item.Name).Count() > 1) { _Result.Code = "510"; _Result.Msg = "Erro:子业态重复"; _Result.Data = ""; return(Json(_Result)); } } if (model.IconFile == null) { model.IconFile = string.Empty; } ShopFormat psf = new ShopFormat { IsDel = false, Color = model.Color, Name = model.Name, NameEn = model.NameEn, ParentCode = string.Empty, IconFile = model.IconFile, AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), UpdateTime = DateTime.Now, MallCode = model.MallCode }; dbContext.ShopFormat.Add(psf); var changecount = await dbContext.SaveChangesAsync(); var ip = Method.GetUserIp(this.HttpContext); dbContext.SysLog.Add(new SysLog { AccountName = model.UserName, ModuleName = "业态管理", LogMsg = model.UserName + "添加了业态:" + model.Name + "及其子业态", AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Type = "创建", IP = ip }); dbContext.SaveChanges(); foreach (var child in model.Child) { if (child.IconFile == null) { child.IconFile = string.Empty; } dbContext.ShopFormat.Add(new ShopFormat { IsDel = false, Color = model.Color, Name = child.Name, NameEn = child.NameEn, ParentCode = psf.Code, IconFile = child.IconFile, AddTime = DateTime.Now, UpdateTime = DateTime.Now, Code = Guid.NewGuid().ToString(), MallCode = model.MallCode }); } changecount += await dbContext.SaveChangesAsync(); if (changecount > 0) { _Result.Code = "200"; _Result.Msg = "添加成功"; _Result.Data = ""; } else { _Result.Code = "2"; _Result.Msg = "添加失败"; _Result.Data = ""; } return(Json(_Result)); }
public async Task <IActionResult> Del(Input_Del model, [FromServices] ContextString dbContext) { QianMuResult _Result = new QianMuResult(); //检测用户登录情况 UserOnLine user = Method.GetLoginUserName(dbContext, this.HttpContext); if (user == null || string.IsNullOrEmpty(user.UserCode)) { _Result.Code = "401"; _Result.Msg = "请登陆后再进行操作"; _Result.Data = ""; return(Json(_Result)); } Stream stream = HttpContext.Request.Body; byte[] buffer = new byte[HttpContext.Request.ContentLength.Value]; stream.Read(buffer, 0, buffer.Length); string inputStr = Encoding.UTF8.GetString(buffer); model = (Input_Del)Newtonsoft.Json.JsonConvert.DeserializeObject(inputStr, model.GetType()); //if (string.IsNullOrEmpty(inputStr)) //{ // _Result.Code = "510"; // _Result.Msg = "请输入一个用户ID"; // _Result.Data = ""; // return Json(_Result); //} //JsonModel model = new JsonModel(); //model = (JsonModel)Newtonsoft.Json.JsonConvert.DeserializeObject(inputStr, model.GetType()); //ID = model.ID; if (model.IDS == null || model.IDS.Count() <= 0) { _Result.Code = "510"; _Result.Msg = "请输入一个用户ID"; _Result.Data = ""; return(Json(_Result)); } var _username = string.Empty; foreach (var ID in model.IDS) { Account _User = Method.GetUserByID(dbContext, (int)ID).Result; if (_User == null) { _Result.Code = "1"; _Result.Msg = "系统中无此用户"; _Result.Data = ""; return(Json(_Result)); } if (!_User.Activity) { _Result.Code = "2"; _Result.Msg = "此用户已废弃"; _Result.Data = ""; return(Json(_Result)); } if (_User.ID == 1) { _Result.Code = "510"; _Result.Msg = "超管不可删除"; _Result.Data = ""; return(Json(_Result)); } //删除用户 _User.Activity = false; _username += _User.AccountName + ","; dbContext.Account.Update(_User); dbContext.UserRoles.RemoveRange(dbContext.UserRoles.Where(i => i.UserCode == _User.Code)); } if (await dbContext.SaveChangesAsync() > 0) { _Result.Code = "200"; _Result.Msg = "删除用户成功"; _Result.Data = ""; } else { _Result.Code = "200"; _Result.Msg = "删除用户成功"; _Result.Data = ""; } var ip = Method.GetUserIp(this.HttpContext); dbContext.SysLog.Add(new SysLog { AccountName = user.UserName, ModuleName = "用户模块", LogMsg = user.UserName + "删除了账户为:" + _username.TrimEnd(',') + "的用户", AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Type = "删除", IP = ip, MallCode = user.MallCode, SystemModule = "Mall" }); dbContext.SaveChanges(); return(Json(_Result)); }
public async Task <IActionResult> Edit(Input_EditRole model, [FromServices] ContextString dbContext) { QianMuResult _Result = new QianMuResult(); Stream stream = HttpContext.Request.Body; byte[] buffer = new byte[HttpContext.Request.ContentLength.Value]; stream.Read(buffer, 0, buffer.Length); string inputStr = Encoding.UTF8.GetString(buffer); model = (Input_EditRole)Newtonsoft.Json.JsonConvert.DeserializeObject(inputStr, model.GetType()); //检测用户登录情况 UserOnLine userOnLine = Method.GetLoginUserName(dbContext, this.HttpContext); if (userOnLine == null || string.IsNullOrEmpty(userOnLine.UserCode)) { _Result.Code = "401"; _Result.Msg = "请登陆后再进行操作"; _Result.Data = ""; return(Json(_Result)); } if (string.IsNullOrEmpty(model.Name)) { _Result.Code = "510"; _Result.Msg = "角色名称不可为空"; _Result.Data = ""; return(Json(_Result)); } Regex regExp = new Regex("[ \\[ \\] \\^ \\-_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]"); if (regExp.IsMatch(model.Name)) { _Result.Code = "510"; _Result.Msg = "角色名称不能有特殊字符"; _Result.Data = ""; return(Json(_Result)); } var count = await dbContext.Roles.Where(i => i.Code == model.Code).AsNoTracking().CountAsync(); if (count <= 0) { _Result.Code = "510"; _Result.Msg = "角色不存在"; _Result.Data = ""; return(Json(_Result)); } //更新角色 if (regExp.IsMatch(model.Intro)) { _Result.Code = "510"; _Result.Msg = "角色描述不能有特殊字符"; _Result.Data = ""; return(Json(_Result)); } var role = await dbContext.Roles.Where(i => i.Code == model.Code).FirstOrDefaultAsync(); if (role.Name == "超级管理员") { _Result.Code = "510"; _Result.Msg = "超级管理员不可修改"; _Result.Data = ""; return(Json(_Result)); } role.Description = model.Name; role.Name = model.Name; role.Intro = model.Intro; dbContext.Roles.UpdateRange(role); dbContext.RolePermissions.RemoveRange(dbContext.RolePermissions.Where(i => i.RoleCode == model.Code)); //如果有设备控制权限 则加上设备查看权限 var _DeviceControl = await dbContext.Permission.Where(i => i.Description == "DeviceControl").FirstOrDefaultAsync(); var _DevListMgr = await dbContext.Permission.Where(i => i.Description == "DevListMgr").FirstOrDefaultAsync(); if (model.PermissionCode.Contains(_DeviceControl.Code)) { if (!model.PermissionCode.Contains(_DevListMgr.Code)) { model.PermissionCode.Add(_DevListMgr.Code); } } List <RolePermissions> list = new List <RolePermissions>(); foreach (var p in model.PermissionCode) { var percount = await dbContext.Permission.Where(i => i.Code == p).CountAsync(); if (percount <= 0) { _Result.Code = "510"; _Result.Msg = "无效的权限编码:" + p; _Result.Data = ""; return(Json(_Result)); } var per = await dbContext.Permission.Where(i => i.Code == p).FirstOrDefaultAsync(); var parents = await dbContext.Permission.Where(i => i.Code == per.ParentCode).FirstOrDefaultAsync(); list.Add(new RolePermissions { AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), PermissionCode = p, RoleCode = role.Code }); if (list.Where(i => i.PermissionCode == parents.Code).Count() <= 0) { list.Add(new RolePermissions { AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), PermissionCode = parents.Code, RoleCode = role.Code }); } // dbContext.RolePermissions.Add(new RolePermissions { AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), PermissionCode = p, RoleCode = role.Code }); } list = list.Distinct().ToList(); dbContext.RolePermissions.AddRange(list); if (await dbContext.SaveChangesAsync() > 0) { _Result.Code = "200"; _Result.Msg = "修改成功"; _Result.Data = ""; var ip = Method.GetUserIp(this.HttpContext); dbContext.SysLog.Add(new SysLog { AccountName = userOnLine.UserName, ModuleName = "用户模块", LogMsg = userOnLine.UserName + "修改角色:" + model.Name, AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Type = "修改", IP = ip, MallCode = userOnLine.MallCode, SystemModule = "Mall" }); dbContext.SaveChanges(); } else { _Result.Code = "1"; _Result.Msg = "修改失败"; _Result.Data = ""; } return(Json(_Result)); }
public async Task <IActionResult> ShopFormatEdit(Input_ShopFormatEdit modele, [FromServices] ContextString dbContext) { QianMuResult _Result = new QianMuResult(); Stream stream = HttpContext.Request.Body; byte[] buffer = new byte[HttpContext.Request.ContentLength.Value]; stream.Read(buffer, 0, buffer.Length); string inputStr = Encoding.UTF8.GetString(buffer); modele = (Input_ShopFormatEdit)Newtonsoft.Json.JsonConvert.DeserializeObject(inputStr, modele.GetType()); var model = modele.Parameter; if (string.IsNullOrEmpty(model.UserName)) { //检测用户登录情况 var uol = Method.GetLoginUserName(dbContext, this.HttpContext); if (string.IsNullOrEmpty(uol.UserName)) { _Result.Code = "401"; _Result.Msg = "请登陆后再进行操作"; _Result.Data = ""; return(Json(_Result)); } else { model.UserName = uol.UserName; } } if (string.IsNullOrEmpty(model.Code) || string.IsNullOrEmpty(model.Name)) { _Result.Code = "510"; _Result.Msg = "输入信息不完整"; _Result.Data = ""; return(Json(_Result)); } var sf = await dbContext.ShopFormat.Where(i => i.Code == model.Code && !i.IsDel).FirstOrDefaultAsync(); if (sf == null) { _Result.Code = "510"; _Result.Msg = "业态不存在"; _Result.Data = ""; return(Json(_Result)); } sf.Name = model.Name; sf.NameEn = model.NameEn; sf.Color = model.Color; // sf.AddTime = DateTime.Now; sf.UpdateTime = DateTime.Now; var oldicon = String.Empty; if (model.IconFile != null) { if (sf.IconFile != model.IconFile) { oldicon = sf.IconFile; } sf.IconFile = model.IconFile; } sf.UpdateTime = DateTime.Now; //判断子业态是否存在 var childsfm = await dbContext.ShopFormat.Where(i => i.ParentCode == sf.Code && !i.IsDel).ToListAsync(); foreach (var item in model.Child) { if (model.Child.Where(i => i.Name == item.Name).Count() > 1) { _Result.Code = "510"; _Result.Msg = "Erro:子业态重复"; _Result.Data = ""; return(Json(_Result)); } var childsf = childsfm.Where(i => i.Name == item.Name).FirstOrDefault(); //不存在 if (childsf == null) { if (item.IconFile == null) { item.IconFile = string.Empty; } //添加 dbContext.ShopFormat.Add(new ShopFormat { IsDel = false, MallCode = sf.MallCode, AddTime = DateTime.Now, Color = model.Color, Name = item.Name, NameEn = item.NameEn, ParentCode = model.Code, IconFile = item.IconFile, UpdateTime = DateTime.Now, Code = Guid.NewGuid().ToString() }); } else { //存在 更新 childsf.Color = model.Color; childsf.NameEn = item.NameEn; // childsf.AddTime = DateTime.Now; childsf.UpdateTime = DateTime.Now; } } //判断子业态是否被删除 foreach (var item in childsfm) { var childsf = model.Child.Where(i => i.Name == item.Name).FirstOrDefault(); //已被删除 if (childsf == null) { //判断 被移除的子业态是否被使用 var shopnum = dbContext.Shops.Where(i => i.ShopFormat == item.Code || i.SecFormat == item.Code).Count(); if (shopnum > 0) { _Result.Code = "510"; _Result.Msg = "Erro:" + item.Name + "业态正被使用中不可删除"; _Result.Data = ""; return(Json(_Result)); } item.IsDel = true; item.UpdateTime = DateTime.Now; dbContext.ShopFormat.Update(item); } } dbContext.ShopFormat.Update(sf); if (await dbContext.SaveChangesAsync() > 0) { //更换了图标 删除旧图标文件 if (!string.IsNullOrEmpty(oldicon)) { await FileHelper.DelFile(oldicon, dbContext); } _Result.Code = "200"; _Result.Msg = "修改成功"; _Result.Data = ""; var ip = Method.GetUserIp(this.HttpContext); dbContext.SysLog.Add(new SysLog { AccountName = model.UserName, ModuleName = "业态管理", LogMsg = model.UserName + "编辑业态", AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Type = "修改", IP = ip }); dbContext.SaveChanges(); } else { _Result.Code = "2"; _Result.Msg = "修改失败"; _Result.Data = ""; } return(Json(_Result)); }
public async Task <IActionResult> SetWarningUser(Input_SetWE model, [FromServices] ContextString dbContext) { QianMuResult _Result = new QianMuResult(); //检测用户登录情况 UserOnLine userOnLine = Method.GetLoginUserName(dbContext, this.HttpContext); if (userOnLine == null || string.IsNullOrEmpty(userOnLine.UserName)) { _Result.Code = "401"; _Result.Msg = "请登陆后再进行操作"; _Result.Data = ""; return(Json(_Result)); } Stream stream = HttpContext.Request.Body; byte[] buffer = new byte[HttpContext.Request.ContentLength.Value]; stream.Read(buffer, 0, buffer.Length); string inputStr = Encoding.UTF8.GetString(buffer); model = (Input_SetWE)Newtonsoft.Json.JsonConvert.DeserializeObject(inputStr, model.GetType()); if (model.ID == null) { _Result.Code = "510"; _Result.Msg = "请输入一个ID"; _Result.Data = ""; return(Json(_Result)); } var count = await dbContext.Account.Where(i => i.Activity == true && i.ID == model.ID).CountAsync(); if (count <= 0) { _Result.Code = "510"; _Result.Msg = "无效的用户ID"; _Result.Data = ""; return(Json(_Result)); } var user = await dbContext.Account.Where(i => i.Activity == true && i.ID == model.ID).FirstOrDefaultAsync(); if (model.Status != 0) { dbContext.UserWarningEmail.RemoveRange(dbContext.UserWarningEmail.Where(i => i.UserCode == user.Code)); dbContext.UserWarningEmail.Add(new UserWarningEmail { AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), UserCode = user.Code, UpdateTime = DateTime.Now }); } else { dbContext.UserWarningEmail.RemoveRange(dbContext.UserWarningEmail.Where(i => i.UserCode == user.Code)); } if (await dbContext.SaveChangesAsync() > 0) { _Result.Code = "200"; _Result.Msg = "设置成功"; _Result.Data = ""; var ip = Method.GetUserIp(this.HttpContext); dbContext.SysLog.Add(new SysLog { AccountName = userOnLine.UserName, ModuleName = "用户模块", LogMsg = userOnLine.UserName + "设置用户:" + user.AccountName + "为报警用户", AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Type = "修改", IP = ip, MallCode = userOnLine.MallCode, SystemModule = "Mall" }); dbContext.SaveChanges(); } else { _Result.Code = "2"; _Result.Msg = "设置失败"; _Result.Data = ""; } return(Json(_Result)); }
public async Task <IActionResult> ChangePassWord(Input_ChangePassWord model, [FromServices] ContextString dbContext) { QianMuResult _Result = new QianMuResult(); //检测用户登录情况 UserOnLine userOnLine = Method.GetLoginUserName(dbContext, this.HttpContext); if (userOnLine == null || string.IsNullOrEmpty(userOnLine.UserCode)) { _Result.Code = "401"; _Result.Msg = "请登陆后再进行操作"; _Result.Data = ""; return(Json(_Result)); } Stream stream = HttpContext.Request.Body; byte[] buffer = new byte[HttpContext.Request.ContentLength.Value]; stream.Read(buffer, 0, buffer.Length); string inputStr = Encoding.UTF8.GetString(buffer); model = (Input_ChangePassWord)Newtonsoft.Json.JsonConvert.DeserializeObject(inputStr, model.GetType()); //判断输入条件 if (string.IsNullOrEmpty(model.AccountName)) { _Result.Code = "510"; _Result.Msg = "请输入一个用户名"; _Result.Data = ""; return(Json(_Result)); } if (model.Password.Length < 6) { _Result.Code = "510"; _Result.Msg = "用户密码应为6-20位字符"; _Result.Data = ""; return(Json(_Result)); } if (model.Password != model.ConfirmPassword) { _Result.Code = "510"; _Result.Msg = "二次输入新密码不一致"; _Result.Data = ""; return(Json(_Result)); } var user = await dbContext.Account.Where(i => i.Activity && i.AccountName == model.AccountName).FirstOrDefaultAsync(); if (user != null) { user.PassWord = Method.StringToPBKDF2Hash(model.Password); dbContext.Account.Update(user); if (await dbContext.SaveChangesAsync() > 0) { _Result.Code = "200"; _Result.Msg = "修改密码成功"; _Result.Data = ""; var ip = Method.GetUserIp(this.HttpContext); dbContext.SysLog.Add(new SysLog { AccountName = userOnLine.UserName, ModuleName = "用户模块", LogMsg = userOnLine.UserName + "修改了用户:" + model.AccountName + "的登录密码", AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Type = "修改", IP = ip, MallCode = userOnLine.MallCode, SystemModule = "Mall" }); dbContext.SaveChanges(); } else { _Result.Code = "2"; _Result.Msg = "修改密码失败"; _Result.Data = ""; } } else { _Result.Code = "510"; _Result.Msg = "无效的用户名"; _Result.Data = ""; } return(Json(_Result)); }
public async Task <IActionResult> EditInfo(Input_UserEditInfo model, [FromServices] ContextString dbContext) { QianMuResult _Result = new QianMuResult(); //检测用户登录情况 UserOnLine user = Method.GetLoginUserName(dbContext, this.HttpContext); if (user == null || string.IsNullOrEmpty(user.UserName)) { _Result.Code = "401"; _Result.Msg = "请登陆后再进行操作"; _Result.Data = ""; return(Json(_Result)); } Stream stream = HttpContext.Request.Body; byte[] buffer = new byte[HttpContext.Request.ContentLength.Value]; stream.Read(buffer, 0, buffer.Length); string inputStr = Encoding.UTF8.GetString(buffer); model = (Input_UserEditInfo)Newtonsoft.Json.JsonConvert.DeserializeObject(inputStr, model.GetType()); if (string.IsNullOrEmpty(model.Phone) || string.IsNullOrEmpty(model.Email) || string.IsNullOrEmpty(model.NickName) ) { _Result.Code = "510"; _Result.Msg = "输入信息不正确"; _Result.Data = ""; return(Json(_Result)); } var _user = await dbContext.Account.Where(i => i.Code == user.UserCode).FirstOrDefaultAsync(); if (_user == null) { _Result.Code = "510"; _Result.Msg = "无效的用户"; _Result.Data = ""; return(Json(_Result)); } var phonenum = await dbContext.Account.Where(i => i.Code != user.UserCode && i.Phone == model.Phone && i.Activity).CountAsync(); if (phonenum > 0) { _Result.Code = "510"; _Result.Msg = "手机号已被使用"; _Result.Data = ""; return(Json(_Result)); } var emailnum = await dbContext.Account.Where(i => i.Code != user.UserCode && i.Email == model.Email && i.Activity).CountAsync(); if (emailnum > 0) { _Result.Code = "510"; _Result.Msg = "邮箱已被使用"; _Result.Data = ""; return(Json(_Result)); } _user.NickName = model.NickName; _user.Phone = model.Phone; _user.Email = model.Email; dbContext.Account.Update(_user); if (await dbContext.SaveChangesAsync() > 0) { _Result.Code = "200"; _Result.Msg = "修改成功"; _Result.Data = ""; var ip = Method.GetUserIp(this.HttpContext); dbContext.SysLog.Add(new SysLog { AccountName = user.UserName, ModuleName = "用户模块", LogMsg = user.UserName + "修改了账户为:" + _user.AccountName + "的用户信息", AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Type = "修改", IP = ip, MallCode = user.MallCode, SystemModule = "Mall" }); dbContext.SaveChanges(); } else { _Result.Code = "2"; _Result.Msg = "修改失败"; _Result.Data = ""; } return(Json(_Result)); }
/// <summary> /// 下载一个远程文件(http方式) /// </summary> /// <param name="apppath">文件路径</param> /// <param name="filename">文件名称</param> /// <param name="dbContext">数据库连接字段</param> /// <returns></returns> public static async Task <long> DownloadFile(string apppath, ContextString dbContext) { QMLog qm = new QMLog(); long _Result = -1; try { var GUID = Guid.NewGuid(); string filename = apppath.Split('/').Last(); var client = new HttpClient { BaseAddress = new Uri(apppath) }; var response = await client.GetAsync(""); var stream = await response.Content.ReadAsStreamAsync(); // var head = response.Headers; //声明字符数据,将获取到的流信息读到字符数组中 byte[] byteArray = new byte[stream.Length]; int readCount = 0; // 已经成功读取的字节的个数 while (readCount < stream.Length) { readCount += stream.Read(byteArray, readCount, (int)stream.Length - readCount); } AssetFile assetfile = new AssetFile(); //文件名 long filesize = stream.Length; //文件类型 var ext = filename.Split('.').Last(); FileTypeJudgment ftj = new FileTypeJudgment() { TypeTarget = new TypeImg() }; assetfile.FileType = "未知"; if (ftj.Judge(ext)) { assetfile.FileType = "图片"; } ftj.TypeTarget = new TypeVideo(); if (ftj.Judge(ext)) { assetfile.FileType = "视频"; } ftj.TypeTarget = new TypeApp(); if (ftj.Judge(ext)) { assetfile.FileType = "应用"; } //文件保存路径 var _FolderPath = @"\Files" + @"\" + GUID; string path = Method._hostingEnvironment.WebRootPath + _FolderPath; if (Directory.Exists(path) == false)//如果不存在就创建file文件夹 { Directory.CreateDirectory(path); } //文件全路径 var filepath = Method._hostingEnvironment.WebRootPath + _FolderPath + @"\" + filename; using (FileStream fs = System.IO.File.Create(filepath)) { fs.Write(byteArray, 0, (int)stream.Length); fs.Flush(); fs.Dispose(); } //添加文件到数据库 assetfile.AddTime = DateTime.Now; assetfile.FileExtName = ext; assetfile.Code = GUID.ToString(); assetfile.FileHash = ""; assetfile.FileName = filename.Split('.').First(); assetfile.FilePath = @"\Files" + @"\" + GUID.ToString() + @"\" + filename; assetfile.FileSize = filesize; dbContext.AssetFiles.Add(assetfile); if (dbContext.SaveChanges() > 0) { _Result = assetfile.ID; } else { qm.WriteLogToFile(filename, "添加文件到数据库失败"); } } catch (Exception e) { qm.WriteLogToFile("", e.ToString()); } return(_Result); }
public async Task <IActionResult> LoginBySMS(Input_LoginViewModelBySMS model, [FromServices] ContextString dbContext, string returnUrl = null) { QianMuResult _Result = new QianMuResult(); Stream stream = HttpContext.Request.Body; byte[] buffer = new byte[HttpContext.Request.ContentLength.Value]; stream.Read(buffer, 0, buffer.Length); string inputStr = Encoding.UTF8.GetString(buffer); model = (Input_LoginViewModelBySMS)Newtonsoft.Json.JsonConvert.DeserializeObject(inputStr, model.GetType()); if (string.IsNullOrEmpty(model.Phone) || string.IsNullOrEmpty(model.vercode)) { _Result.Code = "510"; _Result.Msg = "请输入手机号和验证码"; _Result.Data = ""; return(Json(_Result)); } //判断系统中是否存在用户 var accnum = dbContext.Account.Where(i => i.Phone == model.Phone && i.Activity == true).Count(); if (accnum <= 0) { _Result.Code = "1"; _Result.Msg = "用户不存在或不可用"; _Result.Data = ""; return(Json(_Result)); } var count = await dbContext.PhoneRecord.Where(i => i.Phone == model.Phone && i.VerCode == model.vercode).CountAsync(); if (count > 0) { var pr = await dbContext.PhoneRecord.Where(i => i.Phone == model.Phone).OrderByDescending(o => o.AddTime).FirstOrDefaultAsync(); if (pr.VerCode == model.vercode && pr.UpdateTime >= DateTime.Now.AddMinutes(-5)) { pr.Status = true; pr.UpdateTime = DateTime.Now; dbContext.PhoneRecord.Update(pr); if (await dbContext.SaveChangesAsync() > 0) { Account _User = dbContext.Account.Where(i => i.Phone == model.Phone).FirstOrDefault(); string _LoginSession = Guid.NewGuid().ToString(); //会话唯一标记 //保存会话状态 var _InvalidTime = DateTime.Now.AddDays(1); if (model.RememberMe == "on") { _InvalidTime = DateTime.Now.AddDays(7); } //更新用户信息 var ip = Method.GetUserIp(this.HttpContext); string _LastLoginIP = ip; DateTime _LastLoginTime = DateTime.Now; _User.InvalidTime = _InvalidTime; _User.LoginSession = _LoginSession; _User.LastLoginTime = _LastLoginTime; _User.LastLoginIP = _LastLoginIP; dbContext.Account.Update(_User); dbContext.SaveChanges(); //日志记录 try { dbContext.SysLog.Add(new SysLog { AccountName = _User.AccountName, ModuleName = "用户模块", LogMsg = _User.AccountName + "登陆了系统", AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Type = "登录", IP = ip, SystemModule = _User.SystemModule, MallCode = _User.MallCode }); dbContext.SaveChanges(); } catch (Exception e) { QMLog qm = new QMLog(); qm.WriteLogToFile("", e.ToString()); } _Result.Code = "200"; _Result.Msg = "登陆成功"; _Result.Data = _LoginSession; } else { _Result.Code = "2"; _Result.Msg = "用户名或密码不正确"; _Result.Data = ""; } } else { _Result.Code = "2"; _Result.Msg = "验证码不正确"; _Result.Data = ""; } } return(Json(_Result)); }
public async Task <IActionResult> Del(Input_DelRole model, [FromServices] ContextString dbContext) { QianMuResult _Result = new QianMuResult(); Stream stream = HttpContext.Request.Body; byte[] buffer = new byte[HttpContext.Request.ContentLength.Value]; stream.Read(buffer, 0, buffer.Length); string inputStr = Encoding.UTF8.GetString(buffer); model = (Input_DelRole)Newtonsoft.Json.JsonConvert.DeserializeObject(inputStr, model.GetType()); //检测用户登录情况 UserOnLine userOnLine = Method.GetLoginUserName(dbContext, this.HttpContext); if (userOnLine == null || string.IsNullOrEmpty(userOnLine.UserCode)) { _Result.Code = "401"; _Result.Msg = "请登陆后再进行操作"; _Result.Data = ""; return(Json(_Result)); } var _names = String.Empty; foreach (var c in model.Code) { var rcount = await dbContext.Roles.Where(i => i.Code == c).CountAsync(); if (rcount <= 0) { _Result.Code = "510"; _Result.Msg = "无效的角色编码:" + c; _Result.Data = ""; return(Json(_Result)); } var role = await dbContext.Roles.Where(i => i.Code == c).FirstOrDefaultAsync(); var urcount = await dbContext.UserRoles.Where(i => i.RoleCode == role.Code).CountAsync(); if (urcount > 0) { _Result.Code = "510"; _Result.Msg = "角色:" + role.Name + "正在被使用不可删除"; _Result.Data = ""; return(Json(_Result)); } if (role.Name == "超级管理员") { _Result.Code = "510"; _Result.Msg = "超级管理员不可删除"; _Result.Data = ""; return(Json(_Result)); } dbContext.Roles.Remove(role); dbContext.RolePermissions.RemoveRange(dbContext.RolePermissions.Where(i => i.RoleCode == role.Code)); _names += role.Name + ","; } if (await dbContext.SaveChangesAsync() > 0) { _Result.Code = "200"; _Result.Msg = "删除成功"; _Result.Data = ""; var ip = Method.GetUserIp(this.HttpContext); dbContext.SysLog.Add(new SysLog { AccountName = userOnLine.UserName, ModuleName = "用户模块", LogMsg = userOnLine.UserName + "删除角色:" + _names.TrimEnd(','), AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Type = "删除", IP = ip, MallCode = userOnLine.MallCode, SystemModule = "Mall" }); dbContext.SaveChanges(); } else { _Result.Code = "1"; _Result.Msg = "删除失败"; _Result.Data = ""; } return(Json(_Result)); }
public async Task <IActionResult> Register(RegisterUser model, [FromServices] ContextString dbContext) { QianMuResult _Result = new QianMuResult(); //检测用户登录情况 UserOnLine user = Method.GetLoginUserName(dbContext, this.HttpContext); if (user == null || string.IsNullOrEmpty(user.UserCode)) { _Result.Code = "401"; _Result.Msg = "请登陆后再进行操作"; _Result.Data = ""; return(Json(_Result)); } Stream stream = HttpContext.Request.Body; byte[] buffer = new byte[HttpContext.Request.ContentLength.Value]; stream.Read(buffer, 0, buffer.Length); string inputStr = Encoding.UTF8.GetString(buffer); model = (RegisterUser)Newtonsoft.Json.JsonConvert.DeserializeObject(inputStr, model.GetType()); if (string.IsNullOrEmpty(model.RoleID)) { _Result.Code = "510"; _Result.Msg = "Erro:角色ID不可为空"; _Result.Data = ""; return(Json(_Result)); } if (!Method.IsNumeric(model.RoleID)) { _Result.Code = "510"; _Result.Msg = "Erro:包含非法的角色ID" + model.RoleID; _Result.Data = ""; return(Json(_Result)); } var _RoleID = Convert.ToInt32(model.RoleID); //判断ID是否都为有效角色 var q = await dbContext.Roles.Where(i => i.ID == _RoleID).AsNoTracking().CountAsync(); if (q <= 0) { _Result.Code = "510"; _Result.Msg = "Erro:没有ID为:" + _RoleID + "的角色"; _Result.Data = ""; return(Json(_Result)); } var _AvatarSrc = @"\images\DefaultAvatar.png"; if (string.IsNullOrEmpty(model.AccountName) || string.IsNullOrEmpty(model.Password) || string.IsNullOrEmpty(model.Phone) || string.IsNullOrEmpty(model.Email) || model.Password != model.ConfirmPassword) { _Result.Code = "510"; _Result.Msg = "输入信息不正确"; _Result.Data = ""; return(Json(_Result)); } if (model.Password.Length < 6) { _Result.Code = "510"; _Result.Msg = "密码长度不因少于6位"; _Result.Data = ""; return(Json(_Result)); } //判断系统中是否存在用户 if (Method.FindAllByName(dbContext, model.AccountName)) { _Result.Code = "1"; _Result.Msg = "用户已存在"; _Result.Data = ""; return(Json(_Result)); } var phonenum = await dbContext.Account.Where(i => i.Phone == model.Phone && i.Activity).CountAsync(); if (phonenum > 0) { _Result.Code = "1"; _Result.Msg = "手机号码已被使用"; _Result.Data = ""; return(Json(_Result)); } var emailnum = await dbContext.Account.Where(i => i.Email == model.Email && i.Activity).CountAsync(); if (emailnum > 0) { _Result.Code = "1"; _Result.Msg = "邮箱已被使用"; _Result.Data = ""; return(Json(_Result)); } //创建用户 var _User = new Account() { AccountName = model.AccountName, PassWord = Method.StringToPBKDF2Hash(model.Password), NickName = model.NickName, Phone = model.Phone, Email = model.Email, AvatarSrc = _AvatarSrc, Activity = true, AddTime = DateTime.Now, UpdateTime = DateTime.Now, Code = Guid.NewGuid().ToString(), MallCode = user.MallCode, SystemModule = "Mall" }; int _AccountID = Method.CreateAccount(dbContext, _User).Result; var role = await dbContext.Roles.Where(i => i.ID == _RoleID).FirstOrDefaultAsync(); //添加账户 角色关系 if (_AccountID > 0) { try { dbContext.UserRoles.Add(new UserRoles { UserCode = _User.Code, RoleCode = role.Code }); await dbContext.SaveChangesAsync(); _Result.Code = "200"; _Result.Msg = "创建用户成功"; _Result.Data = ""; var ip = Method.GetUserIp(this.HttpContext); dbContext.SysLog.Add(new SysLog { AccountName = user.UserName, ModuleName = "用户模块", LogMsg = user.UserName + "创建了用户名为:" + model.AccountName + "的用户,访问信息:" + inputStr, AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Type = "创建", IP = ip, MallCode = user.MallCode, SystemModule = "Mall" }); dbContext.SaveChanges(); } catch (Exception e) { _Result.Code = "500"; _Result.Msg = "Erro:关联用户-角色关系失败 " + e.ToString(); _Result.Data = ""; } } else { _Result.Code = "2"; _Result.Msg = "创建用户失败"; _Result.Data = ""; } return(Json(_Result)); }
public async Task <IActionResult> UploadScreenshot([FromServices] ContextString dbContext) { QianMuResult _Result = new QianMuResult(); QMLog qm = new QMLog(); try { System.Text.ASCIIEncoding ASCII = new System.Text.ASCIIEncoding(); int _LenDec = 6; //长度说明占位长度 int _JsonLen = 0; //长度 int _HeadLen = 0; //头部长度 JsonModel jmodel = new JsonModel(); //声明字符数据,将获取到的流信息读到字符数组中 byte[] byteArray = new byte[HttpContext.Request.ContentLength.Value]; using (Stream stream = HttpContext.Request.Body) { int readCount = 0; // 已经成功读取的字节的个数 while (readCount < HttpContext.Request.ContentLength.Value) { readCount += stream.Read(byteArray, readCount, (int)HttpContext.Request.ContentLength.Value - readCount); } } //读取Json长度 string jsonLength = ASCII.GetString(byteArray, 0, _LenDec); _JsonLen = int.Parse(jsonLength.TrimStart('0')); _HeadLen = _LenDec + _JsonLen; //读取json信息 string inputStr = ASCII.GetString(byteArray, _LenDec, _JsonLen); jmodel = (JsonModel)Newtonsoft.Json.JsonConvert.DeserializeObject(inputStr, jmodel.GetType()); string code = jmodel.Code; string filename = jmodel.FileName; //检测用户输入格式 if (String.IsNullOrEmpty(code)) { _Result.Code = "510"; _Result.Msg = "编码不可为空"; _Result.Data = ""; return(Json(_Result)); } //IPAddress ipaddress; //if (!IPAddress.TryParse(ip, out ipaddress)) //{ // _Result.Code = "510"; // _Result.Msg = "IP不合法"; // _Result.Data = ""; // return Json(_Result); //} int count = dbContext.Device.Where(i => i.Code == code).Count(); if (count <= 0) { _Result.Code = "1"; _Result.Msg = "未找到编码为:" + code + "的设备"; _Result.Data = ""; return(Json(_Result)); } AssetFile assetfile = new AssetFile(); //文件名 var GUID = Guid.NewGuid(); long filesize = HttpContext.Request.ContentLength.Value - _HeadLen; //文件类型 var ext = filename.Split('.').Last(); FileTypeJudgment ftj = new FileTypeJudgment() { TypeTarget = new TypeImg() }; //if (Method.ImgType.Contains(ext)) //{ // //图片文件 // assetfile.FileType = "图片"; //} if (ftj.Judge(ext)) { //图片文件 assetfile.FileType = "图片"; } else { _Result.Code = "510"; _Result.Msg = "不是合法的文件类型"; _Result.Data = ""; return(Json(_Result)); } //文件保存路径 var _FolderPath = @"\Files" + @"\" + GUID; string path = Method._hostingEnvironment.WebRootPath + _FolderPath; if (Directory.Exists(path) == false)//如果不存在就创建file文件夹 { Directory.CreateDirectory(path); } //文件全路径 var filepath = Method._hostingEnvironment.WebRootPath + _FolderPath + @"\" + filename; using (FileStream fs = System.IO.File.Create(filepath)) { fs.Write(byteArray, _HeadLen, byteArray.Length - _HeadLen); fs.Flush(); fs.Dispose(); } #region 文件上传到OSS服务器并删除本地存档 var prjinfo = dbContext.ProjectInfo.FirstOrDefault(); FileStream fileFacestream = new FileStream(filepath, FileMode.Open); byte[] btFace = new byte[fileFacestream.Length]; //调用read读取方法 fileFacestream.Read(btFace, 0, btFace.Length); fileFacestream.Close(); string faceImg = Convert.ToBase64String(btFace); qm.WriteLogToFile("截图", "准备上传"); var filedata = new { FileName = filename, FileStr = faceImg, MallCode = prjinfo.CusID }; var param = JsonHelper.SerializeJSON(filedata); var _r = Method.PostMoths(Method.FileServer + "/FileManage/UpLoadScreenshotFiles", param); qm.WriteLogToFile("截图", _r); QianMuResult qianMuResult = new QianMuResult(); qianMuResult = (QianMuResult)Newtonsoft.Json.JsonConvert.DeserializeObject(_r, qianMuResult.GetType()); if (qianMuResult.Code != "200") { _Result.Code = "2"; _Result.Msg = "上传文件到OSS失败"; _Result.Data = ""; return(Json(_Result)); } // MemoryStream majorfs = new MemoryStream(); // majorfs.Write(byteArray, _HeadLen, byteArray.Length - _HeadLen); //// file.CopyTo(majorfs); //var majorfs = FileHelper.FileToStream(filepath); //AliyunOSS aliyunOSS = new AliyunOSS(); //if (!aliyunOSS.UploadFileToOSS("Files" + "/" + assetfile.Code + "/" + assetfile.FileName + "." + assetfile.FileExtName, majorfs)) //{ // _Result.Code = "2"; // _Result.Msg = "上传文件到OSS失败"; // _Result.Data = ""; // return Json(_Result); //} #endregion //添加文件到数据库 assetfile.AddTime = DateTime.Now; assetfile.FileExtName = ext; //assetfile.Code = GUID.ToString(); assetfile.Code = qianMuResult.Data.ToString(); assetfile.FileHash = ""; assetfile.FileName = filename.Split('.').First(); //assetfile.FilePath = @"\Files" + @"\" + GUID.ToString() + @"\" + filename; assetfile.FilePath = @"\Files" + @"\" + qianMuResult.Data.ToString() + @"\" + filename; assetfile.FileSize = filesize; dbContext.AssetFiles.Add(assetfile); //更新截图信息到设备 var dev = await dbContext.Device.Where(i => i.Code == code).FirstOrDefaultAsync(); dev.ScreenshotSrc = assetfile.FilePath; if (dbContext.SaveChanges() > 0) { Output_FileInfo of = new Output_FileInfo(); of.FilePath = assetfile.FilePath; // of.ID = assetfile.ID; of.Code = assetfile.Code; _Result.Code = "200"; _Result.Msg = "文件已被上传完成,并加入数据库"; _Result.Data = of; } else { _Result.Code = "2"; _Result.Msg = "添加文件到数据库失败"; _Result.Data = ""; } } catch (Exception e) { qm.WriteLogToFile("", e.ToString()); _Result.Code = "500"; _Result.Msg = "程序运行错误"; _Result.Data = ""; } return(Json(_Result)); }
public async Task <IActionResult> FloorEdit(Input_FloorEdit model, [FromServices] ContextString dbContext) { QianMuResult _Result = new QianMuResult(); Stream stream = HttpContext.Request.Body; byte[] buffer = new byte[HttpContext.Request.ContentLength.Value]; stream.Read(buffer, 0, buffer.Length); string inputStr = Encoding.UTF8.GetString(buffer); model = (Input_FloorEdit)Newtonsoft.Json.JsonConvert.DeserializeObject(inputStr, model.GetType()); if (string.IsNullOrEmpty(model.UserName)) { //检测用户登录情况 var uol = Method.GetLoginUserName(dbContext, this.HttpContext); if (string.IsNullOrEmpty(uol.UserName)) { _Result.Code = "401"; _Result.Msg = "请登陆后再进行操作"; _Result.Data = ""; return(Json(_Result)); } else { model.UserName = uol.UserName; } } //if (string.IsNullOrEmpty(model.Code) || string.IsNullOrEmpty(model.Code)) if (string.IsNullOrEmpty(model.Code)) { _Result.Code = "510"; _Result.Msg = "请输入一个楼层编码"; _Result.Data = ""; return(Json(_Result)); } if (string.IsNullOrEmpty(model.Name)) { _Result.Code = "510"; _Result.Msg = "楼层名称不可为空"; _Result.Data = ""; return(Json(_Result)); } var floor = await dbContext.Floor.Where(i => i.Code == model.Code && !i.IsDel).FirstOrDefaultAsync(); if (floor == null) { _Result.Code = "510"; _Result.Msg = "楼层不存在"; _Result.Data = ""; return(Json(_Result)); } if (!string.IsNullOrEmpty(model.Code)) { var file = await dbContext.AssetFiles.Where(i => i.Code == model.Code).FirstOrDefaultAsync(); if (file == null) { _Result.Code = "510"; _Result.Msg = "无效的文件ID"; _Result.Data = ""; return(Json(_Result)); } FileTypeJudgment fimg = new FileTypeJudgment() { TypeTarget = new TypeImg() }; if (!fimg.Judge(file.FileExtName)) { _Result.Code = "510"; _Result.Msg = "地图文件不是图片"; _Result.Data = ""; return(Json(_Result)); } floor.Map = model.Code; } floor.Name = model.Name; floor.UpdateTime = DateTime.Now; dbContext.Floor.Update(floor); if (await dbContext.SaveChangesAsync() > 0) { _Result.Code = "200"; _Result.Msg = "修改成功"; _Result.Data = ""; var ip = Method.GetUserIp(this.HttpContext); dbContext.SysLog.Add(new SysLog { AccountName = model.UserName, ModuleName = "楼层管理", LogMsg = model.UserName + "修改了名称:" + model.Name + "的楼层地图", AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Type = "修改", IP = ip }); dbContext.SaveChanges(); } else { _Result.Code = "1"; _Result.Msg = "修改失败"; _Result.Data = ""; } return(Json(_Result)); }
public IActionResult Login(LoginViewModel model, [FromServices] ContextString dbContext, string returnUrl = null) { QianMuResult _Result = new QianMuResult(); Stream stream = HttpContext.Request.Body; byte[] buffer = new byte[HttpContext.Request.ContentLength.Value]; stream.Read(buffer, 0, buffer.Length); string inputStr = Encoding.UTF8.GetString(buffer); model = (LoginViewModel)Newtonsoft.Json.JsonConvert.DeserializeObject(inputStr, model.GetType()); if (string.IsNullOrEmpty(model.AccountName) || string.IsNullOrEmpty(model.Password)) { _Result.Code = "510"; _Result.Msg = "请输入正确格式的用户名或密码"; _Result.Data = ""; return(Json(_Result)); } if (model.Password.Length < 6) { _Result.Code = "510"; _Result.Msg = "用户密码应为6-20位字符"; _Result.Data = ""; return(Json(_Result)); } Account _User = dbContext.Account.Where(i => i.Activity == true && (i.AccountName == model.AccountName || i.Phone == model.AccountName)).FirstOrDefault(); //判断系统中是否存在用户 if (_User == null) { _Result.Code = "503"; _Result.Msg = "用户不存在或不可用"; _Result.Data = ""; return(Json(_Result)); } //加密用户密码 string _PWD = Method.StringToPBKDF2Hash(model.Password); if (_User.PassWord == _PWD) { string _LoginSession = Guid.NewGuid().ToString(); //会话唯一标记 //保存会话状态 var _InvalidTime = DateTime.Now.AddDays(1); if (model.RememberMe == "on") { _InvalidTime = DateTime.Now.AddDays(7); } //更新用户信息 var ip = Method.GetUserIp(this.HttpContext); string _LastLoginIP = ip; DateTime _LastLoginTime = DateTime.Now; _User.InvalidTime = _InvalidTime; _User.LoginSession = _LoginSession; _User.LastLoginTime = _LastLoginTime; _User.LastLoginIP = _LastLoginIP; dbContext.Account.Update(_User); dbContext.SaveChanges(); //日志记录 try { dbContext.SysLog.Add(new SysLog { AccountName = _User.AccountName, ModuleName = "用户模块", LogMsg = _User.AccountName + "登陆了系统", AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Type = "登录", IP = ip, SystemModule = _User.SystemModule, MallCode = _User.MallCode }); dbContext.SaveChanges(); } catch (Exception e) { QMLog qm = new QMLog(); qm.WriteLogToFile("", e.ToString()); } _Result.Code = "200"; _Result.Msg = "登陆成功"; _Result.Data = _LoginSession; } else { _Result.Code = "2"; _Result.Msg = "用户名或密码不正确"; _Result.Data = ""; } return(Json(_Result)); }
public async Task <IActionResult> ShopFormatDel(Input_ShopFormatDel model, [FromServices] ContextString dbContext) { QianMuResult _Result = new QianMuResult(); Stream stream = HttpContext.Request.Body; byte[] buffer = new byte[HttpContext.Request.ContentLength.Value]; stream.Read(buffer, 0, buffer.Length); string inputStr = Encoding.UTF8.GetString(buffer); model = (Input_ShopFormatDel)Newtonsoft.Json.JsonConvert.DeserializeObject(inputStr, model.GetType()); if (string.IsNullOrEmpty(model.UserName)) { //检测用户登录情况 var uol = Method.GetLoginUserName(dbContext, this.HttpContext); if (string.IsNullOrEmpty(uol.UserName)) { _Result.Code = "401"; _Result.Msg = "请登陆后再进行操作"; _Result.Data = ""; return(Json(_Result)); } else { model.UserName = uol.UserName; } } //判断字符串是否合法 if (model.Codes.Count <= 0) { _Result.Code = "510"; _Result.Msg = "未能检测到codes"; _Result.Data = ""; return(Json(_Result)); } List <ShopFormat> _sfList = new List <ShopFormat>(); var snames = string.Empty; foreach (var item in model.Codes) { var sf = await dbContext.ShopFormat.Where(i => (i.Code == item || i.ParentCode == item) && !i.IsDel).ToListAsync(); foreach (var s in sf) { var shopnum = dbContext.Shops.Where(i => (i.ShopFormat == s.Code || i.SecFormat == s.Code) && !i.IsDel).Count(); if (shopnum > 0) { _Result.Code = "510"; _Result.Msg = "Erro:" + s.Name + "业态正被使用中不可删除"; _Result.Data = ""; return(Json(_Result)); } snames += s.Name + ","; } _sfList.AddRange(sf); } foreach (var item in _sfList) { item.IsDel = true; item.UpdateTime = DateTime.Now; //移除图标文件 await FileHelper.DelFile(item.IconFile, dbContext); } dbContext.ShopFormat.UpdateRange(_sfList); if (await dbContext.SaveChangesAsync() > 0) { _Result.Code = "200"; _Result.Msg = "删除成功"; _Result.Data = ""; var ip = Method.GetUserIp(this.HttpContext); dbContext.SysLog.Add(new SysLog { AccountName = model.UserName, ModuleName = "业态管理", LogMsg = model.UserName + "删除业态:" + snames.TrimEnd(','), AddTime = DateTime.Now, Code = Guid.NewGuid().ToString(), Type = "删除", IP = ip }); dbContext.SaveChanges(); } else { _Result.Code = "2"; _Result.Msg = "删除失败"; _Result.Data = ""; } return(Json(_Result)); }