Golang 开发常用代码片段

Struct to JsonString


type BaseRequest struct {
	httpMethod string
	domain     string
	path       string
	params     map[string]string
	formParams map[string]string

	service string
	version string
	action  string
}
type LivenessRecognitionRequest struct {
	*BaseRequest

	// 身份证号
	IdCard *string `json:"IdCard,omitempty" name:"IdCard"`

	// 姓名。中文请使用UTF-8编码。
	Name *string `json:"Name,omitempty" name:"Name"`

	// 用于活体检测的视频,视频的BASE64值;
	// BASE64编码后的大小不超过5M,支持mp4、avi、flv格式。
	VideoBase64 *string `json:"VideoBase64,omitempty" name:"VideoBase64"`

	// 活体检测类型,取值:LIP/ACTION/SILENT。
	// LIP为数字模式,ACTION为动作模式,SILENT为静默模式,三种模式选择一种传入。
	LivenessType *string `json:"LivenessType,omitempty" name:"LivenessType"`

	// 数字模式传参:数字验证码(1234),需先调用接口获取数字验证码;
	// 动作模式传参:传动作顺序(2,1 or 1,2),需先调用接口获取动作顺序;
	// 静默模式传参:空。
	ValidateData *string `json:"ValidateData,omitempty" name:"ValidateData"`

	// 本接口不需要传递此参数。
	Optional *string `json:"Optional,omitempty" name:"Optional"`
}

func (r *LivenessRecognitionRequest) ToJsonString() string {
    b, _ := json.Marshal(r)
    return string(b)
}

func (r *LivenessRecognitionRequest) FromJsonString(s string) error {
    return json.Unmarshal([]byte(s), &r)
}

func StringPtr(v string) *string {
	return &v
}

func NewLivenessRecognitionRequest() (request *LivenessRecognitionRequest) {
    request = &LivenessRecognitionRequest{
        BaseRequest: &tchttp.BaseRequest{},
    }
    request.Init().WithApiInfo("faceid", APIVersion, "LivenessRecognition")
    return
}


func (l *LivenessRecognitionRequest)DoLivenessCompare()  {
	request := NewLivenessRecognitionRequest()
	request.IdCard = StringPtr(l.IdCard)
	request.Name = StringPtr(l.Name)
	request.VideoBase64 = StringPtr(l.VideoBase64)
	request.LivenessType = StringPtr(l.LivenessType)

        requestData:=request.ToJsonString() //转strurct to jsonstring
	err := request.FromJsonString(requestData) //转jsonstring to strurct
	if err != nil {
		panic(err)
	}
}

func LivenessCompare(ctx *gin.Context)  {
	fmt.Println("LivenessCompare")
	l.IdCard=""
	l.Name="haima"
	l.VideoBase64=""
	l.LivenessType="ACTION"
	l.DoLivenessCompare()
}
posted @ 2020-03-28 11:53  HaimaBlog  阅读(338)  评论(0编辑  收藏  举报