From 4409f392f6c0ed65adfde22caa51ad77f5b06132 Mon Sep 17 00:00:00 2001 From: dhuSaverin Date: Mon, 16 Nov 2020 16:34:26 +0800 Subject: [PATCH] add helper methods --- .gitignore | 2 + .../tapas_engine/application_helper.rb | 101 +++++++++++------- 2 files changed, 63 insertions(+), 40 deletions(-) diff --git a/.gitignore b/.gitignore index 9ebd6cb..45604ad 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ test/dummy/db/*.sqlite3-* test/dummy/log/*.log test/dummy/storage/ test/dummy/tmp/ +.idea/ +/.rakeTasks diff --git a/app/helpers/tapas_engine/application_helper.rb b/app/helpers/tapas_engine/application_helper.rb index e23a63b..047691a 100644 --- a/app/helpers/tapas_engine/application_helper.rb +++ b/app/helpers/tapas_engine/application_helper.rb @@ -5,6 +5,27 @@ def format_money(value = nil) format('%.2f', value.to_f) end + #身份证打码 + def hidden_id_no(id_no = '') + return '' if id_no.length < 18 + id_no[7, 9] = '*' * 9 + id_no + end + + #手机号打码 + def hidden_mobile(mobile = '') + return '' if mobile.length < 11 + mobile[3, 4] = '*' * 4 + mobile + end + + #银行卡打码 + def hidden_card_no(card_no = '') + return '' if card_no.length < 16 + card_no[4, 11] = '*' * 11 + card_no + end + # 标准渲染 def render_json_standard(json, obj) case obj.class.superclass.name @@ -36,20 +57,20 @@ def render_json_intelligence(json, obj, accepts = [], excepts = [], extras=[], p end when 'ApplicationRecord' return {} if obj.blank? - + render_json_custom(json, obj, accepts, excepts, extras, partials) else {} end end - + def render_json_custom(json, obj, accepts, excepts, extras=[], partials=[]) accepts = obj.class.columns.map(&:name).map(&:to_sym) if accepts.blank? accepts -= excepts if excepts.present? - + accepts.each do |column| value = obj.__send__(column) - + value = if value.is_a? String case obj.class.column_for_attribute(column).type @@ -67,29 +88,29 @@ def render_json_custom(json, obj, accepts, excepts, extras=[], partials=[]) else value end - + json.__send__(column, value) end - + extras.each do |extra| json.__send__(extra, obj.__send__(extra)) end if extras.present? - + partials.each do |partial| @result = obj.__send__(partial[:result]) - + next if @result.blank? - + @extras = partial[:extras] @accepts = partial[:accepts] @excepts = partial[:excepts] @partials = partial[:partials] - + json.__send__(partial[:result], @result, partial: 'intelligence', as: partial[:result]) - + end if partials.present? end - + # # 渲染json返回属性 # @@ -100,11 +121,11 @@ def render_json_custom(json, obj, accepts, excepts, extras=[], partials=[]) def render_json_attrs(json, obj, attrs = nil) # Rails.logger.info "obj=>#{obj}" return if obj.blank? - + attrs = obj.class.columns.map(&:name) if attrs.blank? attrs.each do |column| next unless column != 'deleted_at' - + key = column.to_sym column_type = obj.class.columns.select { |c| c.name == column.to_s }.first.type value = obj.__send__(column.to_sym) @@ -115,7 +136,7 @@ def render_json_attrs(json, obj, attrs = nil) value = value.to_time&.strftime('%F %H:%M') if obj.class.column_for_attribute(key).type == :datetime # value = format('%.2f', value) if value.is_a?(BigDecimal) && (key.to_s.include?('amount') || key.to_s.include?('price')) value = value.to_f if obj.class.column_for_attribute(key).type == :decimal - + # if (/^([a-zA-Z]+_)*id$/ =~ key).present? || key.to_s == 'whodunnit' # if value.class == Array # value = value.map { |v| v } @@ -131,23 +152,23 @@ def render_json_attrs(json, obj, attrs = nil) json.__send__(key, value) end end - + def render_json_attrs_except(json, object, attrs = nil) attrs = object.class.columns.map(&:name) - attrs.map(&:to_s) if attrs.present? - + render_json_attrs(json, object, attrs) end - + def render_json_array_partial(obj, array, particle, as) obj.__send__('array!', array, partial: particle, as: as) end - + # # 将准确时间转换成相对于当前的时间 # def timeago(time) return '' if time.blank? - + if time.is_a?(String) time = begin Time.zone.parse(time) @@ -157,53 +178,53 @@ def timeago(time) elsif time.is_a?(Time) time = time end - + return '' if time.blank? - + time_now = Time.zone.now - + interval = (time_now - time).to_i - + case interval when 0..3600 minutes = interval / 60 - + time = I18n.t('timeago.minutes', minutes: minutes) - + when 3601..86_400 hours = interval / 3600 - + time = I18n.t('timeago.hours', hours: hours) - + when 86_401..2_592_000 days = interval / 86_400 - + time = I18n.t('timeago.days', days: days) - + else time = time.strftime('%F %H:%M') end - + time end - + def get_model(model_name) ActiveSupport::Dependencies.constantize(model_name.classify) end - + def render_nested_children(json, obj, &block) if obj.children.present? json.children obj.children do |child| block.call(child) render_nested_children(json, child, &block) end - + end end - + def humanize(interval) return '' if interval.blank? - + # interval可能是integer或者24:00:00形式 secs = if interval.include?(':') intervals = interval.split(':').map(&:to_i) @@ -211,15 +232,15 @@ def humanize(interval) else interval.to_i end - + result = [[60, 's'], [60, 'minute'], [24, 'h'], [7, 'd'], [Float::INFINITY, 'w']].map do |count, name| next unless secs > 0 - + secs, n = secs.divmod(count) - + "#{n.to_i}#{name}" unless n.to_i == 0 end.compact.reverse.first - + result end end