当前位置:   article > 正文

实例:用C#.NET手把手教你做微信公众号开发(12)--带参数二维码裂变推广_c#微信公众号开发源码

c#微信公众号开发源码

为什么能裂变推广是营销学和心理学的范畴了,这里举个简单的例子来介绍。公众号有足以吸引粉丝的地方,要么是知识、要么是优惠、要么是服务,反正不同于其他人,能够吸引人关注;有了这个前提,怎么让更多的人知道这个公众号,要有很多的推广人,俗称合伙人,合伙人每推广一个粉丝关注,都有回报,要么短期的一次性回报,要么长期的粉丝消费酬金回报。下面通过实例讲解:

一、演示应用过程

1、对公众号输入合伙人

2、获取合伙人自己定制的海报,内置公众号分配给的带参数二维码

3、推荐他人享受优惠,扫码关注

二、实现逻辑

1、注册合伙人,通过手机号验证是合伙人本人,从空闲的二维码清单中给其分配一个。上篇文章给出了这个二维码清单的数据表。

代码以后再讲。

2、再来看微信用户关注事件,有普通关注和扫描带参数二维码的关注,普通关注之前的文章讲过了,这里重点介绍微信用户扫描带参数二维码事件的处理。用户扫描带场景值二维码时,推送以下两种事件:

(1)如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件推送给开发者。

(2)如果用户已经关注公众号,则微信会将带场景值扫描事件推送给开发者。

用户未关注时,进行关注后的事件推送

推送XML数据包示例:

  1. <xml>
  2. <ToUserName><![CDATA[toUser]]></ToUserName>
  3. <FromUserName><![CDATA[FromUser]]></FromUserName>
  4. <CreateTime>123456789</CreateTime>
  5. <MsgType><![CDATA[event]]></MsgType>
  6. <Event><![CDATA[subscribe]]></Event>
  7. <EventKey><![CDATA[qrscene_123123]]></EventKey>
  8. <Ticket><![CDATA[TICKET]]></Ticket>
  9. </xml>

注意上面EventKey的内容只取_之后的数字,用于获取weixin_qrcode_ticket表中scene_id对应的二维码图片。因为已经事先批量下载准备好二维码,无需通过Ticket方式再次获取。

三、相关源码

在接收到微信服务器发来的事件之后,首先由指定的接口网页AccessWx.aspx对事件类型进行判别,然后交给指定的事件处理类处理和回应。在本系列文章第1篇介绍的AccessWx.aspx.cs中开始部分引入命名空间using QinMing.Weixin.EventHandlerSubscribe;

并完善下面处理subscribe这一段,增加本篇给出的关注事件处理环节。
 

  1. else if(MsgType == "event")
  2. {
  3. string Event = xn.SelectSingleNode("//Event").InnerText;
  4. if(Event == "subscribe")
  5. {
  6. //对用户关注公众号的事件处理,使用QinMing.Weixin.EventHandlerSubscribe命名空间下的SubscribeEventDeal类
  7. SubscribeEventDeal ued = new SubscribeEventDeal();
  8. Response.Write(ued.DealResult(weixinXML));
  9. }
  10. }

