Exemplo n.º 1
0
        public ActionResult Index(long id)
        {
            if (this.PlatformType != Core.PlatformType.WeiXin)
            {
                Log.Info(this.PlatformType.ToString());
                return(Content("只能在微信端访问"));
            }

            var bonus = this._bonusService.GetByGrantId(id);

            if (bonus == null)
            {
                Log.Info("红包失踪,id = " + id);
                return(Redirect("/m-weixin/ShopBonus/Invalidtwo"));
            }

            //授权获取openid相关
            string code = HttpContext.Request["code"];
            OAuthAccessTokenResult wxOpenInfo = null;

            if (string.IsNullOrEmpty(code))
            {
                string selfAddress = Request.Url.ToString();
                string url         = OAuthApi.GetAuthorizeUrl(this._siteSetting.WeixinAppId.Trim(), selfAddress, "STATE", Senparc.Weixin.MP.OAuthScope.snsapi_userinfo, "code");
                return(Redirect(url));
            }
            else
            {
                try
                {
                    wxOpenInfo = OAuthApi.GetAccessToken(this._siteSetting.WeixinAppId.Trim(), this._siteSetting.WeixinAppSecret.Trim(), code, "authorization_code");
                }
                catch (Exception e)
                {
                    Exception innerEx = e.InnerException == null ? e : e.InnerException;
                    return(Content(innerEx.Message));
                }
            }

            OAuthUserInfo  wxUserInfo = OAuthApi.GetUserInfo(wxOpenInfo.access_token, wxOpenInfo.openid);
            ShopBonusModel model      = new ShopBonusModel(bonus);

            if (model.DateEnd <= DateTime.Now || model.IsInvalid || model.BonusDateEnd <= DateTime.Now)  //过期、失效
            {
                return(Redirect("/m-weixin/ShopBonus/Expired/" + model.Id + "?openId=" + wxOpenInfo.openid + "&grantid=" + id + "&wxhead=" + wxUserInfo.headimgurl));
            }
            else if (model.DateStart > DateTime.Now) // 未开始
            {
                return(Redirect("/m-weixin/ShopBonus/NotStart/" + model.Id));
            }
            ShopReceiveModel obj = (ShopReceiveModel)this._bonusService.Receive(id, wxUserInfo.openid, wxUserInfo.headimgurl, wxUserInfo.nickname);

            //防两次刷新
            string cacheDName = "WXSB_U" + wxOpenInfo.openid + "_D" + id.ToString();

            if (obj.State == ShopReceiveStatus.Receive)
            {
                var _tmp = Cache.Get <ShopReceiveModel>(cacheDName);
                if (_tmp != null && _tmp.State == ShopReceiveStatus.CanReceive)
                {
                    obj = _tmp;
                }
            }

            if (obj.State == ShopReceiveStatus.CanReceive)
            {
                Cache.Insert <ShopReceiveModel>(cacheDName, obj, 2);
                return(Redirect("/m-weixin/ShopBonus/Completed/" + model.Id + "?openId=" + wxUserInfo.openid + "&price=" + obj.Price + "&user="******"&grantid=" + id + "&rid=" + obj.Id + "&wxhead=" + wxUserInfo.headimgurl + "&host=" + Request.Url.Host));
            }
            else if (obj.State == ShopReceiveStatus.CanReceiveNotUser)
            {
                return(Redirect("/m-weixin/ShopBonus/CompletedNotUser/" + model.Id + "?openId=" + wxUserInfo.openid + "&price=" + obj.Price + "&grantid=" + id + "&rid=" + obj.Id + "&wxhead=" + wxUserInfo.headimgurl + "&host=" + Request.Url.Host));
            }
            else if (obj.State == ShopReceiveStatus.Receive)
            {
                return(Redirect("/m-weixin/ShopBonus/HasReceive/" + model.Id + "?openId=" + wxUserInfo.openid + "&grantid=" + id + "&wxhead=" + wxUserInfo.headimgurl + "&host=" + Request.Url.Host));
            }
            else if (obj.State == ShopReceiveStatus.HaveNot)
            {
                return(Redirect("/m-weixin/ShopBonus/HaveNot/" + model.Id + "?openId=" + wxUserInfo.openid + "&grantid=" + id + "&wxhead=" + wxUserInfo.headimgurl));
            }
            else if (obj.State == ShopReceiveStatus.Invalid)
            {
                return(Redirect("/m-weixin/ShopBonus/Expired/" + model.Id + "?openId=" + wxUserInfo.openid + "&grantid=" + id + "&wxhead=" + wxUserInfo.headimgurl));
            }
            else
            {
                throw new Exception("领取发生异常");
            }
        }
