SpringBird Erp系统快速开发平台之登录

  SpringBird Erp系统快速开发平台基于通用的三层架构,数据访问层采用了无Sql注入风险的IBatis.net,表现层采用了微软最新的Asp.net mvc3 Razor模板解析引擎和轻量级的Jquery easyui,服务层采用了接口编程,整体使用成熟可靠的Ioc、Aop框架Spring.net进行服务层、数据访问层和表现层之间的整合。讨论qq:2262366309

  • IUserService接口,注意里面的Login方法
View Code
 1 using System.Collections;
2 using System.Collections.Generic;
3 using SpringBird.Erp.Entity;
4
5 namespace SpringBird.Erp.Contract
6 {
7 /// <summary>
8 /// 用户服务
9 /// </summary>
10 public interface IUserService
11 {
12 /// <summary>
13 /// 获取用户
14 /// </summary>
15 /// <param name="name">账号</param>
16 /// <returns>用户</returns>
17 User GetUser(string name);
18
19 /// <summary>
20 /// 获取用户集合
21 /// </summary>
22 /// <param name="parameters">参数集合</param>
23 /// <returns>用户集合</returns>
24 IList<User> GetUsers(IDictionary parameters);
25
26 /// <summary>
27 /// 登录
28 /// </summary>
29 /// <param name="name">账号</param>
30 /// <param name="password">密码</param>
31 /// <returns>结果</returns>
32 bool Login(string name, string password);
33
34 /// <summary>
35 /// 移除用户
36 /// </summary>
37 /// <param name="id">编码</param>
38 void RemoveUser(int id);
39
40 /// <summary>
41 /// 保存用户
42 /// </summary>
43 /// <param name="user">用户</param>
44 void SaveUser(User user);
45 }
46 }
  • UserDao数据访问对象
View Code
 1 using System.Collections;
2 using System.Collections.Generic;
3 using SpringBird.Data;
4 using SpringBird.Erp.Entity;
5
6 namespace SpringBird.Erp.Dao
7 {
8 /// <summary>
9 /// 用户Dao
10 /// </summary>
11 public class UserDao : MyBatisBase<UserDao>
12 {
13 public UserDao()
14 : base("Erp.config", "Erp.config")
15 {
16
17 }
18
19 public User GetUser(string name)
20 {
21 return this.Reader.QueryForObject<User>(this.GetStatementName("GetUser"), name);
22 }
23
24 public int GetUserCount(IDictionary parameters)
25 {
26 return this.Reader.QueryForObject<int>(this.GetStatementName("GetUserCount"), parameters);
27 }
28
29 public IList<User> GetUsers(IDictionary parameters)
30 {
31 return this.Reader.QueryForList<User>(this.GetStatementName("GetUsers"), parameters);
32 }
33
34 public bool HasUser(IDictionary parameters)
35 {
36 return this.Reader.QueryForObject<int>(this.GetStatementName("HasUser"), parameters) > 0;
37 }
38
39 public void InsertUser(User user)
40 {
41 this.Writer.Insert(this.GetStatementName("InsertUser"), user);
42 }
43
44 public bool RemoveUser(int id)
45 {
46 return this.Writer.Delete(this.GetStatementName("RemoveUser"), id) > 0;
47 }
48
49 public bool UpdateUser(User user)
50 {
51 return this.Writer.Update(this.GetStatementName("UpdateUser"), user) > 0;
52 }
53 }
54 }
  • UserDao对应的IBatis映射文件
View Code
 1 <?xml version="1.0" encoding="utf-8" ?>
