示例#1
0
 public void SaveJson(wxUserInfoModel model)
 {
     try
     {
         TRF_WeChatUserInfo entity = new TRF_WeChatUserInfo();
         entity.openid   = model.user_openid;
         entity.nickname = model.user_name;
         idal.Add(entity);
         idal.Save();
     }
     catch (Exception ex)
     {
         Logger.Error(ex);
     }
 }
示例#2
0
 /// <summary>
 /// 保存用户微信信息
 /// </summary>
 /// <param name="model"></param>
 public void Save(wxUserInfoModel model)
 {
     try
     {
         TRF_WeChatUserInfo entity = new TRF_WeChatUserInfo();
         entity.openid     = model.Openid;
         entity.nickname   = model.Nickname;
         entity.province   = model.Province;
         entity.city       = model.City;
         entity.country    = model.Country;
         entity.headimgurl = model.Headimgurl;
         //entity.ID = 1;
         entity.sex = model.Sex;
         idal.Add(entity);
         idal.Save();
     }
     catch (Exception ex)
     {
         Logger.Error(string.Format("保存用户信息异常,异常信息:{0}", ex.ToString()));
     }
 }
        /// <summary>
        /// 红包-获取微信用户信息
        /// </summary>
        /// <param name="code">微信端code</param>
        /// <returns></returns>
        public wxUserInfoModel GetWxUserInfo(string code)
        {
            wxUserInfoModel model = null;

            try
            {
                HttpHelper httpHelper = new HttpHelper();

                //获取tokenjson
                string  tokenJson    = httpHelper.HttpGet(String.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", _appId, _appsecret, code), "");
                JObject tokenJsonObj = null;
                if (!string.IsNullOrWhiteSpace(tokenJson))
                {
                    tokenJsonObj = JObject.Parse(tokenJson);
                    if (tokenJsonObj != null)
                    {
                        string userInfoJson = "";
                        //获取用户信息json
                        userInfoJson = httpHelper.HttpGet(string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", tokenJsonObj["access_token"], tokenJsonObj["openid"]), "");
                        if (!string.IsNullOrWhiteSpace(userInfoJson))
                        {
                            //json反序列化为实体
                            model = JsonConvert.DeserializeObject <wxUserInfoModel>(userInfoJson);

                            if (model.Openid == null)
                            {
                                model = null;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Common.Helper.Logger.Info(string.Format("BaseContrller,获取用户微信信息异常,异常信息:{0}", ex.ToString()));
            }
            return(model);
        }
示例#4
0
        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="model"></param>
        public void updateUserInfo(wxUserInfoModel model)
        {
            try
            {
                TRF_WeChatUserInfo entity = new TRF_WeChatUserInfo();

                Expression <Func <TRF_WeChatUserInfo, bool> > exp = a => 1 == 1;

                exp = a => a.openid == model.Openid;
                Dictionary <string, object> dic = new Dictionary <string, object>();
                dic.Add("nickname", model.Nickname);
                dic.Add("province", model.Province);
                dic.Add("city", model.City);
                dic.Add("country", model.Country);
                dic.Add("headimgurl", model.Headimgurl);
                dic.Add("sex", model.Sex);
                idal.update(exp, dic);
                idal.Save();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
            }
        }
        /// <summary>
        /// 发现金
        /// </summary>
        /// <returns></returns>
        public ActionResult GiveMoney(RequestModel request)
        {
            ActionResult empty = new EmptyResult();

            try
            {
                //扫码计数-所有
                scanCountDi.getBll().CountByNameAndId(request.activityId, request.activityName);


                #region 数据库日志记录  公共部分

                TRP_ClientLog entity = new TRP_ClientLog();
                entity.CreateTime = DateTime.Now;
                entity.DeleteMark = false;
                entity.Enable     = true;
                entity.PageUrl    = HttpContext.Request.Url.AbsoluteUri ?? "http://www.chinazhihuiping.com/wxredpackets/ReGaoPark/GiveMoney";
                entity.IPAddress  = HttpContext.Request.UserHostAddress ?? "127.0.0.1";
                entity.ActivityId = Convert.ToInt32(request.activityId);

                #endregion


                //获取微信用户信息
                wxUserInfoModel wxUser = GetWxUserInfo(request.code);

                if (wxUser == null)
                {
                    Response.Redirect(request.url + "&flag=1");
                    return(empty);
                }

                //微信用户openid
                string openid = wxUser.Openid;

                //保存微信信息
                if (!isExistOpenId(wxUser.Openid))
                {
                    //保存用户微信信息
                    saveUserInfo(wxUser);
                }

                TRP_AwardReceive receivedModel = null;

                #region 今天已参加活动

                if (isAttendToday(request.activityId, wxUser.Openid))
                {
                    //判断奖品是否已领
                    receivedModel = awardDi.getBll().hadTakeAward(wxUser.Openid, request.activityId);

                    #region 奖品已核销

                    //奖品已领
                    if (receivedModel == null)
                    {
                        Response.Redirect(request.url + "&flag=1");
                        return(empty);
                    }

                    #endregion

                    #region 奖品还未核销

                    //奖品还未领取
                    else
                    {
                        //获取奖品ID  int
                        string awardId = receivedModel.AwardDetailId.ToString();

                        //获取奖品详情
                        TRP_AwardDetail detailModel = detailDi.getBll().GetEntityById(awardId);

                        //奖品名称
                        string awardsName = "";

                        //加密奖品id
                        string ecodeAwardId = DESEncrypt.Encrypt(awardId, _key);

                        if (detailModel != null)
                        {
                            //奖品名称
                            awardsName = detailModel.AwardName;
                        }
                        else
                        {
                            Response.Redirect(request.url + "&flag=1");
                            return(empty);
                        }

                        //奖品类型
                        if (!string.IsNullOrWhiteSpace(awardsName))
                        {
                            GiveCash(awardsName, wxUser.Openid, request);
                        }
                        else
                        {
                            Response.Redirect(request.url + "&flag=1");
                            return(empty);
                        }

                        entity.Description = string.Format("用户在{0}点击红包,现金红包(为重复扫码,上次未核销奖品)", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        entity.PageDesc    = "实物扫码,上次未核销奖品";
                        entity.ActivityId  = Convert.ToInt32(request.activityId);
                        logDi.getBll().SaveLog(entity);
                        return(View("GiveMoney"));
                    }
                    #endregion
                }

                #endregion

                #region 今天还未参加活动  则请求奖品

                AwardsInfoModel awardsModel = new AwardsInfoModel();

                //请求奖品
                awardsModel = GetAwardsInfo(request.activityId);

                //奖品实体为null
                if (awardsModel == null)
                {
                    Response.Redirect(request.url + "&flag=1");
                    return(empty);
                }

                //奖品实体的类型为null
                if (awardsModel.Class != null)
                {
                    //奖品实体的类型为""
                    if (awardsModel.Class == "")
                    {
                        Response.Redirect(request.url + "&flag=1");
                        return(empty);
                    }
                }

                #endregion

                //发钱
                if (GiveCash(awardsModel.Class, openid, request))
                {
                    //保存领奖信息-测试
                    saveScanInfo(wxUser.Openid, awardsModel.id);
                    saveUserAwardReceiveInfo(wxUser.Openid, awardsModel.id);
                    return(View("GiveMoney"));
                }
                else
                {
                    Response.Redirect(request.url + "&flag=1");
                    return(empty);
                }
            }
            catch (Exception ex)
            {
                Response.Redirect(request.url);
                Common.Helper.Logger.Info(string.Format("获取用户微信Openid,发现金异常,异常信息:{0}", ex.ToString()));
                return(empty);
            }
        }
        /// <summary>
        /// 微信控制器
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ActionResult WXActivity(RequestModel request)
        {
            ActionResult empty = new EmptyResult();

            try
            {
                #region 数据库日志记录  公共部分

                TRP_ClientLog entity = new TRP_ClientLog();
                entity.CreateTime = DateTime.Now;
                entity.DeleteMark = false;
                entity.Enable     = true;
                entity.PageUrl    = HttpContext.Request.Url.AbsoluteUri ?? "http://www.chinazhihuiping.com/ReGaoPark/WXActivity";
                entity.IPAddress  = HttpContext.Request.UserHostAddress ?? "127.0.0.1";
                entity.ActivityId = Convert.ToInt32(request.activityId);

                #endregion

                #region 获取微信用户信息 昵称,头像等

                wxUserInfoModel wxUser = GetWxUserInfo(request.code);

                if (wxUser == null)
                {
                    //非卡重定向 至微信领奖页
                    string url       = string.Format("http://www.chinazhihuiping.com/wxredpackets/ReGaoPark/WXActivity?activityId={0}&giftType={1}&giftId={2}", request.activityId, request.giftType, request.giftId);
                    string urlencode = System.Web.HttpUtility.UrlEncode(url, System.Text.Encoding.UTF8);
                    ResponseWXRedirect(urlencode);
                    return(empty);
                }

                #endregion

                #region 今天已参加活动

                if (isAttendToday(request.activityId, wxUser.Openid))
                {
                    //判断奖品是否已领
                    var receivedModel = awardDi.getBll().hadTakeAward(wxUser.Openid, request.activityId);

                    #region 奖品已核销

                    //奖品已领
                    if (receivedModel == null)
                    {
                        Dictionary <int, string> dic = new Dictionary <int, string>();
                        dic.Add(1, "https://shop13299622.wxrrd.com/coupon/get_code/68929");
                        dic.Add(2, "https://shop13299622.wxrrd.com/coupon/get_code/68928");
                        dic.Add(3, "https://shop13299622.wxrrd.com/coupon/get_code/68927");
                        dic.Add(4, "https://shop13299622.wxrrd.com/coupon/get_code/68926");
                        dic.Add(5, "https://shop13299622.wxrrd.com/coupon/get_code/68925");
                        dic.Add(6, "https://shop13299622.wxrrd.com/coupon/get_code/68924");
                        //RedirectRandom(dic, 6);
                        scanCountDi.getBll().CountByNameAndId(request.activityId, "【热高乐园红包雨】-实物已领取, 随机跳转");
                        return(empty);
                    }
                    #endregion

                    #region 奖品还未核销

                    //奖品还未领取
                    else
                    {
                        //获取奖品ID  int
                        string awardId = receivedModel.AwardDetailId.ToString();

                        //获取奖品详情
                        TRP_AwardDetail detailModel = detailDi.getBll().GetEntityById(awardId);

                        //奖品名称
                        string awardsName = "";

                        //加密奖品id
                        string ecodeAwardId = DESEncrypt.Encrypt(awardId, _key);


                        if (detailModel != null)
                        {
                            awardsName = detailModel.AwardName;
                        }

                        //奖品类型
                        if (!string.IsNullOrWhiteSpace(awardsName))
                        {
                            string typeCode = "";
                            typeCode         = "A";
                            ViewData["Type"] = typeCode;
                        }

                        ViewData["Openid"]        = wxUser.Openid ?? "";
                        ViewData["wxName"]        = wxUser.Nickname ?? "";
                        ViewData["AwardDetailId"] = ecodeAwardId;
                        ViewData["AwardName"]     = awardsName;
                        ViewData["Activity"]      = request.activityId ?? "";

                        entity.Description = string.Format("用户在{0}点击红包,二维码实物扫码(为重复扫码,上次未核销奖品)", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        entity.PageDesc    = "实物扫码,上次未核销奖品";
                        entity.ActivityId  = Convert.ToInt32(request.activityId);
                        logDi.getBll().SaveLog(entity);
                        return(View("Activity"));
                    }
                    #endregion
                }

                #endregion

                #region 首次参加活动
                if (request.giftType != null)
                {
                    string awardsType = request.giftType;
                    string typeCode   = "";
                    if (awardsType.Contains("笔记本"))
                    {
                        typeCode = "A";
                    }
                    else
                    {
                        Common.Helper.Logger.Info(string.Format("【热高乐园红包雨】,用户获取奖品:微信用户-OpenId:{0}-领取奖品,未接收到奖品类型,发生了重定向"));
                        Response.Redirect(string.Format("http://www.chinazhihuiping.com/wxredpackets/ReGaoPark/Activity?activityId={0}&flag={1}", request.activityId, 2));
                        return(empty);
                    }
                    ViewData["Type"] = typeCode;
                }

                //不存在微信用户
                if (!isExistOpenId(wxUser.Openid))
                {
                    //保存用户微信信息
                    saveUserInfo(wxUser);
                }

                //保存扫码信息
                saveScanInfo(wxUser.Openid, request.giftId);

                string awardName = request.giftType;


                entity.Description = string.Format("【热高乐园红包雨】,用户在{0}点击红包,二维码扫码进入实物领奖页面,奖品为{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), request.giftType);
                entity.PageDesc    = string.Format("实物扫码,得到{0}", awardName);
                entity.ActivityId  = Convert.ToInt32(DESEncrypt.Decrypt(request.giftId, _key));
                logDi.getBll().SaveLog(entity);

                ViewData["Openid"]        = wxUser.Openid ?? "";
                ViewData["wxName"]        = wxUser.Nickname ?? "";
                ViewData["AwardDetailId"] = request.giftId ?? "";
                ViewData["AwardName"]     = request.giftType ?? "";
                ViewData["Activity"]      = request.activityId ?? "";
                return(View("Activity"));

                #endregion

                #region 注释部分 无限制领取次数


                //if (request.giftType != null)
                //{
                //    string awardsType = request.giftType;
                //    string typeCode = "";
                //    if (awardsType.Contains("笔记本"))
                //    {
                //        typeCode = "A";
                //    }
                //    else
                //    {
                //        Common.Helper.Logger.Info(string.Format("【热高乐园红包雨】,用户获取奖品:微信用户-OpenId:{0}-领取奖品,未接收到奖品类型,发生了重定向"));
                //        Response.Redirect(string.Format("http://www.chinazhihuiping.com/wxredpackets/ReGaoPark/Activity?activityId={0}&flag={1}", request.activityId, 2));
                //        return empty;
                //    }
                //    ViewData["Type"] = typeCode;
                //}

                ////不存在微信用户
                //if (!isExistOpenId(wxUser.Openid))
                //{
                //    //保存用户微信信息
                //    saveUserInfo(wxUser);
                //}

                ////保存扫码信息
                //saveScanInfo(wxUser.Openid, request.giftId);

                //#region 保存日志记录至数据库

                //entity.Description = string.Format("【热高乐园红包雨】,用户在{0}点击红包,二维码扫码进入实物领奖页面,奖品为{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), request.giftType);
                //entity.PageDesc = string.Format("实物扫码,得到{0}", request.giftType);
                //entity.ActivityId = Convert.ToInt32(DESEncrypt.Decrypt(request.giftId, _key));
                //logDi.getBll().SaveLog(entity);

                //#endregion

                //ViewData["Openid"] = wxUser.Openid ?? "";
                //ViewData["wxName"] = wxUser.Nickname ?? "";
                //ViewData["AwardDetailId"] = request.giftId ?? "";
                //ViewData["AwardName"] = request.giftType ?? "";
                //ViewData["Activity"] = request.activityId ?? "";

                //return View("Activity");

                #endregion
            }
            catch (Exception ex)
            {
                Common.Helper.Logger.Info(string.Format("【热高乐园红包雨】领取实物异常,异常信息:{0}", ex.ToString()));
                Response.Redirect(string.Format("http://www.chinazhihuiping.com/wxredpackets/ReGaoPark/Activity?activityId={0}&flag={1}", request.activityId, 2));
                return(empty);
            }
        }
示例#7
0
        public ActionResult Index()
        {
            HttpHelper httpHelper = new HttpHelper();

            #region  微信网页授权
            TRF_WeChatUserInfo_DI     weChatDi = new TRF_WeChatUserInfo_DI();
            List <TRF_WeChatUserInfo> list     = weChatDi.getBll().getAll();
            int i = 0;
            foreach (TRF_WeChatUserInfo item in list)
            {
                i++;
                //https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
                //System.Net.ServicePointManager.SecurityProtocol =
                //SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
                string  tokenJson    = httpHelper.HttpGet(String.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", _appId, _appsecret), "");
                JObject tokenJsonObj = null;
                if (!string.IsNullOrWhiteSpace(tokenJson))
                {
                    tokenJsonObj = JObject.Parse(tokenJson);
                }
                else
                {
                    return(View("Error"));
                }

                string          userInfoJson = "";
                wxUserInfoModel model        = null;
                if (tokenJsonObj != null)
                {
                    var access_token = tokenJsonObj["access_token"];
                    var expires_in   = tokenJsonObj["expires_in"];
                    var open_id      = "";



                    open_id      = item.openid;
                    userInfoJson = httpHelper.HttpGet(string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}", access_token, open_id), "");
                    if (!string.IsNullOrWhiteSpace(userInfoJson))
                    {
                        model = JsonConvert.DeserializeObject <wxUserInfoModel>(userInfoJson);
                    }
                    if (model != null)
                    {
                        weChatDi.getBll().updateUserInfo(model);
                        Logger.Error("更新第1条信息" + i);
                    }

                    //userInfoJson = httpHelper.HttpGet(string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}", tokenJsonObj["access_token"], tokenJsonObj["expires_in"]), "");
                    //if (!string.IsNullOrWhiteSpace(userInfoJson))
                    //{
                    //    model = JsonConvert.DeserializeObject<wxUserInfoModel>(userInfoJson);
                    //}
                    //else
                    //{
                    //    return View("Error");
                    //}
                }
                else
                {
                    return(View());
                }
            }

            #endregion
            return(View());
        }
        /// <summary>
        /// 保存微信用户信息
        /// </summary>
        /// <param name="model"></param>
        public void saveUserInfo(wxUserInfoModel model)
        {
            TRF_WeChatUserInfo_BLL bll = weChatDi.getBll();

            bll.Save(model);
        }
示例#9
0
        /// <summary>
        /// 微信控制器
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ActionResult WXActivity(RequestModel request)
        {
            ActionResult empty = new EmptyResult();

            try
            {
                #region 数据库日志记录  公共部分

                TRP_ClientLog entity = new TRP_ClientLog();
                entity.CreateTime = DateTime.Now;
                entity.DeleteMark = false;
                entity.Enable     = true;
                entity.PageUrl    = HttpContext.Request.Url.AbsoluteUri ?? wx_url;
                entity.IPAddress  = HttpContext.Request.UserHostAddress ?? "127.0.0.1";
                entity.ActivityId = Convert.ToInt32(request.activityId);

                #endregion

                #region 获取微信用户信息 昵称,头像等

                wxUserInfoModel wxUser = GetWxUserInfo(request.code);

                if (wxUser == null)
                {
                    //微信用户信息未获取到 重定向至活动入口Action
                    string url = string.Format("{0}?activityId={1}", enter_url, request.activityId);
                    return(empty);
                }

                #endregion

                #region 今天已参加活动

                if (isAttendToday(request.activityId, wxUser.Openid))
                {
                    //判断奖品是否已领
                    var receivedModel = awardDi.getBll().hadTakeAward(wxUser.Openid, request.activityId);

                    #region 奖品已核销

                    //奖品已领
                    if (receivedModel == null)
                    {
                        scanCountDi.getBll().CountByNameAndId(request.activityId, str_d_activityname);
                        return(View("HadAttend"));
                    }
                    #endregion

                    #region 奖品还未核销 返回Activity视图

                    //奖品还未领取
                    else
                    {
                        //获取奖品ID  int
                        string awardId = receivedModel.AwardDetailId.ToString();

                        //获取奖品详情
                        TRP_AwardDetail detailModel = detailDi.getBll().GetEntityById(awardId);

                        //奖品名称
                        string awardsName = "";

                        //加密奖品id
                        string ecodeAwardId = DESEncrypt.Encrypt(awardId, _key);


                        if (detailModel != null)
                        {
                            awardsName = detailModel.AwardName;
                        }

                        //奖品类型
                        if (!string.IsNullOrWhiteSpace(awardsName))
                        {
                            string typeCode = "";
                            if (awards_code.ContainsKey(awardsName.Trim()))
                            {
                                typeCode = awards_code[awardsName.Trim()];
                            }
                            else
                            {
                                return(View("Nogift"));
                            }
                            ViewData["Type"] = typeCode;
                        }

                        ViewData["Openid"]        = wxUser.Openid ?? "";
                        ViewData["wxName"]        = wxUser.Nickname ?? "";
                        ViewData["AwardDetailId"] = ecodeAwardId;
                        ViewData["AwardName"]     = awardsName;
                        ViewData["Activity"]      = request.activityId ?? "";

                        entity.Description = string.Format("用户在{0}点击红包,二维码实物扫码(为重复扫码,上次未核销奖品)", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        entity.PageDesc    = "实物扫码,上次未核销奖品";
                        entity.ActivityId  = Convert.ToInt32(request.activityId);
                        logDi.getBll().SaveLog(entity);
                        return(View("Activity"));
                    }
                    #endregion
                }

                #endregion

                #region 首次参加活动
                else
                {
                    //请求奖品
                    AwardsInfoModel awardsModel = GetAwardsInfo(request.activityId);

                    //奖品的Class不为空
                    if (awardsModel.Class != null)
                    {
                        if (awardsModel.Class == "")
                        {
                            scanCountDi.getBll().CountByNameAndId(request.activityId, str_e_activityname);
                            return(View("Nogift"));
                        }
                        else
                        {
                            string awardsType = awardsModel.Class;
                            string typeCode   = "";

                            //Common.Helper.Logger.Info("--test--");
                            //Common.Helper.Logger.Info(awardsModel.Class);
                            //Common.Helper.Logger.Info(awards_code.ContainsKey(awardsModel.Class.Trim()));
                            if (awards_code.ContainsKey(awardsModel.Class.Trim()))
                            {
                                //Common.Helper.Logger.Info(awards_code[awardsModel.Class.Trim()]);
                                typeCode = awards_code[awardsModel.Class.Trim()];
                            }
                            else
                            {
                                return(View("Nogift"));
                            }
                            ViewData["Type"] = typeCode;

                            //不存在微信用户
                            if (!isExistOpenId(wxUser.Openid))
                            {
                                //保存用户微信信息
                                saveUserInfo(wxUser);
                            }

                            //保存扫码信息
                            saveScanInfo(wxUser.Openid, awardsModel.id);

                            string awardName = awardsType;
                            entity.Description = string.Format("{0},用户在{1}点击红包,二维码扫码进入实物领奖页面,奖品为{1}", str_a_activityname, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), awardsType);
                            entity.PageDesc    = string.Format("实物扫码,得到{0}", awardName);
                            entity.ActivityId  = int.Parse(request.activityId);
                            logDi.getBll().SaveLog(entity);

                            ViewData["Openid"]        = wxUser.Openid ?? "";
                            ViewData["wxName"]        = wxUser.Nickname ?? "";
                            ViewData["AwardDetailId"] = awardsModel.id ?? "";
                            ViewData["AwardName"]     = awardsModel.Class ?? "";
                            ViewData["Activity"]      = request.activityId ?? "";
                            return(View("Activity"));
                        }
                    }
                    else
                    {
                        scanCountDi.getBll().CountByNameAndId(request.activityId, str_f_activityname);
                        return(View("Nogift"));
                    }
                }
                #endregion

                #region 注释部分 无限制领取次数


                //if (request.giftType != null)
                //{
                //    string awardsType = request.giftType;
                //    string typeCode = "";
                //    if (awardsType.Contains("笔记本"))
                //    {
                //        typeCode = "A";
                //    }
                //    else
                //    {
                //        Common.Helper.Logger.Info(string.Format("【热高乐园红包雨】,用户获取奖品:微信用户-OpenId:{0}-领取奖品,未接收到奖品类型,发生了重定向"));
                //        Response.Redirect(string.Format("http://www.chinazhihuiping.com/wxredpackets/ReGaoPark/Activity?activityId={0}&flag={1}", request.activityId, 2));
                //        return empty;
                //    }
                //    ViewData["Type"] = typeCode;
                //}

                ////不存在微信用户
                //if (!isExistOpenId(wxUser.Openid))
                //{
                //    //保存用户微信信息
                //    saveUserInfo(wxUser);
                //}

                ////保存扫码信息
                //saveScanInfo(wxUser.Openid, request.giftId);

                //#region 保存日志记录至数据库

                //entity.Description = string.Format("【热高乐园红包雨】,用户在{0}点击红包,二维码扫码进入实物领奖页面,奖品为{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), request.giftType);
                //entity.PageDesc = string.Format("实物扫码,得到{0}", request.giftType);
                //entity.ActivityId = Convert.ToInt32(DESEncrypt.Decrypt(request.giftId, _key));
                //logDi.getBll().SaveLog(entity);

                //#endregion

                //ViewData["Openid"] = wxUser.Openid ?? "";
                //ViewData["wxName"] = wxUser.Nickname ?? "";
                //ViewData["AwardDetailId"] = request.giftId ?? "";
                //ViewData["AwardName"] = request.giftType ?? "";
                //ViewData["Activity"] = request.activityId ?? "";

                //return View("Activity");

                #endregion
            }
            catch (Exception ex)
            {
                Common.Helper.Logger.Info(string.Format("{0}领取实物异常,异常信息:{1}", str_a_activityname, ex.ToString()));
                Response.Redirect(string.Format("{0}?activityId={1}&flag={2}", enter_url, request.activityId, 2));
                return(empty);
            }
        }
示例#10
0
        /// <summary>
        /// 微信控制器
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ActionResult WXActivity(RequestModel request)
        {
            ActionResult empty = new EmptyResult();

            try
            {
                #region 数据库日志记录  公共部分

                TRP_ClientLog entity = new TRP_ClientLog();
                entity.CreateTime = DateTime.Now;
                entity.DeleteMark = false;
                entity.Enable     = true;
                entity.PageUrl    = HttpContext.Request.Url.AbsoluteUri ?? "http://www.chinazhihuiping.com/NewCity/WXActivity";
                entity.IPAddress  = HttpContext.Request.UserHostAddress ?? "127.0.0.1";
                entity.ActivityId = Convert.ToInt32(request.activityId);

                #endregion

                #region 获取微信用户信息 昵称,头像等

                wxUserInfoModel wxUser = GetWxUserInfo(request.code);

                if (wxUser == null)
                {
                    //非卡重定向 至微信领奖页
                    string url       = string.Format("http://www.chinazhihuiping.com/wxredpackets/NewCity/WXActivity?activityId={0}&giftType={1}&giftId={2}", request.activityId, request.giftType, request.giftId);
                    string urlencode = System.Web.HttpUtility.UrlEncode(url, System.Text.Encoding.UTF8);
                    ResponseWXRedirect(urlencode);
                    return(empty);
                }

                #endregion

                #region 今天已参加活动

                if (isAttendToday(request.activityId, wxUser.Openid))
                {
                    //判断奖品是否已领
                    var receivedModel = awardDi.getBll().hadTakeAward(wxUser.Openid, request.activityId);

                    #region 奖品已核销

                    //奖品已领
                    if (receivedModel == null)
                    {
                        scanCountDi.getBll().CountByNameAndId(request.activityId, string.Format("{0}-实物已领取", _activityName));
                        return(View("HadAttend"));
                    }
                    #endregion

                    #region 奖品还未核销

                    //奖品还未领取
                    else
                    {
                        //获取奖品ID  int
                        string awardId = receivedModel.AwardDetailId.ToString();

                        //获取奖品详情
                        TRP_AwardDetail detailModel = detailDi.getBll().GetEntityById(awardId);

                        //奖品名称
                        string awardsName = "";

                        //加密奖品id
                        string ecodeAwardId = DESEncrypt.Encrypt(awardId, _key);


                        if (detailModel != null)
                        {
                            awardsName = detailModel.AwardName;
                        }

                        //奖品类型
                        if (!string.IsNullOrWhiteSpace(awardsName))
                        {
                            string typeCode = "";
                            if (awardsName.Contains("周末礼金"))
                            {
                                typeCode = "A";
                            }
                            else if (awardsName.Contains("健身月卡"))
                            {
                                typeCode = "B";
                            }
                            else if (awardsName.Contains("圆春饼"))
                            {
                                typeCode = "C";
                            }
                            else if (awardsName.Contains("咖啡券"))
                            {
                                typeCode = "D";
                            }
                            else if (awardsName.Contains("公社五折"))
                            {
                                typeCode = "E";
                            }
                            else if (awardsName.Contains("公社三折"))
                            {
                                typeCode = "F";
                            }
                            else if (awardsName.Contains("美甲护理"))
                            {
                                typeCode = "G";
                            }
                            else if (awardsName.Contains("美甲30元"))
                            {
                                typeCode = "H";
                            }
                            else if (awardsName.Contains("一叶一生"))
                            {
                                typeCode = "I";
                            }
                            else if (awardsName.Contains("含花"))
                            {
                                typeCode = "J";
                            }
                            else if (awardsName.Contains("萌宠"))
                            {
                                typeCode = "K";
                            }
                            else if (awardsName.Contains("南山"))
                            {
                                typeCode = "L";
                            }
                            else if (awardsName.Contains("魅可发妆"))
                            {
                                typeCode = "M";
                            }
                            else if (awardsName.Contains("在宥"))
                            {
                                typeCode = "N";
                            }
                            else if (awardsName.Contains("月饼"))
                            {
                                typeCode = "O";
                            }
                            else if (awardsName.Contains("大麦"))
                            {
                                typeCode = "P";
                            }
                            else
                            {
                                return(View("Nogift"));
                            }
                            ViewData["Type"] = typeCode;
                        }

                        ViewData["Openid"]        = wxUser.Openid ?? "";
                        ViewData["wxName"]        = wxUser.Nickname ?? "";
                        ViewData["AwardDetailId"] = ecodeAwardId;
                        ViewData["AwardName"]     = awardsName;
                        ViewData["Activity"]      = request.activityId ?? "";

                        entity.Description = string.Format("用户在{0}点击红包,二维码实物扫码(为重复扫码,上次未核销奖品)", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        entity.PageDesc    = "实物扫码,上次未核销奖品";
                        entity.ActivityId  = Convert.ToInt32(request.activityId);
                        logDi.getBll().SaveLog(entity);
                        return(View("Activity"));
                    }
                    #endregion
                }

                #endregion

                #region 首次参加活动
                if (request.giftType != null)
                {
                    string awardsName = request.giftType;
                    string typeCode   = "";

                    if (awardsName.Contains("周末礼金"))
                    {
                        typeCode = "A";
                    }
                    else if (awardsName.Contains("健身月卡"))
                    {
                        typeCode = "B";
                    }
                    else if (awardsName.Contains("圆春饼"))
                    {
                        typeCode = "C";
                    }
                    else if (awardsName.Contains("咖啡券"))
                    {
                        typeCode = "D";
                    }
                    else if (awardsName.Contains("公社五折"))
                    {
                        typeCode = "E";
                    }
                    else if (awardsName.Contains("公社三折"))
                    {
                        typeCode = "F";
                    }
                    else if (awardsName.Contains("美甲护理"))
                    {
                        typeCode = "G";
                    }
                    else if (awardsName.Contains("美甲30元"))
                    {
                        typeCode = "H";
                    }
                    else if (awardsName.Contains("一叶一生"))
                    {
                        typeCode = "I";
                    }
                    else if (awardsName.Contains("含花"))
                    {
                        typeCode = "J";
                    }
                    else if (awardsName.Contains("萌宠"))
                    {
                        typeCode = "K";
                    }
                    else if (awardsName.Contains("南山"))
                    {
                        typeCode = "L";
                    }
                    else if (awardsName.Contains("魅可发妆"))
                    {
                        typeCode = "M";
                    }
                    else if (awardsName.Contains("在宥"))
                    {
                        typeCode = "N";
                    }
                    else if (awardsName.Contains("月饼"))
                    {
                        typeCode = "O";
                    }
                    else if (awardsName.Contains("大麦"))
                    {
                        typeCode = "P";
                    }
                    else
                    {
                        return(View("Nogift"));
                        //Common.Helper.Logger.Info(string.Format("【中兴30周年红包雨】,用户获取奖品:微信用户-OpenId:{0}-领取奖品,未接收到奖品类型,发生了重定向"));
                        //Response.Redirect(string.Format("http://www.chinazhihuiping.com/wxredpackets/ZTE/Activity?activityId={0}&flag={1}", request.activityId, 2));
                        //return empty;
                    }
                    ViewData["Type"] = typeCode;
                }

                //不存在微信用户
                if (!isExistOpenId(wxUser.Openid))
                {
                    //保存用户微信信息
                    saveUserInfo(wxUser);
                }

                //保存扫码信息
                saveScanInfo(wxUser.Openid, request.giftId);

                string awardName = request.giftType;


                entity.Description = string.Format("{0},用户在{1}点击红包,二维码扫码进入实物领奖页面,奖品为{2}", _activityName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), request.giftType);
                entity.PageDesc    = string.Format("实物扫码,得到{0}", awardName);
                entity.ActivityId  = Convert.ToInt32(DESEncrypt.Decrypt(request.giftId, _key));
                logDi.getBll().SaveLog(entity);

                ViewData["Openid"]        = wxUser.Openid ?? "";
                ViewData["wxName"]        = wxUser.Nickname ?? "";
                ViewData["AwardDetailId"] = request.giftId ?? "";
                ViewData["AwardName"]     = request.giftType ?? "";
                ViewData["Activity"]      = request.activityId ?? "";
                return(View("Activity"));

                #endregion

                #region 注释部分 无限制领取次数


                //if (request.giftType != null)
                //{
                //    string awardsType = request.giftType;
                //    string typeCode = "";
                //    if (awardsType.Contains("笔记本"))
                //    {
                //        typeCode = "A";
                //    }
                //    else
                //    {
                //        Common.Helper.Logger.Info(string.Format("【热高乐园红包雨】,用户获取奖品:微信用户-OpenId:{0}-领取奖品,未接收到奖品类型,发生了重定向"));
                //        Response.Redirect(string.Format("http://www.chinazhihuiping.com/wxredpackets/ReGaoPark/Activity?activityId={0}&flag={1}", request.activityId, 2));
                //        return empty;
                //    }
                //    ViewData["Type"] = typeCode;
                //}

                ////不存在微信用户
                //if (!isExistOpenId(wxUser.Openid))
                //{
                //    //保存用户微信信息
                //    saveUserInfo(wxUser);
                //}

                ////保存扫码信息
                //saveScanInfo(wxUser.Openid, request.giftId);

                //#region 保存日志记录至数据库

                //entity.Description = string.Format("【热高乐园红包雨】,用户在{0}点击红包,二维码扫码进入实物领奖页面,奖品为{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), request.giftType);
                //entity.PageDesc = string.Format("实物扫码,得到{0}", request.giftType);
                //entity.ActivityId = Convert.ToInt32(DESEncrypt.Decrypt(request.giftId, _key));
                //logDi.getBll().SaveLog(entity);

                //#endregion

                //ViewData["Openid"] = wxUser.Openid ?? "";
                //ViewData["wxName"] = wxUser.Nickname ?? "";
                //ViewData["AwardDetailId"] = request.giftId ?? "";
                //ViewData["AwardName"] = request.giftType ?? "";
                //ViewData["Activity"] = request.activityId ?? "";

                //return View("Activity");

                #endregion
            }
            catch (Exception ex)
            {
                Common.Helper.Logger.Info(string.Format("{0}领取实物异常,异常信息:{1}", _activityName, ex.ToString()));
                Response.Redirect(string.Format("http://www.chinazhihuiping.com/wxredpackets/NewCity/Activity?activityId={0}&flag={1}", request.activityId, 2));
                return(empty);
            }
        }
        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ActionResult Index(RequestModel request)
        {
            //return View();
            //awardDi.getBll().hadTakeAward("omJcruMSo_al_vHgI7dQHdixsPEM", "44");
            try
            {
                HttpHelper httpHelper = new HttpHelper();

                #region  微信网页授权

                string  tokenJson    = httpHelper.HttpGet(String.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", _appId, _appsecret, request.code), "");
                JObject tokenJsonObj = null;
                if (!string.IsNullOrWhiteSpace(tokenJson))
                {
                    tokenJsonObj = JObject.Parse(tokenJson);
                }
                else
                {
                    return(View("Error"));
                }

                string          userInfoJson = "";
                wxUserInfoModel model        = null;
                if (tokenJsonObj != null)
                {
                    userInfoJson = httpHelper.HttpGet(string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", tokenJsonObj["access_token"], tokenJsonObj["openid"]), "");
                    if (!string.IsNullOrWhiteSpace(userInfoJson))
                    {
                        model = JsonConvert.DeserializeObject <wxUserInfoModel>(userInfoJson);
                        if (model.Nickname == null || model.Sex == null)
                        {
                            return(View("Error"));
                        }
                    }
                    else
                    {
                        return(View("Error"));
                    }
                }
                else
                {
                    return(View("Error"));
                }

                #endregion

                ActivityStatus status = new ActivityStatus();
                status = ActivityStatus.Unknown;

                //活动id未取到
                if (string.IsNullOrWhiteSpace(request.activityId))
                {
                    return(View("Error"));
                }



                TRP_ClientLog entity = new TRP_ClientLog();
                entity.CreateTime = DateTime.Now;
                entity.DeleteMark = false;
                entity.Enable     = true;
                entity.PageUrl    = HttpContext.Request.Url.AbsoluteUri ?? "http://www.chinazhihuiping.com/wxredpackets";
                entity.IPAddress  = HttpContext.Request.UserHostAddress ?? "127.0.0.1";

                //今天已参加活动 无领奖资格  直接返回视图 不请求奖品
                if (isAttendToday(request.activityId, model.Openid))
                {
                    status = ActivityStatus.HasAttend;


                    //判断奖品是否已领 如果没有领取 该奖品继续返回页面
                    var receivedModel = awardDi.getBll().hadTakeAward(model.Openid, request.activityId);
                    if (receivedModel == null)
                    {
                        entity.Description = string.Format("用户在{0}点击红包,二维码实物扫码(已参加活动,为重复扫码)", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        entity.PageDesc    = "实物扫码,已参加本次活动";
                        entity.ActivityId  = Convert.ToInt32(request.activityId);
                        logDi.getBll().SaveLog(entity);
                        return(View("HasAttend"));
                    }
                    else
                    {
                        //获取奖品ID  int
                        string awardId = receivedModel.AwardDetailId.ToString();

                        TRP_AwardDetail detailModel = detailDi.getBll().GetEntityById(awardId);

                        //奖品名称
                        string awardsName = "";

                        //加密奖品id
                        string ecodeAwardId = DESEncrypt.Encrypt(awardId, _key);
                        Common.Helper.Logger.Info("发生了重定向奖品Id加密:" + ecodeAwardId);

                        if (detailModel != null)
                        {
                            //奖品名称
                            awardsName = detailModel.AwardName;
                            Common.Helper.Logger.Info("发生了重定向奖品名称:" + awardsName);
                        }

                        if (!string.IsNullOrWhiteSpace(awardsName))
                        {
                            string awardsType = awardsName;
                            string typeCode   = "";
                            if (awardsType.Contains("美食券"))
                            {
                                typeCode = "A";
                            }
                            else if (awardsType.Contains("电影券"))
                            {
                                typeCode = "B";
                            }
                            else if (awardsType.Contains("水趣多"))
                            {
                                typeCode = "C";
                            }
                            else
                            {
                                typeCode = "D";
                                Common.Helper.Logger.Info(string.Format("重定向:微信用户-OpenId:{0}-领取奖品,未接收到奖品类型,默认发瓶水",
                                                                        model.Openid));
                            }
                            ViewData["Type"] = typeCode;
                            Common.Helper.Logger.Info("发生了重定向奖品type:" + typeCode);
                        }
                        else
                        {
                            ViewData["Type"] = "";
                        }

                        ViewData["Openid"]        = model.Openid ?? "";
                        ViewData["wxName"]        = model.Nickname ?? "";
                        ViewData["AwardDetailId"] = ecodeAwardId;
                        ViewData["AwardName"]     = awardsName;
                        ViewData["Activity"]      = request.activityId ?? "";

                        entity.Description = string.Format("用户在{0}点击红包,二维码实物扫码(为重复扫码,上次未核销奖品)", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        entity.PageDesc    = "实物扫码,上次未核销奖品";
                        entity.ActivityId  = Convert.ToInt32(request.activityId);
                        logDi.getBll().SaveLog(entity);
                        return(View());
                    }
                    //return View("HasAttend");
                }

                string activityId = request.activityId;
                //http://localhost:89/RedPacketService/GetRedPacketServie?activityId=44
                //获取奖品信息
                string awards = httpHelper.HttpGet(String.Format("http://localhost:89/RedPacketService/GetRedPacketServie?activityId={0}", activityId), "");

                if (string.IsNullOrWhiteSpace(awards))
                {
                    return(View("Error"));
                }

                //反序列化获取奖品信息
                AwardsInfoModel awardsInfo = JsonConvert.DeserializeObject <AwardsInfoModel>(awards);
                if (awardsInfo != null)
                {
                    if (awardsInfo.Class != null)
                    {
                        string awardsType = awardsInfo.Class;
                        string typeCode   = "";
                        if (awardsType.Contains("美食券"))
                        {
                            typeCode = "A";
                        }
                        else if (awardsType.Contains("电影券"))
                        {
                            typeCode = "B";
                        }
                        else if (awardsType.Contains("水趣多"))
                        {
                            typeCode = "C";
                        }
                        else
                        {
                            typeCode = "D";
                            Common.Helper.Logger.Info(string.Format("获取奖品:微信用户-OpenId:{0}-领取奖品,未接收到奖品类型,默认发瓶水",
                                                                    model.Openid));
                        }
                        ViewData["Type"] = typeCode;
                    }
                }



                //首次参加本次活动
                status = ActivityStatus.FirstTime;

                //不存在微信用户
                if (!isExistOpenId(model.Openid))
                {
                    //保存用户微信信息
                    saveUserInfo(model);
                }

                //保存扫码信息
                saveScanInfo(model.Openid, awardsInfo.id);

                string awardName = awardsInfo.Class;

                if (status == ActivityStatus.FirstTime)
                {
                    entity.Description = string.Format("用户在{0}点击红包,二维码扫码进入实物领奖页面,奖品为{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), awardName);
                    entity.PageDesc    = string.Format("实物扫码,得到{0}", awardName);
                    entity.ActivityId  = Convert.ToInt32(DESEncrypt.Decrypt(awardsInfo.id, _key));
                    logDi.getBll().SaveLog(entity);
                    ViewData["Openid"]        = model.Openid ?? "";
                    ViewData["wxName"]        = model.Nickname ?? "";
                    ViewData["AwardDetailId"] = awardsInfo.id ?? "";
                    ViewData["AwardName"]     = awardsInfo.Class ?? "";
                    ViewData["Activity"]      = request.activityId ?? "";
                    return(View());
                }
            }
            catch (Exception ex)
            {
                Common.Helper.Logger.Info(string.Format("进入领奖页面异常,异常信息:{0}", ex.ToString()));
            }

            return(View("Error"));
        }