Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ouyeel update #147

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open

ouyeel update #147

wants to merge 1 commit into from

Conversation

u2
Copy link

@u2 u2 commented Oct 9, 2022

No description provided.

@@ -0,0 +1,18 @@
# frozen_string_literal: true

class Api::DecodeTransactionsController < ApplicationController
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

controller里是些后端API逻辑的地方,controller里的一个公共方法,对应一个接口。
在路由routes文件里能看到具体的定义。
例如这个get "decode_transactions/:tx_hash", to: "decode_transactions#show",这个"decode_transactions/:tx_hash" GET的URl,对应的是decode_transactions controller里的show方法,tx_hash在方法里面是params[:tx_hash]


class Api::DecodeTransactionsController < ApplicationController
def show
decode_tx = DecodeTransaction.find_by(tx_hash: params[:tx_hash])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

从数据库中查找tx_hash为params[:tx_hash]的decode_tx,这里只找一条记录,limit 1

decode_tx = DecodeTransaction.find_by(tx_hash: params[:tx_hash])
unless decode_tx
return render json: { error_code: 40200, error_message: "不存在的交易或正在排队解析" }
end
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

找不到则返回 json结构
{ error_code: 40200, error_message: "不存在的交易或正在排队解析" }

return render json: { error_code: 40200, error_message: "不存在的交易或正在排队解析" }
end

render json: {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

找到则返回如下json

contract_name: decode_tx.contract_name,
api_name: decode_tx.api_name,
request_args: decode_tx.request_args,
logs: decode_tx.decode_logs.map{|log|log["info"]},
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个map跟python或者racket里的map一个意思,遍历list并执行lambda函数。

before_action :validate_platform

def hold_list
open_tongbao = OpenTongbao.find_by(system_id: params[:systemId], tongbao_id: params[:tongbao_id])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

从数据库里查找,根据system_id和tongbao_id查找


class Api::FrontEndController < ApplicationController
# before_action :validate_sign
before_action :validate_platform
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在所有的方法前,先调用validate_platform方法,这个validate_platform在ApplicationController里有定义。

可以理解为是一个filter

return render({json: {error_code: 40027, error_message: "平台还未审核"}}) if open_tongbao.platform_time.blank?
return render({json: {error_code: 40028, error_message: "非快速开立,核心企业还未复核"}}) if !open_tongbao.is_quick && open_tongbao.enterprise_time.blank?
return render({json: {error_code: 40029, error_message: "供应商未接收"}}) if open_tongbao.receive_time.blank?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是以一系列判断,如果不满足条件,直接返回错误。有if 和 unless两种。
present?表示是否存在,和blank?是相反的,语法糖可以看下表。
https://ruby-china.org/topics/24675

return render({json: {error_code: 40028, error_message: "非快速开立,核心企业还未复核"}}) if !open_tongbao.is_quick && open_tongbao.enterprise_time.blank?
return render({json: {error_code: 40029, error_message: "供应商未接收"}}) if open_tongbao.receive_time.blank?

list = open_tongbao.tongbaos.order(timestamp: :desc).map do |tongbao|
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

按照时间降序排列,然后在遍历处理。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant