Esempio n. 1
0
        /// <summary>
        /// 在某个action执行之前进行拦截
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            IUserBLL ownerBll = BLLFactory <IUserBLL> .GetBLL("UserBLL");

            //获取当前保存的Session
            HttpContextBase ctx     = filterContext.HttpContext;
            string          Unionid = (string)ctx.Session["Unionid"];
            string          Openid  = (string)ctx.Session["OpenId"];

            //获取请求地址
            string controller = filterContext.RouteData.Values["controller"].ToString();
            string action     = filterContext.RouteData.Values["action"].ToString();

            if (Openid == null || Unionid == null || !ownerBll.Exist(o => o.WeixinUnionId == Unionid && o.DelFlag == ConstantParam.DEL_FLAG_DEFAULT) ||
                !ownerBll.Exist(o => o.WeixinOpenId == Openid && o.DelFlag == ConstantParam.DEL_FLAG_DEFAULT))
            {
                string url = HttpContext.Current.Request.Url.ToString();
                ctx.Session["VisitUrl"] = url;
                string oAuthUrl = OAuthApi.GetAuthorizeUrl(ConstantParam.AppId, "http://v.homekeeper.com.cn/WeixinOAuth2/UserInfoCallback", "sarnath", OAuthScope.snsapi_userinfo);
                filterContext.Result = new RedirectResult(oAuthUrl);
            }
            else if (!"WeixinPersonalCenter".Equals(controller) && !"WeixinIdentityBind".Equals(controller) && !"WeixinHome".Equals(controller))
            {
                var owner      = ownerBll.GetEntity(o => o.WeixinUnionId == Unionid && o.DelFlag == ConstantParam.DEL_FLAG_DEFAULT);
                var userPlaces = owner.UserPlaces.Where(p => p.PropertyPlace.DelFlag == ConstantParam.DEL_FLAG_DEFAULT);
                if (userPlaces.Count() == 0)
                {
                    filterContext.Result = new RedirectResult("/WeixinIdentityBind/AddPlace");
                }
                else if (!"WeixinPropertyNotice".Equals(controller) && userPlaces.Select(p => IsVerified(p.PropertyPlace, p.User)).Count(i => i) == 0)
                {
                    filterContext.Result = new RedirectResult("/WeixinIdentityBind/Index");
                }
            }
            base.OnActionExecuting(filterContext);
        }
        /// <summary>
        /// OAuthScope.snsapi_userinfo方式回调
        /// </summary>
        /// <param name="code"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public ActionResult UserInfoCallback(string code, string state)
        {
            if (string.IsNullOrEmpty(code))
            {
                return(Content("您拒绝了授权!"));
            }
            if (state != "sarnath")
            {
                //实际上可以存任何想传递的数据,比如用户ID,并且需要结合例如下面的Session["OAuthAccessToken"]进行验证
                return(Content("验证失败!请从正规途径进入!"));
            }
            OAuthAccessTokenResult result = null;

            //通过,用code换取access_token
            try
            {
                result = OAuthApi.GetAccessToken(ConstantParam.AppId, ConstantParam.AppSecret, code);
            }
            catch (Exception ex)
            {
                return(Content(ex.Message));
            }
            if (result.errcode != ReturnCode.请求成功)
            {
                return(Content("错误:" + result.errmsg));
            }
            //下面2个数据也可以自己封装成一个类,储存在数据库中(建议结合缓存)
            //如果可以确保安全,可以将access_token存入用户的cookie中,每一个人的access_token是不一样的
            Session["OAuthAccessTokenStartTime"] = DateTime.Now;
            Session["OAuthAccessToken"]          = result;

            //获取用户详细信息
            try
            {
                OAuthUserInfo userInfo = OAuthApi.GetUserInfo(result.access_token, result.openid);

                IUserBLL ownerBll = BLLFactory <IUserBLL> .GetBLL("UserBLL");

                //如果微信Unionid不存在
                if (!ownerBll.Exist(o => o.WeixinUnionId == userInfo.unionid && o.DelFlag == ConstantParam.DEL_FLAG_DEFAULT))
                {
                    //随机字符串
                    string str       = "1234567890abcdefghijklmnopqrstuvwxyz";
                    Random r         = new Random();
                    string RandomStr = "";
                    for (int i = 0; i < 16; i++)
                    {
                        RandomStr += str[r.Next(str.Length)].ToString();
                    }
                    T_User user = new T_User()
                    {
                        UserName      = RandomStr,
                        WeixinOpenId  = userInfo.openid,
                        WeixinUnionId = userInfo.unionid
                    };
                    if (userInfo.sex > 0)
                    {
                        user.Gender = userInfo.sex == 1 ? 0 : 1;
                    }
                    ownerBll.Save(user);
                }
                else if (!ownerBll.Exist(o => o.WeixinOpenId == userInfo.openid && o.DelFlag == ConstantParam.DEL_FLAG_DEFAULT))
                {
                    T_User user = ownerBll.GetEntity(o => o.WeixinUnionId == userInfo.unionid && o.DelFlag == ConstantParam.DEL_FLAG_DEFAULT);
                    user.WeixinOpenId = userInfo.openid;
                    ownerBll.Update(user);
                }
                var owner = ownerBll.GetEntity(o => o.WeixinUnionId == userInfo.unionid && o.DelFlag == ConstantParam.DEL_FLAG_DEFAULT);
                Session["OpenId"]  = owner.WeixinOpenId;
                Session["Unionid"] = owner.WeixinUnionId;
                return(Redirect((string)Session["VisitUrl"]));
            }
            catch (ErrorJsonResultException ex)
            {
                return(Content(ex.Message));
            }
        }
