예제 #1
0
        internal async Task <byte[]> GetQRPic(string uniacid, ObjectId accountID, string contentRootPath)
        {
            var account = GetModelByIDAndUniacID(accountID, uniacid);

            if (string.IsNullOrEmpty(account.AccountPhoneNumber) || string.IsNullOrEmpty(account.CarNumber))
            {
                string no_qrPath = $@"{contentRootPath}/wwwroot/images/no_qr.jpg";
                return(File.ReadAllBytes(no_qrPath));
            }
            byte[] qrData;
            var    bucket = new GridFSBucket(mongoDB);

            if (account.QRFileID != ObjectId.Empty)
            {
                return(await bucket.DownloadAsBytesAsync(account.QRFileID));
            }
            string       qrInfo = $@"{We7Config.SiteRoot}account/GetAccountInfo?uniacid={uniacid}&AccountID={account.AccountID}";
            Bitmap       qr     = QRCodeHelper.Create(qrInfo, 300);
            Image        qrRaw  = ImageTools.ResizeImage(qr, 286, 286, 0);
            string       bgPath = $@"{contentRootPath}/wwwroot/images/qr_bg.jpg";
            Bitmap       qrBit  = ImageTools.CombinImage(new Bitmap(bgPath), qrRaw, 171, 128);
            MemoryStream ms     = new MemoryStream();

            qrBit.Save(ms, ImageFormat.Jpeg);
            qrData = ms.GetBuffer();
            var options = new GridFSUploadOptions
            {
                Metadata = new BsonDocument {
                    { "content-type", "Image/jpg" }
                }
            };
            var id = await bucket.UploadFromBytesAsync($"qr_{accountID.ToString()}.jpg", qrData, options);

            collection.UpdateOne(x => x.AccountID.Equals(accountID) && x.uniacid.Equals(uniacid), Builders <AccountModel> .Update.Set(x => x.QRFileID, id));
            return(qrData);
        }