public JsonResult Check(AppTypeFlag?AppType, bool?Shopkeeper)  //AppType等所以App版本更新后启用【必传不允许空】
        {
            OperationResult oper = new OperationResult(OperationResultType.Error);

            try
            {
                string strName                = Request["AdminName"];
                string strPassword            = Request["AdminPass"];
                string strJpushRegistrationID = Request["JPushRegistrationID"];
                if (string.IsNullOrEmpty(strName))
                {
                    oper.Message = "请填写登录名或者手机号码";
                    return(Json(oper));
                }
                if (string.IsNullOrEmpty(strPassword))
                {
                    oper.Message = "请填写密码";
                    return(Json(oper));
                }

                //if (dtoinfo.IsNull() || dtoinfo.AppVersion.IsNullOrEmpty() || dtoinfo.DeviceToken.IsNullOrEmpty())
                //{
                //    oper.Message = "登录失败,参数丢失";
                //    return Json(oper);
                //}

                Administrator dto = new Administrator();
                dto.Member              = new Member();
                dto.Member.MemberName   = strName;
                dto.Member.MobilePhone  = strName;
                dto.Member.MemberPass   = strPassword;
                dto.JPushRegistrationID = strJpushRegistrationID;
                oper = _administratorContract.CheckLogin(dto);
                if (oper.ResultType == OperationResultType.Success)
                {
                    var entity = (Administrator)oper.Data;

                    // 查找部门下的店铺
                    var store = entity.Department.Stores.FirstOrDefault();

                    var storeId   = 14;
                    var storeName = "零时尚";

                    if (store != null)
                    {
                        //部门没有归属店铺,就不需要统计
                        storeId   = store.Id;
                        storeName = store.StoreName;
                    }

                    //蝶掌柜登录权限判断,App全部更新后需要弃用
                    if (!ConfigurationHelper.IsDevelopment())
                    {
                        if (Shopkeeper.HasValue)
                        {
                            if (!entity.JobPosition.AppVerManages.Any(a => a.AppType == AppTypeFlag.碟掌柜 && a.IsEnabled && !a.IsDeleted))
                            {
                                oper = new OperationResult(OperationResultType.LoginError, "无权登录");
                                return(Json(oper));
                            }
                        }

                        if (AppType.HasValue)
                        {
                            if (!entity.JobPosition.AppVerManages.Any(a => a.IsEnabled && !a.IsDeleted && a.AppType == AppType))
                            {
                                return(Json(new OperationResult(OperationResultType.Error, $"没有登录 {AppType + ""} 的权限")));
                            }
                        }
                    }


                    DateTime expiration = DateTime.Now.Add(FormsAuthentication.Timeout);
                    var      userData   = new { Id                  = entity.Id, AdminName = entity.Member.MemberName, RealName = entity.Member.RealName,
                                                JPushRegistrationID = strJpushRegistrationID, };
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, (userData.AdminName + userData.Id.ToString()), DateTime.Now, expiration, false, userData.ToJsonString(), FormsAuthentication.FormsCookiePath);
                    var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket))
                    {
                        HttpOnly = true,
                        Secure   = FormsAuthentication.RequireSSL,
                        Domain   = FormsAuthentication.CookieDomain,
                        Path     = FormsAuthentication.FormsCookiePath,
                    };
                    Response.Cookies.Remove(cookie.Name);
                    Response.Cookies.Add(cookie);
                    //oper.Data = null;
                    bool EnablePhone = false;

                    if (entity.Member.IsNotNull() && !entity.Member.MobilePhone.IsNullOrEmpty())
                    {
                        EnablePhone = true;
                    }

                    oper = _administratorContract.GetWorkTime(entity.Id);
                    WorkTime workTime = new WorkTime();
                    if (oper.ResultType == OperationResultType.Success)
                    {
                        workTime = oper.Data as WorkTime;
                        workTime = OfficeHelper.CheckworkTime(workTime);
                    }
                    oper.Data = new
                    {
                        entity.Id,
                        EnablePhone,
                        workTime.AmStartTime,
                        workTime.PmEndTime,
                        workTime.IsVacations,
                        workTime.WorkWeek,
                        storeId,
                        storeName,
                        entity.IsPersonalTime
                    };
                    #region 添加设置标签Jpush使用
                    if (!userData.JPushRegistrationID.IsNullOrEmpty())
                    {
                        JPushClient jclient = new JPushClient(JpushApi.app_key, JpushApi.master_secret);
                        var         dresult = jclient.updateDeviceTagAlias(userData.JPushRegistrationID, userData.Id.ToString(), null, new HashSet <string>()
                        {
                            "yuangong"
                        }, null);
                        //oper.Other = dresult;
                    }
                    #endregion
                }
                return(Json(oper, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                oper.Message = "服务器忙,请稍后重试";
                _Logger.Error <string>(ex.ToString());
                return(Json(oper, JsonRequestBehavior.AllowGet));
            }
        }
