Beispiel #1
0
            protected QrCodeType GetQrCodeType(JObject jObject)
            {
                QrCodeType result = null;

                bool hasItem = jObject.ContainsKey("TypeName");

                if (hasItem)
                {
                    JToken encodeType = jObject["TypeName"];
                    if (encodeType != null)
                    {
                        string encodeTypeName = encodeType.Value <string>();
                        if (!String.IsNullOrEmpty(encodeTypeName))
                        {
                            foreach (QrCodeType item in QrCodeTypes.AllTypes)
                            {
                                if (item.TypeName == encodeTypeName)
                                {
                                    result = item;
                                    break;
                                }
                            }
                        }
                    }
                }

                return(result);
            }
Beispiel #2
0
 public QrCodeData(string id, string title, QrCodeType type, QrCodeResultType resultType = QrCodeResultType.Message, DateTime?expireDt = null)
 {
     Type       = type;
     Title      = title;
     ID         = id;
     ResultType = resultType;
     if (expireDt != null)
     {
         ExpireDt = expireDt.ToDateTime();
     }
 }
Beispiel #3
0
        public Guid GetCreateId(QrCodeType type)
        {
            var id    = Guid.NewGuid();
            var model = new QrCode()
            {
                Id   = id,
                Type = type
            };

            _repository.Insert(model);
            return(id);
        }
Beispiel #4
0
        public async Task <byte[]?> RenderAsync(QrCodeType type, object model)
        {
            var data = await dataFactory.CreateAsync(type, model);

            if (data == null)
            {
                return(null);
            }

            var qrCodeMemorySpan = await imageRenderer.RenderAsync(data);

            return(qrCodeMemorySpan);
        }
Beispiel #5
0
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="sceneId"></param>
        /// <param name="qrCodeType"></param>
        /// <param name="expire"></param>
        /// <returns></returns>
        public async Task <CreateQrCodeResult> Post(string sceneId, QrCodeType qrCodeType = QrCodeType.QrScene, int expire = 1800)
        {
            var intSceneId = 0;

            if (int.TryParse(sceneId, out intSceneId))
            {
                return(await _service.CreateQrCode(intSceneId, qrCodeType, expire));
            }
            else
            {
                if (qrCodeType == QrCodeType.QrScene)
                {
                    throw new ErrorJsonResultException("临时二维码必须为场景编码 Int 类型");
                }
                return(await _service.CreateQrCode(sceneId));
            }
        }
        public async Task <QRCodeData?> CreateAsync(QrCodeType type, object content)
        {
            var data = type switch
            {
                QrCodeType.Text or QrCodeType.Json => await GenerateTextContentQrCode((string)content),
                QrCodeType.Bookmark => await GenerateBookmarkQrCode((CreateBookmarkModel)content),
                QrCodeType.CalendarEvent => await GenerateCalendarEventQrCode((CreateCalendarEventModel)content),
                QrCodeType.Contact => await GenerateContactDataQrCode((CreateContactDataEventModel)content),
                QrCodeType.Email => await GenerateEmailQrCode((CreateEmailModel)content),
                QrCodeType.Geolocation => await GenerateGeolocationQrCode((CreateGeoLocationModel)content),
                QrCodeType.TextMessage => await GenerateTextMessageQrCode((CreateTextMessageModel)content),
                QrCodeType.Url => await GenerateURLQrCode((CreateUrlModel)content),
                QrCodeType.WhatsApp => await GenerateWhatsAppMessageQrCode((CreateWhatsAppMessageModel)content),
                QrCodeType.Wifi => await GenerateWifiQrCode((CreateWifiModel)content),
                _ => null
            };

            return(data);
        }
Beispiel #7
0
        private async Task <IActionResult> HandleAsync(byte[] result, QrCodeType qrCodeType, object model)
        {
            // Generate a file name for the qr code
            var id = Guid.NewGuid();

            var filename = $"{id}.{ImageUtils.GetFileExtensionFromImageType(renderOptions.RenderType)}";
            var host     = config["HostUrl"] ?? $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}";

            // Generate a database result for the file
            var qrCode = new RestfulQrCode
            {
                Id         = id,
                Created    = DateTime.Now.ToUniversalTime(),
                CreatedBy  = apiKeyProvider.ApiKey.Id,
                Filename   = filename,
                Model      = model,
                RenderType = renderOptions.RenderType,
                Type       = qrCodeType,
                PublicUrl  = string.Join(@"/", host, "images", apiKeyProvider.ApiKey.LocationId, filename)
            };

            // Save the file in S3
            var s3Result = await imageService.UploadAsync(apiKeyProvider.ApiKey, filename, result);

            if (!s3Result)
            {
                return(StatusCode(500));
            }

            // Save the db
            var saveResult = await imageService.CreateAsync(qrCode);

            if (saveResult == null)
            {
                return(StatusCode(500));
            }

            return(Created(qrCode.PublicUrl, qrCode));
        }
        public IEnumerable <QrCodeEntity> ReadAll()
        {
            List <QrCodeEntity> qrCodes = new List <QrCodeEntity>();
            string sql = $"Select * from {GetTableName()}";

            using (SqlConnection sqlConnection = new SqlConnection(GetConnectionString()))
            {
                sqlConnection.Open();
                using (SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection))
                {
                    SqlDataReader reader = sqlCommand.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            int        Id         = Int32.Parse(reader["Id"].ToString());
                            int        UserId     = Int32.Parse(reader["UserId"].ToString());
                            QrCodeType QrCodeType = (QrCodeType)reader["QrCodeType"];
                            byte[]     Content    = (byte[])reader["Content"];

                            qrCodes.Add(new QrCodeEntity
                            {
                                Id         = Id,
                                UserId     = UserId,
                                QrCodeType = QrCodeType,
                                Content    = Content
                            });
                        }
                        return(qrCodes);
                    }
                    else
                    {
                        throw new Exception("No data found!");
                    }
                }
            }
        }