2 <sqlMap namespace="SpringBird.Erp.Dao" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3 <alias>
4 <typeAlias alias="User" type="SpringBird.Erp.Entity.User, SpringBird.Erp"/>
5 </alias>
6 <resultMaps>
7 <resultMap id="User" class="User">
8 <result property="Id" column="Id"/>
9 <result property="DeptId" column="DeptId"/>
10 <result property="DeptName" column="DeptName"/>
11 <result property="Name" column="Name"/>
12 <result property="RealName" column="RealName"/>
13 <result property="Password" column="Password"/>
14 <result property="Email" column="Email"/>
15 <result property="Sequence" column="Sequence"/>
16 <result property="Created" column="Created"/>
17 <result property="Modified" column="Modified"/>
18 <result property="State" column="State"/>
19 </resultMap>
20 </resultMaps>
21 <statements>
22 <select id="GetUser" parameterClass="string" resultMap="User">
23 select A.*, B.Name as DeptName
24 from UserInfo A (nolock)
25 left join Dept B (nolock)
26 on A.DeptId = B.Id
27 where A.Name = #value#
28 </select>
29 <select id="GetUserCount" parameterClass="System.Collections.IDictionary" resultClass="int">
30 select count(1)
31 from UserInfo (nolock)
32 </select>
33 <select id="GetUsers" parameterClass="System.Collections.IDictionary" resultMap="User">
34 select *
35 from
36 (
37 select A.*, B.Name as DeptName, row_number() over (order by A.$SortName$ $SortType$) as RowNo
38 from UserInfo A (nolock)
39 left join Dept B (nolock)
40 on A.DeptId = B.Id
41 ) as Rows
42 where RowNo between ($PageSize$ * ($PageIndex$ - 1) + 1) and $PageSize$ * $PageIndex$
43 </select>
44 <select id="HasUser" parameterClass="System.Collections.IDictionary" resultClass="int">
45 select count(1)
46 from UserInfo (nolock)
47 where Name = #Name# and Password = #Password#
48 </select>
49 <insert id="InsertUser" parameterClass="User">
50 <selectKey property="Id" type="post" resultClass="int">
51 select @@identity
52 </selectKey>
53 insert into UserInfo
54 (
55 DeptId,
56 Name,
57 RealName,
58 Password,
59 Email,
60 Sequence,
61 Created,
62 Modified,
63 State
64 )
65 values
66 (
67 #DeptId#,
68 #Name#,
69 #RealName#,
70 #Password#,
71 #Email#,
72 #Sequence#,
73 getdate(),
74 getdate(),
75 #State#
76 )
77 </insert>
78 <delete id="RemoveUser" parameterClass="int">
79 delete
80 from UserInfo
81 where Id = #value#
82 </delete>
83 <update id="UpdateUser" parameterClass="User">
84 update UserInfo
85 set
86 DeptId = #DeptId#,
87 Name = #Name#,
88 RealName = #RealName#,
89 Password = #Password#,
90 Email = #Email#,
91 Sequence = #Sequence#,
92 Modified = getdate(),
93 State = #State#
94 where Id = #Id#
95 </update>
96 </statements>
97 </sqlMap>
  • UserService实现,用到了UserDao中的HasUser方法
View Code
 1 using System.Collections;
2 using System.Collections.Generic;
3 using SpringBird.Erp.Contract;
4 using SpringBird.Erp.Dao;
5 using SpringBird.Erp.Entity;
6
7 namespace SpringBird.Erp.Impl
8 {
9 public class UserService : IUserService
10 {
11 /// <summary>
12 /// 用户Dao
13 /// </summary>
14 public UserDao UserDao
15 {
16 get;
17 set;
18 }
19
20 public User GetUser(string name)
21 {
22 return this.UserDao.GetUser(name);
23 }
24
25 public IList<User> GetUsers(IDictionary parameters)
26 {
27 parameters["RowCount"] = this.UserDao.GetUserCount(parameters);
28
29 return this.UserDao.GetUsers(parameters);
30 }
31
32 public bool Login(string name, string password)
33 {
34 IDictionary parameters = new Hashtable
35 {
36 {"Name", name},
37 {"Password", password}
38 };
39
40 return this.UserDao.HasUser(parameters);
41 }
42
43 public void RemoveUser(int id)
44 {
45 this.UserDao.RemoveUser(id);
46 }
47
48 public void SaveUser(User user)
49 {
50 if (!this.UserDao.UpdateUser(user))
51 {
52 this.UserDao.InsertUser(user);
53 }
54 }
55 }
56 }
  • Mvc中的V:Login.cshtml。视图中用到了jquery1.6.2、jquery easyui1.2.4、jquery validate1.8.1、jquery metadata1.0,其中jquery easyui中所带的验证不好用,用jquery validate替换,依赖于jquery metadata。

   为了更好的用户体验,在输入框中回车时直接登录,这个效果是使用以下脚本完成的。

    $(":input").keydown(function (event) {
                if (event.keyCode == "13" && $("#form").valid()) {
                    $("#form").submit();
                }
            });

View Code
 1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>SpringBird Erp</title>
