...... Orz" 結果最後被棄用了,無聊丟到這邊做垃圾回收|||
class Bank
ALPHABET = 'ABCDEFGHJKLMNPQRSTUVWXYZIO'
BASE = 10000000
FRAME = BASE * ALPHABET.length
#FRAME切換數字為sex(1 or 2) , 所以最大user_id為 26(字母) * 10000000(可用長度) * 2(sex) = 520000000
#驗證身份證
def self.confirm_id(str)
return confirm_code(str , true)
end
#ID轉身份證
def self.id_builder(user_id)
user_id = user_id.to_i
return false if user_id < 1 || user_id > FRAME * 2
serial = "#{ALPHABET[user_id % FRAME / BASE]}#{user_id / FRAME % 2 + 1}#{"#{user_id % BASE}".rjust(7 , '0')}"
return "#{serial}#{confirm_code(serial)}"
end
private
def self.confirm_code(str , is_confirm = false)
temp = ALPHABET.index(str[0]) + 10
counter = temp / 10
([temp % 10] + str[1..-1].split(//).map{|i|i.to_i}).each_with_index do |t , i|
counter += t * (9 - i < 1 ? 1 : 9 - i)
end
return is_confirm ? counter % 10 == 0 : (10 - counter) % 10
end
end
沒有留言:
張貼留言