Esempio n. 3
0
        public JsonResult GetValidateCode(string phoneNum, int actionCode)
        {
            JsonModel jm = new JsonModel();

            try
            {
                //如果是注册(设置手机号)操作获取验证码
                if (actionCode == 0)
                {
                    IUserBLL ownerBll = BLLFactory <IUserBLL> .GetBLL("UserBLL");

                    //如果手机号已存在
                    if (ownerBll.Exist(o => o.Phone == phoneNum && o.DelFlag == ConstantParam.DEL_FLAG_DEFAULT))
                    {
                        jm.Msg = APIMessage.PHONE_EXIST;
                        return(Json(jm, JsonRequestBehavior.AllowGet));
                    }
                }
                else if (actionCode == 1)
                {
                    IUserBLL ownerBll = BLLFactory <IUserBLL> .GetBLL("UserBLL");

                    //如果手机号对应用户不存在
                    if (!ownerBll.Exist(o => o.Phone == phoneNum && o.DelFlag == ConstantParam.DEL_FLAG_DEFAULT))
                    {
                        jm.Msg = APIMessage.PHONE_NO_EXIST;
                        return(Json(jm, JsonRequestBehavior.AllowGet));
                    }
                }
                string code = PropertyUtils.CreateValidateCode(6);
                string msg  = "感谢使用【Ai我家】微信公众号,验证码为:" + code + ",请在页面输入完成验证,如非本人操作请忽略";
                //如果短信发送成功
                if (PropertyUtils.SendSMS(phoneNum, msg, null))
                {
                    IPhoneValidateBLL phoneValidateBll = BLLFactory <IPhoneValidateBLL> .GetBLL("PhoneValidateBLL");

                    var phoneValidate = phoneValidateBll.GetEntity(v => v.PhoneNum == phoneNum && v.ActionCode == actionCode);
                    //如果该手机号在相同操作中不存在
                    if (phoneValidate == null)
                    {
                        T_PhoneValidate v = new T_PhoneValidate()
                        {
                            PhoneNum     = phoneNum,
                            ValidateCode = code,
                            InvalidTime  = DateTime.Now.AddMinutes(Convert.ToInt32(PropertyUtils.GetConfigParamValue("ValidateCodeInvalid"))),
                            ActionCode   = actionCode
                        };
                        phoneValidateBll.Save(v);
                    }
                    else
                    {
                        phoneValidate.ValidateCode = code;
                        phoneValidate.InvalidTime  = DateTime.Now.AddMinutes(Convert.ToInt32(PropertyUtils.GetConfigParamValue("ValidateCodeInvalid")));
                        phoneValidateBll.Update(phoneValidate);
                    }
                }
                else
                {
                    jm.Msg = APIMessage.VALDATE_GET_FAIL;
                }
            }
            catch
            {
                jm.Msg = APIMessage.REQUEST_EXCEPTION;
            }
            return(Json(jm, JsonRequestBehavior.AllowGet));
        }