/// <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); }
/// <summary> /// 保存角色权限 /// </summary> /// <param name="userPermissions"></param> /// <param name="roleId"></param> /// <returns></returns> public async Task <WebResponseContent> SavePermission(List <UserPermissions> userPermissions, int roleId) { WebResponseContent webResponse = new WebResponseContent(); string message = ""; try { UserInfo user = UserContext.Current.UserInfo; if (!(await GetAllChildrenAsync(user.Role_Id)).Exists(x => x.Id == roleId)) { return(webResponse.Error("没有权限修改此角色的权限信息")); } //当前用户的权限 List <Permissions> permissions = UserContext.Current.Permissions; List <int> originalMeunIds = new List <int>(); //被分配角色的权限 List <Sys_RoleAuth> roleAuths = await repository.FindAsync <Sys_RoleAuth>(x => x.Role_Id == roleId); List <Sys_RoleAuth> updateAuths = new List <Sys_RoleAuth>(); foreach (UserPermissions x in userPermissions) { Permissions per = permissions.Where(p => p.Menu_Id == x.Id).FirstOrDefault(); //不能分配超过当前用户的权限 if (per == null) { continue; } //per.UserAuthArr.Contains(a.Value)校验权限范围 string[] arr = x.Actions == null || x.Actions.Count == 0 ? new string[0] : x.Actions.Where(a => per.UserAuthArr.Contains(a.Value)) .Select(s => s.Value).ToArray(); //如果当前权限没有分配过,设置Auth_Id默认为0,表示新增的权限 var auth = roleAuths.Where(r => r.Menu_Id == x.Id).Select(s => new { s.Auth_Id, s.AuthValue, s.Menu_Id }).FirstOrDefault(); string newAuthValue = string.Join(",", arr); //权限没有发生变化则不处理 if (auth == null || auth.AuthValue != newAuthValue) { updateAuths.Add(new Sys_RoleAuth() { Role_Id = roleId, Menu_Id = x.Id, AuthValue = string.Join(",", arr), Auth_Id = auth == null ? 0 : auth.Auth_Id, ModifyDate = DateTime.Now, Modifier = user.UserTrueName, CreateDate = DateTime.Now, Creator = user.UserTrueName }); } else { originalMeunIds.Add(auth.Menu_Id); } } //更新权限 repository.UpdateRange(updateAuths.Where(x => x.Auth_Id > 0), x => new { x.Menu_Id, x.AuthValue, x.Modifier, x.ModifyDate }); //新增的权限 repository.AddRange(updateAuths.Where(x => x.Auth_Id <= 0)); //获取权限取消的权限 int[] authIds = roleAuths.Where(x => userPermissions.Select(u => u.Id) .ToList().Contains(x.Menu_Id) || originalMeunIds.Contains(x.Menu_Id)) .Select(s => s.Auth_Id) .ToArray(); List <Sys_RoleAuth> delAuths = roleAuths.Where(x => x.AuthValue != "" && !authIds.Contains(x.Auth_Id)).ToList(); delAuths.ForEach(x => { x.AuthValue = ""; }); //将取消的权限设置为"" repository.UpdateRange(delAuths, x => new { x.Menu_Id, x.AuthValue, x.Modifier, x.ModifyDate }); int addCount = updateAuths.Where(x => x.Auth_Id <= 0).Count(); int updateCount = updateAuths.Where(x => x.Auth_Id > 0).Count(); await repository.SaveChangesAsync(); string _version = DateTime.Now.ToString("yyyyMMddHHMMssfff"); //标识缓存已更新 base.CacheContext.Add(roleId.GetRoleIdKey(), _version); webResponse.OK($"保存成功:新增加配菜单权限{addCount}条,更新菜单{updateCount}条,删除权限{delAuths.Count()}条"); } catch (Exception ex) { message = "异常信息:" + ex.Message + ex.StackTrace + ","; } finally { Logger.Info($"权限分配置:{message}{webResponse.Message}"); } return(webResponse); }
/// <summary> /// 修改密码 /// </summary> /// <param name="parameters"></param> /// <returns></returns> public async Task <WebResponseContent> ModifyPwd(string oldPwd, string newPwd) { oldPwd = oldPwd?.Trim(); newPwd = newPwd?.Trim(); string message = ""; WebResponseContent webResponse = new WebResponseContent(); try { if (string.IsNullOrEmpty(oldPwd)) { return(webResponse.Error("旧密码不能为空")); } if (string.IsNullOrEmpty(newPwd)) { return(webResponse.Error("新密码不能为空")); } if (newPwd.Length < 6) { return(webResponse.Error("密码不能少于6位")); } int userId = UserContext.Current.UserId; string userCurrentPwd = await base.repository.FindFirstAsync(x => x.User_Id == userId, s => s.UserPwd); string _oldPwd = oldPwd.EncryptDES(AppSetting.Secret.User); if (_oldPwd != userCurrentPwd) { return(webResponse.Error("旧密码不正确")); } string _newPwd = newPwd.EncryptDES(AppSetting.Secret.User); if (userCurrentPwd == _newPwd) { return(webResponse.Error("新密码不能与旧密码相同")); } await Task.Run(() => { base.repository.Update(new Sys_User { User_Id = userId, UserPwd = _newPwd, LastModifyPwdDate = DateTime.Now }, x => new { x.UserPwd, x.LastModifyPwdDate }, true); }); webResponse.OK("密码修改成功"); } catch (Exception ex) { message = ex.Message; webResponse.Error("服务器了点问题,请稍后再试"); } finally { if (message == "") { Logger.OK(LoggerType.ApiModifyPwd, "密码修改成功"); } else { Logger.Error(LoggerType.ApiModifyPwd, message); } } return(webResponse); }
/// <summary> /// 新建或编辑菜单 /// </summary> /// <param name="menu"></param> /// <returns></returns> public async Task <WebResponseContent> Save(Sys_Menu menu) { bool result = true;; 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.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); }
public PorttalHandler(Microsoft.AspNetCore.Http.HttpContext context) : base(context) { this.webResponseContent = new WebResponseContent().Error("未知错误"); }
/// <summary> /// 获取分类 信息 /// </summary> /// <param name="page">页面大小</param> /// <param name="pageSize">行数</param> /// <param name="rowcount">获取到的数据行数</param> /// <param name="sortType">排序类型 默认通过id排序(传0,可不传) 1 创建时间 2 价格降序 3 价格升序</param> /// <param name="CartTypeID">分类 ID</param> /// <param name="BrandId">品牌ID</param> /// <param name="returnRowCount">是否返回行数</param> /// <returns></returns> public WebResponseContent GetCategoryPageData(int page, int pageSize, out int rowcount, int sortType = 0, int CartTypeID = 0, int BrandId = 0, bool returnRowCount = true) { dynamic data = new System.Dynamic.ExpandoObject(); var b = Instance.DbContext.Set <Hiiops_Cart_Brand>().AsQueryable().ToList(); var t = Instance.DbContext.Set <Hiiops_Cart_Category>().AsQueryable().ToList(); //排序字段 Dictionary <string, QueryOrderBy> orderbyDic = new Dictionary <string, QueryOrderBy>(); switch (sortType) { case 1: orderbyDic.Add("CreateDate", QueryOrderBy.Desc); break; case 2: orderbyDic.Add("Price", QueryOrderBy.Desc); break; case 3: orderbyDic.Add("Price", QueryOrderBy.Asc); break; default: orderbyDic.Add("Id", QueryOrderBy.Desc); break; } // Hiiops_Cart PageDataOptions options = new PageDataOptions(); options.TableName = "Hiiops_Cart"; options.Page = page; options.Rows = pageSize; //生成查询条件 List <SearchParameters> searchParameters = new List <SearchParameters>(); if (CartTypeID != 0) { searchParameters.Add(new SearchParameters() { DisplayType = "int", Name = "CategoryId", Value = CartTypeID + "" }); } if (BrandId != 0) { searchParameters.Add(new SearchParameters() { DisplayType = "int", Name = "BrandId", Value = BrandId + "" }); } options.Wheres = searchParameters.Serialize(); page = page <= 0 ? 1 : page; pageSize = pageSize <= 0 ? 10 : pageSize; options.Page = page; options.Rows = pageSize; _ = ValidatePageOptions(options, out IQueryable <Hiiops_Cart> queryable, searchParameters); rowcount = returnRowCount ? queryable.Count() : 0; var row = queryable.GetIQueryableOrderBy <Hiiops_Cart>(orderbyDic) .Skip((page - 1) * pageSize) .Take(pageSize); data.brand = b; data.type = t; var cartlist = row.ToList(); if (cartlist.Count > 0) { cartlist.ForEach(x => { x.LicenseTime.ToString("yyy-MM-dd"); }); } data.cartlist = cartlist; decimal d = rowcount / pageSize; data.count = Math.Ceiling(d); WebResponseContent webResponseContent = new WebResponseContent(); return(webResponseContent.OK("获取成功", data)); }
public virtual ActionResult Del([FromBody] object[] keys) { _baseWebResponseContent = InvokeService("Del", new object[] { keys, true }) as WebResponseContent; Logger.Info(Enums.LoggerType.Del, keys.Serialize(), _baseWebResponseContent.Status ? "Ok" : _baseWebResponseContent.Message); return(Json(_baseWebResponseContent)); }
public virtual ActionResult Audit([FromBody] object[] id, int?auditStatus, string auditReason) { _baseWebResponseContent = InvokeService("Audit", new object[] { id, auditStatus, auditReason }) as WebResponseContent; Logger.Info(Enums.LoggerType.Del, id?.Serialize() + "," + (auditStatus ?? -1) + "," + auditReason, _baseWebResponseContent.Status ? "Ok" : _baseWebResponseContent.Message); return(Json(_baseWebResponseContent)); }
public ServiceBase(TRepository repository) { Response = new WebResponseContent(true); this.repository = repository; }
/// <summary> /// 小程序-通过OpenId快捷登录 /// </summary> /// <param name="code">微信临时code</param> /// <returns></returns> public async Task <WebResponseContent> Login(string code) { WebResponseContent responseContent = new WebResponseContent(); WxUtils _wxUtils = new WxUtils(); //取出appid和secret var config = DbContext.Set <Hiiops_Cart_System_Config>().Where(x => x.KEYNAME == Constant.WECHATAPPID || x.KEYNAME == Constant.WECHATAPPSECRET).ToList(); if (config.Count < 2) { return(responseContent.Error("请先配置小程序参数")); } var appid = CacheContext.Get <string>(Constant.WECHATAPPID); string applet_appid = ""; string applet_secret = ""; if (appid == null) { applet_appid = config.Where(x => x.KEYNAME == Constant.WECHATAPPID).FirstOrDefault().VAL; CacheContext.Add(Constant.WECHATAPPID, applet_appid); } else { applet_appid = appid; } var secret = CacheContext.Get <string>(Constant.WECHATAPPSECRET); if (secret == null) { applet_secret = config.Where(x => x.KEYNAME == Constant.WECHATAPPSECRET).FirstOrDefault().VAL; CacheContext.Add(Constant.WECHATAPPSECRET, applet_secret); } else { applet_secret = secret; } //通过code取openid string jsonStr = _wxUtils.GetXcxKey(code, applet_appid, applet_secret); JObject json = JObject.Parse(jsonStr); string openid = json["openid"].ToString(); if (string.IsNullOrWhiteSpace(openid)) { return(responseContent.Error("服务繁忙,请重试!")); } //通过openid判断系统是否存在当前微信用户 var user = await DbContext.Set <Hiiops_Cart_SellerUser>().Where(x => x.OpenId == openid).FirstOrDefaultAsync(); if (user == null) { return(responseContent.Error("第一次使用请先注册!")); } string token = JwtHelper.IssueJwt(user.Id.ToString()); user.Token = token; responseContent.Data = new { token, user.NickName, user.Phone, user.Account, user.Country, user.HeadImgUrl, user.Id, user.Name, user.Privilege, user.Province, user.Remark, user.SearchKey, user.Sex, user.Status }; DbContext.Update(user); SellerContent.Current.LogOut(user.Id); return(responseContent.OK(ResponseType.LoginSuccess)); }
/// <summary> /// 获取短信验证码 /// </summary> /// <param name="guid"></param> /// <returns></returns> public WebResponseContent ValidateSMSCode(string sessionKey, string code, string phone) { WebResponseContent webResponseContent = new WebResponseContent(); //取出短信配置参数 string regionId, accessKeyId, secret; string signName, templateCode, templateParam; var config = DbContext.Set <Hiiops_Cart_System_Config>(); var _regionId = CacheContext.Get <string>(Constant.REGIONID); if (_regionId == null) { regionId = config.Where(x => x.KEYNAME == Constant.REGIONID).FirstOrDefault().VAL; if (regionId == null || regionId == "") { return(webResponseContent.Error("阿里云短信节点配置有误")); } CacheContext.Add(Constant.REGIONID, regionId); } else { regionId = _regionId; } var _accessKeyId = CacheContext.Get <string>(Constant.ACCESSKEYID); if (_accessKeyId == null) { accessKeyId = config.Where(x => x.KEYNAME == Constant.ACCESSKEYID).FirstOrDefault().VAL; if (accessKeyId == null || accessKeyId == "") { return(webResponseContent.Error("阿里云短信配置有误")); } CacheContext.Add(Constant.ACCESSKEYID, accessKeyId); } else { accessKeyId = _accessKeyId; } var _secret = CacheContext.Get <string>(Constant.SECRET); if (_secret == null) { secret = config.Where(x => x.KEYNAME == Constant.SECRET).FirstOrDefault().VAL; if (secret == null || secret == "") { return(webResponseContent.Error("阿里云短信配置有误")); } CacheContext.Add(Constant.SECRET, secret); } else { secret = _secret; } var _signName = CacheContext.Get <string>(Constant.SIGNNAME); if (_signName == null) { signName = config.Where(x => x.KEYNAME == Constant.SIGNNAME).FirstOrDefault().VAL; if (signName == null || signName == "") { return(webResponseContent.Error("阿里云短信配置有误")); } CacheContext.Add(Constant.SIGNNAME, signName); } else { signName = _signName; } templateCode = config.Where(x => x.KEYNAME == Constant.TEMPLATECODE && x.Remark.Contains("登录验证码")).FirstOrDefault().VAL; if (templateCode == null || templateCode == "") { return(webResponseContent.Error("阿里云短信配置有误")); } templateParam = config.Where(x => x.KEYNAME == Constant.TEMPLATEPARAM && x.Remark.Contains("登录验证码模板")).FirstOrDefault().VAL; if (templateParam == null || templateParam == "") { return(webResponseContent.Error("阿里云短信配置有误")); } string _code = CacheContext.Get <string>(sessionKey); if (_code == null) { return(webResponseContent.Error("请先获取验证码")); } if (_code != code) { return(webResponseContent.Error("输入的验证码有误!")); } //组装一下数据 Random rnd = new Random(); int rand = rnd.Next(1000, 9999); CacheContext.Add(phone + "ValidateSMSCode", rand + "", DateTime.Now.AddMinutes(10).GetTimeSpan()); return(_smsservice.SendTemplateSms(regionId, accessKeyId, secret, phone, signName, templateCode, templateParam)); //return new WebResponseContent().OK(message: "获取成功", data: new { img = $"data:image/jpeg;base64,{Convert.ToBase64String(data.ToArray())}", sessionKey = guid }); }
/// <summary> /// 修改密码 /// </summary> /// <param name="phone">手机号</param> /// <param name="newPwd">新密码</param> /// <param name="verificationCode">验证码</param> /// <param name="code">短信验证码</param> /// <param name="sessionKey">获取图片验证码的ID</param> /// <returns></returns> public async Task <WebResponseContent> ModifyPwd(string phone, string newPwd, string verificationCode, string code, string sessionKey) { newPwd = newPwd?.Trim(); string message = ""; WebResponseContent webResponse = new WebResponseContent(); try { if (string.IsNullOrEmpty(phone)) { return(webResponse.Error("手机号不能为空")); } if (string.IsNullOrEmpty(newPwd)) { return(webResponse.Error("新密码不能为空")); } if (string.IsNullOrEmpty(verificationCode)) { return(webResponse.Error("验证码不能为空")); } if (string.IsNullOrEmpty(code)) { return(webResponse.Error("短信验证码有误")); } if (string.IsNullOrEmpty(sessionKey)) { return(webResponse.Error("参数有误")); } if (newPwd.Length < 6) { return(webResponse.Error("密码不能少于6位")); } string imgCode = CacheContext.Get <string>(sessionKey); if (imgCode == null || imgCode != verificationCode) { if (string.IsNullOrEmpty(verificationCode)) { return(webResponse.Error("请输入正确验证码")); } } string phoneCode = CacheContext.Get <string>(phone + "ValidateSMSCode"); if (phoneCode == null || phoneCode != code) { return(webResponse.Error("短信验证码有误")); } var a = await DbContext.Set <Hiiops_Cart_SellerUser>().Where(x => x.Phone == phone).FirstOrDefaultAsync(); string userCurrentPwd = a.Password; string _newPwd = newPwd.EncryptDES(AppSetting.Secret.User); if (userCurrentPwd == _newPwd) { return(webResponse.Error("新密码不能与旧密码相同")); } await Task.Run(() => { DbContext.Database.ExecuteSqlRaw($"UPDATA Hiiops_Cart_SellerUser SET Password = '******' , ModifyDate = '{DateTime.Now}'"); }); webResponse.OK("密码修改成功"); } catch (Exception ex) { message = ex.Message; webResponse.Error("服务器了点问题,请稍后再试"); } finally { if (message == "") { Logger.OK(LoggerType.ApiModifyPwd, "密码修改成功"); } else { Logger.Error(LoggerType.ApiModifyPwd, message); } } return(webResponse); }
/// <summary> /// 修改密码 /// </summary> /// <param name="parameters"></param> /// <returns></returns> public async Task <WebResponseContent> ModifyPwd(string oldPwd, string newPwd, string verificationCode) { oldPwd = oldPwd?.Trim(); newPwd = newPwd?.Trim(); string message = ""; WebResponseContent webResponse = new WebResponseContent(); try { if (string.IsNullOrEmpty(oldPwd)) { return(webResponse.Error("旧密码不能为空")); } if (string.IsNullOrEmpty(newPwd)) { return(webResponse.Error("新密码不能为空")); } if (newPwd.Length < 6) { return(webResponse.Error("密码不能少于6位")); } int userId = SellerContent.Current.Id; var a = await DbContext.Set <Hiiops_Cart_SellerUser>().Where(x => x.Id == userId).FirstOrDefaultAsync(); string userCurrentPwd = a.Password; string _oldPwd = oldPwd.EncryptDES(AppSetting.Secret.User); if (_oldPwd != userCurrentPwd) { return(webResponse.Error("旧密码不正确")); } string _newPwd = newPwd.EncryptDES(AppSetting.Secret.User); if (userCurrentPwd == _newPwd) { return(webResponse.Error("新密码不能与旧密码相同")); } await Task.Run(() => { DbContext.Database.ExecuteSqlRaw($"UPDATA Hiiops_Cart_SellerUser SET Password = '******' , ModifyDate = '{DateTime.Now}'"); }); webResponse.OK("密码修改成功"); } catch (Exception ex) { message = ex.Message; webResponse.Error("服务器了点问题,请稍后再试"); } finally { if (message == "") { Logger.OK(LoggerType.ApiModifyPwd, "密码修改成功"); } else { Logger.Error(LoggerType.ApiModifyPwd, message); } } return(webResponse); }