Ejemplo n.º 1
0
        //[ValidateAntiForgeryToken] 安全必须 外网出错 所需的防伪 Cookie“__RequestVerificationToken”不存在。 暂时去掉,待解决
        public ActionResult Index(LoginModel mode)
        {
            // if (ModelState.IsValid)

            ViewData["UserType"] = GenerateList();
            //if (Session["ValidateCode"] != null)
            //{
            #region  获取cookies
            //if (!string.IsNullOrEmpty(Request.Cookies["UserData"]["AdminUserInfo"]))//Cookies保存 获取解析
            //{
            //    string dataj = Request.Cookies["UserData"]["AdminUserInfo"];
            //    UserData = JsonHelper.FromJson<AdminUserInfo>(dataj);
            //    if (UserData != null)
            //    {
            //        return RedirectToAction("index", "home");
            //    }
            //}
            #endregion

            string Vcode = Session["ValidateCode"].ToString();
            if (mode.VCode != null && mode.VCode.Trim().Equals(Vcode))//验证码
            {
                UserData = null;
                List <V_UserRole> adminRole = null;
                bool IsHaveP = false; //是否有权限登录
                mode.UserType = "1";  //
                #region  根据类型登录
                switch (mode.UserType)
                {
                case "1"://账号密码登录
                    var sql = V_UserRoleSet.SelectAll().Where(V_UserRoleSet.LoginName.Equal(mode.LoginName).And(V_UserRoleSet.Password.Equal(mode.Password)));
                    adminRole = URBiz.GetOwnList <V_UserRole>(sql);
                    break;

                case "0"://姓名登录
                    var sql0 = V_UserRoleSet.SelectAll().Where(V_UserRoleSet.LoginName.Equal(mode.LoginName).And(V_UserRoleSet.UserType.Equal(0)));
                    adminRole = URBiz.GetOwnList <V_UserRole>(sql0);
                    if (adminRole != null && adminRole.Count > 0)//检查是否存在,不存在就添加,给登录用户的角色
                    {
                        break;
                    }
                    else
                    {
                        RMS_User item = new RMS_User();
                        item.Id         = Guid.NewGuid();
                        item.LoginName  = mode.LoginName;
                        item.UserType   = 0;
                        item.CreateTime = DateTime.Now;
                        item.ModifyTime = DateTime.Now;
                        uBiz.Add(item);

                        List <RMS_Role> listRole = URBiz.GetOwnList <RMS_Role>(RMS_RoleSet.SelectAll().Where(RMS_RoleSet.RoleTypes.Equal(0)));//所有的姓名登录角色

                        RMS_UserRole urItem = new RMS_UserRole();
                        urItem.Id     = Guid.NewGuid();
                        urItem.UserId = item.Id;
                        urItem.RoleId = listRole[0].Id;
                        urBiz.Add(urItem);

                        adminRole = URBiz.GetOwnList <V_UserRole>(sql0);
                    }



                    break;

                case "2"://身份证登录
                    var sql2 = V_UserRoleSet.SelectAll().Where(V_UserRoleSet.LoginName.Equal(mode.LoginName).And(V_UserRoleSet.UserType.Equal(2)));
                    adminRole = URBiz.GetOwnList <V_UserRole>(sql2);
                    if (adminRole != null && adminRole.Count > 0)//检查是否存在,不存在就添加,给身份证用户的角色
                    {
                        break;
                    }
                    else
                    {
                        RMS_User item = new RMS_User();
                        item.Id         = Guid.NewGuid();
                        item.LoginName  = mode.LoginName;
                        item.UserType   = 2;
                        item.CreateTime = DateTime.Now;
                        item.ModifyTime = DateTime.Now;
                        uBiz.Add(item);

                        List <RMS_Role> listRole = URBiz.GetOwnList <RMS_Role>(RMS_RoleSet.SelectAll().Where(RMS_RoleSet.RoleTypes.Equal(2)));//所有的姓名登录角色

                        RMS_UserRole urItem = new RMS_UserRole();
                        urItem.Id     = Guid.NewGuid();
                        urItem.UserId = item.Id;
                        urItem.RoleId = listRole[0].Id;
                        urBiz.Add(urItem);

                        adminRole = URBiz.GetOwnList <V_UserRole>(sql2);
                    }
                    break;

                default:

                    break;
                }

                #endregion

                if ((adminRole != null && adminRole.Count > 0)) // 账号是否存在,添加权限配置
                {
                    #region 存用户数据

                    UserData           = new AdminUserInfo();
                    UserData.UserTypes = int.Parse(adminRole[0].UserType.ToString());
                    // UserData.adminUserInfo = adminRole[0];
                    UserData.Id       = adminRole[0].Id;
                    UserData.UserName = adminRole[0].LoginName;
                    UserData.RoleId   = adminRole[0].RoleId;
                    UserData.Password = adminRole[0].Password;

                    if (adminRole[0].DepartmentId != null)
                    {
                        var            dpsql  = RMS_DepartmentSet.SelectAll().Where(RMS_DepartmentSet.Id.Equal(adminRole[0].DepartmentId));
                        RMS_Department dpItem = dpBiz.GetEntity(dpsql);
                        if (dpItem != null)
                        {
                            UserData.DepartmentId   = dpItem.Id;
                            UserData.DepartmentName = dpItem.Name;
                        }
                    }
                    IsHaveP = true;

                    #endregion

                    #region  获取权限
                    if (IsHaveP)                                                                                                                                                                //可以登录
                    {
                        List <V_RoleManus>       manus      = URBiz.GetOwnList <V_RoleManus>(V_RoleManusSet.SelectAll().Where(V_RoleManusSet.RoleId.Equal(UserData.RoleId)));                   //所有的菜单
                        List <V_RoleManuButtons> buttons    = URBiz.GetOwnList <V_RoleManuButtons>(V_RoleManuButtonsSet.SelectAll().Where(V_RoleManuButtonsSet.RoleId.Equal(UserData.RoleId))); //角色拥有的菜单的所有按钮
                        List <V_MenuButtons>     AllButtons = URBiz.GetOwnList <V_MenuButtons>(V_MenuButtonsSet.SelectAll());                                                                   //所有菜单的所有按钮
                        List <Manu> ListManus = new List <Manu>();
                        if (manus != null && manus.Count > 0)
                        {
                            foreach (V_RoleManus item in manus)
                            {
                                Manu OneManu = new Manu();
                                OneManu.manuInfo      = item;
                                OneManu.ListButtons   = buttons.FindAll(p => p.ManuId.Equals(item.ManuId)).OrderBy(p => p.OrderNo).ToList();
                                OneManu.ManuAllButton = AllButtons.FindAll(p => p.ManuId.Equals(item.ManuId));
                                ListManus.Add(OneManu);
                            }
                            UserData.ListManus = ListManus;
                        }

                        #region   存cookies
                        //string datajson = JsonHelper.ToJson(UserData);
                        //HttpCookie cookie = new HttpCookie("UserData");//初使化并设置Cookie的名称
                        //DateTime dt = DateTime.Now;
                        //TimeSpan ts = new TimeSpan(0, 1, 0, 0, 0);//过期时间为1分钟
                        //cookie.Expires = dt.Add(ts);//设置过期时间

                        //cookie.Values.Add("AdminUserInfo", datajson);
                        //Response.AppendCookie(cookie);

                        #endregion


                        return(RedirectToAction("index", "home"));
                    }

                    #endregion
                }
                else
                {
                    //  return RedirectToAction("index", "Login");
                    // 如果我们进行到这一步时某个地方出错,则重新显示表单
                    ViewData["IsShowAlert"] = true;
                    ViewData["Alert"]       = "账号或者密码有误";
                }
            }
            else
            {
                ViewData["IsShowAlert"] = true;
                ViewData["Alert"]       = "验证码有误";
            }
            return(View());
        }