在命名空间QinMing.Weixin.EventHandlerSubscribe下新建一个类SubscribeEventDeal,用来处理微信服务器发来的关注事件消息。记得类源码文件要放在App_Code目录下!下面是关注事件消息处理源码,这段在第(10)篇中讲过一部分,当时还没加入扫码带参数二维码的处理以及发红包处理。
QinMingWeixinEventHandlerSubscribe.cs文件内容如下:
 

  1. using System;
  2. using System.Web;
  3. using System.Xml;
  4. using System.Text;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.IO;
  8. using System.Net;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using QinMing.Config;
  12. using QinMing.Tools;
  13. using QinMing.Tools.DB;
  14. using QinMing.Weixin.ReturnContent;
  15. using QinMing.WeixinUserInfo;
  16. using QinMing.WeixinPayPayment;
  17. namespace QinMing.Weixin.EventHandlerSubscribe
  18. {
  19. //事件消息处理:关注公众号
  20. public class SubscribeEventDeal :System.Web.UI.Page
  21. {
  22. public string DealResult(string weixinXML)
  23. {
  24. string content = DealSubscribe(weixinXML);
  25. return content;
  26. }
  27. public string DealSubscribe(string weixinXML)
  28. {
  29. string content1="";
  30. string strresponse = "";
  31. XmlDocument doc = new XmlDocument();
  32. doc.LoadXml(weixinXML);
  33. XmlNodeList list = doc.GetElementsByTagName("xml");
  34. XmlNode xn = list[0];
  35. string FromUserName = xn.SelectSingleNode("//FromUserName").InnerText; //关注用户的加密后openid
  36. string ToUserName = xn.SelectSingleNode("//ToUserName").InnerText; //公众微信号原始ID
  37. string MsgType=xn.SelectSingleNode("//MsgType").InnerText;
  38. string Event=xn.SelectSingleNode("//Event").InnerText;
  39. string EventKey = xn.SelectSingleNode("//EventKey").InnerText;
  40. ReturnMsg rm = new ReturnMsg();
  41. if(String.IsNullOrEmpty(EventKey)) //常规关注
  42. {
  43. DealJoin(FromUserName);
  44. SaveEventCommon(FromUserName, ToUserName);
  45. content1 = "欢迎您:\n\n"
  46. + "[玫瑰]超值优惠券[玫瑰]\n"
  47. + "肯德基、麦当劳、汉堡王、德克士、必胜客、星巴克、Coco、猫眼、美团外卖、饿了么、滴滴等上百家全国知名连锁商家4折起优惠券,随时买随时用,无需加会员和预充值,全国门店通用!\n\n"
  48. + "<a href='https://' >点击这里购买!</a>"
  49. + "\n\n"
  50. + "<a href='https://' >点击这里购买!!</a>"
  51. + "\n\n"
  52. + "<a href='https://' >点击这里购买!!!</a>"
  53. + "\n\n"
  54. + "您也可以通过菜单”超值优惠券“进入购买页面,总有惊喜在等您哦!";
  55. strresponse = rm.ReturnText(FromUserName, ToUserName, content1);
  56. //给管理员发送新粉丝关注通知
  57. QinMing.WeixinTemplateMessage.SendTemplateMessage.SendRemindMsg("管理员openid", "新粉丝关注提醒(来源:普通关注)" + FromUserName, "http://www.yourweb.com/Weixin/DisplayOneUser.aspx?open_id=" + FromUserName); //告知管理员有新粉丝关注
  58. }
  59. else //扫描带参数的二维码关注,一人一码、一店一码
  60. {
  61. DealJoin(FromUserName);
  62. string tmpEventKey = EventKey.Substring(EventKey.LastIndexOf("_") + 1);
  63. string ticket = xn.SelectSingleNode("//Ticket").InnerText;
  64. SaveEventScan(FromUserName, ToUserName, tmpEventKey, ticket);
  65. if(tmpEventKey == "10") //某个指定的带参数二维码scene_id
  66. {
  67. }
  68. else
  69. {
  70. strresponse = ScanWithQrCodeNew(FromUserName, ToUserName, tmpEventKey);
  71. }
  72. }
  73. return strresponse;
  74. }
  75. //记录粉丝关注公众号时的相关信息
  76. public void DealJoin(string FromUserName)
  77. {
  78. SqlConnection conn = new SqlConnection(QinMingConfig.DatabaseConnStr);
  79. conn.Open();
  80. SqlCommand cmd = new SqlCommand();
  81. SqlDataReader dr;
  82. cmd.Connection = conn;
  83. cmd.CommandText = "insert into weixin_join_log (open_id,join_time,remove_flag) values('" + FromUserName + "',getdate(),0)";
  84. cmd.ExecuteScalar();
  85. cmd.CommandText = "select * from weixin_user_info where open_id ='" + FromUserName + "' ";
  86. dr = cmd.ExecuteReader();
  87. if(dr.Read())
  88. {
  89. dr.Close();
  90. cmd.CommandText = "update weixin_user_info set remove_flag='已关注' where open_id='" + FromUserName + "'";
  91. cmd.ExecuteScalar(); //新增粉丝如曾关注过,取关后重新关注的,变更状态
  92. }
  93. else
  94. {
  95. dr.Close();
  96. cmd.CommandText = "insert into weixin_user_info (open_id,remove_flag,personal_score,join_time) values('" + FromUserName + "','已关注',0,getdate())";
  97. cmd.ExecuteScalar(); //新增粉丝如曾未关注过,增加粉丝记录
  98. //微信用户首次关注公众号时,发送一个随机红包。
  99. SendNewUserRedPacket(FromUserName);
  100. }
  101. if (conn.State == ConnectionState.Open)
  102. {
  103. conn.Close();
  104. conn.Dispose();
  105. }
  106. QinMingWeixinUserInfo wui=new QinMingWeixinUserInfo();
  107. wui.UpdateUserInfo(FromUserName); //获取用户信息并更新weixin_user_info表
  108. }
  109. /*给一个新关注公众号的粉丝发红包,仅限首次关注,取消关注后再次关注时没有此红包*/
  110. public void SendNewUserRedPacket(string open_id)
  111. {
  112. Random r = new Random();
  113. string ran1 = r.Next(1000, 9999).ToString();
  114. string ran2 = r.Next(随机红包最小值, 随机红包最大值).ToString();
  115. string out_trade_no = QinMingConfig.Weixin_MchId + DateTime.Now.ToString("yyyyMMddHHmmss") + ran1;
  116. QinMingWeixinPayPayment.SendRedPackOne("降价联盟", "新粉福利", open_id, ran2, "新粉关注红包", "", out_trade_no);
  117. }
  118. //用户扫描带参数的二维码时处理动作
  119. public string ScanWithQrCodeNew(string FromUserName, string ToUserName, string EventKey)
  120. {
  121. string strresponse = "";
  122. ReturnMsg rm = new ReturnMsg();
  123. string guishu_mobile = "";
  124. string guishu_name = "";
  125. string guishu_type = "";
  126. string scan_title = "";
  127. string scan_title_sub = "";
  128. string scan_image_url = "";
  129. string scan_redirect_url = "";
  130. string r_red_packet_min = "";
  131. string r_red_packet_max = "";
  132. SqlConnection conn = new SqlConnection(QinMingConfig.DatabaseConnStr);
  133. conn.Open();
  134. SqlCommand cmd = new SqlCommand();
  135. SqlDataReader dr;
  136. cmd.Connection = conn;
  137. cmd.CommandText = "select * from weixin_qrcode_ticket where scene_id=" + EventKey + " ";
  138. dr = cmd.ExecuteReader();
  139. if(dr.Read())
  140. {
  141. if(dr["scan_type"].ToString() == "优惠券")
  142. {
  143. guishu_mobile = dr["user_id"].ToString();
  144. guishu_name = dr["user_name"].ToString();
  145. guishu_type = dr["scan_type"].ToString();
  146. scan_title = dr["scan_title"].ToString();
  147. scan_title_sub = dr["scan_title_sub"].ToString();
  148. scan_image_url = dr["scan_image_url"].ToString();
  149. scan_redirect_url = dr["scan_redirect_url"].ToString();
  150. r_red_packet_min = dr["r_red_packet_min"].ToString(); //合伙人推荐单粉红包最小值
  151. r_red_packet_max = dr["r_red_packet_max"].ToString(); //合伙人推荐单粉红包最大值
  152. Random r = new Random();
  153. int RandomRedpack = r.Next(int.Parse(r_red_packet_min), int.Parse(r_red_packet_max));
  154. strresponse = rm.ReturnText(FromUserName,ToUserName,"欢迎您:[咖啡]\n\n"
  155. + "[玫瑰]超值优惠券[玫瑰]\n"
  156. + "肯德基、麦当劳、汉堡王、德克士、必胜客、星巴克、Coco、猫眼、美团外卖、饿了么、滴滴等上百家全国知名连锁商家4折起优惠券,随时买随时用,无需加会员和预充值,全国门店通用!\n\n"
  157. + "<a href='https://' >点击这里购买!</a>"
  158. + "\n\n"
  159. + "<a href='https://' >点击这里购买!!</a>"
  160. + "\n\n"
  161. + "<a href='https://' >点击这里购买!!!</a>"
  162. + "\n\n"
  163. + "您也可以通过菜单”超值优惠券“进入购买页面,总有惊喜在等您哦!");
  164. dr.Close();
  165. //给推荐的合伙人发红包,用生成红包记录自行提现方式;非首次扫码关注公众号,不发推荐红包包
  166. /**/
  167. if(int.Parse(QinMingToolsDB.SearchOneField("select count(*) r_m from weixin_user_info where open_id= '" + FromUserName + "'","r_m")) >= 1 )
  168. {
  169. //如果用户表中已经存在微信用户openid,则表示非首次关注,不给推荐人发红包
  170. }
  171. else
  172. {
  173. //如果用户表中没有微信用户openid,则表示首次关注,给推荐人发红包;在推荐红包表中追加记录,需推荐人自行提现
  174. string test = CreateScanQrCodeRedpack(FromUserName, guishu_mobile, guishu_type, RandomRedpack.ToString());
  175. }
  176. //如果是带参数二维码扫码关注,更新新粉丝扫码推荐人归属信息。
  177. cmd.CommandText = "update weixin_user_info set guishu_mobile='" + guishu_mobile + "',guishu_type ='" + guishu_type + "' where remove_flag='已关注' and open_id='" + FromUserName + "' ";
  178. cmd.ExecuteScalar();
  179. //告知管理员有新粉丝扫码关注
  180. QinMing.WeixinTemplateMessage.SendTemplateMessage.SendRemindMsg("管理员openid", "新粉丝关注提醒(来源:" + guishu_name + ")" + FromUserName, "http://www.yourweb.com/Weixin/DisplayOneUser.aspx?open_id=" + FromUserName);
  181. //插入带参数二维码被扫码的记录
  182. cmd.CommandText = "insert into weixin_ad_image_read_log (share_qrcode_id,read_open_id,read_time) values ("
  183. + EventKey + ",'" + FromUserName + "',getdate())"; //EventKey对应的是weixin_qrcode_ticket表中的scene_id,二维码分配关系
  184. cmd.ExecuteScalar();
  185. }
  186. else
  187. {
  188. dr.Close();
  189. }
  190. if (conn.State == ConnectionState.Open)
  191. {
  192. conn.Close();
  193. conn.Dispose();
  194. }
  195. }
  196. return strresponse;
  197. }
  198. //微信用户通过扫描带参数二维码关注公众号时,给推荐扫码的人追加一条红包记录
  199. public string CreateScanQrCodeRedpack(string FromUserName, string guishu_mobile, string guishu_type, string redpack_fee)
  200. {
  201. string order_id = QinMingToolsDB.SearchOneField("select isnull(max(order_id),0)+1 r_num from weixin_redpack_log_yhq_spread","r_num");
  202. string fromopenid = QinMingToolsDB.SearchOneField("select open_id from weixin_bind_mobile_all where mobile = '"+ guishu_mobile +"'","open_id");
  203. string scene_id = QinMingToolsDB.SearchOneField("select top 1 * from weixin_qrcode_ticket where user_id = '"+ guishu_mobile +"' and scan_type = '"+ guishu_type +"'","scene_id");
  204. string fromname = QinMingToolsDB.SearchOneField("select top 1 * from weixin_qrcode_ticket where user_id = '"+ guishu_mobile +"' and scan_type = '"+ guishu_type +"'","user_name");
  205. 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"+
  206. "values("+ order_id +",'"+ FromUserName +"',getdate(),'优惠券推荐新粉红包',"+ redpack_fee +",'"+ scene_id +"','"+ guishu_mobile +"','"+ fromname +"','"+ fromopenid +"','','')\n";
  207. QinMingToolsDB.InsertTable(strsql);
  208. return "2"; //用户
  209. }
  210. //保存普通关注事件信息。
  211. public void SaveEventCommon(string FromUserName, string ToUserName)
  212. {
  213. SqlConnection conn = new SqlConnection(QinMingConfig.DatabaseConnStr);
  214. conn.Open();
  215. SqlCommand cmd = new SqlCommand();
  216. cmd.Connection = conn;
  217. cmd.CommandText = "insert into weixin_recv_event (msg_type,event_type,open_id,gh_id,recv_time) "
  218. + "values ('event','subscribe','" + FromUserName + "','" + ToUserName + "',getdate()) ";
  219. QinMingTools.WriteLog("sql语句:", cmd.CommandText);
  220. cmd.ExecuteScalar();
  221. if (conn.State == ConnectionState.Open)
  222. {
  223. conn.Close();
  224. conn.Dispose();
  225. }
  226. }
  227. //保存扫描带参数二维码事件信息。
  228. public void SaveEventScan(string FromUserName, string ToUserName,string EventKey, string Ticket)
  229. {
  230. SqlConnection conn = new SqlConnection(QinMingConfig.DatabaseConnStr);
  231. conn.Open();
  232. SqlCommand cmd = new SqlCommand();
  233. cmd.Connection = conn;
  234. cmd.CommandText = "insert into weixin_recv_event (msg_type,event_type,open_id,gh_id,recv_time,event_key,ticket) "
  235. + "values ('event','subscribe','" + FromUserName + "','" + ToUserName + "',getdate(),'" + EventKey + "','" + Ticket + "') ";
  236. QinMingTools.WriteLog("sql语句:", cmd.CommandText);
  237. cmd.ExecuteScalar();
  238. if (conn.State == ConnectionState.Open)
  239. {
  240. conn.Close();
  241. conn.Dispose();
  242. }
  243. }
  244. }
  245. }

上面代码中用到了数据库操作类Tools.DB;还有微信支付类QinMingWeixinPayPayment中的发送红包操作SendRedPackOne,后期讲解微信支付时给出源码。

四、应用演示

1、新粉丝红包

2、合伙人推荐红包

 收到红包的感觉是不是很爽?

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/661349
推荐阅读
相关标签
  

闽ICP备14008679号