module_block_chain.go 2.2 KB
package allied_creation_user

import (
	"encoding/json"
	"fmt"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
)

func (gateway HttplibAlliedCreationUser) ListBlockChains(param ReqListBlockChain) (DataListBlockChain, error) {
	if !constant.EnableBlockChain || param.Disable {
		return []DataListBlockChainItem{}, nil
	}
	url := gateway.baseUrL + "/block-chains/"
	method := "post"
	req := gateway.CreateRequest(url, method)
	moduleDesc := "获取区块链数据列表"
	//TODO traceID
	log.Logger.Debug("向用户模块请求数据:"+moduleDesc, map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("%v失败:%w", moduleDesc, err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("%v失败:%w", moduleDesc, err)
	}

	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析错误:%w", err)
	}
	var data DataListBlockChain
	err = gateway.GetResponseData(result, &data)
	return data, err
}

func (gateway HttplibAlliedCreationUser) BlockChainsToken(param ReqBlockChainToken) (*DataBlockChainToken, error) {
	url := gateway.baseUrL + "/block-chains/token"
	method := "post"
	req := gateway.CreateRequest(url, method)
	moduleDesc := "获取区块链数据授权"
	//TODO traceID
	log.Logger.Debug("向用户模块请求数据:"+moduleDesc, map[string]interface{}{
		"api":   method + ":" + url,
		"param": param,
	})
	req, err := req.JSONBody(param)
	if err != nil {
		return nil, fmt.Errorf("%v失败:%w", moduleDesc, err)
	}

	byteResult, err := req.Bytes()
	if err != nil {
		return nil, fmt.Errorf("%v失败:%w", moduleDesc, err)
	}

	var result service_gateway.GatewayResponse
	err = json.Unmarshal(byteResult, &result)
	if err != nil {
		return nil, fmt.Errorf("解析错误:%w", err)
	}
	var data DataBlockChainToken
	err = gateway.GetResponseData(result, &data)
	return &data, err
}