From dbc3d25fec6227c98b6506308410e09aa2cb7df3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Wed, 3 Apr 2024 13:14:47 +0800 Subject: [PATCH] =?UTF-8?q?feat(server):=E5=A2=9E=E5=8A=A0=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E5=87=BD=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- license_server/src/utils.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/license_server/src/utils.rs b/license_server/src/utils.rs index b490034..b4c32c2 100644 --- a/license_server/src/utils.rs +++ b/license_server/src/utils.rs @@ -1,3 +1,5 @@ +use rand::{seq::SliceRandom, thread_rng}; + pub fn empty_to_none>(s: S) -> Option { let s = s.into(); if s.is_empty() { @@ -6,3 +8,18 @@ pub fn empty_to_none>(s: S) -> Option { Some(s) } } + +const RAND_LINCENSE_STR_SRC: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + +pub fn generate_product_license_id() -> String { + let choices = RAND_LINCENSE_STR_SRC.as_bytes(); + let mut rng = thread_rng(); + let mut code: Vec = vec![]; + while code.len() < 10 { + match choices.choose(&mut rng) { + Some(c) => code.push(String::from_utf8(vec![*c]).unwrap()), + None => continue, + } + } + code.join("") +}