Beispiel #9
0
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="sceneId">场景值ID(字符串形式的ID),字符串类型,长度限制为1到64,仅永久二维码支持此字段</param>
        /// <param name="qrCodeType" ref="QrCodeType">二维码类型</param>
        /// <param name="expireSeconds">有效期【临时二维码需要设置】</param>
        /// <returns></returns>
        public async Task <CreateQrCodeResult> CreateQrCode(int sceneId, QrCodeType qrCodeType = QrCodeType.QrScene, int expireSeconds = 1800)
        {
            object data = null;

            if (qrCodeType == QrCodeType.QrScene)
            {
                data = new
                {
                    expire_seconds = expireSeconds,
                    action_name    = "QR_SCENE",
                    action_info    = new
                    {
                        scene = new
                        {
                            scene_id = sceneId
                        }
                    }
                };
            }
            else
            {
                data = new
                {
                    action_name = "QR_LIMIT_SCENE",
                    action_info = new
                    {
                        scene = new
                        {
                            scene_id = sceneId
                        }
                    }
                };
            }
            var url = RequestUrl.PostCreateQrCodeUrl(this.Token);

            return(await PostSendJson <CreateQrCodeResult>(url, data));
        }
 public QrCodeItemBase(uint id, string name, string description, uint folderId, QrCodeType qrCodeType, IQrCode qrCode, long created, long updated, long deleted, long viewed)
     : base(id, name, description, folderId, BarcodeType.QrCode, null, created, updated, deleted, viewed)
 {
     this.qrCodeType = qrCodeType;
     this.qrCode     = qrCode;
 }
 public QrCodeItemBase(uint id, string name, string description, uint folderId, QrCodeType qrCodeType, IQrCode qrCode)
     : this(id, name, description, folderId, qrCodeType, qrCode, 0L, 0L, 0L, 0L)
 {
 }
Beispiel #12
0
 /// <summary>
 /// 初始化一个新的创建二维码模型。
 /// </summary>
 /// <param name="qrCodeType">二维码类型。</param>
 protected CreateQrCodeModel(QrCodeType qrCodeType)
 {
     Type = qrCodeType;
 }
Beispiel #13
0
 /// <summary>
 /// 初始化一个新的创建二维码模型。
 /// </summary>
 /// <param name="qrCodeType">二维码类型。</param>
 /// <param name="sceneId">数字型的场景Id。</param>
 protected CreateQrCodeModel(QrCodeType qrCodeType, uint sceneId)
 {
     Type = qrCodeType;
     SceneId = sceneId;
 }
Beispiel #14
0
 public QrCodeScan(uint id, string name, string description, uint folderId, QrCodeType qrCodeType, IQrCode qrCode, long created, long updated, long deleted, long viewed)
     : base(id, name, description, folderId, qrCodeType, qrCode, created, updated, deleted, viewed)
 {
 }
Beispiel #15
0
 /// <summary>
 /// 初始化一个新的创建二维码模型。
 /// </summary>
 /// <param name="qrCodeType">二维码类型。</param>
 protected CreateQrCodeModel(QrCodeType qrCodeType)
 {
     Type = qrCodeType;
 }
Beispiel #16
0
        /// <summary>
        /// Renders a Quick Reponse code, that can be used with Google Authenticator or compatiple software.
        /// </summary>
        /// <param name="ident">ident of device as shown to user</param>
        /// <param name="key">private key</param>
        /// <param name="type">type of QR code, HOTP or TOTP</param>
        /// <param name="size">pixels height/width of png image</param>
        /// <returns>png image as byte array</returns>
        public static byte[] RenderQrCode(string ident, byte[] key, QrCodeType type, int size=200)
        {
            string url = "otpauth://" + type.ToString().ToLower() + "/" + ident + "?secret=" + Base32.Convert(key);

            QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.M);
            QrCode qrCode;
            encoder.TryEncode(url, out qrCode);

            ISizeCalculation isize = new FixedCodeSize(size, QuietZoneModules.Zero);
            GraphicsRenderer gRenderer = new GraphicsRenderer(isize);

            using (MemoryStream ms = new MemoryStream())
            {
                gRenderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms);
                return ms.GetBuffer();
            }
        }