Example #1
0
 public override void OnException(ExceptionContext filterContext)
 {
     if (!filterContext.ExceptionHandled)
     {
         Exception            ex  = filterContext.Exception;
         Model.Entites.SysLog log = new Model.Entites.SysLog();
         log.CreateOn = DateTime.Now;
         log.ErrorMsg = ex.StackTrace;
         OperateContext.Current.BLLSession.ISysLogBLL.Add(log);
     }
     base.OnException(filterContext);
 }
        /// <summary>
        /// 前台用户登录方法
        /// </summary>
        /// <param name="userInfo">登录用户信息</param>
        /// <param name="type">1、手机号 2、微信</param>
        /// <returns>登录是否成功</returns>
        public bool UserLogin(Model.Entites.UserInfoView loginUser, int type, bool isAutoLogin)
        {
            try
            {
                Model.Entites.UserInfoView userInfo = null;

                if (type == 1)
                {
                    userInfo = OperateContext.Current.BLLSession.IUserInfoViewBLL.GetListBy(m => m.PhoneAccount == loginUser.PhoneAccount).FirstOrDefault();
                }
                else if (type == 2)
                {
                    userInfo = OperateContext.Current.BLLSession.IUserInfoViewBLL.GetListBy(m => m.WeixinUnionid == loginUser.WeixinUnionid).FirstOrDefault();
                }
                else if (type == 3)
                {
                    userInfo = OperateContext.Current.BLLSession.IUserInfoViewBLL.GetListBy(h => h.LoginAccount == loginUser.LoginAccount && h.LoginPwd == loginUser.LoginPwd).FirstOrDefault();
                }

                if (userInfo == null)
                {
                    string userToken     = null;
                    string phoneAccount  = null;
                    string weixinAccount = null;

                    if (type == 1)
                    {
                        phoneAccount = loginUser.PhoneAccount;
                    }
                    else if (type == 2)
                    {
                        weixinAccount = loginUser.WeixinUnionid;
                    }

                    if (isAutoLogin)
                    {
                        userToken = Guid.NewGuid().ToString();
                    }

                    // 用户账户信息
                    Model.Entites.UserAccount userAccount = new Model.Entites.UserAccount()
                    {
                        PhoneAccount  = phoneAccount,
                        WeixinUnionid = weixinAccount,
                        State         = 0,
                        CreateTime    = DateTime.Now,
                        UserToken     = userToken
                    };

                    OperateContext.Current.BLLSession.IUserAccountBLL.Add(userAccount);

                    // 用户信息
                    if (loginUser.Nikename != null && loginUser.Nikename.Length > 20)
                    {
                        loginUser.Nikename = loginUser.Nikename.Substring(0, 20);
                    }
                    if (loginUser.Username != null && loginUser.Username.Length > 20)
                    {
                        loginUser.Username = loginUser.Username.Substring(0, 20);
                    }

                    string nikename = String.IsNullOrEmpty(loginUser.Nikename) ? "我要去度假用户" + userAccount.ID : loginUser.Nikename;
                    string username = String.IsNullOrEmpty(loginUser.Username) ? "我要去度假用户" + userAccount.ID : loginUser.Username;

                    OperateContext.Current.BLLSession.IUserInfoBLL.Add(new Model.Entites.UserInfo()
                    {
                        AccountID  = userAccount.ID,
                        PhoneNo    = phoneAccount,
                        Gender     = loginUser.Gender == 1 ? 0 : loginUser.Gender == 2 ? 1 : 2,
                        UserType   = 1,
                        Img        = loginUser.Img,
                        CreateTime = DateTime.Now,
                        IsRealName = 0,
                        Nikename   = nikename,
                        Username   = username
                    });
                }

                if (type == 1)
                {
                    userInfo = OperateContext.Current.BLLSession.IUserInfoViewBLL.GetListBy(m => m.PhoneAccount == loginUser.PhoneAccount).FirstOrDefault();
                }
                else if (type == 2)
                {
                    userInfo = OperateContext.Current.BLLSession.IUserInfoViewBLL.GetListBy(m => m.WeixinUnionid == loginUser.WeixinUnionid).FirstOrDefault();
                }
                else if (type == 3)
                {
                    userInfo = OperateContext.Current.BLLSession.IUserInfoViewBLL.GetListBy(h => h.LoginAccount == loginUser.LoginAccount && h.LoginPwd == loginUser.LoginPwd).FirstOrDefault();
                }
                if (userInfo != null)
                {
                    // 登录成功 将登录信息存入Session
                    UserInfo = userInfo;

                    if (isAutoLogin)
                    {
                        HttpCookie cookie = new HttpCookie(USER_KEY);
                        cookie.Expires = DateTime.Now.AddMonths(1);
                        cookie.Value   = Newtonsoft.Json.JsonConvert.SerializeObject(userInfo);

                        OperateContext.Current.Response.Cookies.Add(cookie);
                    }

                    return(true);
                }
            }
            catch (Exception ex)
            {
                Model.Entites.SysLog log = new Model.Entites.SysLog();
                log.CreateOn = DateTime.Now;
                log.ErrorMsg = ex.Message;
                OperateContext.Current.BLLSession.ISysLogBLL.Add(log);
            }

            //尝试子账号登录
            var childUser = OperateContext.Current.BLLSession.IUserBLL.GetListBy(s => s.IsDeleted == false && s.LoginName == loginUser.LoginAccount && s.Password == loginUser.LoginPwd);

            if (childUser.Count > 0)
            {
                var userInfo = childUser.FirstOrDefault();
                Session[CHILDUSER_KEY] = userInfo;
                //获取主账号信息
                var account = OperateContext.Current.BLLSession.IUserBLL.GetListBy(s => s.IsDeleted == false && s.Id == userInfo.ParentId).FirstOrDefault();
                UserInfo = OperateContext.Current.BLLSession.IUserInfoViewBLL.GetListBy(s => s.ID == account.AccountId).FirstOrDefault();
                if (isAutoLogin)
                {
                    HttpCookie cookie = new HttpCookie(CHILDUSER_KEY);
                    cookie.Expires = DateTime.Now.AddMonths(1);
                    cookie.Value   = Newtonsoft.Json.JsonConvert.SerializeObject(userInfo);

                    OperateContext.Current.Response.Cookies.Add(cookie);
                }

                return(true);
            }
            return(false);
        }