zoukankan      html  css  js  c++  java
  • C#开发微信公众平台开发-微信海报介绍和开发流程

    “让客户发展客户”,微信海报才是微信公众平台最高明的吸粉手段,海报上有粉丝的专属二维码,有粉丝的头像及商户宣传的广告等。新粉丝扫描这个专属二维码会关注公众号,同时分享海报的粉丝会增加积分换取礼品或者优惠劵!本文介绍微信公众平台下二维码海报的开发过程。

    一、微信二维码海报介绍

    微信二维码海报是指在海报中嵌入和微信用户关联的参数二维码的海报,用户分享推广之后,新用户可以被统计为被推广人员数,从而达到增加粉丝的传播效果。其使用场景如下:

    二、开发流程

    在微信二维码海报生成中,需要用到以下信息

    1. 自定义菜单中设置一个菜单项,点击后返回二维码海报给用户

    2. 接口接收到菜单点击之后,获取用户的头像、ID(可以使用OpenID)

    3. 生成和用户关联的参数二维码,

    4. 将参数二维码进行缩放

    5. 将头像和参数二维码合并成新的参数二维码图片

    6. 将新参数二维码图片做为水印合成到背景海报中

    7. 将用户昵称,以及二维码时间戳(类型为临时二维码时)等文字合成到背景海报中

    8. 将海报上传成临时图片素材

    9. 将图片素材使用客服接口发送给用户。

       上述便是生成这张海报所需的资源及知识点,咱一步一步来哈

       1、首先,模板图片,找公司PS高手,做一个就行

       2、微信带参数二维码的生成

    #region 获取用于换取二维码的Ticket
            /// <summary>
            /// 获取用于换取二维码(临时二维码和长久二维码)的Ticket  
            /// </summary>
            /// <param name="senceId">场景值ID</param>
            /// <param name="type">值为:long 时:代表长久性二维码  值为short时:代表临时二维码</param>
            /// <returns></returns>
            public static string GeterweimaTicket(int senceId, string type = "short")
            {
                string Ticket = "";
                CreateQrCodeResult re = new CreateQrCodeResult();
                try
                {
                    if (type == "short")//临时二维码 
                    {
    
                        re = QrCodeApi.Create(IsExistAccess_Token2(), 604800, senceId);
                    }
                    else
                    {
                        re = QrCodeApi.Create(IsExistAccess_Token2(), 0, senceId);
                    }
                    Ticket = re.ticket;
                }
                catch
                {
                    if (type == "short")//临时二维码 
                    {
    
                        Ticket = CreateTicket(IsExistAccess_Token2());
                    }
                    else
                    {
                        Ticket = CreateLongTicket(IsExistAccess_Token2());
                    }
                }
    
                return Ticket;
            }
    
            #region 原方法
    
            /// <summary>  
            /// 创建二维码ticket  临时二维码
            /// </summary>  
            /// <returns></returns>  
            public static string CreateTicket(string TOKEN)
            {
    
                string result = "";
                string strJson = @"{""expire_seconds"": 604800, ""action_name"": ""QR_SCENE"", ""action_info"": {""scene"": {""scene_id"": 321}}}";
                string wxurl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + TOKEN + "";
                result = GetPage(wxurl, strJson);
                result = GetJsonValue(result, "ticket");//获取票据
                ////LogHelper.WriteLog(result);
                return result;
            }
    
            /// <summary>
            /// 获取永久二维码Ticket
            /// </summary>
            /// <param name="TOKEN"></param>
            /// <returns></returns>
            public static string CreateLongTicket(string TOKEN)
            {
    
                string result = "";
                string strJson = @"{""action_name"": ""QR_LIMIT_SCENE"", ""action_info"": {""scene"": {""scene_id"": 123}}}";
                string wxurl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + TOKEN + "";
                result = GetPage(wxurl, strJson);
                result = GetJsonValue(result, "ticket");//获取票据
                ////LogHelper.WriteLog(result);
                return result;
            }
    
            //{"ticket":"gQFw8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL3pFZ3lETjdsMVNDcy1DRW9PMmE3AAIEP1TcVAMECAcAAA==","expire_seconds":1800,"url":"http://weixin.qq.com/q/zEgyDN7l1SCs-CEoO2a7"}
            /// <summary>
            /// 通过ticket换取二维码
            /// </summary>
            /// <param name="TICKET">票据</param>
            /// <param name="openId">二维码依照openId 命名</param>
            /// <param name="Pth">相对路径 @"weixinHuLuerweima2"</param>
            /// <returns>下载二维码成功后的物理路径:D:XXXXXX.comkkkerweima2201503031044566511190.jpg</returns>
            public static string GetTicketImage(string TICKET, string openId, string Pth)
            {
                string content = string.Empty;
                string strpath = string.Empty;//生成的URL 也就是 关注的URL
                string savepath = string.Empty;//图片保存的路径
    
                string stUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + HttpContext.Current.Server.UrlEncode(TICKET);
    
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(stUrl);
    
                req.Method = "GET";
    
                using (WebResponse wr = req.GetResponse())
                {
                    HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
                    strpath = myResponse.ResponseUri.ToString();
    
                    WebClient mywebclient = new WebClient();
                    // @"
                    savepath = HttpContext.Current.Server.MapPath(Pth) + "\" + openId + "." + myResponse.ContentType.Split('/')[1].ToString();
                    // //LogHelper.WriteLog(savepath);
    
                    try
                    {
                        mywebclient.DownloadFile(strpath, savepath);//下载生成的二维码图片
                    }
                    catch (Exception ex)
                    {
                        savepath = ex.ToString();
                        LogHelper.WriteLog("错误了" + savepath);
                    }
    
    
                }
    
                //LogHelper.WriteLog(savepath.ToString() + "都给我滚!");
                return savepath.ToString();
    
    
            }
            #endregion
    
            #endregion

       3、获取用户头像并下载:

    #region 获取用户的详细信息
            /// <summary>
            /// 获取用户的详细信息
            /// </summary>
            /// <param name="REFRESH_TOKEN">Token</param>
            /// <param name="OPENID">用户的OpenId</param>
            /// <returns>str</returns>
            public static Dictionary<string, object> Get_UserInfo(string REFRESH_TOKEN, string OPENID)
            {
                // Response.Write("获得用户信息REFRESH_TOKEN:" + REFRESH_TOKEN + "||OPENID:" + OPENID);  
                string UserInfo = GetJson("https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + REFRESH_TOKEN + "&openid=" + OPENID);
                Dictionary<string, Object> obj2 = JsonConvert.DeserializeObject<Dictionary<string, Object>>(UserInfo);
                return obj2;
            }
    
            protected static string GetJson(string url)
            {
                WebClient wc = new WebClient();
                wc.Credentials = CredentialCache.DefaultCredentials;
                wc.Encoding = Encoding.UTF8;
                string returnText = wc.DownloadString(url);
    
                if (returnText.Contains("errcode"))
                {
                    //可能发生错误  
                }
                //Response.Write(returnText);  
                return returnText;
            }
            #endregion

      用户信息用有个HeardImage 就是粉丝的头像路径,然后根据这个路径,我们把它下载下来!

    public class HttpDownLoad
        {
            private long fileLength;
            private long downLength;//已经下载文件大小,外面想用就改成公共属性
            private static bool stopDown;
            public HttpDownLoad()
            {
                fileLength = 0;
                downLength = 0;
                stopDown = false;
                //
                //   TODO:   在此处添加构造函数逻辑
                //
            }
    
            ///   <summary>
            ///   文件下载
            ///   </summary>
            ///   <param   name="url">连接</param>
            ///   <param   name="fileName">本地保存文件名</param>
            public void httpDownFile(string url, string fileName)
            {
                stopDown = false;
                Stream str = null, fs = null;
                try
                {
                    //获取下载文件长度
                    fileLength = getDownLength(url);
                    downLength = 0;
                    if (fileLength > 0)
                    {
                        WebClient DownFile = new WebClient();
                        str = DownFile.OpenRead(url);
                        //判断并建立文件
                        if (createFile(fileName))
                        {
                            byte[] mbyte = new byte[1024];
                            int readL = str.Read(mbyte, 0, 1024);
                            fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
                            //读取流
                            while (readL != 0)
                            {
                                if (stopDown)
                                    break;
                                downLength += readL;//已经下载大小
                                fs.Write(mbyte, 0, readL);//写文件
                                readL = str.Read(mbyte, 0, 1024);//读流
                                //progressBar.Value = (int)(downLength * 100 / fileLength);
                                //label.Text = progressBar.Value.ToString() + "%";
                                //System.Windows.Forms.Application.DoEvents();
                            }
                            str.Close();
                            fs.Close();
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (str != null)
                        str.Close();
                    if (fs != null)
                        fs.Close();
                    
                }
            }
            ///   <summary>
            ///   文件下载
            ///   </summary>
            ///   <param   name="url">连接</param>
            ///   <param   name="fileName">本地保存文件名</param>
            public void httpDownFile2(string url, string fileName)
            {
                try
                {
                    WebClient DownFile = new WebClient();
                    DownFile.DownloadFile(url, fileName);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            ///   <summary>
            ///   获取下载文件大小
            ///   </summary>
            ///   <param   name="url">连接</param>
            ///   <returns>文件长度</returns>
            private long getDownLength(string url)
            {
                try
                {
                    WebRequest wrq = WebRequest.Create(url);
                    WebResponse wrp = (WebResponse)wrq.GetResponse();
                    wrp.Close();
                    return wrp.ContentLength;
                }
                catch (Exception ex)
                {
                    return 0;
                    throw ex;
                    
                }
            }
            ///   <summary>
            ///   建立文件(文件如已经存在,删除重建)
            ///   </summary>
            ///   <param   name="fileName">文件全名(包括保存目录)</param>
            ///   <returns></returns>
            private bool createFile(string fileName)
            {
                try
                {
                    if (File.Exists(fileName))
                    {
                        File.Delete(fileName);
                    }
                    Stream s = File.Create(fileName);
                    s.Close();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                    throw ex;
                   
                }
            }
            public void downClose()
            {
                stopDown = true;
            }
    
    
        }

       此时:二维码和头像都生成了,我们就可以调用图片合成的方法了<注意:此处需要二次图片合成,第一次是模板和二维码合成,第二次是把得到的新图片和头像再做一次合成

    #region C#图片处理 合并图片
            /// <summary>
            /// 调用此函数后使此两种图片合并,类似相册,有个
            /// 背景图,中间贴自己的目标图片
            /// </summary>
            /// <param name="sourceImg">粘贴的源图片</param>
            /// <param name="destImg">粘贴的目标图片</param>
            /// 使用说明: string pic1Path = Server.MapPath(@"	estImgwf.png");
            /// 使用说明: string pic2Path = Server.MapPath(@"	estImgyj.png");
            /// 使用说明: System.Drawing.Image img = CombinImage(pic1Path, pic2Path);
            /// 使用说明:img.Save(Server.MapPath(@"	estImgNewwf.png"));
            public static System.Drawing.Image CombinImage(string sourceImg, string destImg)
            {
                System.Drawing.Image imgBack = System.Drawing.Image.FromFile(sourceImg);     //相框图片 
                System.Drawing.Image img = System.Drawing.Image.FromFile(destImg);        //照片图片
    
    
    
                //从指定的System.Drawing.Image创建新的System.Drawing.Graphics       
                Graphics g = Graphics.FromImage(imgBack);
    
                g.DrawImage(imgBack, 0, 0, 148, 124);      // g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);
                g.FillRectangle(System.Drawing.Brushes.Black, 16, 16, (int)112 + 2, ((int)73 + 2));//相片四周刷一层黑色边框
    
    
    
                //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);
                g.DrawImage(img, 17, 17, 112, 73);
                GC.Collect();
                return imgBack;
            }
            #endregion

       最后就是上传至服务器并获取MediaId和发送给粉丝了,这一块相信大家都比较熟悉,不作多的解释!

       不懂的加我微信:18217688158 或者QQ:1936048197

  • 相关阅读:
    ide 安装eval reset插件 Pycharm 永久破解
    Centos7.9安装RabbitMQ
    Linux环境下安装Centos7.9时没有图形界面的解决办法
    JS,对部分值base64解码
    consul 配置备份
    AutoMapper后遇到的“Missing map”与“Missing type map configuration”问题
    osg绘制圆柱体
    IfcFlowMovingDevice
    opencv absdiff
    IfcFlowMovingDeviceType
  • 原文地址:https://www.cnblogs.com/yechangzhong-826217795/p/6734359.html
Copyright © 2011-2022 走看看