登录handler(一般处理程序)层
using BLL;
using Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
namespace _123
{
/// <summary>
/// LoginHandler 的摘要说明
/// </summary>
public class LoginHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
string struserid = context.Request["UserID"].ToString();
string strpwd = context.Request["Pwd"].ToString();
UserInfo user = new UserInfo();
UserInfoManager bll = new UserInfoManager();
user = bll.Login(struserid, strpwd);
string result = "";
result = "ok";
if (!string.IsNullOrEmpty(user.UserName))
{
context.Session["userinfo"] = user;
if (user.UserType == 0)
{
result = "user";
}
else if (user.UserType == 1)
{
result = "manager";
}
else if (user.UserType == 2)
{
result = "admin";
}
}
else
{
result = "false";
}
context.Response.Write(result);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
登录实体层:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Entity
{
public class UserInfo
{
public string UserID { get; set; }
public string UserName { get; set; }
public int DeptID { get; set; }
public string Password { get; set; }
public string CallPhone { get; set; }
public int UserType { get; set; }
public int RowIndex { get; set; }
public int TatalCount { get; set; }
public string DeptName { get; set; }
public string ckbDeptID { get; set; }
}
}
登录服务层:
using Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace DAL
{
public class UserInfoService
{
//登录
public UserInfo Login(string UserID, string Pwd)
{
UserInfo ui = new UserInfo();
string sql = string.Format("select*from UserInfo where UserID='{0}' and Password='{1}'", UserID, Pwd);
DataTable dt = DBHelper.GetData(sql);
if (dt.Rows.Count > 0)
{
ui.UserID = dt.Rows[0]["UserID"].ToString();
ui.Password = dt.Rows[0]["Password"].ToString();
if (!string.IsNullOrEmpty(dt.Rows[0]["DeptID"].ToString()))
{
ui.DeptID = Convert.ToInt32(dt.Rows[0]["DeptID"]);
}
ui.UserName = dt.Rows[0]["UserName"].ToString();
ui.UserType = Convert.ToInt32(dt.Rows[0]["UserType"].ToString());
}
return ui;
}
}
}
登录逻辑层:
using DAL;
using Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
public class UserInfoManager
{
UserInfoService ui = new UserInfoService();
public UserInfo Login(string UserID, string Pwd)
{
return ui.Login(UserID, Pwd);
}
}
}
列表Handler(一般处理程序)层:
using BLL;
using Entity;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace _123
{
/// <summary>
/// DepartmentHandler 的摘要说明
/// </summary>
public class DepartmentHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//取前台传入的参数
DepartmentManager bll = new DepartmentManager();
Department deptInfo = new Department();
string results = "";
string type = context.Request["type"].ToString();//区分方法块
if (type.ToUpper() == "DEPTPAGELIST")//部门分页列表
{
//处理分页逻辑
int sPageIndex = int.Parse(context.Request["pageIndex"]);
int sPageSize = int.Parse(context.Request["pageSize"]);
Department dept = new Department();//传入实体参数
int startIndex = (sPageIndex - 1) * sPageSize + 1;//开始数
int endIndex = sPageIndex * sPageSize;//截止数
List<Department> deptinfoList = new List<Department>();
deptinfoList = bll.GetDeptPageList(dept, startIndex, endIndex);
results = JsonConvert.SerializeObject(deptinfoList);//序列化成json
}
//输出到前台
context.Response.Write(results);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
列表实体层:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Entity
{
public class Department
{
public int DeptID { get; set; }
public string DeptName { get; set; }
public string ManagerID { get; set; }
public string DeptInfo { get; set; }
public string UserInfo { get; set; }
public string UserName { get; set; }
public int RowIndex { get; set; }
public int TatalCount { get; set; }
public string UserID { get; set; }
}
}
列表服务层
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DAL
{
public class DepartmentService
{
public List<Department> GetDeptPageList(Department deptInfo, int startIndex, int endIndex)
{
List<Department> deptList = new List<Department>();
string strWhere1 = " "; //不加where
if (!string.IsNullOrEmpty(deptInfo.DeptName))
{
strWhere1 += " and deptname like '%" + deptInfo.DeptName + "%'";
}
if (!string.IsNullOrEmpty(deptInfo.ManagerID) && deptInfo.ManagerID != "0")
{
strWhere1 += " and managerid =" + deptInfo.ManagerID;
}
string sql = "select * from (select ROW_NUMBER() over(order by a.Deptid) RowIndex ,a.*,b.UserName from Department a "
+ "left join UserInfo b on a.ManagerID=b.UserID where 1=1 " + strWhere1 + ") t where RowIndex between " + startIndex + " and " + endIndex + "" + strWhere1;
DataTable dt = DBHelper.GetData(sql);
#region 取总记录数
string sql1 = "select count(*) totalCount from Department where 1=1 " + strWhere1;
DataTable sdt = DBHelper.GetData(sql1);
int totalCount = int.Parse(sdt.Rows[0]["totalCount"].ToString());
#endregion
for (int i = 0; i < dt.Rows.Count; i++)///把table转换成list<T>
{
Department info = new Department();
info.DeptID = Convert.ToInt32(dt.Rows[i]["DeptID"]);
info.DeptName = dt.Rows[i]["DeptName"].ToString();
info.ManagerID = dt.Rows[i]["ManagerID"].ToString();
info.DeptInfo = dt.Rows[i]["DeptInfo"].ToString();
info.UserName = dt.Rows[i]["UserName"].ToString();
info.RowIndex = int.Parse(dt.Rows[i]["RowIndex"].ToString());
info.TatalCount = totalCount;
deptList.Add(info);
}
return deptList;
}
}
}
列表逻辑层:
using DAL;
using Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
public class DepartmentManager
{
DepartmentService dal = new DepartmentService();
/// <summary>
/// 部门分页列表
/// </summary>
/// <param name="deptInfo">传入的部门信息</param>
/// <param name="pageIndex">查询开始数</param>
/// <param name="endIndex">查询结束数量</param>
/// <returns></returns>
public List<Department> GetDeptPageList(Department deptInfo, int startIndex, int endIndex)
{
return dal.GetDeptPageList(deptInfo, startIndex, endIndex);
}
}
}
服务层中还要添加DBHelper文件:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DAL
{
public class DBHelper
{
public static string connstr = ConfigurationManager.ConnectionStrings["connstr"].ToString();
/// <summary>
/// GetTable
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static DataTable GetData(string sql)
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand();
connection.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
da.Fill(dt);
connection.Close();
return dt;
}
/// <summary>
/// 增删改
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static int CommData(string sql)
{
int result = 0;
SqlConnection connection = new SqlConnection(connstr);
connection.Open();
SqlCommand command = new SqlCommand(sql, connection);
result = command.ExecuteNonQuery();
connection.Close();
return result;
}
}
}