Esempio n. 2
0
        public ActionResult Check(Administrator dto, bool Remembered = false)
        {
            OperationResult result          = new OperationResult(OperationResultType.Error, "页面失效,刷新重试");
            var             _tc_key_session = HttpContext.Session["_tc_key"];
            var             _tc_key         = HttpContext.Request.Cookies["_tc_key"];

            if (_tc_key_session.IsNotNull() || _tc_key.IsNotNull())
            {
                var key = string.Empty;
                if (_tc_key_session.IsNotNull())
                {
                    key = _tc_key_session.ToString();
                }
                else if (_tc_key.IsNotNull())
                {
                    var ticket = FormsAuthentication.Decrypt(_tc_key.Value);
                    key = ticket.IsNotNull() ? ticket.UserData : string.Empty;
                }
                dto.Member.MemberPass = AesHelper.Decrypt(dto.Member.MemberPass, key).TrimEnd('\0');
                result = _administratorContract.CheckLogin(dto);
                if (result.ResultType == OperationResultType.Success)
                {
                    var entity     = (Administrator)result.Data;
                    var timeoutDay = Utils.GetCheckLoginTimeOutDay();

                    var attTime = _attendanceContract.Attendances.Where(w => w.IsEnabled && !w.IsDeleted && w.AdminId == entity.Id).OrderByDescending(o => o.AttendanceTime).Select(s => s.AttendanceTime).FirstOrDefault();

                    if (!ConfigurationHelper.IsDevelopment())
                    {
                        if (entity?.JobPosition?.CheckLogin == true && attTime < DateTime.Now.Date.AddDays(-Math.Abs(timeoutDay)))
                        {
                            if (!ConfigurationHelper.IsDevelopment())
                            {
                                return(Json(new OperationResult(OperationResultType.LoginError, "账号已冻结,请使用小蝶办公签到解冻")));
                            }
                        }

                        if (entity?.JobPosition?.AllowPwd == true)
                        {
                            result.Data = null;

                            ResponseLoginCookie(entity, Remembered);

                            #region 清除cookie_tc_key
                            if (_tc_key.IsNotNull())
                            {
                                _tc_key.Expires = DateTime.Now.AddDays(-1);
                                HttpContext.Response.SetCookie(_tc_key);
                            }
                            #endregion
                        }
                        else
                        {
                            result.ResultType = OperationResultType.LoginError;
                            result.Data       = null;
                            result.Message    = "请使用扫码登录";
                        }
                    }
                    else
                    {
                        result.Data = null;

                        ResponseLoginCookie(entity, Remembered);

                        #region 清除cookie_tc_key
                        if (_tc_key.IsNotNull())
                        {
                            _tc_key.Expires = DateTime.Now.AddDays(-1);
                            HttpContext.Response.SetCookie(_tc_key);
                        }
                        #endregion
                    }
                }
            }
            return(Json(result));
        }