c# MVC的EXTjs4.1登陆,该源码需要三部分:
第一部分:(1)Module
using System;
using System.Collections;
namespace Model
{
public class userinfo
{
#region 构造函数
public userinfo()
{}
public userinfo(int userid, string username, string password, int roleid, string telephone, string usersex, string address, string email, string logintime, string createtime,int cpid)
{
this._userid=userid;
this._username=username;
this._password=password;
this._roleid=roleid;
this._telephone=telephone;
this._usersex=usersex;
this._address=address;
this._email=email;
this._logintime=logintime;
this._createtime=createtime;
this._cpid = cpid;
}
#endregion
#region 成员
private int _userid;
private string _username;
private string _password;
private int _roleid;
private string _telephone;
private string _usersex;
private string _address;
private string _email;
private string _logintime;
private string _createtime;
private int _cpid;
#endregion
#region 属性
public int userid
{
get { return _userid; }
set { _userid = value; }
}
public string username
{
get { return _username; }
set { _username = value; }
}
public string password
{
get { return _password; }
set { _password = value; }
}
public int roleid
{
get { return _roleid; }
set { _roleid = value; }
}
public string telephone
{
get { return _telephone; }
set { _telephone = value; }
}
public string usersex
{
get { return _usersex; }
set { _usersex = value; }
}
public string address
{
get { return _address; }
set { _address = value; }
}
public string email
{
get { return _email; }
set { _email = value; }
}
public string logintime
{
get { return _logintime; }
set { _logintime = value; }
}
public string createtime
{
get { return _createtime; }
set { _createtime = value; }
}
public int cpid
{
get { return _cpid; }
set { _cpid = value; }
}
#endregion
//返回自动增长的ID
private string ReturnAutoID()
{
return "userid";
}
}
}
(2)DAO
using Model;
using DBUtility;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
namespace DAO
{
public class userinfoDao
{
/// <summary>
/// 得到最大ID
/// </summary>
public int GetMaxId()
{
return DbHelperSQL.GetMaxID("userid", "userinfo");
}
/// <summary>
/// 是否存在该记录
/// </summary>
public bool Exists(int userid)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select count(1) from userinfo");
strSql.Append(" where userid=@userid ");
SqlParameter[] parameters = {
new SqlParameter("@userid", SqlDbType.Int,4)};
parameters[0].Value = userid;
return DbHelperSQL.Exists(strSql.ToString(),parameters);
}
/// <summary>
/// 增加一条数据
/// </summary>
public bool Add_userinfo(userinfo model)
{
if (DbHelperSQL.Insert(model))
return true;
else
return false;
}
/// <summary>
/// 更新一条数据
/// </summary>
public bool Update_userinfo(userinfo model)
{
if (DbHelperSQL.Update(model))
return true;
else
return false;
}
/// <summary>
/// 删除一条数据
/// </summary>
public bool Del_userinfo(userinfo userinfo)
{
if (DbHelperSQL.Delete(userinfo))
return true;
else
return false;
}
/// <summary>
/// 得到一个对象实体
/// </summary>
public userinfo FindById_userinfo(string userid)
{
userinfo model = new userinfo();
DataTable dt=DbHelperSQL.FindByConditions(model, "username='" + userid + "'");
if (dt.Rows.Count <= 0)
return null;
else
{
return Common<userinfo>.Dt2Model(model, dt);
}
}
/// <summary>
/// 获得数据列表
/// </summary>
public IList<userinfo> FindAll_userinfo(string strWhere)
{
return Common<userinfo>.Dt2List(DbHelperSQL.FindByConditions(new userinfo(),strWhere));
}
}
}
(3)业务Bil
sing System;
using System.Drawing;
using System.Web;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace BLL
{
/// 生成验证码的类
///</summary>
public class ValidateCode
{
public ValidateCode()
{
}
///<summary>
/// 验证码的最大长度
///</summary>
public int MaxLength
{
get { return 10; }
}
///<summary>
/// 验证码的最小长度
///</summary>
public int MinLength
{
get { return 1; }
}
///<summary>
/// 生成验证码
///</summary>
///<param name="length">指定验证码的长度</param>
///<returns></returns>
public string CreateValidateCode(int length)
{
int[] randMembers = new int[length];
int[] validateNums = new int[length];
string validateNumberStr = "";
//生成起始序列值
int seekSeek = unchecked((int)DateTime.Now.Ticks);
Random seekRand = new Random(seekSeek);
int beginSeek = (int)seekRand.Next(0, Int32.MaxValue - length * 10000);
int[] seeks = new int[length];
for (int i = 0; i < length; i++)
{
beginSeek += 10000;
seeks[i] = beginSeek;
}
//生成随机数字
for (int i = 0; i < length; i++)
{
Random rand = new Random(seeks[i]);
int pownum = 1 * (int)Math.Pow(10, length);
randMembers[i] = rand.Next(pownum, Int32.MaxValue);
}
//抽取随机数字
for (int i = 0; i < length; i++)
{
string numStr = randMembers[i].ToString();
int numLength = numStr.Length;
Random rand = new Random();
int numPosition = rand.Next(0, numLength - 1);
validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1));
}
//生成验证码
for (int i = 0; i < length; i++)
{
validateNumberStr += validateNums[i].ToString();
}
return validateNumberStr;
}
///<summary>
/// 创建验证码的图片
///</summary>
///<param name="containsPage">要输出到的page对象</param>
///<param name="validateNum">验证码</param>
public byte[] CreateValidateGraphic(string validateCode)
{
Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 12.0), 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的干扰线
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(validateCode, font, brush, 3, 2);
//画图片的前景干扰点
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
//保存图片数据
MemoryStream stream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);
//输出图片流
return stream.ToArray();
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
}
using System.Collections.Generic;
using Model;
using DAO;
/**
* 作者:陈杰
* QQ : 710782046
* Email:ovenjackchain@gmail.com
* Web :http://www.qidian10.com
*/
namespace BLL
{
public class userinfoMgr
{
//一定注意这里的userinfoDao是和Services.xml是对应的,必须相同
userinfoDao userinfoDao = new userinfoDao();
public bool Add_userinfo(userinfo userinfo)
{
return userinfoDao.Add_userinfo(userinfo);
}
public userinfo FindById_userinfo(string id)
{
return userinfoDao.FindById_userinfo(id);
}
public bool Del_userinfo(userinfo userinfo)
{
return userinfoDao.Del_userinfo(userinfo);
}
public bool Update_userinfo(userinfo userinfo)
{
return userinfoDao.Update_userinfo(userinfo);
}
public IList<userinfo> FindAll_userinfo()
{
return userinfoDao.FindAll_userinfo("");
}
}
}
(4)Controllers
using Model;
using DBUtility;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
namespace DAO
{
public class userinfoDao
{
/// <summary>
/// 得到最大ID
/// </summary>
public int GetMaxId()
{
return DbHelperSQL.GetMaxID("userid", "userinfo");
}
/// <summary>
/// 是否存在该记录
/// </summary>
public bool Exists(int userid)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("select count(1) from userinfo");
strSql.Append(" where userid=@userid ");
SqlParameter[] parameters = {
new SqlParameter("@userid", SqlDbType.Int,4)};
parameters[0].Value = userid;
return DbHelperSQL.Exists(strSql.ToString(),parameters);
}
/// <summary>
/// 增加一条数据
/// </summary>
public bool Add_userinfo(userinfo model)
{
if (DbHelperSQL.Insert(model))
return true;
else
return false;
}
/// <summary>
/// 更新一条数据
/// </summary>
public bool Update_userinfo(userinfo model)
{
if (DbHelperSQL.Update(model))
return true;
else
return false;
}
/// <summary>
/// 删除一条数据
/// </summary>
public bool Del_userinfo(userinfo userinfo)
{
if (DbHelperSQL.Delete(userinfo))
return true;
else
return false;
}
/// <summary>
/// 得到一个对象实体
/// </summary>
public userinfo FindById_userinfo(string userid)
{
userinfo model = new userinfo();
DataTable dt=DbHelperSQL.FindByConditions(model, "username='" + userid + "'");
if (dt.Rows.Count <= 0)
return null;
else
{
return Common<userinfo>.Dt2Model(model, dt);
}
}
/// <summary>
/// 获得数据列表
/// </summary>
public IList<userinfo> FindAll_userinfo(string strWhere)
{
return Common<userinfo>.Dt2List(DbHelperSQL.FindByConditions(new userinfo(),strWhere));
}
}
}
第二部分:web部分
(1)login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="Web.View.Login" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>起点10--WebMisDeveloper后台登陆</title>
<link rel="stylesheet" type="text/css" href="../Content/css/userLogin.css" />
<link rel="stylesheet" type="text/css" href="../Content/scripts/extjs/resources/css/ext-all.css" />
<script type="text/JavaScript" src="../Content/scripts/extjs/ext-all.js"></script>
<script type="text/javascript" src="../Content/scripts/extjs/ux/Jxc.checkcode.js"></script>
<script type="text/javascript" src="../Content/scripts/extjs/ux/Toast.js"></script>
<script type="text/javascript" src="../Content/scripts/login/userLogin.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div id='loginDialogId'>
</div>
</form>
</body>
</html>
(2)使用到的一个网上验证码插件
Ext.define('jxc.ui.CheckCode', {
extend: 'Ext.form.field.Base',
alias: 'widget.checkcode',
codeUrl: Ext.BLANK_IMAGE_URL, //生成验证码对应的URL
isLoader: true,
checkcodeWidth: this.checkcodeWidth || 75, //设置验证码宽度
initComponent: function () {
var self = this;
Ext.apply(this, {
listeners: {
afterrender: function () {
this.codeEl = this.bodyEl.createChild({
tag: 'img', title: '点击刷新', src: Ext.BLANK_IMAGE_URL
});
this.inputEl.setWidth(this.width - this.labelWidth - (this.checkcodeWidth + 10)); //这里减掉验证码的宽度
this.codeEl.on('click',this.loadCodeImg,this);
this.codeEl.addCls('x-form-code');
if (this.isLoader) this.loadCodeImg();
}
}
});
this.callParent(arguments);
},
loadCodeImg: function () {
this.codeEl.set({ src: this.codeUrl + '?id=' + Math.random() });
}
})
(3)UserLogin.js
function SetCookie(name, value) {
var Days = 30;
var exp = new Date(); exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
document.cookie = name + '=' + escape(value) + 'expires=' + exp.toGMTString()
};
function getCookie(name) {
var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
if (arr != null) return unescape(arr[2]); return "ext-all.css"
};
Ext.onReady(function () {
var win, form, submitUrl = '/userinfo.mvc/Login';
Ext.util.CSS.swapStyleSheet('theme', '../Content/scripts/extjs/resources/css/' + getCookie("cssSheet"));
var myMask = new Ext.LoadMask(Ext.getBody(), { msg: '正在登录,请稍后......', removeMask: true });
var checkcode = Ext.create('jxc.ui.CheckCode', {
cls: 'key', fieldLabel: '验证码',
name: 'checkcode',
id: 'checkcode',
allowBlank: false,
isLoader: true,
blankText: '验证码不能为空',
codeUrl: '/userinfo.mvc/GetValidateCode',
width: 260
});
var logoPanel = new Ext.Panel({ baseCls: 'x-plain', id: 'login-logo', region: 'center' });
var formPanel = Ext.create('Ext.form.Panel', {
baseCls: 'x-plain', defaults: { width: 230 }, frame: false, height: 120,
id: 'login-form', labelWidth: 120, labelAlign: 'right', region: 'south',
items: [
{
xtype: 'textfield', fieldLabel: "用户名", id: 'UserID', name: 'UserID',
allowBlank: false, blankText: '请填写用户名',
selectOnFocus: true, anchor: '80%',height:30
},
{
xtype: 'textfield', fieldLabel: "密码",
inputType: 'password', name: 'UserPassword',
allowBlank: false, blankText: '请填写密码', selectOnFocus: true, anchor: '80%', height: 30
},
checkcode
]
});
win = Ext.create('Ext.window.Window', {
buttonAlign: 'right', closable: false, draggable: true, height: 320,
id: 'login-win', layout: 'border', minHeight: 300, minWidth: 500, width: 500,
plain: false, resizable: false, title: '依米登陆',
keys: [{
key: [13],
fn:login
}],
items: [logoPanel,
formPanel],
buttons: [{
text: '登陆',
id: 'sure',
handler:login
}, {
text: '重置',
id: 'clear',
handler: reset
}]
});
function reset() {
formPanel.getForm().reset();
};
function login() {
form=formPanel.getForm()
if(form.isValid()){
var formvalue=form.getValues();
myMask.show();
Ext.Ajax.request({
url: '/userinfo.mvc/Login', method: "POST",
params: { userid: formvalue.UserID, password: formvalue.UserPassword, validstr: formvalue.checkcode },
success: function (response, options) {
myMask.hide();
var responseMessage = Ext.JSON.decode(response.responseText);
if (responseMessage.Result) {
form.destroy();
window.location = "backMgr.aspx"
}
else { Ext.Msg.alert("提示", '用户名或密码错误,请你重试!') }
},
failure: function (response, options) {
myMask.hide();
Ext.Msg.alert("提示", '用户名或密码错误,请你重试!')
}
})
}
else{
form.markInvalid();
myMask.hide();
Ext.ux.Toast.msg('登录失败','用户名或密码不能为空,请你重试!')
}
};
win.show();
});
(4)login.css
body {
background-color:#93b0cc;
}
#login-win .x-window-mc {
background:#fff none;
border:1px solid #eee;
}
#login-logo .x-plain-body {
background:#f9f9f9 url(/Content/images/logo.jpg) no-repeat center center;
}
#login-form .x-plain-body {
background:#f9f9f9 none;
color:#222;
padding:5px 35px;
}
#login-win .x-window-body {
background:#FFFFFF none repeat scroll 0%;
border:1px solid #99BBE8;
}
第三部分:完成界面
如果需要下载,请到我的源码空间下载http://download.csdn.net/detail/zwp20002/4807381