protected void Page_Load(object sender, EventArgs e) { int wid = MyCommFun.RequestWid(); int actid = MyCommFun.RequestInt("aid"); string thisUrl = MyCommFun.getWebSite() + "/weixin/cashred/index.aspx?wid=" + wid + "&aid=" + actid; //授权 BLL.wx_userweixin bll = new BLL.wx_userweixin(); Model.wx_userweixin uWeiXinModel = bll.GetModel(wid); OAuth2BaseProc(uWeiXinModel, "index", thisUrl); //授权 end BLL.wx_xjhongbao_action actBll = new BLL.wx_xjhongbao_action(); act = actBll.GetPageHongBaoModel(actid, wid); if (act == null) { litActionRemark.Text = "该时间段没有该活动"; return; } //jssdk this.Title = act.act_name; fxModel.fxImg = MyCommFun.getWebSite() + "" + act.share_imgurl; fxModel.fxTitle = act.act_name; fxModel.fxContent = act.share_content; jssdkInit(uWeiXinModel); //jssdk end ActionBaseInfo(wid); }
/// <summary> /// 关注时红包 /// </summary> /// <param name="openid"></param> /// <param name="wid"></param> public void SubscribeHongBao(string openid, int wid) { BLL.wx_xjhongbao_action actBll = new BLL.wx_xjhongbao_action(); bool has = actBll.ExistsSubscribeAction(wid);//这个时间段有红包 if (has) { //如果有关注时红包,则给人家发红包 Model.wx_xjhongbao_action actModel = actBll.GetGZHBModelByWid(wid); proecssSendHB(openid, wid, actModel); } }
/// <summary> /// 显示信息 /// </summary> /// <param name="id">活动的id</param> protected void Show(int id) { ddlhbType.Enabled = false; //不启用 Model.wx_userweixin weixin = GetWeiXinCode(); Model.wx_xjhongbao_action xjModel = xjactBll.GetModel(id); //活动基本信息 txtact_name.Text = xjModel.act_name; hidid.Value = xjModel.id.ToString(); ddlhbType.SelectedValue = xjModel.hbType.Value.ToString(); lblLink.Text = MyCommFun.getWebSite() + "/weixin/cashred/index.aspx?wid=" + weixin.id + "&aid=" + id; txtactPic.Text = xjModel.actPic; imgactPic.ImageUrl = xjModel.actPic; txtbeginDate.Text = xjModel.beginDate.ToString(); txtendDate.Text = xjModel.endDate.ToString(); radlqType.SelectedValue = xjModel.lqType.ToString(); txttotalMoney.Text = (xjModel.totalMoney / 100.00).ToString(); lblLqMoney.Text = (xjModel.totalLqMoney / 100.00).ToString(); radmoneyType.SelectedValue = xjModel.moneyType.ToString(); txtmin_value.Text = (xjModel.min_value / 100.00).ToString(); txtmax_value.Text = (xjModel.max_value / 100.00).ToString(); txtremark.Value = xjModel.remark; txtKW.Text = xjModel.keywords; //if (xjModel.hbType == 1) //{ // Model.wx_requestRule rule = rBll.GetModelList("modelFunctionName='现金红包' and modelFunctionId=" + id)[0]; // txtKW.Text = rule.reqKeywords; //} //红包相关的参数 txtwishing.Text = xjModel.wishing; txtnick_name.Text = xjModel.nick_name; txtsend_name.Text = xjModel.send_name; imglogo_imgurl.ImageUrl = xjModel.logo_imgurl; txtlogo_imgurl.Text = xjModel.logo_imgurl; txtclient_ip.Text = xjModel.client_ip; //分享 txtshare_content.Value = xjModel.share_content; txtshare_url.Value = xjModel.share_url; imgshare_imgurl.ImageUrl = xjModel.share_imgurl; txtshare_imgurl.Text = xjModel.share_imgurl; }
protected void Page_Load(object sender, EventArgs e) { this.keywords = MXRequest.GetQueryString("keywords"); actid = MyCommFun.RequestInt("id"); Model.wx_userweixin weixin = GetWeiXinCode(); if (!xjactBll.Exists(actid, weixin.id)) { JscriptMsg("记录不存在或已被删除!", "back", "Error"); return; } Model.wx_xjhongbao_action actModel = xjactBll.GetModel(actid); lblActName.Text = actModel.act_name; this.pageSize = GetPageSize(10); //每页数量 if (!Page.IsPostBack) { RptBind(actid, CombSqlTxt(keywords), "Send_time desc,id desc"); } }
/// <summary> /// 处理关键词红包 /// </summary> /// <param name="keywords"></param> /// <param name="openid"></param> /// <param name="wid"></param> public string KeyWordsHongBao(string keywords, string openid, int wid) { BLL.wx_xjhongbao_action actBll = new BLL.wx_xjhongbao_action(); bool has = actBll.ExistsKeyWordsAction(wid, keywords);//这个时间段有红包 if (has) { //如果有关注时红包,则给人家发红包 Model.wx_xjhongbao_action actModel = actBll.GetKeyWordsModelByWid(wid, keywords); try { proecssSendHB(openid, wid, actModel); } catch (Exception ex) { WXLogs.AddLog(wid, "微信红包", actModel.act_name, "KeyWordsHongBao() 现金红包领取失败:" + ex.Message); } } return(""); }
//保存 protected void btnSubmit_Click(object sender, EventArgs e) { Model.wx_userweixin weixin = GetWeiXinCode(); int id = MyCommFun.Str2Int(hidid.Value); #region //先判断 string strErr = ""; if (this.txtact_name.Text.Trim().Length == 0) { strErr += "活动名称不能为空!"; } if (ddlhbType.SelectedItem.Value == "") { strErr += "活动类型不能为空!"; } if (ddlhbType.SelectedItem.Value == "1") { if (this.txtKW.Text.Trim().Length == 0) { strErr += "关键词不能为空!"; } } if (this.txtbeginDate.Text.Trim().Length == 0 || !MyCommFun.isDateTime(txtbeginDate.Text)) { strErr += "开始时间不能为空!"; } if (this.txtendDate.Text.Trim().Length == 0 || !MyCommFun.isDateTime(txtendDate.Text)) { strErr += "结束时间不能为空!"; } if (this.txtwishing.Text.Trim().Length == 0) { strErr += "祝福语不能为空!"; } if (this.txtnick_name.Text.Trim().Length == 0) { strErr += "提供方名称不能为空!"; } if (this.txtsend_name.Text.Trim().Length == 0) { strErr += "商户名称不能为空!"; } if (this.txtclient_ip.Text.Trim().Length == 0) { strErr += "调用接口的机器 Ip 地址不能为空!"; } if (this.txttotalMoney.Text.Trim().Length == 0) { strErr += "红包总金额不能为空!"; } if (strErr != "") { JscriptMsg(strErr, "back", "Error"); return; } DateTime begin = DateTime.Parse(txtbeginDate.Text.Trim()); DateTime end = DateTime.Parse(txtendDate.Text.Trim()); if (begin >= end) { JscriptMsg("开始时间必须小于结束时间", "back", "Error"); return; } if (ddlhbType.SelectedItem.Value == "0") { //如果是关注红包,则要判断该微账号同一时间段是否已经包含了关注红包 IList <Model.wx_xjhongbao_action> actlist = xjactBll.GetModelList("wid=" + weixin.id + " and id!=" + id + " and hbType=0 and endDate>='" + begin.ToString() + "' and beginDate<='" + end.ToString() + "' "); if (actlist != null && actlist.Count > 0) { JscriptMsg("该时间段内不能有2个关注红包", "back", "Error"); return; } } #endregion #region 赋值 Model.wx_xjhongbao_action xjActionModel = new Model.wx_xjhongbao_action(); if (id > 0) { //修改 xjActionModel = xjactBll.GetModel(id); } xjActionModel.act_name = txtact_name.Text.Trim(); xjActionModel.actPic = txtactPic.Text.Trim(); xjActionModel.beginDate = MyCommFun.Obj2DateTime(txtbeginDate.Text); xjActionModel.endDate = MyCommFun.Obj2DateTime(txtendDate.Text); xjActionModel.lqType = MyCommFun.Str2Int(radlqType.Text); xjActionModel.totalMoney = (int)(MyCommFun.Str2Decimal(txttotalMoney.Text) * 100); xjActionModel.moneyType = MyCommFun.Str2Int(radmoneyType.SelectedItem.Value); xjActionModel.min_value = (int)(MyCommFun.Str2Decimal(txtmin_value.Text) * 100); xjActionModel.keywords = txtKW.Text.Trim(); if (ddlhbType.SelectedItem.Value == "0") { //如果红包为关注时红包,则领取方式为:一次性 xjActionModel.lqType = 0; } if (xjActionModel.moneyType == 0) { xjActionModel.max_value = xjActionModel.min_value; } else { xjActionModel.max_value = (int)(MyCommFun.Str2Decimal(txtmax_value.Text) * 100); } xjActionModel.remark = txtremark.Value; //红包参数 xjActionModel.wishing = txtwishing.Text.Trim(); xjActionModel.nick_name = txtnick_name.Text.Trim(); xjActionModel.send_name = txtsend_name.Text.Trim(); xjActionModel.logo_imgurl = txtlogo_imgurl.Text.Trim(); xjActionModel.client_ip = txtclient_ip.Text.Trim(); //分享的参数 xjActionModel.share_content = txtshare_content.Value.Trim(); xjActionModel.share_url = txtshare_url.Value.Trim(); xjActionModel.share_imgurl = txtshare_imgurl.Text.Trim(); if (id > 0) { //修改 bool updateOK = xjactBll.Update(xjActionModel); //if (xjActionModel.hbType == 1) //{ // //添加关键词 // IList<Model.wx_requestRule> rlist = rBll.GetModelList("modelFunctionName = '现金红包' and modelFunctionId=" + id); // if (rlist != null && rlist.Count > 0) // { // Model.wx_requestRule rule = new Model.wx_requestRule(); // rule = rlist[0]; // rule.reqKeywords = txtKW.Text.Trim(); // rBll.Update(rule); // } // else // { // AddRule(weixin.id, id); // } //} AddAdminLog(MXEnums.ActionEnum.Edit.ToString(), "修改现金红包活动,主键为" + id); //记录日志//1e2124dd04e11d01b9df2865f85944be JscriptMsg("修改现金红包活动成功!", "actionmgr.aspx", "Success"); } else { //新增 xjActionModel.hbType = MyCommFun.Str2Int(ddlhbType.Text.Trim()); xjActionModel.totalLqMoney = 0; xjActionModel.wid = weixin.id; xjActionModel.createDate = DateTime.Now; int addId = xjactBll.Add(xjActionModel); //if (xjActionModel.hbType == 1) //{ // //添加关键词 // AddRule(weixin.id, addId); //} AddAdminLog(MXEnums.ActionEnum.Add.ToString(), "添加现金红包活动,主键为" + addId); //记录日志//1e2124dd04e11d01b9df2865f85944be JscriptMsg("添加现金红包活动成功!", "actionmgr.aspx", "Success"); } #endregion }
/// <summary> /// 处理发关注红包的逻辑 /// </summary> /// <param name="openid"></param> /// <param name="wid"></param> /// <param name="actModel"></param> public int proecssSendHB(string openid, int wid, Model.wx_xjhongbao_action actModel) { BLL.wx_xjhongbao_action actBll = new BLL.wx_xjhongbao_action(); BLL.wx_xjhongbao_base baseBll = new BLL.wx_xjhongbao_base(); Model.wx_xjhongbao_base baseModel = baseBll.GetModelByWid(wid); BLL.wx_payment_wxpay weixinPayBLL = new BLL.wx_payment_wxpay(); Model.wx_payment_wxpay weixinPayModel = weixinPayBLL.GetModelByWid(wid); if (baseModel == null || weixinPayModel == null || actModel == null) { return(0); } #region 取用户昵称--注释掉了 //string error = ""; //string accessToken=WeiXinCRMComm.getAccessToken(wid, out error); //if (error.Trim().Length > 0) //{ // WXLogs.AddErrLog(wid, "微信红包", actModel.act_name, "accessToken取值有错误error:"+error); // return; //} //string nick = UserApi.Info(accessToken, openid).nickname; #endregion //用户此次获得的红包(单位分) int hongbaoFen = 0; if (actModel.totalLqMoney >= actModel.totalMoney) { //1 红包金额已经领取完了,记录到日志 InsertToLQinfo(wid, actModel.id, openid, 0, DateTime.Parse("1970-1-1"), "", "", "", "", "红包余额不足,需要调整活动的金额"); return(0); } //2 判断该用户是否已经领取了 bool hasLQ = lqBll.ExistsOpenid(actModel.id, openid, actModel.lqType == 0 ? true : false); if (hasLQ) { //已经领取了 return(0); } int leftMoney = actModel.totalMoney.Value - actModel.totalLqMoney.Value;//剩余金额 //3 计算本次的红包 if (actModel.moneyType == 0) { //定额 hongbaoFen = actModel.min_value.Value; } else { //随机 Random rd = new Random(); hongbaoFen = rd.Next(actModel.min_value.Value, actModel.max_value.Value); } if (hongbaoFen >= leftMoney) { hongbaoFen = leftMoney; } //4 给用户发红包 string mchbillno = weixinPayModel.mch_id + DateTime.Now.ToString("yyyymmddHHmmss") + TenPayV3Util.BuildRandomStr(4); Model.wx_xjhongbao_lqinfo lqinfoEntity = new Model.wx_xjhongbao_lqinfo(); lqinfoEntity.wid = wid; lqinfoEntity.actionId = actModel.id; lqinfoEntity.openid = openid; lqinfoEntity.total_amount = hongbaoFen; lqinfoEntity.createDate = DateTime.Now; XmlDocument doc = new XmlDocument(); if (!SendHBBase(weixinPayModel, actModel, wid, openid, hongbaoFen, mchbillno, doc)) { //发红包的方法出现问题 WXLogs.AddErrLog(wid, "微信红包", actModel.act_name, "发现金红包底层方法报错。"); lqinfoEntity.Send_time = DateTime.Now; lqinfoEntity.mch_billno = mchbillno; lqinfoEntity.mch_id = weixinPayModel.mch_id; lqinfoEntity.hbstatus = "FAILED"; lqinfoEntity.send_type = "API"; lqinfoEntity.hb_type = "NORMAL"; lqinfoEntity.reason = "result_code:" + doc.InnerXml.ToString(); lqBll.Add(lqinfoEntity); //记录领取日志 WXLogs.AddLog(wid, "微信红包", actModel.act_name, "现金红包领取失败:" + doc.InnerXml.ToString()); return(0); } //5修改现金:1活动的领取金额增加;2用户的中奖记录增加 actModel = actBll.GetModel(actModel.id); actModel.totalLqMoney += hongbaoFen; actBll.Update(actModel); //6修改配置文件的领取金额; baseModel.totalLQMoney += hongbaoFen; baseBll.Update(baseModel); WXLogs.AddLog(wid, "微信红包", actModel.act_name, "现金红包领取成功2:" + doc.InnerXml.ToString()); lqinfoEntity.Send_time = MyCommFun.Obj2DateTime(MyCommFun.GetXmlNode("send_time", doc), DateTime.Now); lqinfoEntity.mch_billno = mchbillno; lqinfoEntity.mch_id = weixinPayModel.mch_id; lqinfoEntity.detail_id = MyCommFun.GetXmlNode("send_listid", doc); lqinfoEntity.hbstatus = "SUCCESS"; lqinfoEntity.send_type = "API"; lqinfoEntity.hb_type = "NORMAL"; lqinfoEntity.reason = ""; lqinfoEntity.remark = doc.InnerXml.ToString(); lqBll.Add(lqinfoEntity); WXLogs.AddLog(wid, "微信红包", actModel.act_name, "现金红包成功领取3:" + doc.InnerXml.ToString()); return(hongbaoFen); }
/// <summary> /// 发红包的基础表 /// </summary> /// <param name="baseModel"></param> /// <param name="actModel"></param> /// <param name="wid"></param> /// <param name="openid"></param> /// <param name="hbMoney">红包金额(分)</param> /// <returns></returns> private bool SendHBBase(Model.wx_payment_wxpay weixinPayModel, Model.wx_xjhongbao_action actModel, int wid, string openid, int hbMoney, string mchbillno, XmlDocument doc) { try { BLL.wx_userweixin bll = new BLL.wx_userweixin(); Model.wx_userweixin weixinModel = bll.GetModel(wid); if (actModel == null || weixinModel == null || weixinPayModel == null) { return(false); } string nonceStr = TenPayV3Util.GetNoncestr(); RequestHandler packageReqHandler = new RequestHandler(null); //设置package订单参数 packageReqHandler.SetParameter("nonce_str", nonceStr); //随机字符串 packageReqHandler.SetParameter("wxappid", weixinModel.AppId); //公众账号ID(Appid) packageReqHandler.SetParameter("mch_id", weixinPayModel.mch_id); //商户号(mch_id) packageReqHandler.SetParameter("mch_billno", mchbillno); //填入商家订单号 packageReqHandler.SetParameter("nick_name", actModel.nick_name); //提供方名称 packageReqHandler.SetParameter("send_name", actModel.send_name); //红包发送者名称 packageReqHandler.SetParameter("re_openid", openid); //接受收红包的用户的openId packageReqHandler.SetParameter("total_amount", hbMoney.ToString()); //付款金额,单位分 packageReqHandler.SetParameter("min_value", hbMoney.ToString()); //最小红包金额,单位分 packageReqHandler.SetParameter("max_value", hbMoney.ToString()); //最大红包金额,单位分 packageReqHandler.SetParameter("total_num", "1"); //红包发放总人数 packageReqHandler.SetParameter("wishing", actModel.wishing); //红包祝福语 packageReqHandler.SetParameter("client_ip", "1.0.0.1"); //调用接口的机器Ip地址.1.0.0.1 为无法获取客户端ip packageReqHandler.SetParameter("act_name", actModel.act_name); //活动名称 packageReqHandler.SetParameter("remark", actModel.remark); //备注信息 string sign = packageReqHandler.CreateMd5Sign("key", weixinPayModel.paykey); packageReqHandler.SetParameter("sign", sign); //签名 //发红包需要post的数据 string data = packageReqHandler.ParseXML(); //发红包接口地址 string url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"; //本地或者服务器的证书位置(证书在微信支付申请成功发来的通知邮件中) string cert = HttpContext.Current.Server.MapPath(weixinPayModel.certInfoPath); // @"F:\apiclient_cert.p12"; //私钥(在安装证书时设置) string password = weixinPayModel.cerInfoPwd; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); //调用证书 X509Certificate2 cer = new X509Certificate2(cert, password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet); #region 发起post请求 HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url); webrequest.ClientCertificates.Add(cer); webrequest.Method = "post"; byte[] postdatabyte = Encoding.UTF8.GetBytes(data); webrequest.ContentLength = postdatabyte.Length; Stream stream; stream = webrequest.GetRequestStream(); stream.Write(postdatabyte, 0, postdatabyte.Length); stream.Close(); HttpWebResponse httpWebResponse = (HttpWebResponse)webrequest.GetResponse(); StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()); string responseContent = streamReader.ReadToEnd(); doc.LoadXml(responseContent); WXLogs.AddErrLog(wid, "微信红包", actModel.act_name, "发现金红包底层方法,:xml内容:" + doc.InnerXml.ToString()); if (MyCommFun.GetXmlNode("return_code", doc).ToLower() == "success" && MyCommFun.GetXmlNode("result_code", doc).ToLower() == "success") { //发红包成功 return(true); } else { return(false); } #endregion } catch (Exception ex) { WXLogs.AddErrLog(wid, "微信红包", actModel.act_name, "发现金红包底层方法报错:" + ex.Message); return(false); } }