Ejemplo n.º 2
0
        //[ValidateAntiForgeryToken] 安全必须 外网出错 所需的防伪 Cookie“__RequestVerificationToken”不存在。 暂时去掉,待解决
        public ActionResult Index(LoginModel mode)
        {
            // if (ModelState.IsValid)

            ViewData["UserType"] = GenerateList();
            if (Session["ValidateCode"] != null)
            {
                //if (Request.Cookies["User"] != null)//Cookies保存 获取解析
                //{
                //    HttpCookie cookie = new HttpCookie("User");//初使化并设置Cookie的名称
                //    DateTime dt = DateTime.Now;
                //    TimeSpan ts = new TimeSpan(0, 1, 0, 0, 0);//过期时间为1分钟
                //    cookie.Expires = dt.Add(ts);//设置过期时间
                //    string info = JsonHelper.ToJson(UserData.ListManus, true);
                //    string manuInfo = Server.HtmlEncode(info);
                //    cookie.Values.Add("Manus", manuInfo);

                //    string UserDataString = Server.HtmlEncode(Request.Cookies["User"]["UserInfo"]);
                //    string ManusString = Server.HtmlEncode(Request.Cookies["User"]["Manus"]);
                //    V_UserRole Rmodel = JsonHelper.FromJson<V_UserRole>(UserDataString);
                //}


                string Vcode = Session["ValidateCode"].ToString();
                if (mode.VCode.Trim().Equals(Vcode))//验证码
                {
                    UserData = null;
                    List <V_UserRole>        adminRole = null;
                    List <v_TS_ShopUserRole> shopRole  = null;
                    bool IsHaveP = false;     //是否有权限登录
                    if (mode.UserType == "0") //根据用户类型去找数据
                    {
                        var sql = V_UserRoleSet.SelectAll().Where(V_UserRoleSet.LoginName.Equal(mode.LoginName).And(V_UserRoleSet.Password.Equal(mode.Password)));
                        adminRole = URBiz.GetOwnList <V_UserRole>(sql);
                    }
                    else
                    {
                        var sql = v_TS_ShopUserRoleSet.SelectAll().Where(v_TS_ShopUserRoleSet.UserName.Equal(mode.LoginName).And(v_TS_ShopUserRoleSet.Pwd.Equal(mode.Password)).And(v_TS_ShopUserRoleSet.isDeleted.Equal(0)));
                        shopRole = URBiz.GetOwnList <v_TS_ShopUserRole>(sql);
                    }

                    if ((adminRole != null && adminRole.Count > 0) || (shopRole != null && shopRole.Count > 0)) // 账号是否存在,添加权限配置
                    {
                        #region 存用户数据

                        if (mode.UserType == "0")// 商家为1 管理员为0 缓存用户信息
                        {
                            UserData           = new AdminUserInfo();
                            UserData.UserTypes = UserType.admin;
                            // UserData.adminUserInfo = adminRole[0];
                            UserData.Id       = adminRole[0].Id;
                            UserData.UserName = adminRole[0].LoginName;
                            UserData.RoleId   = adminRole[0].RoleId;
                            UserData.Password = adminRole[0].Password;
                            IsHaveP           = true;
                        }
                        else
                        {
                            if (shopRole[0].States <= 0)
                            {
                                ViewData["IsShowAlert"] = true;
                                ViewData["Alert"]       = "您的账号未审核通过,请联系管理员";
                            }
                            else
                            {
                                UserData           = new AdminUserInfo();
                                IsHaveP            = true;
                                UserData.UserTypes = UserType.ShopUser;
                                // UserData.shopUserInfo = shopRole[0];
                                UserData.Id       = shopRole[0].Id;
                                UserData.UserName = shopRole[0].UserName;
                                UserData.RoleId   = shopRole[0].RoleId;
                                UserData.Password = shopRole[0].Pwd;
                            }
                        }
                        #endregion

                        #region  获取权限
                        if (IsHaveP)                                                                                                                                                                //可以登录
                        {
                            List <V_RoleManus>       manus      = URBiz.GetOwnList <V_RoleManus>(V_RoleManusSet.SelectAll().Where(V_RoleManusSet.RoleId.Equal(UserData.RoleId)));                   //所有的菜单
                            List <V_RoleManuButtons> buttons    = URBiz.GetOwnList <V_RoleManuButtons>(V_RoleManuButtonsSet.SelectAll().Where(V_RoleManuButtonsSet.RoleId.Equal(UserData.RoleId))); //角色拥有的菜单的所有按钮
                            List <V_MenuButtons>     AllButtons = URBiz.GetOwnList <V_MenuButtons>(V_MenuButtonsSet.SelectAll());                                                                   //所有菜单的所有按钮
                            List <Manu> ListManus = new List <Manu>();
                            if (manus != null && manus.Count > 0)
                            {
                                foreach (V_RoleManus item in manus)
                                {
                                    Manu OneManu = new Manu();
                                    OneManu.manuInfo      = item;
                                    OneManu.ListButtons   = buttons.FindAll(p => p.ManuId.Equals(item.ManuId)).OrderBy(p => p.OrderNo).ToList();
                                    OneManu.ManuAllButton = AllButtons.FindAll(p => p.ManuId.Equals(item.ManuId));
                                    ListManus.Add(OneManu);
                                }
                                UserData.ListManus = ListManus;
                            }
                            return(RedirectToAction("index", "home"));
                        }

                        #endregion
                    }
                    else
                    {
                        //  return RedirectToAction("index", "Login");
                        // 如果我们进行到这一步时某个地方出错,则重新显示表单
                        ViewData["IsShowAlert"] = true;
                        ViewData["Alert"]       = "账号或者密码有误";
                    }
                }
                else
                {
                    ViewData["IsShowAlert"] = true;
                    ViewData["Alert"]       = "验证码有误";
                }
            }
            return(View());
        }