예제 #1
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)));
        }
 /// <summary>
 /// 根据grantid获取
 /// </summary>
 public static ShopBonusInfo GetByGrantId(long grantid)
 {
     return(_iShopBonusService.GetByGrantId(grantid));
 }