Asp.net MVC利用knockoutjs实现登陆并记录用户的内外(2)

var viewModel = function () { var self = this; this.form = { usercode: ko.observable(), password: ko.observable(), remember:ko.observable(false), ip: null, city: null }; this.message = ko.observable(); this.loginClick = function (form) { $.ajax({ type: "POST", url: "/login/doAction", data: ko.toJSON(self.form), dataType: "json", contentType: "application/json", success: function (d) { if (d.status == 'success') { self.message("登陆成功正在跳转,请稍候..."); window.location.href = 'https://www.jb51.net/'; } else { self.message(d.message); } }, error: function (e) { self.message(e.responseText); }, beforeSend: function () { $(form).find("input").attr("disabled", true); self.message("正在登陆处理,请稍候..."); }, complete: function () { $(form).find("input").attr("disabled", false); } }); }; this.resetClick = function () { self.form.usercode(""); self.form.password(""); self.form.remember(false); }; this.init = function () { self.form.ip = ILData[0]; $.getJSON("http://api.map.baidu.com/location/ip?ak=F454f8a5efe5e577997931cc01de3974&callback=?", function (d) { self.form.city = d.content.address; }); if (top != window) top.window.location = window.location; }; this.init(); }; $(function () { ko.applyBindings(new viewModel());});

定义viewModel,其属性包括from表单信息,message提示信息,loginClick登陆,resetClick重置。其中的init部分其实可以不放到viewModel中。

1、$.getJSON即为JSONP的访问,其中加上了参数callback=?,jQuery会自动处理成当前的回调函数,即跨域成功后会自动回调当前函数并传入数据。我们用viewModel中的form.city接收请求的数据中的城市信息。

2、最后一句ko.applyBindings(new viewModel())即实现了页面和viewModel的绑定,至此,前台全部完成。接下来写登陆处理doAction,还是放在LoginController中,访问地址为/login/doAction。

第四步:在LoginController中添加doAction的方法返回JSON数据。代码如下:

public JsonResult DoAction(JObject request) { var message = new sys_userService().Login(request); return Json(message, JsonRequestBehavior.DenyGet); }

然后在service层中处理

using System; using System.Collections.Generic; using Zephyr.Core; using System.Dynamic; using Newtonsoft.Json.Linq; using Newtonsoft.Json; using Zephyr.Utils; using Zephyr.Web.Areas.Mms.Common; namespace Zephyr.Models { public class sys_userService : ServiceBase<sys_user> { public object Login(JObject request) { var UserCode = request.Value<string>("usercode"); var Password = request.Value<string>("password"); //用户名密码检查 if (String.IsNullOrEmpty(UserCode) || String.IsNullOrEmpty(Password)) return new { status = "error", message = "用户名或密码不能为空!" }; //用户名密码验证 var result = this.GetModel(ParamQuery.Instance() .AndWhere("UserCode", UserCode) .AndWhere("Password", Password) .AndWhere("IsEnable", true)); if (result == null || String.IsNullOrEmpty(result.UserCode)) return new { status = "error", message = "用户名或密码不正确!" }; //调用框架中的登陆机制 var loginer = new LoginerBase { UserCode = result.UserCode, UserName = result.UserName }; FormsAuth.SignIn(loginer.UserCode, loginer, 60 * 8); //登陆后处理 this.UpdateUserLoginCountAndDate(UserCode); //更新用户登陆次数及时间 this.AppendLoginHistory(request); //添加登陆履历 MmsService.LoginHandler(request); //MMS系统的其它的业务处理 //返回登陆成功 return new { status = "success", message = "登陆成功!" }; } //更新用户登陆次数及时间 public void UpdateUserLoginCountAndDate(string UserCode) { db.Sql(@" update sys_user set LoginCount = isnull(LoginCount,0) + 1 ,LastLoginDate = getdate() where UserCode = @0 " , UserCode).Execute(); } //添加登陆履历 public void AppendLoginHistory(JObject request) { var lanIP = ZHttp.ClientIP; var hostName = ZHttp.IsLanIP(lanIP) ? ZHttp.ClientHostName : string.Empty; //如果是内网就获取,否则出错获取不到,且影响效率 var UserCode = request.Value<string>("usercode"); var UserName = MmsHelper.GetUserName(); var IP = request.Value<string>("ip"); var City = request.Value<string>("city"); if (IP != lanIP) IP = string.Format("{0}/{1}", IP, lanIP).Trim('https://www.jb51.net/').Replace("::1", "localhost"); var item = new sys_loginHistory(); item.UserCode = UserCode; item.UserName = UserName; item.HostName = hostName; item.HostIP = IP; item.LoginCity = City; item.LoginDate = DateTime.Now; db.Insert<sys_loginHistory>("sys_loginHistory", item).AutoMap(x => x.ID).Execute(); } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdpssp.html