Ejemplo n.º 1
0
        /// <summary>
        /// 1.根据classid找到课程助力配置
        /// 2.根据openid判断是否助力过
        /// 3.生成该openid该课程助力数据
        /// 4.更新助力人的助力人数
        /// 5.插入助力人员表
        /// </summary>
        /// <param name="openid"></param>
        /// <param name="classid"></param>
        /// <returns></returns>
        public ActionResult HelpClass(string code, string state, string classid, string ownOpenid, string helpId)
        {
            string msg      = string.Empty;
            string link     = string.Empty;
            string fileName = string.Empty;
            string openid   = "";

            try
            {
                if (new AppSetting().IsDebug != null &&
                    new AppSetting().IsDebug.ToLower() == "true")
                {
                    openid = "123";
                }
                else
                {
                    if (Request.Cookies[SystemConfig.WXOpenIDCookieKey] != null)
                    {
                        openid = Request.Cookies[SystemConfig.WXOpenIDCookieKey].Value;
                    }
                    if (string.IsNullOrEmpty(openid) && string.IsNullOrEmpty(code))
                    {
                        Response.Redirect(CommonHelper.GetRedirect("WxOrder%2fHelpClass?helpId=" + helpId));
                    }
                    try
                    {
                        if (string.IsNullOrWhiteSpace(openid))
                        {
                            openid = GetOpenId(code).openid;


                            // 合法用户,允许访问
                            Response.Cookies[SystemConfig.WXOpenIDCookieKey].Value   = openid;
                            Response.Cookies[SystemConfig.WXOpenIDCookieKey].Path    = "/";
                            Response.Cookies[SystemConfig.WXOpenIDCookieKey].Expires = DateTime.Now.AddDays(1);
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }



                OrderBC bc = new OrderBC();

                HelpInfoEntity entity = new HelpInfoEntity();
                if (!string.IsNullOrEmpty(helpId))
                {
                    entity    = bc.GetHelpByHelpInfoId(helpId);
                    ownOpenid = entity.OpenId;
                    classid   = entity.ClassId;
                }
                var config = bc.GetHelpConfigByClassId(classid);
                if (!string.IsNullOrEmpty(ownOpenid) && !string.IsNullOrEmpty(classid))
                {
                    entity = bc.GetHelpByOpenIdAndClassId(classid, ownOpenid);
                }


                if (ownOpenid != openid)//非发起用户进入
                {
                    var helpinfo = bc.GetHelpMemberByOpenid(openid);
                    if (helpinfo != null)
                    {
                        msg = "该用户已助力";
                        return(Json(msg, JsonRequestBehavior.AllowGet));
                    }

                    helpId = Guid.NewGuid().ToString();

                    //生成二维码
                    SaveIamge QR = CreateQR(helpId);
                    //添加文字水印
                    string WxName = new WxUserBC().GetWxUserByOpenid(openid).Nickname;

                    string path = HttpContext.Server.MapPath("/Images/upload/");

                    string[] sArray = Regex.Split(config.ImageUrl, "Images/upload/", RegexOptions.IgnoreCase);

                    string    filename = sArray[1].ToString();
                    SaveIamge WordsPic = new WaterImageManager().DrawWordsForSaveIamge(filename, path, WxName, 1, FontFamilys.宋体, FontStyle.Bold, ImagePosition.TopMiddle);
                    //添加二维码水印
                    string QrPic = new WaterImageManager().DrawImage(WordsPic.filename, WordsPic.showImg, QR.filename, QR.showImg, 1, ImagePosition.BottomMiddle);
                    LogHelp.WriteLog("QrPic:::" + QrPic);
                    fileName = QrPic;
                    link     = CommonHelper.LinkImageUrl("/Images/upload/" + QrPic);

                    HelpInfoEntity help = new HelpInfoEntity()
                    {
                        HelpInfoId = helpId,
                        ClassId    = classid,
                        OpenId     = openid,
                        NowCount   = 0,
                        imgUrl     = link
                    };

                    var addResult = bc.AddHelpInfo(help, openid);
                    //增加助力记录
                    HelpMemberEntity member = new HelpMemberEntity()
                    {
                        HelpInfoId = entity.HelpInfoId,
                        OpenId     = openid,
                    };
                    var addMember = bc.AddHelpMember(member, openid);
                    //更新助力信息人数
                    var updateInfo = bc.UpdateHelpNowCount(entity.HelpInfoId, entity.NowCount + 1);

                    entity = bc.GetHelpByHelpInfoId(entity.HelpInfoId);

                    int diff   = config.HelpCount - entity.NowCount;
                    var wxUser = new WxUserBC().GetWxUserByOpenid(entity.OpenId);
                    if (diff <= 0)
                    {
                        string      OrderNo = CommonHelper.CreateOrderNo();
                        OrderEntity order   = new OrderEntity()
                        {
                            OrderNo     = OrderNo,
                            Openid      = entity.OpenId,
                            PayPrice    = 0,
                            OrderSource = "4",
                            Nickname    = wxUser.Nickname
                        };
                        List <OrderGoodsEntity> goods = new List <OrderGoodsEntity>();

                        var classEntity       = new ClassBC().GetClassByKey(classid);
                        OrderGoodsEntity good = new OrderGoodsEntity()
                        {
                            ClassId   = classEntity.ClassId,
                            ClassName = classEntity.ClassName,
                            Price     = 0
                        };
                        goods.Add(good);

                        var result = new OrderBC().SaveOrder(order, goods, entity.OpenId);


                        msg = "助力成功";
                    }
                }
                else//发起用户进入
                {
                    LogHelp.WriteLog("HelpClass:::22222");
                    if (entity != null)//如果主力已存在,展示助力情况
                    {
                        link = entity.imgUrl;
                        int diff = config.HelpCount - entity.NowCount;
                        if (diff > 0)
                        {
                            msg = string.Format("还差 {0} 人助力成功", diff);
                            //return Json(msg, JsonRequestBehavior.AllowGet);
                        }
                        else
                        {
                            msg = string.Format("已助力成功");
                            //return Json(msg, JsonRequestBehavior.AllowGet);
                        }
                    }
                    else //如果助力不存在,新增助力
                    {
                        helpId = Guid.NewGuid().ToString();
                        //生成二维码
                        SaveIamge QR = CreateQR(helpId);
                        //添加文字水印
                        string WxName = new WxUserBC().GetWxUserByOpenid(ownOpenid).Nickname;
                        string path   = HttpContext.Server.MapPath("/Images/upload/");

                        string[]  sArray   = Regex.Split(config.ImageUrl, "Images/upload/", RegexOptions.IgnoreCase);
                        string    filename = sArray[1].ToString();
                        SaveIamge WordsPic = new WaterImageManager().DrawWordsForSaveIamge(filename, path, WxName, 1, FontFamilys.宋体, FontStyle.Bold, ImagePosition.TopMiddle);
                        //添加二维码水印
                        string QrPic = new WaterImageManager().DrawImage(WordsPic.filename, WordsPic.showImg, QR.filename, QR.showImg, 1, ImagePosition.BottomMiddle);
                        fileName = QrPic;
                        link     = CommonHelper.LinkImageUrl("/Images/upload/" + QrPic);

                        HelpInfoEntity help = new HelpInfoEntity()
                        {
                            HelpInfoId = helpId,
                            ClassId    = classid,
                            OpenId     = ownOpenid,
                            NowCount   = 0,
                            imgUrl     = link
                        };
                        var addResult = bc.AddHelpInfo(help, ownOpenid);

                        msg = "分享图片,请好友帮忙助力吧";
                    }
                }

                //string media_id = Util.uploadMedia(HttpContext.Server.MapPath("/Images/upload/") + fileName, fileName);
                //Util.SendCustomMessage(openid, media_id);
            }
            catch (Exception ex)
            {
                LogHelp.WriteLog("HelpClass:::" + ex.Message);
            }


            ViewBag.Message = msg;
            ViewBag.Link    = link;

            return(View());
        }