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!