Exemplo n.º 2
0
        public ActionResult Index(long id)
        {
            ActionResult actionResult;

            Log.Info("进入ShopBonus Index");
            if (base.PlatformType != Himall.Core.PlatformType.WeiXin)
            {
                Log.Info(base.PlatformType.ToString());
                return(base.Content("只能在微信端访问"));
            }
            ShopBonusInfo byGrantId = _bonusService.GetByGrantId(id);

            if (byGrantId == null)
            {
                Log.Info(string.Concat("红包失踪,id = ", id));
                return(Redirect("/m-weixin/ShopBonus/Invalidtwo"));
            }
            string item = base.HttpContext.Request["code"];
            OAuthAccessTokenResult accessToken = null;

            if (string.IsNullOrEmpty(item))
            {
                string absoluteUri  = base.Request.Url.AbsoluteUri;
                string authorizeUrl = OAuthApi.GetAuthorizeUrl(_siteSetting.WeixinAppId.Trim(), absoluteUri, "STATE#wechat_redirect", OAuthScope.snsapi_userinfo, "code");
                return(Redirect(authorizeUrl));
            }
            try
            {
                accessToken = OAuthApi.GetAccessToken(_siteSetting.WeixinAppId.Trim(), _siteSetting.WeixinAppSecret.Trim(), item, "authorization_code");
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                actionResult = base.Content(((exception.InnerException == null ? exception : exception.InnerException)).Message);
                return(actionResult);
            }
            Log.Info("------------------------------");
            OAuthUserInfo  userInfo       = OAuthApi.GetUserInfo(accessToken.access_token, accessToken.openid, Language.zh_CN);
            ShopBonusModel shopBonusModel = new ShopBonusModel(byGrantId);

            if (shopBonusModel.DateEnd <= DateTime.Now || shopBonusModel.IsInvalid || shopBonusModel.BonusDateEnd <= DateTime.Now)
            {
                object[] objArray = new object[] { "/m-weixin/ShopBonus/Expired/", shopBonusModel.Id, "?openId=", accessToken.openid, "&grantid=", id, "&wxhead=", userInfo.headimgurl };
                return(Redirect(string.Concat(objArray)));
            }
            if (shopBonusModel.DateStart > DateTime.Now)
            {
                return(Redirect(string.Concat("/m-weixin/ShopBonus/NotStart/", shopBonusModel.Id)));
            }
            ShopReceiveModel shopReceiveModel = (ShopReceiveModel)_bonusService.Receive(id, userInfo.openid, userInfo.headimgurl, userInfo.nickname);

            Log.Info(string.Concat("获取State=", shopReceiveModel.State.ToString()));
            if (shopReceiveModel.State == ShopReceiveStatus.CanReceive)
            {
                object[] objArray1 = new object[] { "/m-weixin/ShopBonus/Completed/", shopBonusModel.Id, "?openId=", userInfo.openid, "&price=", shopReceiveModel.Price, "&user="******"&grantid=", id, "&rid=", shopReceiveModel.Id, "&wxhead=", userInfo.headimgurl, "&host=", base.Request.Url.Host };
                return(Redirect(string.Concat(objArray1)));
            }
            if (shopReceiveModel.State == ShopReceiveStatus.CanReceiveNotUser)
            {
                object[] objArray2 = new object[] { "/m-weixin/ShopBonus/CompletedNotUser/", shopBonusModel.Id, "?openId=", userInfo.openid, "&price=", shopReceiveModel.Price, "&grantid=", id, "&rid=", shopReceiveModel.Id, "&wxhead=", userInfo.headimgurl, "&host=", base.Request.Url.Host };
                return(Redirect(string.Concat(objArray2)));
            }
            if (shopReceiveModel.State == ShopReceiveStatus.Receive)
            {
                object[] objArray3 = new object[] { "/m-weixin/ShopBonus/HasReceive/", shopBonusModel.Id, "?openId=", userInfo.openid, "&grantid=", id, "&wxhead=", userInfo.headimgurl, "&host=", base.Request.Url.Host };
                return(Redirect(string.Concat(objArray3)));
            }
            if (shopReceiveModel.State == ShopReceiveStatus.HaveNot)
            {
                object[] objArray4 = new object[] { "/m-weixin/ShopBonus/HaveNot/", shopBonusModel.Id, "?openId=", userInfo.openid, "&grantid=", id, "&wxhead=", userInfo.headimgurl };
                return(Redirect(string.Concat(objArray4)));
            }
            if (shopReceiveModel.State != ShopReceiveStatus.Invalid)
            {
                throw new Exception("领取发生异常");
            }
            object[] objArray5 = new object[] { "/m-weixin/ShopBonus/Expired/", shopBonusModel.Id, "?openId=", userInfo.openid, "&grantid=", id, "&wxhead=", userInfo.headimgurl };
            return(Redirect(string.Concat(objArray5)));
        }