예제 #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.Query["code"].ToString();
            OAuthAccessTokenResult wxOpenInfo = null;
            var settings = SiteSettingApplication.SiteSettings;

            if (string.IsNullOrEmpty(code))
            {
                string selfAddress = Request.GetDisplayUrl();
                string url         = OAuthApi.GetAuthorizeUrl(settings.WeixinAppId.Trim(), selfAddress, "STATE", Senparc.Weixin.MP.OAuthScope.snsapi_userinfo, "code");
                return(Redirect(url));
            }
            else
            {
                try
                {
                    wxOpenInfo = OAuthApi.GetAccessToken(settings.WeixinAppId.Trim(), settings.WeixinAppSecret.Trim(), code, "authorization_code");
                }
                catch (Exception e)
                {
                    Exception innerEx = e.InnerException == null ? e : e.InnerException;
                    Log.Error(innerEx.Message);
                    if (innerEx.Message.IndexOf("code been used") != -1)
                    {
                        return(Content("该红包已被领取过"));
                    }
                    else
                    {
                        return(Content(innerEx.Message));
                    }
                }
            }

            OAuthUserInfo wxUserInfo = OAuthApi.GetUserInfo(wxOpenInfo.access_token, wxOpenInfo.openid);
            var           openId     = wxOpenInfo.openid;

            if (!string.IsNullOrEmpty(wxUserInfo.unionid))
            {
                var moInfo = MemberApplication.GetMemberOpenIdInfoByOpenIdOrUnionId(openId, wxUserInfo.unionid);
                if (moInfo != null && !string.IsNullOrEmpty(moInfo.OpenId))
                {
                    openId = moInfo.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=" + 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, openId, wxUserInfo.headimgurl, wxUserInfo.nickname);

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

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

            var host = CurrentUrlHelper.CurrentUrl();

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