mirror of
https://gitlink.org.cn/Gitlink/forgeplus.git
synced 2026-05-02 19:30:48 +08:00
区块链确权相关
This commit is contained in:
@@ -6,4 +6,53 @@ class ApplicationQuery
|
||||
def strip_param(key)
|
||||
params[key].to_s.strip.presence
|
||||
end
|
||||
|
||||
# find all the repos that a user has tokens
|
||||
def find_repo_with_token(user_id)
|
||||
params = {
|
||||
"request-type": "query user balance of all repos",
|
||||
"username": user_id.to_s
|
||||
}.to_json
|
||||
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
||||
if resp_body['status'] != 0
|
||||
raise "区块链接口请求失败."
|
||||
else
|
||||
token_list = resp_body['UserBalanceList'].nil? ? [] : resp_body['UserBalanceList']
|
||||
return token_list
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# find one repo that a user has tokens
|
||||
def find_one_balance(user_id, project_id)
|
||||
# return 3 statuses: UnknownErr/ResUserNotExisted/Success
|
||||
params = {
|
||||
"request-type": "query user balance of single repo",
|
||||
"username": user_id.to_s,
|
||||
"token_name": project_id.to_s
|
||||
}.to_json
|
||||
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
||||
if resp_body['status'] == 0
|
||||
return resp_body['balance']
|
||||
elsif resp_body['status'] == 100
|
||||
return 0 # 找不到用户返回0
|
||||
else
|
||||
raise "区块链接口请求失败."
|
||||
end
|
||||
end
|
||||
|
||||
# query the basic info of a repository
|
||||
def find_blockchain_repo_info(project_id)
|
||||
params = {
|
||||
"request-type": "query repo basic info",
|
||||
"token_name": project_id.to_s
|
||||
}.to_json
|
||||
resp_body = Blockchain::InvokeBlockchainApi.call(params)
|
||||
if resp_body['status'] == 0
|
||||
return resp_body
|
||||
else
|
||||
raise "区块链接口请求失败."
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
31
app/queries/blockchain/balance_query.rb
Normal file
31
app/queries/blockchain/balance_query.rb
Normal file
@@ -0,0 +1,31 @@
|
||||
class Blockchain::BalanceQuery < ApplicationQuery
|
||||
|
||||
attr_reader :params, :is_current_admin_user
|
||||
|
||||
def initialize(params,is_current_admin_user)
|
||||
@params = params
|
||||
@is_current_admin_user = is_current_admin_user
|
||||
end
|
||||
|
||||
def call
|
||||
if is_current_admin_user
|
||||
token_list = find_repo_with_token(params["user_id"])
|
||||
result_list = []
|
||||
token_list.each do |t|
|
||||
project = Project.find_by(id: t['token_name'].to_i)
|
||||
if project.nil?
|
||||
next
|
||||
end
|
||||
owner = User.find_by(id: project.user_id)
|
||||
if owner.nil? || project.nil?
|
||||
else
|
||||
balance = t['balance']
|
||||
result_list << [owner, project, balance]
|
||||
end
|
||||
end
|
||||
results = {"status": 0, "projects": result_list}
|
||||
else
|
||||
results = {"status": 1} # query failed
|
||||
end
|
||||
end
|
||||
end
|
||||
12
app/queries/blockchain/balance_query_one_project.rb
Normal file
12
app/queries/blockchain/balance_query_one_project.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
class Blockchain::BalanceQueryOneProject < ApplicationQuery
|
||||
|
||||
attr_reader :params, :is_current_admin_user
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
find_one_balance(params[:user_id].to_s, params[:project_id].to_s)
|
||||
end
|
||||
end
|
||||
13
app/queries/blockchain/repo_basic_info.rb
Normal file
13
app/queries/blockchain/repo_basic_info.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
class Blockchain::RepoBasicInfo < ApplicationQuery
|
||||
|
||||
attr_reader :params, :is_current_admin_user
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
info = find_blockchain_repo_info(params[:project_id].to_s)
|
||||
return info
|
||||
end
|
||||
end
|
||||
@@ -38,6 +38,12 @@ class Projects::ListMyQuery < ApplicationQuery
|
||||
# projects = projects.visible.joins(:members).where(members: { user_id: user.id })
|
||||
# elsif params[:category].to_s == "private"
|
||||
# projects = projects.is_private.joins(:members).where(members: { user_id: user.id })
|
||||
elsif params[:category].to_s == "blockchain_token" # 所有钱包中有token的项目有哪些
|
||||
token_list = find_repo_with_token(user.id)
|
||||
project_names = token_list.map { |x| x['token_name'] }
|
||||
projects = projects.where(name: project_names)
|
||||
tokens = token_list.map { |x| x['balance'] }
|
||||
puts "pause"
|
||||
end
|
||||
|
||||
if params[:is_public].present?
|
||||
|
||||
Reference in New Issue
Block a user