From e80280a9a8b83811f3d497df77f563b51deac18e Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Fri, 15 Nov 2019 17:54:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=8D=E5=88=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/libs/util/uuid.rb | 18 ++++++++++++++++++ app/services/subjects/copy_subject_service.rb | 1 + 2 files changed, 19 insertions(+) diff --git a/app/libs/util/uuid.rb b/app/libs/util/uuid.rb index 2f6757139..8147ff3a3 100644 --- a/app/libs/util/uuid.rb +++ b/app/libs/util/uuid.rb @@ -2,8 +2,26 @@ module Util module UUID module_function + DCODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z) + def time_uuid(format: '%Y%m%d%H%M%S', suffix: 8) "#{Time.zone.now.strftime(format)}#{Random.rand(10**suffix).to_i}" end + + # 随机生成字符 + def generate_identifier(container, num, pre='') + code = DCODES.sample(num).join + if container == User + while container.exists?(login: pre+code) do + code = DCODES.sample(num).join + end + else + while container.exists?(identifier: code) do + code = DCODES.sample(num).join + end + end + code + end + end end diff --git a/app/services/subjects/copy_subject_service.rb b/app/services/subjects/copy_subject_service.rb index 99f5dcdc5..924eb10ef 100644 --- a/app/services/subjects/copy_subject_service.rb +++ b/app/services/subjects/copy_subject_service.rb @@ -60,6 +60,7 @@ class Subjects::CopySubjectService < ApplicationService to_shixun = Shixun.new to_shixun.attributes = shixun.attributes.dup.except('id', 'user_id', 'identifier', 'homepage_show', 'use_scope', 'averge_star', 'myshixuns_count') + to_shixun.identifier = Util::UUID.generate_identifier(Shixun, 8) to_shixun.user_id = user_id to_shixun.save!