Beispiel #1
0
        public async Task <IActionResult> ContentWxAppletQrCode([FromBody] ContentPlayBillModel info)
        {
            CommonResult      result            = new CommonResult();
            YuebonCacheHelper yuebonCacheHelper = new YuebonCacheHelper();

            try
            {
                if (info == null)
                {
                    return(ToJsonContent(result));
                }
                string page  = info.Page;   //此接口不可以带参数,如果需要加参数,必须加到scene中
                string scene = info.Scene;; //id=xxxxxx,scene最多允许32个字符
                int    width = string.IsNullOrEmpty(info.Width.ToString()) ? 480 : info.Width.ToInt();

                //图片名称
                string picname       = "ref" + width + GuidUtils.CreateNo() + ".png";
                var    _tempfilepath = "/upload/contentqrcode/";
                var    uploadPath    = _filePath + _tempfilepath;
                if (!Directory.Exists(uploadPath))
                {
                    Directory.CreateDirectory(uploadPath);
                }
                string       qrcodePicPath = uploadPath + picname;//小程序二维码图片
                MemoryStream ms            = new MemoryStream();
                if (!System.IO.File.Exists(qrcodePicPath))
                {
                    var resultImg = await WxAppApi.GetWxaCodeUnlimitAsync(WxOpenAppId, qrcodePicPath, scene, page, width, false, null, true);

                    if (resultImg.errcode == ReturnCode.请求成功)
                    {
                        result.Success = true;
                        result.ErrCode = ErrCode.successCode;
                        result.ResData = _tempfilepath + picname;
                    }
                    else
                    {
                        result.ErrCode = resultImg.errcode.ToString();
                        result.ErrMsg  = resultImg.errmsg;
                    }
                }
                else
                {
                    result.ErrCode = ErrCode.successCode;
                    result.ErrMsg  = _tempfilepath + picname;
                }
            }
            catch (Exception ex)
            {
                Log4NetHelper.Error("代码生成异常", ex);
                result.ErrMsg  = "代码生成异常:" + ex.Message;
                result.ErrCode = ErrCode.failCode;
            }
            return(ToJsonContent(result));
        }
Beispiel #2
0
        public async Task <IActionResult> ContentWxAppletQrCodeRes([FromQuery] ContentPlayBillModel info)
        {
            CommonResult      result            = new CommonResult();
            YuebonCacheHelper yuebonCacheHelper = new YuebonCacheHelper();
            MemoryStream      ms = new MemoryStream();

            try
            {
                if (info == null)
                {
                    return(ToJsonContent(result));
                }
                string page      = info.Page;   //此接口不可以带参数,如果需要加参数,必须加到scene中
                string scene     = info.Scene;; //id=xxxxxx,scene最多允许32个字符
                int    width     = string.IsNullOrEmpty(info.Width.ToString()) ? 480 : info.Width.ToInt();
                var    resultImg = await WxAppApi.GetWxaCodeUnlimitAsync(WxOpenAppId, ms, scene, page, width);

                ms.Position = 0;
                if (resultImg.errcode == ReturnCode.请求成功)
                {
                    var imgBase64 = Convert.ToBase64String(ms.GetBuffer());
                    result.Success = true;
                    result.ErrCode = ErrCode.successCode;
                    result.ResData = imgBase64;
                }
                else
                {
                    result.ErrCode = resultImg.errcode.ToString();
                    result.ErrMsg  = resultImg.errmsg;
                }
            }
            catch (Exception ex)
            {
                Log4NetHelper.Error("代码生成异常", ex);
                result.ErrMsg  = "代码生成异常:" + ex.Message;
                result.ErrCode = ErrCode.failCode;
            }
            finally{
                ms.Dispose();
            }
            return(ToJsonContent(result));
        }
