110 lines
2.7 KiB
Ruby
110 lines
2.7 KiB
Ruby
class ApplicationService
|
|
include Callable
|
|
|
|
Error = Class.new(StandardError)
|
|
|
|
def regix_emoji content
|
|
" " if content.blank?
|
|
regex = /[^a-zA-Z0-9\u4E00-\u9FFF]/
|
|
content.gsub(regex, '')
|
|
end
|
|
|
|
protected
|
|
def try_lock(key)
|
|
raise Error, "请稍后再试!" unless $redis_cache.set(key, 1, nx: true, ex: 60.seconds)
|
|
end
|
|
|
|
def unlock(key)
|
|
$redis_cache.del(key)
|
|
end
|
|
|
|
private
|
|
|
|
def strip(str)
|
|
str.to_s.strip.presence
|
|
end
|
|
|
|
def str_to_boolean str
|
|
ActiveModel::Type::Boolean.new.cast str
|
|
end
|
|
|
|
# params: params from index.js page
|
|
def create_repo_on_blockchain(params, project)
|
|
username = params['user_id'].to_s
|
|
token_name = project.id.to_s
|
|
total_supply = params['blockchain_token_all'].to_i
|
|
token_balance = [[username, (total_supply * params['blockchain_init_token'].to_i / 100).to_i]]
|
|
|
|
params = {
|
|
"request-type": "create repo",
|
|
"username": username,
|
|
"token_name": token_name,
|
|
"total_supply": total_supply,
|
|
"token_balance": token_balance
|
|
}.to_json
|
|
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
|
if resp_body['status'] != 0
|
|
raise "区块链接口请求失败."
|
|
end
|
|
end
|
|
|
|
|
|
def transfer_balance_on_blockchain(payer, payee, token_name, amount)
|
|
|
|
params = {
|
|
"request-type": "transfer amount",
|
|
"payer": payer,
|
|
"payee": payee,
|
|
"token_name": token_name,
|
|
"amount": amount
|
|
}.to_json
|
|
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
|
if resp_body['status'] == 5 or resp_body['status'] == 106 or resp_body['status'] == 105
|
|
raise resp_body['message']
|
|
elsif resp_body['status'] != 0
|
|
raise "区块链接口请求失败."
|
|
else
|
|
end
|
|
end
|
|
|
|
|
|
def lock_balance_on_blockchain(username, tokenname, amount)
|
|
|
|
params = {
|
|
"request-type": "lock user balance",
|
|
"username": username,
|
|
"token_name": tokenname,
|
|
"amount": amount
|
|
}.to_json
|
|
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
|
if resp_body['status'] == 5 or resp_body['status'] == 106 or resp_body['status'] == 103
|
|
raise resp_body['message']
|
|
elsif resp_body['status'] != 0
|
|
raise "区块链接口请求失败."
|
|
else
|
|
end
|
|
end
|
|
|
|
|
|
def unlock_balance_on_blockchain(username, tokenname, amount)
|
|
|
|
params = {
|
|
"request-type": "unlock user balance",
|
|
"username": username,
|
|
"token_name": tokenname,
|
|
"amount": amount
|
|
}.to_json
|
|
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
|
if resp_body['status'] == 5 or resp_body['status'] == 106 or resp_body['status'] == 104
|
|
raise resp_body['message']
|
|
elsif resp_body['status'] != 0
|
|
raise "区块链接口请求失败."
|
|
else
|
|
end
|
|
end
|
|
def phone_mail_type value
|
|
value =~ /^1\d{10}$/ ? 1 : 0
|
|
end
|
|
|
|
end
|