5 <link rel="stylesheet" type="text/css" href="@Url.Content("~/Themes/site.css")"/>
6 <link rel="stylesheet" type="text/css" href="@Url.Content("~/Themes/default/easyui.css")"/>
7 <link rel="stylesheet" type="text/css" href="@Url.Content("~/Themes/icon/easyui.css")"/>
8 <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.js?v=1.6.2")"></script>
9 <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.easyui.js?v=1.2.4")"></script>
10 <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.easyui.zh_cn.js?v=1.2.4")"></script>
11 <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.metadata.js?v=1.0.0")"></script>
12 <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.js?v=1.8.1")"></script>
13 <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.messages.zh_cn.js?v=1.8.1")"></script>
14 <script type="text/javascript">
15 $(function () {
16 $("#forRememberMe").click(function () {
17 $("#rememberMe").attr("checked", !$("#rememberMe").prop("checked"));
18 });
19
20 $(":input").keydown(function (event) {
21 if (event.keyCode == "13" && $("#form").valid()) {
22 $("#form").submit();
23 }
24 });
25
26 $("#submit").click(function () {
27 if ($("#form").valid()) {
28 $("#form").submit();
29 }
30 });
31 });
32 </script>
33 </head>
34 <body>
35 <div id="login" title="登录" class="easyui-window" iconCls="icon-reload" style="width: 300px; height: auto;">
36 <form id="form" method="post" action="Login">
37 <table class="view">
38 <tr>
39 <td class="name">
40 账号
41 </td>
42 <td class="value">
43 <input id="name" name="Name" type="text" class="{required: true, maxlength: 50}"/>
44 </td>
45 </tr>
46 <tr>
47 <td class="name">
48 密码
49 </td>
50 <td class="value">
51 <input id="password" name="Password" type="password" class="{required: true, maxlength: 50}"/>
52 </td>
53 </tr>
54 <tr>
55 <td class="name">
56 </td>
57 <td class="value">
58 <input id="rememberMe" name="RememberMe" type="checkbox"/>
59 <span id="forRememberMe">自动登录</span>
60 </td>
61 </tr>
62 <tr>
63 <td class="bottom" colspan="2">
64 <a id="submit" class="easyui-linkbutton" iconCls="icon-save" href="javascript:void(0)">登录</a>
65 @Html.ValidationMessage("")
66 </td>
67 </tr>
68 </table>
69 </form>
70 </div>
71 </body>
72 </html>
  • Mvc中的C:HomeController。其中有两个LoginAction,其中有HttpPost的是我们用于接收验证数据的Action。这里使用了Forms窗体验证,Web.config中应增加如下配置

  <system.web>

    <authentication mode="Forms">
          <forms name="SpringBird.Erp" defaultUrl="~/Home" loginUrl="~/Home/Login"/>
        </authentication>
        //禁止匿名用户访问

    <authorization>
              <deny users="?"/>
        </authorization>

  </system.web>

  //允许任何用户访问脚本、样式

  <location path="Scripts">
        <system.web>
            <authorization>
                <allow users="*"/>
            </authorization>
        </system.web>
    </location>
    <location path="Themes">
        <system.web>
            <authorization>
                <allow users="*"/>
            </authorization>
        </system.web>
    </location>

View Code
 1 using System.Collections;
2 using System.Web.Mvc;
3 using System.Web.Security;
4 using SpringBird.Erp.Contract;
5 using SpringBird.Erp.Mvc.Models;
6
7 namespace SpringBird.Erp.Mvc.Controllers
8 {
9 /// <summary>
10 /// 主页控制器
11 /// </summary>
12 public class HomeController : Controller
13 {
14 /// <summary>
15 /// 应用服务
16 /// </summary>
17 public IAppService AppService
18 {
19 get;
20 set;
21 }
22
23 /// <summary>
24 /// 菜单服务
25 /// </summary>
26 public IMenuService MenuService
27 {
28 get;
29 set;
30 }
31
32 /// <summary>
33 /// 用户服务
34 /// </summary>
35 public IUserService UserService
36 {
37 get;
38 set;
39 }
40
41 public ActionResult Index()
42 {
43 IDictionary parameters = new PagingParameters
44 {
45 sort = "Sequence"
46 };
47 this.ViewBag.Apps = this.AppService.GetApps(parameters);
48 this.ViewBag.User = this.UserService.GetUser(this.User.Identity.Name);
49
50 return this.View();
51 }
52
53 public ActionResult List(int id)
54 {
55 IDictionary parameters = new Hashtable
56 {
57 {"AppId", id},
58 {"UserName", this.User.Identity.Name}
59 };
60
61 return this.Json(this.MenuService.GetMenus(parameters), JsonRequestBehavior.AllowGet);
62 }
63
64 public ActionResult Login()
65 {
66 return this.View();
67 }
68
69 [HttpPost]
70 public ActionResult Login(string name, string password, bool? rememberMe, string returnUrl)
71 {
72 ActionResult result = this.View();
73 if (this.UserService.Login(name, password))
74 {
75 FormsAuthentication.SetAuthCookie(name, rememberMe.HasValue && rememberMe.Value);
76 if (string.IsNullOrWhiteSpace(returnUrl))
77 {
78 returnUrl = FormsAuthentication.DefaultUrl;
79 }
80 result = this.Redirect(returnUrl);
81 }
82 else
83 {
84 this.ModelState.AddModelError("", "账号或密码错误");
85 }
86
87 return result;
88 }
89 }
90 }
  • 效果图如下

posted @ 2011-07-23 10:11  IT咨询培训  阅读(2555)  评论(2)    收藏  举报