Browse Source

add common function for blockchain operation

pull/319/head
nigel007 5 years ago
parent
commit
f230c0d640
1 changed files with 86 additions and 1 deletions
  1. +86
    -1
      app/controllers/application_controller.rb

+ 86
- 1
app/controllers/application_controller.rb View File

@@ -344,7 +344,7 @@ class ApplicationController < ActionController::Base
User.current = User.find 8686
elsif params[:debug] == 'admin'
logger.info "@@@@@@@@@@@@@@@@@@@@@@ debug mode....."
user = User.find 36480
user = User.find 35
User.current = user
cookies.signed[:user_id] = user.id
end
@@ -814,4 +814,89 @@ class ApplicationController < ActionController::Base
HotSearchKeyword.add(keyword)
end


# author: zxh
# blockchain存证api
def invoke_blockchain_api(uri, params)
begin
uri = URI.parse(URI.encode(uri.strip))
res = Net::HTTP.start(uri.host, uri.port) do |http|
req = Net::HTTP::Post.new(uri)
req['Content-Type'] = 'application/json'
req.body = params
http.request(req)
end
if res.code.to_i != 200
puts '区块链接口请求失败.'
return false
else
res_body = JSON.parse(res.body)
if res_body.has_key?("data") && JSON.parse(res_body["data"]).has_key?("status") && JSON.parse(res_body["data"])['status'] == "Success"
else
puts '区块链接口请求出错.'
return false
end
end

return true
rescue Exception => e
puts '区块链接口请求失败.'
return false
end
end

# author: zxh
# blockchain相关项目活动调用函数
# return true: 表示上链操作成功; return false: 表示上链操作失败
def push_activity_2_blockchain(activity_type, model)
if activity_type == "issue_create"

project_id = model['project_id']
project = Project.find(project_id)
if project['use_blockchain'] == 0
# 无需执行上链操作
return true
end

id = model['id']

owner_id = project['user_id']
owner = User.find(owner_id)
ownername = owner['login']
reponame = project['name']

author_id = project['user_id']
author = User.find(author_id)
username = author['login']

action = 'opened'

title = model['subject']
content = model['description']
created_at = model['created_on']
updated_at = model['updated_on']

# 调用区块链接口
param = {
"action": "executeContract",
"contractID": "RepositoryDB1",
"operation": "putIssue",
"arg": {
"issue_id": id,
"repo_id": project_id,
"reponame": reponame,
"ownername": ownername,
"username": username,
"action": action,
"title": title,
"content": content,
"created_at": created_at,
"updated_at": updated_at
}.to_json
}.to_json
success_blockchain = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
return success_blockchain
end
end

end

Loading…
Cancel
Save