小程序码带参请求(邀请码)

        #region 生成小程序推荐码
        /// <summary>
        /// 微信小程序带参数二维码的生成
        /// </summary>
        /// <param name="ParentIDs">邀请人员ID</param>
        /// <returns></returns>
        public static string CreateWxCode(string ParentIDs)
        {
            string access_token = string.Empty;
            string url1 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url1);
            webrequest.Method = "GET";
            HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
            StreamReader reader = new StreamReader(webresponse.GetResponseStream());
            access_token = reader.ReadToEnd();
            JObject jData = (JObject)JsonConvert.DeserializeObject(access_token);
            access_token = jData["access_token"].ToString();
            string ret = string.Empty;
            try
            {
                string DataJson = string.Empty;
                //适用于需要的码数量极多,或仅临时使用的业务场景
                //通过该接口生成的小程序码,永久有效,数量暂无限制。
                string url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + access_token;

                //DataJson的配置见小程序开发文档,B接口:https://mp.weixin.qq.com/debug/wxadoc/dev/api/qrcode.html
                ret = CreateWeChatQrCode(url, ParentIDs);
            }
            catch (Exception e)
            {
                ret = e.Message;
            }
            return ret;//返回图片地址
        }

        /// <summary>
        /// 返回二维码图片
        /// </summary>
        /// <param name="url"></param>
        /// <param name="ParentIDs"></param>
        /// <returns></returns>
        public static string CreateWeChatQrCode(string url, string ParentIDs)
        {
            HttpWebRequest request;
            string imgName = string.Empty;
            string path = string.Empty;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/json;charset=UTF-8";
                string paraUrlCoded = "{\"scene\": \"" + ParentIDs + "\"}";
                //byte[] payload;
                byte[] payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
                request.ContentLength = payload.Length;
                Stream writer = request.GetRequestStream();
                writer.Write(payload, 0, payload.Length);
                writer.Close();
                System.Net.HttpWebResponse response;
                response = (System.Net.HttpWebResponse)request.GetResponse();
                System.IO.Stream s;
                s = response.GetResponseStream();//返回图片数据流

                byte[] tt = StreamToBytes(s);//将数据流转为byte[]
                imgName = "user_" + ParentIDs + ".png";
                path = "/upload/ewm/";

                //获取相对于应用的基目录创建目录
                //string imgPath = System.Web.Hosting.HostingEnvironment.MapPath(path);//System.AppDomain.CurrentDomain.baxxxxseDirectory + path;     //通过此对象获取文件名
                string imgPath = System.AppDomain.CurrentDomain.BaseDirectory + path;     //通过此对象获取文件名
                if (!Directory.Exists(imgPath))
                {
                    Directory.CreateDirectory(imgPath);
                }
                System.IO.File.WriteAllBytes(HttpContext.Current.Server.MapPath(path + imgName), tt);//讲byte[]存储为图片
            }
            catch (Exception ex)
            {
                Common.Log("CreateWeChatQrCode:" + ex.Message);
            }
            return path + imgName;
        }

        /// <summary>
        /// 将数据流转为byte[]
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static byte[] StreamToBytes(Stream stream)
        {
            List<byte> bytes = new List<byte>();
            int temp = stream.ReadByte();
            while (temp != -1)
            {
                bytes.Add((byte)temp);
                temp = stream.ReadByte();
            }
            return bytes.ToArray();
        }
        #endregion 





//写法比较随意。提供参考思路

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值