Exemplo n.º 1
0
        // 用户登录
        private string userLogin()
        {
            myJson my = new myJson();

            try
            {
                #region 检测用户名,密码
                string userName = Funcs.Get("userName"); //用户名
                string pwd      = Funcs.Get("pwd");      //密码

                string         strSql = "select su.*,sp.paramsName userTypeName from system_users su inner join system_params sp on su.userType=sp.id where su.userName=@userName and su.password=@pwd";
                SqlParameter[] param  = new SqlParameter[] {
                    new SqlParameter("@userName", SqlDbType.VarChar)
                    {
                        Value = userName
                    },
                    new SqlParameter("@pwd", SqlDbType.VarChar)
                    {
                        Value = Funcs.MD5(pwd)
                    }
                };

                DataTable tb = Utility.SqlHelper.GetDataTable(strSql, param);
                if (tb == null || tb.Rows.Count < 1)
                {
                    my.flag = 0;
                    my.msg  = "用户名或密码错误!";
                    return(JsonConvert.SerializeObject(my));
                }
                if (int.Parse(tb.Rows[0]["userstatus"].ToString()) == 0)
                {
                    my.flag = 0;
                    my.msg  = "您的帐号已暂停使用,请联系管理员!";
                    return(JsonConvert.SerializeObject(my));
                }
                #endregion

                #region 保存用户信息,权限到Session
                string loginUserId = tb.Rows[0]["id"].ToString();
                //防止一个帐号多处登录
                Global.Add(int.Parse(loginUserId), HttpContext.Current.Session.SessionID);


                //保存用户的信息到Session
                MySession.Add("userId", tb.Rows[0]["id"]);
                MySession.Add("userName", tb.Rows[0]["userName"]);
                MySession.Add("userTypeName", tb.Rows[0]["userTypeName"]);
                MySession.Add("trueName", tb.Rows[0]["trueName"]);

                #endregion

                my.flag = 1;
                my.msg  = "登录成功";
                return(JsonConvert.SerializeObject(my));
            }
            catch (Exception ex)
            {
                my.flag = 0;
                my.msg  = "登录失败:" + ex.Message;
                return(JsonConvert.SerializeObject(my));
            }
        }