public ResponseUserLogin UserLogin(RequestUserLogin request) { ResponseUserLogin response = new ResponseUserLogin(); using (cnn = GetConnection()) { var ts = cnn.BeginTransaction(); try { string pass = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(request.Password, "MD5"); var users = cnn.Query <TKS_FAS_UserExt>(@"select A.*,B.NodeId ,C.Name as NodeName from tks_fas_user A left join TKS_FAS_User2Node B on A.id=b.UserId left join TKS_FAS_Node C on B.NodeId=c.id where A.userName=@UserName and A.password=@Password", new { UserName = request.UserName, Password = pass }, ts).ToList(); if (users.Count() == 0) { users = cnn.Query <TKS_FAS_UserExt>(@"select A.*,B.NodeId ,C.Name as NodeName from tks_fas_user A left join TKS_FAS_User2Node B on A.id=b.UserId left join TKS_FAS_Node C on B.NodeId=c.id where A.Mobile=@Mobile and A.password=@Password", new { Mobile = request.Mobile, Password = pass }, ts).ToList(); if (users.Count() == 0) { throw new NormalException("用户名或者密码错误"); } } TKS_FAS_UserExt user = users[0]; if (user.Status == "0") { throw new NormalException("您的账号已被停用"); } //token cnn.Execute("delete from tks_fas_token where userid=@UserId and source='WEB'", new { UserId = user.Id }, ts); TKS_FAS_Token token = new TKS_FAS_Token(); token.Id = Guid.NewGuid().ToString("N"); token.UserId = user.Id; token.Token = Guid.NewGuid().ToString("N"); token.ActiveTime = DateTime.Now; token.Source = "WEB"; cnn.Insert <TKS_FAS_Token>(token, ts); ts.Commit(); response.IsSuccess = true; response.Id = user.Id; response.Sex = user.Sex; response.Token = token.Token; response.UserName = user.UserName; response.TrueName = user.TrueName; response.NodeName = user.NodeName; response.Message = "登陆成功"; return(response); } catch (Exception ex) { ts.Rollback(); return(this.DealException(response, ex) as ResponseUserLogin); } } }
/// <summary> /// 获取权限 /// </summary> /// <param name="token"></param> /// <param name="ts"></param> /// <returns></returns> protected TKS_FAS_PermissionInfo GetPermission(string token, IDbTransaction ts) { #region timeout string sql = "select * from tks_fas_token where token=@Token"; var tokens = cnn.Query <TKS_FAS_Token>(sql, new { Token = token }, ts).ToList(); if (tokens.Count() == 0) { //System.Web.HttpContext.Current.Session.Clear(); //System.Web.HttpContext.Current.Response.Write("~/Login.aspx"); throw new AppException("", "UserInfoGet", "登陆超时,请重新登陆", "登陆超时,请重新登陆"); } TKS_FAS_Token curToken = tokens[0]; DateTime now = DateTime.Now; var interval = now - DateTime.Parse(curToken.ActiveTime.ToString()); var sec = interval.TotalSeconds; var timeOut = double.Parse(ConfigHelper.Read("timeOut")); if (timeOut < sec) { //System.Web.HttpContext.Current.Session.Clear(); //System.Web.HttpContext.Current.Response.Write("~/Login.aspx"); throw new AppException("", "UserInfoGet", "登陆超时,请重新登陆", "token 超时" + sec + "秒"); } sql = @"update tks_fas_token set ActiveTime=@ActiveTime where token=@Token"; cnn.Execute(sql, new { ActiveTime = DateTime.Now, Token = token }, ts); #endregion var user = cnn.Query <TKS_FAS_User>("select * from tks_fas_user where id=@UserId", new { UserId = curToken.UserId }, ts).ToList(); if (user.Count == 0) { throw new AppException(curToken.UserId, "UserInfoGet", "用户信息异常,请联系管理员", "当前token没有对应的用户信息" + curToken.UserId); } var node = cnn.Query <TKS_FAS_Node>(@"select A.* from tks_fas_node A left join tks_fas_user2Node B on A.id=B.nodeId where B.userid=@UserId", new { UserId = curToken.UserId }, ts).ToList(); if (node.Count() > 1) { throw new AppException(curToken.UserId, "UserInfoGet", "用户的机构信息异常,请联系管理员", "用户属于多个机构"); } var roles = cnn.Query <TKS_FAS_Role>(@"select A.* from TKS_FAS_Role A left join TKS_FAS_User2Role B on A.id=B.roleId where B.userid=@UserId", new { UserId = curToken.UserId }, ts).ToList(); TKS_FAS_PermissionInfo res = new TKS_FAS_PermissionInfo(); if (roles.Count > 0) { string where = "("; foreach (var item in roles) { if (where == "(") { where += "'" + item.Id + "'"; } else { where += ",'" + item.Id + "'"; } } where += ")"; sql = string.Format(@"select Permission,PLevel from TKS_FAS_Role2Permission where RoleId in{0} order by PLevel", where); var Permission = cnn.Query <TKS_FAS_PermissionInfo>(sql, null, ts).Distinct().ToList(); if (Permission.Count > 0) { res = Permission[0]; } else { res = null; } } return(res); }
public ResponseUserLogin_WX WXLogin(RequestUserLogin request) { ResponseUserLogin_WX response = new ResponseUserLogin_WX(); using (cnn = GetConnection()) { var ts = cnn.BeginTransaction(); try { var users = cnn.Query <TKS_FAS_MobileVerification>(@"select * from TKS_FAS_MobileVerification where Status='OP' and CodeType='WX_Regist' and Mobile=@Mobile and VerCode=@VerCode", new { Mobile = request.Mobile, VerCode = request.VerCode }, ts).FirstOrDefault(); if (users == null) { response.IsSuccess = false; response.Message = "验证码失效"; return(response); } else { string userid = ""; string role = ConfigurationManager.AppSettings["GZQY-ADMIN"]; cnn.Execute("update TKS_FAS_MobileVerification set Status='SE' where Id=@Id", new { Id = users.Id }, ts); UserBLL bll = new UserBLL(); string sql = @"select * from TKS_FAS_User where mobile=@Mobile"; var user = cnn.Query <TKS_FAS_User>(sql, new { Mobile = request.Mobile }, ts).FirstOrDefault(); if (user != null) { userid = user.Id; response.user = user; //判断是否有企业主角色,没有则添加企业主角色权限 var QY = cnn.Query <TKS_FAS_User2Role>(@"select * from TKS_FAS_User2Role where UserId=@UserId and RoleId=@RoleId", new { UserId = user.Id, RoleId = role }, ts).FirstOrDefault(); if (QY == null) { TKS_FAS_User2Role u2r = new TKS_FAS_User2Role(); u2r.Id = Guid.NewGuid().ToString(); u2r.UserId = user.Id; u2r.RoleId = role;//雇主企业管理员角色ID,注册默认; cnn.Insert <TKS_FAS_User2Role>(u2r, ts); } response.IsSuccess = true; //response.UserType = "0";//有关联的企业账套,直接登录进入主页面 response.Message = "登陆成功"; } else { //创建企业主账号,赋予企业主角色权限 TKS_FAS_User newuser = new TKS_FAS_User(); newuser.Id = Guid.NewGuid().ToString("N"); newuser.UserName = request.Mobile; newuser.TrueName = request.Mobile; //newuser.Sex = request.User.Sex; newuser.Mobile = request.Mobile; string pass = System.Web.Security.FormsAuthentication. HashPasswordForStoringInConfigFile("123456", "MD5"); newuser.Password = pass; newuser.Status = "1";//启用 newuser.CreateUser = "******"; newuser.CreateDate = DateTime.Now; cnn.Insert <TKS_FAS_User>(newuser, ts); TKS_FAS_User2Role u2r = new TKS_FAS_User2Role(); u2r.Id = Guid.NewGuid().ToString(); u2r.UserId = newuser.Id; u2r.RoleId = role;//雇主企业管理员角色ID,注册默认; cnn.Insert <TKS_FAS_User2Role>(u2r, ts); response.user = newuser; response.IsSuccess = true; response.Message = "登陆成功"; userid = newuser.Id; } //token cnn.Execute("delete from tks_fas_token where userid=@UserId and source='WXAPP'", new { UserId = userid }, ts); TKS_FAS_Token token = new TKS_FAS_Token(); token.Id = Guid.NewGuid().ToString("N"); token.UserId = userid; token.Token = Guid.NewGuid().ToString("N"); token.ActiveTime = DateTime.Now; token.Source = "WXAPP"; cnn.Insert <TKS_FAS_Token>(token, ts); ts.Commit(); response.Token = token.Token; return(response); } } catch (Exception ex) { ts.Rollback(); return(this.DealException(response, ex) as ResponseUserLogin_WX); } } }
protected MM_UserInfo UserInfoGet(string token, IDbTransaction ts) { #region timeout string sql = "select * from tks_fas_token where token=@Token"; var tokens = cnn.Query <TKS_FAS_Token>(sql, new { Token = token }, ts).ToList(); if (tokens.Count() == 0) { //System.Web.HttpContext.Current.Session.Clear(); //System.Web.HttpContext.Current.Response.Write("~/Login.aspx"); throw new AppException("", "UserInfoGet", "登陆超时,请重新登陆", "登陆超时,请重新登陆"); } TKS_FAS_Token curToken = tokens[0]; DateTime now = DateTime.Now; var interval = now - DateTime.Parse(curToken.ActiveTime.ToString()); var sec = interval.TotalSeconds; var timeOut = double.Parse(ConfigHelper.Read("timeOut")); if (timeOut < sec) { //System.Web.HttpContext.Current.Session.Clear(); //System.Web.HttpContext.Current.Response.Write("~/Login.aspx"); throw new AppException("", "UserInfoGet", "登陆超时,请重新登陆", "token 超时" + sec + "秒"); } sql = @"update tks_fas_token set ActiveTime=@ActiveTime where token=@Token"; cnn.Execute(sql, new { ActiveTime = DateTime.Now, Token = token }, ts); #endregion var user = cnn.Query <TKS_FAS_User>("select * from tks_fas_user where id=@UserId", new { UserId = curToken.UserId }, ts).ToList(); var node = cnn.Query <TKS_FAS_Node>(@"select A.* from tks_fas_node A left join tks_fas_user2Node B on A.id=B.nodeId where B.userid=@UserId", new { UserId = curToken.UserId }, ts).ToList(); if (node.Count() > 1) { throw new AppException(curToken.UserId, "UserInfoGet", "用户的机构信息异常,请联系管理员", "用户属于多个机构"); } var roles = cnn.Query <TKS_FAS_Role>(@"select A.* from TKS_FAS_Role A left join TKS_FAS_User2Role B on A.id=B.roleId where B.userid=@UserId", new { UserId = curToken.UserId }, ts).ToList(); var currentAccount = cnn.Query <TKS_FAS_UserCurrentAccount>( @"select * from TKS_FAS_UserCurrentAccount where userId=@UserId", new { UserId = curToken.UserId }, ts).ToList(); MM_UserInfo res = new MM_UserInfo(); if (currentAccount.Count() == 0) { //update by Hero.Zhang //throw new AppException(curToken.UserId,"","请选择账套", "请选择账套"); } else { res.AccountId = currentAccount[0].AccountId; } res.User = user[0]; res.Node = node.Count > 0 ? node[0] : null; res.Roles = roles; return(res); }