码迷,mamicode.com
首页 > 其他好文 > 详细

Admin.Admin/Login --- 后台项目中的管理员及登录模块

时间:2015-09-18 20:11:48      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

 

管理员模块:

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using DIDAO.BLL;
using DIDAO.Common;
using DIDAO.Model;

namespace DIDAO.Admin.Admin
{
    /// <summary>
    /// AdminController 的摘要说明
    /// </summary>
    public class AdminController : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            MyORM_BLL myORM_BLL = new MyORM_BLL();
            string btnSave = context.Request["btnSave"];
            string action = context.Request["action"];
            if (string.IsNullOrWhiteSpace(btnSave))
            {
                if (action == "list") //列表
                {
                    List<object> list = myORM_BLL.SelectModelByField(typeof(TD_ADMIN), "STATUS=1"); //查询正常的管理员
                    RazorHelper.RazorParse(context, "~/Admin/AdminList.cshtml", list);
                }
                else if (action == "addnew") //新增展示
                {
                    TD_ADMIN admin = new TD_ADMIN();
                    RazorHelper.RazorParse(context, "~/Admin/AdminAlter.cshtml", new { action = "addnew", admin = admin });
                }
                else if (action == "edit") //编辑展示
                {
                    #region 编辑展示
                    string idStr = context.Request["id"];
                    int id = VolidHelper.CheckStrToInt(idStr);
                    object obj = myORM_BLL.SelectModelById(typeof(TD_ADMIN), id);
                    if (obj == null)
                    {
                        RazorHelper.RazorParse(context, "~/error.cshtml", new { Msg = "未找到用户:" + id }); //出错
                        return;
                    }
                    TD_ADMIN admin = obj as TD_ADMIN;
                    RazorHelper.RazorParse(context, "~/Admin/AdminAlter.cshtml", new { action="edit",admin=admin});
                    #endregion
                }
                else if (action == "delete") //删除
                {
                    #region 删除
                    string idStr = context.Request["id"];
                    int id = VolidHelper.CheckStrToInt(idStr);
                    bool flag = myORM_BLL.DeleteOrDisableModelByUpdateStatus(typeof(TD_ADMIN),2, id); //status=2
                    if (flag)
                    {
                        context.Response.Redirect("/Admin/AdminController.ashx?action=list");
                    }
                    else
                    {
                        RazorHelper.RazorParse(context, "~/error.cshtml",  new { Msg = "删除操作失败:"+id }); //出错
                    }
                    #endregion
                }
                else if (action == "batchDelete") //批量删除
                {
                    string idStr = context.Request["idStr"].Trim(,);
                    if (!VolidHelper.CheckStringIsAllInt(idStr)) //判断idStr是否每一项都是数字
                    {
                        AjaxHelper.WriteJson(context, "error", "批量禁用失败:id字符串中含有非数字");
                    }
                    bool flag = myORM_BLL.DeleteOrDiableModelByIdList(typeof(TD_ADMIN), 2, idStr);
                    if (!flag)
                    {
                        AjaxHelper.WriteJson(context, "error", "批量禁用失败");
                    }
                    AjaxHelper.WriteJson(context, "ok", "成功");
                }
                else if (action == "disable") //禁用
                {
                    #region 禁用
                    string idStr = context.Request["id"];
                    int id = VolidHelper.CheckStrToInt(idStr);
                    bool flag = myORM_BLL.DeleteOrDisableModelByUpdateStatus(typeof(TD_ADMIN), 3, id); //status=3
                    if (flag)
                    {
                        context.Response.Redirect("/Admin/AdminController.ashx?action=list");
                    }
                    else
                    {
                        RazorHelper.RazorParse(context, "~/error.cshtml", new { Msg = "禁用操作失败:" + id }); //出错
                    }
                    #endregion
                }
                else
                {
                    throw new Exception("action错误:" + action);
                }
            }
            else //保存
            {
                string USERNAME = context.Request["USERNAME"];
                string PASSWORD = context.Request["PASSWORD"];
                string RePASSWORD = context.Request["RePASSWORD"];
                string REALNAME = context.Request["REALNAME"];
                string MOBILE = context.Request["MOBILE"];
                string EMAIL = context.Request["EMAIL"];
                string IDCARD = context.Request["IDCARD"];
                string ADDRESS = context.Request["ADDRESS"];

                #region 验证 非空,长度,是否符合格式
                //验证 非空,长度,是否符合格式
                if (string.IsNullOrWhiteSpace(USERNAME) || USERNAME.Length < 3)
                {
                    AjaxHelper.WriteJson(context, "error", "用户名至少3位");
                    return;
                }
                if (string.IsNullOrWhiteSpace(PASSWORD) || PASSWORD.Length < 6)
                {
                    AjaxHelper.WriteJson(context, "error", "密码至少6位");
                    return;
                }
                if (string.IsNullOrWhiteSpace(RePASSWORD) || RePASSWORD != PASSWORD)
                {
                    AjaxHelper.WriteJson(context, "error", "密码不一致");
                    return;
                }
                if (string.IsNullOrWhiteSpace(REALNAME) || REALNAME.Length < 2)
                {
                    AjaxHelper.WriteJson(context, "error", "必须是真是姓名");
                    return;
                }
                if (string.IsNullOrWhiteSpace(MOBILE) || MOBILE.Length != 11)
                {
                    AjaxHelper.WriteJson(context, "error", "手机号不正确");
                    return;
                }
                if (string.IsNullOrWhiteSpace(EMAIL) || EMAIL.Length < 5)
                {
                    AjaxHelper.WriteJson(context, "error", "邮箱不正确");
                    return;
                }
                if (string.IsNullOrWhiteSpace(IDCARD) || IDCARD.Length != 18)
                {
                    AjaxHelper.WriteJson(context, "error", "身份证不正确");
                    return;
                } 
                #endregion

                TD_ADMIN admin = new TD_ADMIN();
                admin.ADDRESS = ADDRESS;
                admin.DELETEBY = null;
                admin.DELETEDATE = null;
                admin.EMAIL = EMAIL;
                admin.IDCARD = IDCARD;
                admin.MOBILE = MOBILE;
                admin.PASSWORD = CommonHelper.Md5Encode(PASSWORD);
                admin.REALNAME = REALNAME;
                admin.STATUS = 1;
                admin.USERNAME = USERNAME;

                if (action == "addnew") //新增保存
                {
                    #region 新增保存
                    admin.INSERTBY = 1; //当前用户id
                    admin.INSERTDATE = DateTime.Now;
                    admin.LASTEDITBY = null;
                    admin.LASTEDITDATE = null;
                    bool flag = myORM_BLL.InsertModel(admin, "SE_TD_ADMIN");
                    if (flag)
                    {
                        AjaxHelper.WriteJson(context, "ok", "新增成功");
                    }
                    else
                    {
                        RazorHelper.RazorParse(context, "~/error.cshtml", new { Msg = "新增失败:" + USERNAME }); //出错
                    } 
                    #endregion
                }
                else if (action == "edit") //编辑保存
                {
                    #region 编辑保存
                    string idStr = context.Request["id"];
                    int id = VolidHelper.CheckStrToInt(idStr);
                    //根据Id获得实例
                    object obj = myORM_BLL.SelectModelById(typeof(TD_ADMIN), id);
                    if (obj == null)
                    {
                        RazorHelper.RazorParse(context, "~/error.cshtml", new { Msg = "未找到用户:" + id }); //出错
                        return;
                    }
                    admin.ID = id;
                    TD_ADMIN adm = obj as TD_ADMIN;
                    admin.INSERTBY = adm.INSERTBY;
                    admin.INSERTDATE = adm.INSERTDATE;
                    admin.LASTEDITBY = 1;
                    admin.LASTEDITDATE = DateTime.Now;
                    bool flag = myORM_BLL.UpdateModel(admin);
                    if (flag)
                    {
                        AjaxHelper.WriteJson(context, "ok", "编辑成功");
                    }
                    else
                    {
                        RazorHelper.RazorParse(context, "~/error.cshtml", new { Msg = "编辑失败:" + USERNAME }); //出错
                    } 
                    #endregion
                }
                else
                {
                    throw new Exception("action错误:" + action);
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
AdminController.ashx

 

技术分享
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="/js/jquery-1.4.2.min.js"></script>
    <script src="../js/myjs/mapArrayElement.js"></script>
    <script type="text/javascript">
        $(function () {
            $(#btnBatchDelete).click(function () {
                var arrs = $(:checkbox[name="chkIds"]:checked); //数组元素
                //var ids = arrs.map(function () { //值的数组
                //    return $(this).val();
                //});
                //var idStr = "";
                //for (var i = 0; i < ids.length; i++) {
                //    idStr += ids[i]+‘,‘;
                //}
                //idStr = idStr.trim(‘,‘);
                var idStr = mapArrayElement(arrs);

                $.ajax({
                    type: post,
                    dataType:json,
                    url: /Admin/AdminController.ashx?,
                    data: { action: "batchDelete", idStr: idStr },
                    success: function (data) {
                        if (data.status == error) {
                            $(#spMsg).text(data.msg);
                        } else if (data.status == ok) {
                            window.location.href = /Admin/AdminController.ashx?action=list;
                        } else {
                            alert(服务器错误:+data.status);
                        }
                    },
                    error: function () {
                        alert(服务器错误);
                    }
                });
            });
        })
    </script>
</head>
<body>
    <div>
        <a href="AdminController.ashx?action=addnew">新增</a>
        <table>
            <thead>
                <tr><th>用户名</th><th>真实姓名</th><th>手机</th><th>邮箱</th><th>身份证</th><th>地址</th><th></th><th></th></tr>
            </thead>
            <tbody>
                @foreach(object obj in Model){
                    DIDAO.Model.TD_ADMIN admin = obj as DIDAO.Model.TD_ADMIN;
                    <tr>
                        <td><input type="checkbox" name="chkIds" value="@admin.ID" /></td>
                        <td>@admin.USERNAME</td><td>@admin.REALNAME</td><td>@admin.MOBILE</td><td>@admin.EMAIL</td><td>@admin.IDCARD</td><td>@admin.ADDRESS</td>
                        <td><a href="AdminController.ashx?action=edit&id=@admin.ID">编辑</a></td>
                        <td><a href="AdminController.ashx?action=delete&id=@admin.ID" onclick="return confirm(‘您确定要删除吗?‘)">删除</a></td>
                        <td><a href="AdminController.ashx?action=disable&id=@admin.ID" onclick="return confirm(‘您确定要禁用吗?‘)">禁用</a></td>
                    </tr>
                    }
            </tbody>
            <tfoot>
                <tr><td><input type="button" id="btnBatchDelete" value="批量删除" /></td><td><span id="spMsg"></span></td></tr>
            </tfoot>
        </table>
    </div>
</body>
</html>
AdminList.cshtml

 

技术分享
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="/js/jquery-1.4.2.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $(#btnSave).click(function () {
                var action = $(#action).val().trim();
                var id = $(#id).val().trim();
                var USERNAME = $(#USERNAME).val().trim();
                var PASSWORD = $(#PASSWORD).val().trim();
                var RePASSWORD = $(#RePASSWORD).val().trim();
                var REALNAME = $(#REALNAME).val().trim();
                var MOBILE = $(#MOBILE).val().trim();
                var EMAIL = $(#EMAIL).val().trim();
                var IDCARD = $(#IDCARD).val().trim();
                var ADDRESS = $(#ADDRESS).val().trim();
                //验证 非空,长度,是否符合格式
                $(#USERNAME).blur(function () {
                    if (USERNAME.length < 3) {
                        $(#spUSERNAME).text(用户名须不少于3个字符);
                        return;
                    }
                    $(#spUSERNAME).text(‘‘);
                });
                
                $(#PASSWORD).blur(function () {
                    if (PASSWORD.length < 6) {
                        $(#spPASSWORD).text(密码须不少于6个字符);
                        return;
                    }
                    $(#spPASSWORD).text(‘‘);
                });
                
                $(#RePASSWORD).blur(function () {
                    if (RePASSWORD != PASSWORD) {
                        $(#spRePASSWORD).text(两次密码不一致);
                        return;
                    }
                    $(#spRePASSWORD).text(‘‘);
                });
                
                $(#REALNAME).blur(function () {
                    if (REALNAME.length < 2) {
                        $(#spREALNAME).text(真实姓名必须填写);
                        return;
                    }
                    $(#spREALNAME).text(‘‘);
                });
               
                $(#MOBILE).blur(function () {
                    if (MOBILE.length != 11) {
                        $(#spMOBILEE).text(手机号必须是11位);
                        return;
                    }
                    $(#spMOBILEE).text(‘‘);
                });

                $(#EMAIL).blur(function () {
                    if (EMAIL.length < 5) {
                        $(#spEMAIL).text(邮箱必须填写);
                        return;
                    }
                    $(#spEMAIL).text(‘‘);
                });

                $(#IDCARD).blur(function () {
                    if (IDCARD.length != 18) {
                        $(#spIDCARD).text(身份证必须18位);
                        return;
                    }
                    $(#spIDCARD).text(‘‘);
                });

                var btnSave = $(#btnSave).val();

                $.ajax({
                    type: post,
                    dataType: json,
                    url: /Admin/AdminController.ashx,
                    data: {
                        action:action,id:id, USERNAME: USERNAME, PASSWORD: PASSWORD, RePASSWORD: RePASSWORD, REALNAME: REALNAME, MOBILE: MOBILE, EMAIL: EMAIL,
                        IDCARD: IDCARD, ADDRESS: ADDRESS, btnSave: btnSave
                    },
                    success: function (data) {
                        if (data.status == error) {
                            $(#spMsg).text(data.msg);
                        }
                        else if (data.status == ok) {
                            window.location.href = /Admin/AdminController.ashx?action=list;
                        } else {
                            alert(服务器错误:+data.status);
                        }
                    },
                    error: function () {
                        alert(服务器错误);
                    }
                });
            });
        })
    </script>
</head>
<body>
    <div>
        <form id="form1">
        <table>
            <tr><td></td><td><input type="hidden" id="action" value="@Model.action" /></td></tr>
            <tr><td></td><td><input type="hidden" id="id" value="@Model.admin.ID" /></td></tr>
            <tr><td>用户名</td><td><input type="text" id="USERNAME" value="@Model.admin.USERNAME" /></td><td><span id="spUSERNAME"></span></td></tr>
            <tr><td>密码</td><td><input type="password" id="PASSWORD"  /></td><td><span id="spPASSWORD"></span></td></tr>
            <tr><td>重复密码</td><td><input type="password" id="RePASSWORD"  /></td><td><span id="spRePASSWORD"></span></td></tr>
            <tr><td>真实姓名</td><td><input type="text" id="REALNAME" value="@Model.admin.REALNAME" /></td><td><span id="spREALNAME"></span></td></tr>
            <tr><td>手机</td><td><input type="text" id="MOBILE" value="@Model.admin.MOBILE" /></td><td><span id="spMOBILE"></span></td></tr>
            <tr><td>邮箱</td><td><input type="text" id="EMAIL" value="@Model.admin.EMAIL" /></td><td><span id="spEMAIL"></span></td></tr>
            <tr><td>身份证</td><td><input type="text" id="IDCARD" value="@Model.admin.IDCARD" /></td><td><span id="spIDCARD"></span></td></tr>
            <tr><td>地址</td><td><input type="text" id="ADDRESS" value="@Model.admin.ADDRESS" /></td></tr>
            <tr><td><input type="button" id="btnSave" value="保存" /></td><td><span id="spMsg"></span></td></tr>
        </table>
        </form>
    </div>
</body>
</html>
AdminAlter.cshtml

 

 

登录模块:

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using DIDAO.Admin.Common;
using DIDAO.BLL;
using DIDAO.Common;
using DIDAO.Model;

namespace DIDAO.Admin.Login
{
    /// <summary>
    /// LoginController 的摘要说明
    /// </summary>
    public class LoginController : IHttpHandler,IRequiresSessionState
    {
        MyORM_BLL myORM_BLL = new MyORM_BLL();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string action = context.Request["action"];
            if(action=="index") //登录展示
            {
                #region 登录展示
                //看看 是否自动登录,是的话自动登录,否则继续
                //看看 是否记住密码,是的话填充,否则继续
                string username, password;
                ConstStringHelper.AutoLoginResult autoLoginResult = LoginHelper.TryAutoLoginOrMemoryPwd(context, out username, out password);
                switch (autoLoginResult)
                {
                    case ConstStringHelper.AutoLoginResult.AutoLogin:
                        context.Response.Redirect("/index.html"); break;
                    case ConstStringHelper.AutoLoginResult.MemoryPwd:
                        RazorHelper.RazorParse(context, "~/Login/Login.cshtml", new { username = username, password = password }); break;
                    case ConstStringHelper.AutoLoginResult.NO:
                        RazorHelper.RazorParse(context, "~/Login/Login.cshtml", new { username = "", password = "" }); break;
                    default: throw new Exception("服务器错误:未知的" + autoLoginResult);
                } 
                #endregion
            }
            else if (action == "login") //登录
            {
                #region 登录
                //获得请求
                string username = context.Request["username"];
                string password = context.Request["password"];
                string validCode = context.Request["validCode"];
                string chkMemoryPwd = context.Request["chkMemoryPwd"];
                string chkAutoLogin = context.Request["chkAutoLogin"];
                //验证 非空及验证码 
                #region 验证 非空
                if (string.IsNullOrWhiteSpace(username) || username.Length < 3)
                {
                    AjaxHelper.WriteJson(context, "error", "用户名长度要大于3个字符");
                    return;
                }

                if (string.IsNullOrWhiteSpace(password) || password.Length < 6)
                {
                    AjaxHelper.WriteJson(context, "error", "密码长度要大于6个字符");
                    return;
                }

                if (string.IsNullOrWhiteSpace(validCode) || validCode.Length != 4)
                {
                    AjaxHelper.WriteJson(context, "error", "验证码必须是4个字符");
                    return;
                }
                #endregion
                if (!LoginHelper.CheckValidCode(context, validCode))
                {
                    GenerateValidCode.ReGenerateValidCode(context); //避免用户修改验证码id 使验证码不发生变化也可以登录,正常情况使用不到
                    AjaxHelper.WriteJson(context, "error", "验证码错误");
                    return;
                }
                //验证 用户及密码
                if (!CommonHelper.CheckStringIsSpecialChar(username))
                {
                    AjaxHelper.WriteJson(context, "error", "用户名中含有特殊字符");
                    return;
                }
                ConstStringHelper.LoginResult result = LoginHelper.CheckLoginStatus(context, username, password);
                if (result == ConstStringHelper.LoginResult.UserNameNotExist)
                {
                    AjaxHelper.WriteJson(context, "error", "用户名不存在");
                    return;
                }
                else if (result == ConstStringHelper.LoginResult.PasswordError)
                {
                    AjaxHelper.WriteJson(context, "error", "密码错误");
                    return;
                }
                else if (result == ConstStringHelper.LoginResult.OK) //成功
                {
                    if (chkMemoryPwd == "true" || chkAutoLogin == "true")
                    {
                        //存入cookie
                        LoginHelper.StoreCookie(context, chkMemoryPwd, chkAutoLogin, username, password);
                    }
                    AjaxHelper.WriteJson(context, "ok", "登录成功");
                    return;
                }
                else
                {
                    throw new Exception("不存在这样的枚举结果:ConstStringHelper.LoginResult:" + result);
                } 
                #endregion
            }
            else if(action=="loginout") //登录退出
            {
                #region 登录退出
                //只将session释放
                context.Session.Abandon(); 
                #endregion
            }
            else if(action=="exit") //完全退出
            {
                #region 完全退出
                //取消会话
                context.Session.Abandon();
                //消除cookie
                HttpCookie chkMemoryPwd = context.Request.Cookies[ConstStringHelper.ADMINCOOKIE_CHKMEMORYPWD];
                if (chkMemoryPwd != null)
                {
                    chkMemoryPwd.Expires.AddDays(-1);
                }
                HttpCookie chkAutoLogin = context.Request.Cookies[ConstStringHelper.ADMINCOOKIE_CHKAUTOLOGIN];
                if (chkAutoLogin != null)
                {
                    chkAutoLogin.Expires.AddDays(-1);
                }
                HttpCookie username = context.Request.Cookies[ConstStringHelper.ADMINCOOKIE_USERNAME];
                if (username != null)
                {
                    username.Expires.AddDays(-1);
                }
                HttpCookie password = context.Request.Cookies[ConstStringHelper.ADMINCOOKIE_PASSWORD];
                if (password != null)
                {
                    password.Expires.AddDays(-1);
                } 
                #endregion
            }
            else
            {
                throw new Exception("action错误:"+action);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
LoginController.ashx

 

技术分享
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using DIDAO.Common;

namespace DIDAO.Admin.Login
{
    /// <summary>
    /// GenerateValidCode 的摘要说明
    /// </summary>
    public class GenerateValidCode : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";
            string code = ReGenerateValidCode(context);
            Random ran = new Random();

            //画图
            using(Bitmap bmp=new Bitmap(70,30))
            {
                using(Graphics g=Graphics.FromImage(bmp))
                using(Font font=new Font(new FontFamily("微软雅黑"),13))
                {
                    g.DrawString(code, font, Brushes.White, new PointF(0, 0));
                    for (int i = 0; i < 100;i++ ) //画100个点
                    {
                        int x = ran.Next(0, 65);
                        int y = ran.Next(0, 30);
                        g.DrawLine(Pens.White, new PointF(x, y), new PointF(x, y));
                    }
                }
                bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);
            }

        }

        /// <summary>
        /// 重复产生验证码
        /// </summary>
        /// <param name="context"></param>
        /// <returns>验证码</returns>
        public static string ReGenerateValidCode(HttpContext context)
        {
            //常用汉字
            string cc = @"寿弄麦形进戒吞远违运扶抚坛技坏扰拒找批扯址走抄坝贡攻赤折抓扮抢孝均抛投坟坑抗坊抖护壳志块扭声把报却劫芽花芹芬苍芳严芦劳克苏杆杜杠材村杏极李杨求更束豆两丽医辰励否还歼来连步坚旱盯呈时吴助县里呆园旷围呀吨足邮男困吵串员听吩吹呜吼吧别岗帐财钉针告我乱利秃秀私每兵估体何但伸作伯伶佣低你住位伴身皂佛近彻役返余希坐谷妥含邻岔肝肚肠龟免狂犹角删条卵岛迎饭饮系言冻状亩况床库疗应冷这序辛弃冶忘闲间闷判灶灿弟汪沙汽沃泛沟没沈沉怀忧快完宋宏牢究穷灾良证启评补初社识诉诊词译君灵即层尿尾迟局改张忌际陆阿陈阻附妙妖妨努忍劲鸡驱纯纱纲纳纵驳纷纸纹纺驴纽";
            Random ran = new Random();
            string str = "";
            for (int i = 0; i < 4; i++)
            {
                int index = ran.Next(0, cc.Length);
                str += cc[index];
            }
            context.Session[ConstStringHelper. ADMINSESSION_VALIDCODE] = str;
            return str;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
GenerateValidCode.ashx

 

技术分享
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="/js/jquery-1.4.2.min.js"></script>
    <script type="text/javascript">
        $(function () {
            //刷新验证码
            function refreshValidCode() {
                $(#imgValidCode).attr(src, GenerateValidCode.ashx? + new Date());
            }
            //刷新验证码
            $(#imgValidCode).click(function () {
                refreshValidCode();
            });

            $(#btnLogin).click(function () {
                var username = $(#username).val().trim();
                var password = $(#password).val().trim();
                var validCode = $(#validCode).val().trim();
                var chkMemoryPwd = $(#chkMemoryPwd).attr(checked);
                var chkAutoLogin = $(#chkAutoLogin).attr(checked);
                //验证 非空、长度、格式等
                $(#username).blur(function () {
                    if ($(this).val().length < 3) {
                        $(#spusername).text(用户名长度要大于3个字符);
                        refreshValidCode();
                    }
                    $(#spusername).text(‘‘);
                });
                $(#password).blur(function () {
                    if ($(this).val().length < 6) {
                        $(#sppassword).text(密码长度要大于6个字符);
                        refreshValidCode();
                    }
                    $(#sppassword).text(‘‘);
                });
                $(#validCode).blur(function () {
                    if ($(this).val().length !=4) {
                        $(#spvalidCode).text(验证码必须是4个字符);
                        refreshValidCode();
                    }
                    $(#spvalidCode).text(‘‘);
                });
                $.ajax({
                    type: post,
                    dataType: json,
                    url: /Login/LoginController.ashx,
                    data: { username: username, password: password, validCode: validCode, chkMemoryPwd: chkMemoryPwd, chkAutoLogin: chkAutoLogin, action: "login" },
                    success: function (data) {
                        if (data.status == error) {
                            $(#spMsg).text(data.msg);
                            refreshValidCode();
                        } else if (data.status == ok) {
                            window.location.href = /index.html;
                        } else {
                            alert(服务器错误: + data.status);
                            refreshValidCode();
                        }
                    },
                    error: function () {
                        alert(服务器错误);
                    }
                });

            });
        })
    </script>
</head>
<body>
    <div>
        <table>
            <tr><td>用户名</td><td><input type="text" id="username" value="@Model.username" /></td><td><span id="spusername"></span></td></tr>
            <tr><td>密码</td><td><input type="password" id="password" value="@Model.password" /></td><td><span id="sppassword"></span></td></tr>
            <tr><td><img id="imgValidCode" src="GenerateValidCode.ashx" /></td><td><input type="text" id="validCode" /></td><td><span id="spvalidCode"></span></td></tr>
            <tr><td><input type="checkbox" id="chkMemoryPwd" />记住密码</td><td><input type="checkbox" id="chkAutoLogin" /><label for="chkAutoLogin">自动登录</label></td></tr>
            <tr><td></td><td><input type="button" id="btnLogin" value="登录" /></td><td><span id="spMsg"></span></td></tr>
        </table>
    </div>
</body>
</html>
Login.cshtml

 

Admin.Admin/Login --- 后台项目中的管理员及登录模块

标签:

原文地址:http://www.cnblogs.com/adolphyang/p/4820072.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!