Beispiel #3
0
        public async Task <IActionResult> GetQrCode(string sessionId, string useBase64, string codeType = "1")
        {
            var sessionBag = SessionContainer.GetSession(sessionId);

            if (sessionBag == null)
            {
                return(Json(new { success = false, msg = "请先登录!" }));
            }

            MemoryStream ms        = new MemoryStream();
            var          openId    = sessionBag.OpenId;
            var          page      = "pages/QrCode/QrCode";                                                 //此接口不可以带参数,如果需要加参数,必须加到scene中
            var          scene     = $"OpenIdSuffix:{openId.Substring(openId.Length - 10, 10)}#{codeType}"; //储存OpenId后缀,以及codeType。scene最多允许32个字符
            LineColor    lineColor = null;                                                                  //线条颜色

            if (codeType == "2")
            {
                lineColor = new LineColor(221, 51, 238);
            }

            var result = await WxAppApi.GetWxaCodeUnlimitAsync(WxOpenAppId, ms, scene, page, lineColor : lineColor);

            ms.Position = 0;

            if (!useBase64.IsNullOrEmpty())
            {
                //转base64
                var imgBase64 = Convert.ToBase64String(ms.GetBuffer());
                return(Json(new { success = true, msg = imgBase64, page = page }));
            }
            else
            {
                //返回文件流
                return(File(ms, "image/jpeg"));
            }
        }
Beispiel #4
0
        public async Task <IActionResult> ContentPlayBillQrCode(ContentPlayBillModel info)
        {
            CommonResult      result            = new CommonResult();
            YuebonCacheHelper yuebonCacheHelper = new YuebonCacheHelper();
            MemoryStream      ms = new MemoryStream();

            try
            {
                if (info == null)
                {
                    return(ToJsonContent(result));
                }
                string page  = info.Page;   //此接口不可以带参数,如果需要加参数,必须加到scene中
                string scene = info.Scene;; //id=xxxxxx,scene最多允许32个字符

                //图片名称
                string picname       = "ref_" + GuidUtils.CreateNo() + ".jpg";
                var    _tempfilepath = "/upload/" + scene + "/contentqrcode/";
                var    uploadPath    = _filePath + _tempfilepath;
                if (!Directory.Exists(uploadPath))
                {
                    Directory.CreateDirectory(uploadPath);
                }
                string qrcodePicPath = uploadPath + picname;//小程序二维码图片
                var    resultImg     = await WxAppApi.GetWxaCodeUnlimitAsync(WxOpenAppId, qrcodePicPath, scene, page, 280);

                if (resultImg.errcode == ReturnCode.请求成功)
                {
                    string picnameh = "c_" + GuidUtils.CreateNo() + ".jpg";
                    var    sor      = _filePath + "/images/";
                    string qrcodebg = "share_content_bg.jpg";

                    Bitmap   cardbmp = new Bitmap(460, 736);
                    Graphics g       = Graphics.FromImage(cardbmp);
                    g.SmoothingMode      = SmoothingMode.HighQuality;; //抗锯齿
                    g.CompositingQuality = CompositingQuality.HighQuality;
                    g.InterpolationMode  = InterpolationMode.High;
                    g.Clear(System.Drawing.Color.White); //白色填充

                    Bitmap bgimg = new Bitmap(460, 736);
                    //如果背景图片存在,
                    bgimg = (Bitmap)Image.FromFile(qrcodebg); //如果存在,读取背景图片
                    g.DrawImage(bgimg, 0, 0, 460, 736);
                    //合成二维码
                    Bitmap productImg = new Bitmap(128, 128);
                    productImg = (Bitmap)Image.FromFile(qrcodePicPath);
                    g.DrawImage(productImg, 276, 580, 128, 128);
                    //合成文字
                    Font nickName = new Font("微软雅黑", 22);
                    Font adtxt    = new Font("微软雅黑", 18);

                    StringFormat stringFormat = new StringFormat(StringFormatFlags.DisplayFormatControl);
                    Brush        fontBrush    = SystemBrushes.ControlText;
                    stringFormat.Alignment     = StringAlignment.Center;
                    stringFormat.LineAlignment = StringAlignment.Center;
                    RectangleF rectangleF = new RectangleF(40, 500, 380, 80);
                    g.DrawString(info.Title, adtxt, fontBrush, rectangleF, stringFormat);
                    cardbmp.Save(uploadPath + picnameh, ImageFormat.Jpeg);
                    cardbmp.Dispose();
                    ms.Dispose();

                    result.ResData = _tempfilepath + picnameh;
                }
                else
                {
                    result.ErrCode = resultImg.errcode.ToString();
                    result.ErrMsg  = resultImg.errmsg;
                }
            }
            catch (Exception ex)
            {
                Log4NetHelper.Error("代码生成异常", ex);
                result.ErrMsg  = "代码生成异常:" + ex.Message;
                result.ErrCode = ErrCode.failCode;
            }
            finally
            {
                ms.Dispose();
            }
            return(ToJsonContent(result));
        }
