赞
踩
为什么能裂变推广是营销学和心理学的范畴了,这里举个简单的例子来介绍。公众号有足以吸引粉丝的地方,要么是知识、要么是优惠、要么是服务,反正不同于其他人,能够吸引人关注;有了这个前提,怎么让更多的人知道这个公众号,要有很多的推广人,俗称合伙人,合伙人每推广一个粉丝关注,都有回报,要么短期的一次性回报,要么长期的粉丝消费酬金回报。下面通过实例讲解:
一、演示应用过程
1、对公众号输入合伙人
2、获取合伙人自己定制的海报,内置公众号分配给的带参数二维码
3、推荐他人享受优惠,扫码关注
二、实现逻辑
1、注册合伙人,通过手机号验证是合伙人本人,从空闲的二维码清单中给其分配一个。上篇文章给出了这个二维码清单的数据表。
代码以后再讲。
2、再来看微信用户关注事件,有普通关注和扫描带参数二维码的关注,普通关注之前的文章讲过了,这里重点介绍微信用户扫描带参数二维码事件的处理。用户扫描带场景值二维码时,推送以下两种事件:
(1)如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件推送给开发者。
(2)如果用户已经关注公众号,则微信会将带场景值扫描事件推送给开发者。
用户未关注时,进行关注后的事件推送
推送XML数据包示例:
- <xml>
- <ToUserName><![CDATA[toUser]]></ToUserName>
- <FromUserName><![CDATA[FromUser]]></FromUserName>
- <CreateTime>123456789</CreateTime>
- <MsgType><![CDATA[event]]></MsgType>
- <Event><![CDATA[subscribe]]></Event>
- <EventKey><![CDATA[qrscene_123123]]></EventKey>
- <Ticket><![CDATA[TICKET]]></Ticket>
- </xml>
注意上面EventKey的内容只取_之后的数字,用于获取weixin_qrcode_ticket表中scene_id对应的二维码图片。因为已经事先批量下载准备好二维码,无需通过Ticket方式再次获取。
三、相关源码
在接收到微信服务器发来的事件之后,首先由指定的接口网页AccessWx.aspx对事件类型进行判别,然后交给指定的事件处理类处理和回应。在本系列文章第1篇介绍的AccessWx.aspx.cs中开始部分引入命名空间using QinMing.Weixin.EventHandlerSubscribe;
并完善下面处理subscribe这一段,增加本篇给出的关注事件处理环节。
- else if(MsgType == "event")
- {
- string Event = xn.SelectSingleNode("//Event").InnerText;
- if(Event == "subscribe")
- {
- //对用户关注公众号的事件处理,使用QinMing.Weixin.EventHandlerSubscribe命名空间下的SubscribeEventDeal类
- SubscribeEventDeal ued = new SubscribeEventDeal();
- Response.Write(ued.DealResult(weixinXML));
- }
- }
在命名空间QinMing.Weixin.EventHandlerSubscribe下新建一个类SubscribeEventDeal,用来处理微信服务器发来的关注事件消息。记得类源码文件要放在App_Code目录下!下面是关注事件消息处理源码,这段在第(10)篇中讲过一部分,当时还没加入扫码带参数二维码的处理以及发红包处理。
QinMingWeixinEventHandlerSubscribe.cs文件内容如下:
- using System;
- using System.Web;
- using System.Xml;
- using System.Text;
- using System.Data;
- using System.Data.SqlClient;
- using System.IO;
- using System.Net;
- using System.Threading;
- using System.Threading.Tasks;
- using QinMing.Config;
- using QinMing.Tools;
- using QinMing.Tools.DB;
- using QinMing.Weixin.ReturnContent;
- using QinMing.WeixinUserInfo;
- using QinMing.WeixinPayPayment;
-
- namespace QinMing.Weixin.EventHandlerSubscribe
- {
- //事件消息处理:关注公众号
- public class SubscribeEventDeal :System.Web.UI.Page
- {
-
- public string DealResult(string weixinXML)
- {
- string content = DealSubscribe(weixinXML);
- return content;
- }
-
- public string DealSubscribe(string weixinXML)
- {
- string content1="";
- string strresponse = "";
- XmlDocument doc = new XmlDocument();
- doc.LoadXml(weixinXML);
- XmlNodeList list = doc.GetElementsByTagName("xml");
- XmlNode xn = list[0];
- string FromUserName = xn.SelectSingleNode("//FromUserName").InnerText; //关注用户的加密后openid
- string ToUserName = xn.SelectSingleNode("//ToUserName").InnerText; //公众微信号原始ID
- string MsgType=xn.SelectSingleNode("//MsgType").InnerText;
- string Event=xn.SelectSingleNode("//Event").InnerText;
- string EventKey = xn.SelectSingleNode("//EventKey").InnerText;
- ReturnMsg rm = new ReturnMsg();
-
- if(String.IsNullOrEmpty(EventKey)) //常规关注
- {
- DealJoin(FromUserName);
- SaveEventCommon(FromUserName, ToUserName);
- content1 = "欢迎您:\n\n"
- + "[玫瑰]超值优惠券[玫瑰]\n"
- + "肯德基、麦当劳、汉堡王、德克士、必胜客、星巴克、Coco、猫眼、美团外卖、饿了么、滴滴等上百家全国知名连锁商家4折起优惠券,随时买随时用,无需加会员和预充值,全国门店通用!\n\n"
- + "<a href='https://' >点击这里购买!</a>"
- + "\n\n"
- + "<a href='https://' >点击这里购买!!</a>"
- + "\n\n"
- + "<a href='https://' >点击这里购买!!!</a>"
- + "\n\n"
- + "您也可以通过菜单”超值优惠券“进入购买页面,总有惊喜在等您哦!";
- strresponse = rm.ReturnText(FromUserName, ToUserName, content1);
-
- //给管理员发送新粉丝关注通知
- QinMing.WeixinTemplateMessage.SendTemplateMessage.SendRemindMsg("管理员openid", "新粉丝关注提醒(来源:普通关注)" + FromUserName, "http://www.yourweb.com/Weixin/DisplayOneUser.aspx?open_id=" + FromUserName); //告知管理员有新粉丝关注
- }
- else //扫描带参数的二维码关注,一人一码、一店一码
- {
- DealJoin(FromUserName);
- string tmpEventKey = EventKey.Substring(EventKey.LastIndexOf("_") + 1);
- string ticket = xn.SelectSingleNode("//Ticket").InnerText;
- SaveEventScan(FromUserName, ToUserName, tmpEventKey, ticket);
- if(tmpEventKey == "10") //某个指定的带参数二维码scene_id
- {
-
- }
- else
- {
- strresponse = ScanWithQrCodeNew(FromUserName, ToUserName, tmpEventKey);
- }
-
- }
-
- return strresponse;
- }
-
- //记录粉丝关注公众号时的相关信息
- public void DealJoin(string FromUserName)
- {
- SqlConnection conn = new SqlConnection(QinMingConfig.DatabaseConnStr);
- conn.Open();
- SqlCommand cmd = new SqlCommand();
- SqlDataReader dr;
- cmd.Connection = conn;
- cmd.CommandText = "insert into weixin_join_log (open_id,join_time,remove_flag) values('" + FromUserName + "',getdate(),0)";
- cmd.ExecuteScalar();
-
- cmd.CommandText = "select * from weixin_user_info where open_id ='" + FromUserName + "' ";
- dr = cmd.ExecuteReader();
- if(dr.Read())
- {
- dr.Close();
- cmd.CommandText = "update weixin_user_info set remove_flag='已关注' where open_id='" + FromUserName + "'";
- cmd.ExecuteScalar(); //新增粉丝如曾关注过,取关后重新关注的,变更状态
- }
- else
- {
- dr.Close();
- cmd.CommandText = "insert into weixin_user_info (open_id,remove_flag,personal_score,join_time) values('" + FromUserName + "','已关注',0,getdate())";
- cmd.ExecuteScalar(); //新增粉丝如曾未关注过,增加粉丝记录
-
- //微信用户首次关注公众号时,发送一个随机红包。
- SendNewUserRedPacket(FromUserName);
- }
-
- if (conn.State == ConnectionState.Open)
- {
- conn.Close();
- conn.Dispose();
- }
-
- QinMingWeixinUserInfo wui=new QinMingWeixinUserInfo();
- wui.UpdateUserInfo(FromUserName); //获取用户信息并更新weixin_user_info表
- }
-
- /*给一个新关注公众号的粉丝发红包,仅限首次关注,取消关注后再次关注时没有此红包*/
- public void SendNewUserRedPacket(string open_id)
- {
- Random r = new Random();
- string ran1 = r.Next(1000, 9999).ToString();
- string ran2 = r.Next(随机红包最小值, 随机红包最大值).ToString();
- string out_trade_no = QinMingConfig.Weixin_MchId + DateTime.Now.ToString("yyyyMMddHHmmss") + ran1;
- QinMingWeixinPayPayment.SendRedPackOne("降价联盟", "新粉福利", open_id, ran2, "新粉关注红包", "", out_trade_no);
- }
-
- //用户扫描带参数的二维码时处理动作
- public string ScanWithQrCodeNew(string FromUserName, string ToUserName, string EventKey)
- {
- string strresponse = "";
- ReturnMsg rm = new ReturnMsg();
- string guishu_mobile = "";
- string guishu_name = "";
- string guishu_type = "";
- string scan_title = "";
- string scan_title_sub = "";
- string scan_image_url = "";
- string scan_redirect_url = "";
- string r_red_packet_min = "";
- string r_red_packet_max = "";
-
- SqlConnection conn = new SqlConnection(QinMingConfig.DatabaseConnStr);
- conn.Open();
- SqlCommand cmd = new SqlCommand();
- SqlDataReader dr;
- cmd.Connection = conn;
-
- cmd.CommandText = "select * from weixin_qrcode_ticket where scene_id=" + EventKey + " ";
- dr = cmd.ExecuteReader();
- if(dr.Read())
- {
- if(dr["scan_type"].ToString() == "优惠券")
- {
- guishu_mobile = dr["user_id"].ToString();
- guishu_name = dr["user_name"].ToString();
- guishu_type = dr["scan_type"].ToString();
- scan_title = dr["scan_title"].ToString();
- scan_title_sub = dr["scan_title_sub"].ToString();
- scan_image_url = dr["scan_image_url"].ToString();
- scan_redirect_url = dr["scan_redirect_url"].ToString();
- r_red_packet_min = dr["r_red_packet_min"].ToString(); //合伙人推荐单粉红包最小值
- r_red_packet_max = dr["r_red_packet_max"].ToString(); //合伙人推荐单粉红包最大值
- Random r = new Random();
- int RandomRedpack = r.Next(int.Parse(r_red_packet_min), int.Parse(r_red_packet_max));
-
- strresponse = rm.ReturnText(FromUserName,ToUserName,"欢迎您:[咖啡]\n\n"
- + "[玫瑰]超值优惠券[玫瑰]\n"
- + "肯德基、麦当劳、汉堡王、德克士、必胜客、星巴克、Coco、猫眼、美团外卖、饿了么、滴滴等上百家全国知名连锁商家4折起优惠券,随时买随时用,无需加会员和预充值,全国门店通用!\n\n"
- + "<a href='https://' >点击这里购买!</a>"
- + "\n\n"
- + "<a href='https://' >点击这里购买!!</a>"
- + "\n\n"
- + "<a href='https://' >点击这里购买!!!</a>"
- + "\n\n"
- + "您也可以通过菜单”超值优惠券“进入购买页面,总有惊喜在等您哦!");
-
- dr.Close();
-
- //给推荐的合伙人发红包,用生成红包记录自行提现方式;非首次扫码关注公众号,不发推荐红包包
- /**/
- if(int.Parse(QinMingToolsDB.SearchOneField("select count(*) r_m from weixin_user_info where open_id= '" + FromUserName + "'","r_m")) >= 1 )
- {
- //如果用户表中已经存在微信用户openid,则表示非首次关注,不给推荐人发红包
- }
- else
- {
- //如果用户表中没有微信用户openid,则表示首次关注,给推荐人发红包;在推荐红包表中追加记录,需推荐人自行提现
- string test = CreateScanQrCodeRedpack(FromUserName, guishu_mobile, guishu_type, RandomRedpack.ToString());
- }
-
-
- //如果是带参数二维码扫码关注,更新新粉丝扫码推荐人归属信息。
- cmd.CommandText = "update weixin_user_info set guishu_mobile='" + guishu_mobile + "',guishu_type ='" + guishu_type + "' where remove_flag='已关注' and open_id='" + FromUserName + "' ";
- cmd.ExecuteScalar();
-
- //告知管理员有新粉丝扫码关注
- QinMing.WeixinTemplateMessage.SendTemplateMessage.SendRemindMsg("管理员openid", "新粉丝关注提醒(来源:" + guishu_name + ")" + FromUserName, "http://www.yourweb.com/Weixin/DisplayOneUser.aspx?open_id=" + FromUserName);
-
- //插入带参数二维码被扫码的记录
- cmd.CommandText = "insert into weixin_ad_image_read_log (share_qrcode_id,read_open_id,read_time) values ("
- + EventKey + ",'" + FromUserName + "',getdate())"; //EventKey对应的是weixin_qrcode_ticket表中的scene_id,二维码分配关系
- cmd.ExecuteScalar();
- }
- else
- {
- dr.Close();
- }
-
- if (conn.State == ConnectionState.Open)
- {
- conn.Close();
- conn.Dispose();
- }
- }
-
- return strresponse;
- }
-
- //微信用户通过扫描带参数二维码关注公众号时,给推荐扫码的人追加一条红包记录
- public string CreateScanQrCodeRedpack(string FromUserName, string guishu_mobile, string guishu_type, string redpack_fee)
- {
- string order_id = QinMingToolsDB.SearchOneField("select isnull(max(order_id),0)+1 r_num from weixin_redpack_log_yhq_spread","r_num");
- string fromopenid = QinMingToolsDB.SearchOneField("select open_id from weixin_bind_mobile_all where mobile = '"+ guishu_mobile +"'","open_id");
- string scene_id = QinMingToolsDB.SearchOneField("select top 1 * from weixin_qrcode_ticket where user_id = '"+ guishu_mobile +"' and scan_type = '"+ guishu_type +"'","scene_id");
- string fromname = QinMingToolsDB.SearchOneField("select top 1 * from weixin_qrcode_ticket where user_id = '"+ guishu_mobile +"' and scan_type = '"+ guishu_type +"'","user_name");
- string strsql = "insert into weixin_redpack_log_yhq_spread(order_id,open_id,gen_time,redpack_class,redpack_fee,scene_id,from_user_id,from_user_name,from_open_id,remark,out_trade_no) \n"+
- "values("+ order_id +",'"+ FromUserName +"',getdate(),'优惠券推荐新粉红包',"+ redpack_fee +",'"+ scene_id +"','"+ guishu_mobile +"','"+ fromname +"','"+ fromopenid +"','','')\n";
- QinMingToolsDB.InsertTable(strsql);
-
- return "2"; //用户
- }
-
- //保存普通关注事件信息。
- public void SaveEventCommon(string FromUserName, string ToUserName)
- {
- SqlConnection conn = new SqlConnection(QinMingConfig.DatabaseConnStr);
- conn.Open();
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = conn;
- cmd.CommandText = "insert into weixin_recv_event (msg_type,event_type,open_id,gh_id,recv_time) "
- + "values ('event','subscribe','" + FromUserName + "','" + ToUserName + "',getdate()) ";
- QinMingTools.WriteLog("sql语句:", cmd.CommandText);
- cmd.ExecuteScalar();
-
- if (conn.State == ConnectionState.Open)
- {
- conn.Close();
- conn.Dispose();
- }
- }
-
- //保存扫描带参数二维码事件信息。
- public void SaveEventScan(string FromUserName, string ToUserName,string EventKey, string Ticket)
- {
- SqlConnection conn = new SqlConnection(QinMingConfig.DatabaseConnStr);
- conn.Open();
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = conn;
- cmd.CommandText = "insert into weixin_recv_event (msg_type,event_type,open_id,gh_id,recv_time,event_key,ticket) "
- + "values ('event','subscribe','" + FromUserName + "','" + ToUserName + "',getdate(),'" + EventKey + "','" + Ticket + "') ";
- QinMingTools.WriteLog("sql语句:", cmd.CommandText);
- cmd.ExecuteScalar();
-
- if (conn.State == ConnectionState.Open)
- {
- conn.Close();
- conn.Dispose();
- }
- }
-
- }
- }
上面代码中用到了数据库操作类Tools.DB;还有微信支付类QinMingWeixinPayPayment中的发送红包操作SendRedPackOne,后期讲解微信支付时给出源码。
四、应用演示
1、新粉丝红包
2、合伙人推荐红包
收到红包的感觉是不是很爽?
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。