using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Fm.Bussiness;

public partial class To_Login : System.Web.UI.Page
{ 
   
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    //点击登陆
    protected void Login_Click(object sender, ImageClickEventArgs e)
    {
        Fm_Admin_Login login = new Fm_Admin_Login();
        //判断验证码的正误
        if (UserCode.Text != Session["CheckCode"].ToString())
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请输入正确的验证码!');</script>");
        }
        else
        {
            //将获取的密码进行加密
            string pass = FormsAuthentication.HashPasswordForStoringInConfigFile(u_pass.Text.Trim(), "MD5");
            //验证用户名的正误
            DataView ds = login.Admin_Login(u_name.Text, pass) as DataView;
            if (ds.Count != 0)
            {
                
                Session["uid"] = ds[0]["u_id"].ToString();
                Session["uname"] = u_name.Text.Trim();
                DataView rid = login.Admin_UR(int.Parse(Session["uid"].ToString())) as DataView;//获取用户所属权限组

                Fm.EntityClass.Login lo = new Fm.EntityClass.Login();
                lo.lo_user=u_name.Text;
                lo.lo_ip=Request.UserHostAddress.ToString();
                lo.lo_time=DateTime.Now;
                login.Login_Ip(lo);
                if (rid.Count > 0)
                {
                    //循环得到所属用户组
                    List<int> r_id = new List<int>();
                    foreach (DataRow dr in rid.Table.Rows)
                    {
                        r_id.Add(int.Parse(dr["r_id"].ToString()));
                    }
                    Session["r_id"] = r_id;
                    //跳转
                    Response.Redirect("Main.htm");
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('您无权登陆后台!');</script>");
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请输入正确的用户名和密码!');</script>");
            }
        }
    }
}