Beispiel #5
0
        public async Task <IActionResult> GetReferralQrCode()
        {
            CommonResult result = new CommonResult();

            try
            {
                MemoryStream ms    = new MemoryStream();
                string       page  = "pages/index/index";         //此接口不可以带参数,如果需要加参数,必须加到scene中
                string       scene = "Ref=" + CurrentUser.UserId; //储存OpenId后缀,以及codeType。scene最多允许32个字符

                //图片名称
                string picname       = "ref_" + GuidUtils.CreateNo() + ".jpg";
                var    _tempfilepath = "/upload/" + CurrentUser.UserId + "/qrcode/";
                var    uploadPath    = _filePath + _tempfilepath;
                if (!Directory.Exists(uploadPath))
                {
                    Directory.CreateDirectory(uploadPath);
                }
                string qrcodePicPath = uploadPath + picname;//小程序二维码图片
                var    resultImg     = await WxAppApi.GetWxaCodeUnlimitAsync(WxOpenAppId, qrcodePicPath, scene, page, 280);

                if (resultImg.errcode == ReturnCode.请求成功)
                {
                    string picnameh = "refs_" + CurrentUser.UserId + ".jpg";
                    var    sor      = _filePath + "/images/";
                    string qrcodebg = "share_bg2.jpg";

                    Bitmap   cardbmp = new Bitmap(1080, 1926);
                    Graphics g       = Graphics.FromImage(cardbmp);
                    g.SmoothingMode      = SmoothingMode.HighQuality;; //抗锯齿
                    g.CompositingQuality = CompositingQuality.HighQuality;
                    g.InterpolationMode  = InterpolationMode.High;
                    g.Clear(System.Drawing.Color.White); //白色填充

                    Bitmap bgimg = new Bitmap(1080, 1926);
                    //如果背景图片存在,
                    bgimg = (Bitmap)System.Drawing.Image.FromFile(sor + qrcodebg); //如果存在,读取背景图片
                    g.DrawImage(bgimg, 0, 0, 1080, 1926);
                    //合成二维码
                    Bitmap productImg = new Bitmap(280, 280);
                    productImg = (Bitmap)System.Drawing.Image.FromFile(qrcodePicPath);
                    g.DrawImage(productImg, 713, 1570, 280, 280);
                    //合成头像
                    Bitmap headerimg = new Bitmap(110, 110);
                    if (CurrentUser.HeadIcon.Contains("wx.qlogo.cn"))
                    {
                        headerimg = ImgHelper.GetNetImg(CurrentUser.HeadIcon);
                    }
                    else
                    {
                        headerimg = ImgHelper.GetNetImg(CurrentUser.HeadIcon);
                        //headerimg = (Bitmap)System.Drawing.Image.FromFile(_filePath + CurrentUser.HeadIcon);
                    }
                    headerimg = ImgHelper.CutEllipse(headerimg, new Rectangle(0, 0, 110, 110), new Size(110, 110));
                    g.DrawImage(headerimg, 100, 1505, 110, 110);

                    //合成文字
                    Font nickName = new Font("微软雅黑", 22);
                    Font adtxt    = new Font("微软雅黑", 26);

                    StringFormat StringFormat = new StringFormat(StringFormatFlags.DisplayFormatControl);
                    g.DrawString(CurrentUser.NickName, nickName, new SolidBrush(System.Drawing.Color.LightGray), 100, 1630, StringFormat);
                    g.DrawString("推荐你一个超棒的汽车工程师圈子”", adtxt, new SolidBrush(System.Drawing.Color.Black), 100, 1680, StringFormat);

                    cardbmp.Save(uploadPath + picnameh, ImageFormat.Jpeg);
                    cardbmp.Dispose();

                    result.ResData = _tempfilepath + picnameh;
                }
                else
                {
                    result.ErrCode = resultImg.errcode.ToString();
                    result.ErrMsg  = resultImg.errmsg;
                }
            }
            catch (Exception ex)
            {
                Log4NetHelper.Error("代码生成异常", ex);
                result.ErrMsg  = "代码生成异常:" + ex.Message;
                result.ErrCode = ErrCode.failCode;
            }
            return(ToJsonContent(result));
        }
        public async Task <Result> QRCode()
        {
            var user = await _customerManager.GetUserAsync();

            var appId = WechatService.WxOpenAppId;
            var scene = $"id={user.Id}";
            var page  = "pages/index/index";

            try
            {
                var myShareCodePath = Path.Combine("images", "sharecodes", user.Id + ".png");
                var physicalPath    = Path.Combine(_enviromenet.WebRootPath, myShareCodePath);
                var httpPath        = Request.GetHostUrl() + "/" + myShareCodePath.Replace(@"\", "/");
                if (System.IO.File.Exists(physicalPath))
                {
                    return(Result.Ok(httpPath));
                }

                Image  backgroundImg = null;
                string sharePath     = Path.Combine(_enviromenet.WebRootPath, "images/20210107152422.png");
                // var qrCodeImgDemo = Path.Combine(_enviromenet.WebRootPath, "images/qrcode_example.png");
                if (System.IO.File.Exists(sharePath))
                {
                    backgroundImg = Image.Load(sharePath);
                }

                using (var stream = new MemoryStream())
                {
                    var wxResult = await WxAppApi.GetWxaCodeUnlimitAsync(appId, stream, scene, page);

                    if (wxResult.errcode == 0 && stream.Length > 1024)
                    {
                        Image qrCodeImg = Image.Load <Rgba32>(stream.GetBuffer());

                        if (!string.IsNullOrEmpty(user.AvatarUrl))
                        {
                            using (var httpClient = _httpFactory.CreateClient())
                            {
                                var response = await httpClient.GetAsync(user.AvatarUrl);

                                if (response.IsSuccessStatusCode)
                                {
                                    var avatarStream = await response.Content.ReadAsStreamAsync();

                                    var avatarPoint = 120;
                                    var avatarSize  = 190;
                                    var avatarImg   = Image.Load <Rgba32>(avatarStream).Clone(e => e.ConvertToAvatar(new Size(avatarSize, avatarSize), avatarSize / 2));
                                    qrCodeImg.Mutate(e =>
                                    {
                                        e.DrawImage(avatarImg, new Point(avatarPoint, avatarPoint), 1);
                                    });
                                }
                            }
                        }

                        var qrSize  = 211;
                        var qrPoint = new Point(270, 620);
                        qrCodeImg = qrCodeImg.Clone(e => e.Resize(qrSize, qrSize));
                        backgroundImg.Mutate(e =>
                        {
                            e.DrawImage(qrCodeImg, qrPoint, 1);
                        });
                        using (var ms = new MemoryStream())
                        {
                            backgroundImg.Save(physicalPath, new PngEncoder());
                            // backgroundImg.SaveAsPng(ms);
                            // string base64String = $"data:image/png;base64,{Convert.ToBase64String(ms.ToArray())}";
                            // return Result.Ok(base64String);
                        }
                        return(Result.Ok(httpPath));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Result.Fail(ResultCodes.SysError, ex.Message));
            }
            return(Result.Fail(ResultCodes.RequestParamError, "请求生成小程序码失败"));
        }